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