keccak256-circom/go-keccak256-bits-impl/stepmappings_test.go
go-keccak256-bits-impl/stepmappings_test.goRaw
1package keccak
2
3import (
4 "encoding/json"
5 "fmt"
6 "testing"
7
8 qt "github.com/frankban/quicktest"
9)
10
11func printS(n string, s []uint64) {
12 jS, err := json.Marshal(s)
13 if err != nil {
14 panic(err)
15 }
16 fmt.Printf("%s: %s\n", n, string(jS))
17}
18func newS() ([25 * 64]bool, [25]uint64) {
19 var s [25 * 64]bool
20 var sU64 [25]uint64
21 for i := 0; i < len(s)/64; i++ {
22 copy(s[i*64:i*64+64], u64ToBits(uint64(i)))
23 sU64[i] = uint64(i)
24 }
25 return s, sU64
26}
27
28func TestTheta(t *testing.T) {
29 s, sU64 := newS()
30
31 s = theta(s)
32 sU64 = thetaU64Version(sU64)
33
34 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
35 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
36 []uint64{26, 9, 13, 29, 47, 31, 14, 8, 22, 34, 16, 3, 3,
37 19, 37, 21, 24, 30, 12, 56, 14, 29, 25, 9, 51})
38
39 // compute again theta on the current state
40 s = theta(s)
41 sU64 = thetaU64Version(sU64)
42 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
43}
44
45func TestTheta2(t *testing.T) {
46 input := []uint64{26388279066651, 246290629787648, 26388279902208, 25165850, 246290605457408, 7784628352, 844424965783552, 2305843009213694083, 844432714760192, 2305843009249345539, 637534226, 14848, 641204224, 14354, 3670528, 6308236288, 2130304761856, 648518346341354496, 6309216256, 648520476645130240, 4611706359392501763, 792677514882318336, 20340965113972, 4611732197915754499, 792633534417207412}
47 var s [25 * 64]bool
48 var sU64 [25]uint64
49 copy(s[:], u64ArrayToBits(input))
50 copy(sU64[:], input)
51
52 s = theta(s)
53 sU64 = thetaU64Version(sU64)
54
55 // fmt.Println(bitsToU64Array(s[:]))
56 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
57 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
58 []uint64{3749081831850030700, 1297317621190464868, 10017560217643747862, 7854780639862409219, 13836147678645575967, 3749090635727681271, 1297915755455157604, 12323429615135705749, 7855062122598582297, 16141814766035214620, 3749090628446369381, 1297071330560683876, 10017586606556924438, 7854780639837253643, 13835971756788491039, 3749090634251287159, 1297070162329376100, 9369068259580659222, 7854780645071013913, 14484490034407743775, 8360757404916954740, 1801500877105239396, 10017570663003408994, 3243123208712177690, 14628605291203076459})
59 fmt.Println(bitsToU64(s[1*64:2*64]), s[1*64:2*64])
60
61 // compute again theta on the current state
62 s = theta(s)
63 sU64 = thetaU64Version(sU64)
64 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
65}
66
67func TestRhoPi(t *testing.T) {
68 s, sU64 := newS()
69
70 s = rhopi(s)
71 sU64 = rhopiU64Version(sU64)
72
73 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
74 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
75 []uint64{0, 105553116266496, 105553116266496, 37748736, 393216,
76 805306368, 9437184, 80, 562949953421312, 13835058055282163714,
77 2, 448, 436207616, 4864, 5242880, 536870912, 343597383680,
78 11264, 557056, 1657324662872342528, 9223372036854775808,
79 288230376151711744, 7696581394432, 32985348833280, 84})
80
81 // compute again rhopi on the current state
82 s = rhopi(s)
83 sU64 = rhopiU64Version(sU64)
84 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
85}
86
87func TestChi(t *testing.T) {
88 s, sU64 := newS()
89
90 s = chi(s)
91 sU64 = chiU64Version(sU64)
92
93 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
94 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
95 []uint64{2, 0, 6, 3, 5, 4, 14, 6, 12, 11, 14, 10, 14, 13, 15,
96 14, 18, 16, 30, 3, 22, 20, 30, 19, 25})
97
98 // compute again theta on the current state
99 s = rhopi(s)
100 sU64 = rhopiU64Version(sU64)
101 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
102 s = chi(s)
103 sU64 = chiU64Version(sU64)
104 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
105}
106
107func TestIota(t *testing.T) {
108 s, sU64 := newS()
109
110 s = iot(s, 3)
111 sU64 = iotU64Version(sU64, 3)
112
113 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
114 qt.Assert(t, bitsToU64(s[0:64]), qt.Equals, uint64(9223372039002292224))
115
116 // compute again theta on the current state
117 s = iot(s, 10)
118 sU64 = iotU64Version(sU64, 10)
119 qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
120}