keccak256-circom/go-keccak256-bits-impl/keccak_test.go
go-keccak256-bits-impl/keccak_test.goRaw
1package keccak
2
3import (
4 "encoding/hex"
5 "fmt"
6 "testing"
7
8 "github.com/ethereum/go-ethereum/crypto"
9 qt "github.com/frankban/quicktest"
10)
11
12func TestKeccak(t *testing.T) {
13 testKeccak(t, []byte("test"), "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658")
14 testKeccak(t, make([]byte, 32), "290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
15 testKeccak(t, make([]byte, 100), "913fb9e1f6f1c6d910fd574a5cad8857aa43bfba24e401ada4f56090d4d997a7")
16}
17
18func testKeccak(t *testing.T, input []byte, expectedHex string) {
19 expected := crypto.Keccak256(input)
20
21 hBits := ComputeKeccak(bytesToBits(input))
22 h := bitsToBytes(hBits)
23
24 qt.Assert(t, h, qt.DeepEquals, expected)
25 qt.Assert(t, hex.EncodeToString(h), qt.Equals, expectedHex)
26}
27
28func TestPad(t *testing.T) {
29 b := make([]byte, 32)
30 for i := 0; i < len(b); i++ {
31 b[i] = byte(i)
32 }
33 bBits := bytesToBits(b)
34 fBits := pad(bBits)
35
36 qt.Assert(t, bitsToBytes(fBits[:]), qt.DeepEquals,
37 []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
38 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
39 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128})
44}
45
46func TestKeccakfRound(t *testing.T) {
47 s, _ := newS()
48
49 s = keccakfRound(s, 0)
50 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
51 []uint64{
52 26388279066651, 246290629787648, 26388279902208,
53 25165850, 246290605457408, 7784628352, 844424965783552,
54 2305843009213694083, 844432714760192,
55 2305843009249345539, 637534226, 14848, 641204224,
56 14354, 3670528, 6308236288, 2130304761856,
57 648518346341354496, 6309216256, 648520476645130240,
58 4611706359392501763, 792677514882318336,
59 20340965113972, 4611732197915754499,
60 792633534417207412})
61
62 s = keccakfRound(s, 20)
63 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
64 []uint64{17728382861289829725, 13654073086381141005,
65 9912591532945168756, 2030068283137172501, 5084683018496047808,
66 151244976540463006, 11718217461613725815, 11636071286320763433,
67 15039144509240642782, 11629028282864249197,
68 2594633730779457624, 14005558505838459171, 4612881094252610438,
69 2828009553220809993, 4838578484623267135, 1006588603063111352,
70 11109191860075454495, 1187545859779038208,
71 14661669042642437042, 5345317080454741069, 8196674451365552863,
72 635818354583088260, 13515759754032305626, 1708499319988748543,
73 7509292798507899312})
74
75}
76
77func TestKeccakf(t *testing.T) {
78 s, _ := newS()
79
80 s = keccakf(s)
81
82 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
83 []uint64{9472389783892099349, 2159377575142921216,
84 17826682512249813373, 2325963263767348549,
85 15086930817298358378, 11661812091723830419,
86 3517755057770134847, 5223775837645169598, 933274647126506074,
87 3451250694486589320, 825065683101361807, 6192414258352188799,
88 14426505790672879210, 3326742392640380689,
89 16749975585634164134, 17847697619892908514,
90 11598434253200954839, 6049795840392747215, 8610635351954084385,
91 18234131770974529925, 15330347418010067760,
92 12047099911907354591, 4763389569697138851, 6779624089296570504,
93 15083668107635345971})
94
95 // compute again keccakf on the current state
96 s = keccakf(s)
97 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
98 []uint64{269318771259381490, 15892848561416382510,
99 12485559500958802382, 4360182510883008729,
100 14284025675983944434, 8800366419087562177, 7881853509112258378,
101 9503857914080778528, 17110477940977988953,
102 13825318756568052601, 11460650932194163315,
103 13272167288297399439, 13599957064256729412,
104 12730838251751851758, 13736647180617564382,
105 5651695613583298166, 15496251216716036782, 9748494184433838858,
106 3637745438296580159, 3821184813198767406, 15603239432236101315,
107 3726326332491237029, 7819962668913661099, 2285898735263816116,
108 13518516210247555620})
109}
110
111func printBytes(name string, b []byte) {
112 fmt.Printf("%s\n", name)
113 for _, v := range b {
114 fmt.Printf("\"%v\", ", v)
115 }
116 fmt.Println("")
117}
118func printU64Array(name string, b []uint64) {
119 fmt.Printf("%s\n", name)
120 for _, v := range b {
121 fmt.Printf("\"%v\", ", v)
122 }
123 fmt.Println("")
124}
125
126func TestAbsorb(t *testing.T) {
127 s, _ := newS()
128 block := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
129 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
130 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}
135 // printU64Array("s", bitsToU64Array(s[:]))
136 // printBytes("block", block[:])
137
138 absorbed := absorb(s, bytesToBits(block))
139 // printU64Array("absorbed", bitsToU64Array(absorbed[:]))
140
141 qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
142 []uint64{8342348566319207042, 319359607942176202, 14410076088654599075,
143 15666111399434436772, 9558421567405313402, 3396178318116504023,
144 794353847439963108, 12717011319735989377, 3503398863218919239,
145 5517201702366862678, 15999361614129160496, 1325524015888689985,
146 11971708408118944333, 14874486179441062217, 12554876384974234666,
147 11129975558302206043, 11257826431949606534, 2740710607956478714,
148 15000019752453010167, 15593606854132419294, 2598425978562809333,
149 8872504799797239246, 1212062965004664308, 5443427421087086722,
150 10946808592826700411})
151
152 absorbed = absorb(absorbed, bytesToBits(block))
153 // printU64Array("absorbed", bitsToU64Array(absorbed[:]))
154
155 qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
156 []uint64{8909243822027471379, 1111840847970088140,
157 12093072708540612559, 11255033638786021658, 2082116894939842214,
158 12821085060245261575, 6901785969834988344, 3182430130277914993,
159 2164708585929408975, 14402143231999718904, 16231444410553803968,
160 1850945423480060493, 12856855675247400303, 1137248620532111171,
161 7389129221921446308, 12932467982741614601, 1350606937385760406,
162 10983682292859713641, 10305595434820307765, 13958651111365489854,
163 17206620388135196198, 4238113785249530092, 7230868147643218103,
164 603011106238724524, 16480095441097880488})
165}
166
167func TestFinal(t *testing.T) {
168 b := make([]byte, 32)
169 for i := 0; i < len(b); i++ {
170 b[i] = byte(i)
171 }
172 bBits := bytesToBits(b)
173
174 fBits := final(bBits)
175
176 // printBytes("in", b[:])
177 // printU64Array("out", bitsToU64Array(fBits[:]))
178
179 qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
180 []uint64{16953415415620100490, 7495738965189503699,
181 12723370805759944158, 3295955328722933810,
182 12121371508560456016, 174876831679863147, 15944933357501475584,
183 7502339663607726274, 12048918224562833898,
184 16715284461100269102, 15582559130083209842,
185 1743886467337678829, 2424196198791253761, 1116417308245482383,
186 10367365997906434042, 1849801549382613906,
187 13294939539683415102, 4478091053375708790, 2969967870313332958,
188 14618962068930014237, 2721742233407503451,
189 12003265593030191290, 8109318293656735684, 6346795302983965746,
190 12210038122000333046})
191
192 // 2nd test
193
194 for i := 0; i < len(b); i++ {
195 b[i] = byte(254)
196 }
197 bBits = bytesToBits(b)
198 fBits = final(bBits)
199
200 // printBytes("in", b[:])
201 // printU64Array("out", bitsToU64Array(fBits[:]))
202 qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
203 []uint64{16852464862333879129, 9588646233186836430, 693207875935078627,
204 6545910230963382296, 3599194178366828471, 13130606490077331384,
205 10374798023615518933, 7285576075118720444, 4097382401500492461,
206 3968685317688314807, 3350659309646210303, 640023485234837464,
207 2550030127986774041, 8948768022010378840, 10678227883444996205,
208 1395278318096830339, 2744077813166753978, 13362598477502046010,
209 14601579319881128511, 4070707967569603186, 16833768365875755098,
210 1486295134719870048, 9161068934282437999, 8245604251371175619,
211 8421994351908003183})
212}
213
214func TestSqueeze(t *testing.T) {
215 in := []uint64{16852464862333879129, 9588646233186836430, 693207875935078627,
216 6545910230963382296, 3599194178366828471, 13130606490077331384,
217 10374798023615518933, 7285576075118720444, 4097382401500492461,
218 3968685317688314807, 3350659309646210303, 640023485234837464,
219 2550030127986774041, 8948768022010378840, 10678227883444996205,
220 1395278318096830339, 2744077813166753978, 13362598477502046010,
221 14601579319881128511, 4070707967569603186, 16833768365875755098,
222 1486295134719870048, 9161068934282437999, 8245604251371175619,
223 8421994351908003183}
224 inBits := u64ArrayToBits(in)
225 var inBits1600 [25 * 64]bool
226 copy(inBits1600[:], inBits[:])
227
228 outBits := squeeze(inBits1600)
229
230 // printU64Array("in", in)
231 // printBytes("out", bitsToBytes(outBits[:]))
232
233 qt.Assert(t, bitsToBytes(outBits[:]), qt.DeepEquals,
234 []byte{89, 195, 41, 13, 129, 251, 223, 233, 206, 31, 253, 61,
235 242, 182, 17, 133, 227, 8, 157, 240, 227, 196, 158, 9, 24, 232,
236 42, 96, 172, 190, 215, 90})
237
238 // 2nd test
239 in = []uint64{16953415415620100490, 7495738965189503699,
240 12723370805759944158, 3295955328722933810, 12121371508560456016,
241 174876831679863147, 15944933357501475584, 7502339663607726274,
242 12048918224562833898, 16715284461100269102, 15582559130083209842,
243 1743886467337678829, 2424196198791253761, 1116417308245482383,
244 10367365997906434042, 1849801549382613906, 13294939539683415102,
245 4478091053375708790, 2969967870313332958, 14618962068930014237,
246 2721742233407503451, 12003265593030191290, 8109318293656735684,
247 6346795302983965746, 12210038122000333046}
248 inBits = u64ArrayToBits(in)
249 copy(inBits1600[:], inBits[:])
250
251 outBits = squeeze(inBits1600)
252
253 // printU64Array("in", in)
254 // printBytes("out", bitsToBytes(outBits[:]))
255 qt.Assert(t, bitsToBytes(outBits[:]), qt.DeepEquals,
256 []byte{138, 225, 170, 89, 127, 161, 70, 235, 211, 170, 44, 237,
257 223, 54, 6, 104, 222, 165, 229, 38, 86, 126, 146, 176, 50, 24,
258 22, 164, 232, 149, 189, 45})
259}