go-iden3-crypto/poseidon/poseidon_test.go
poseidon/poseidon_test.goRaw
1package poseidon
2
3import (
4 "encoding/hex"
5 "math/big"
6 "testing"
7
8 "github.com/iden3/go-iden3-crypto/utils"
9 "github.com/stretchr/testify/assert"
10 "golang.org/x/crypto/blake2b"
11)
12
13func TestBlake2bVersion(t *testing.T) {
14 h := blake2b.Sum256([]byte("poseidon_constants"))
15 assert.Equal(t, "e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1", hex.EncodeToString(h[:]))
16}
17
18func TestPoseidonHash(t *testing.T) {
19 b0 := big.NewInt(0)
20 b1 := big.NewInt(1)
21 b2 := big.NewInt(2)
22 h, err := Hash([T]*big.Int{b1, b2, b0, b0, b0, b0})
23 assert.Nil(t, err)
24 assert.Equal(t, "12242166908188651009877250812424843524687801523336557272219921456462821518061", h.String())
25
26 b3 := big.NewInt(3)
27 b4 := big.NewInt(4)
28 h, err = Hash([T]*big.Int{b3, b4, b0, b0, b0, b0})
29 assert.Nil(t, err)
30 assert.Equal(t, "17185195740979599334254027721507328033796809509313949281114643312710535000993", h.String())
31}
32
33func TestPoseidonHashArbitraryLen(t *testing.T) {
34 b1 := big.NewInt(1)
35 b2 := big.NewInt(2)
36 h, err := HashSlice([]*big.Int{b1, b2})
37 assert.Nil(t, err)
38 assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154855", h.String())
39
40 b3 := big.NewInt(3)
41 b4 := big.NewInt(4)
42 h, err = HashSlice([]*big.Int{b3, b4})
43 assert.Nil(t, err)
44 assert.Equal(t, "4635491972858758537477743930622086396911540895966845494943021655521913507504", h.String())
45
46 b5 := big.NewInt(5)
47 b6 := big.NewInt(6)
48 b7 := big.NewInt(7)
49 b8 := big.NewInt(8)
50 b9 := big.NewInt(9)
51 b10 := big.NewInt(10)
52 b11 := big.NewInt(11)
53 b12 := big.NewInt(12)
54 h, err = HashSlice([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12})
55 assert.Nil(t, err)
56 assert.Equal(t, "15278801138972282646981503374384603641625274360649669926363020545395022098027", h.String())
57
58 msg := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
59 n := 31
60 msgElems := make([]*big.Int, 0, len(msg)/n+1)
61 for i := 0; i < len(msg)/n; i++ {
62 v := new(big.Int)
63 utils.SetBigIntFromLEBytes(v, msg[n*i:n*(i+1)])
64 msgElems = append(msgElems, v)
65 }
66 if len(msg)%n != 0 {
67 v := new(big.Int)
68 utils.SetBigIntFromLEBytes(v, msg[(len(msg)/n)*n:])
69 msgElems = append(msgElems, v)
70 }
71 hmsg, err := HashSlice(msgElems)
72 assert.Nil(t, err)
73 assert.Equal(t, "16019700159595764790637132363672701294192939959594423814006267756172551741065", hmsg.String())
74
75 msg2 := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet.")
76 msg2Elems := make([]*big.Int, 0, len(msg2)/n+1)
77 for i := 0; i < len(msg2)/n; i++ {
78 v := new(big.Int)
79 utils.SetBigIntFromLEBytes(v, msg2[n*i:n*(i+1)])
80 msg2Elems = append(msg2Elems, v)
81 }
82 if len(msg2)%n != 0 {
83 v := new(big.Int)
84 utils.SetBigIntFromLEBytes(v, msg2[(len(msg2)/n)*n:])
85 msg2Elems = append(msg2Elems, v)
86 }
87 hmsg2, err := HashSlice(msg2Elems)
88 assert.Nil(t, err)
89 assert.Equal(t, "2978613163687734485261639854325792381691890647104372645321246092227111432722", hmsg2.String())
90}
91
92func TestPoseidonHashArbitraryLenBrokenChunks(t *testing.T) {
93 h1, err := HashSlice([]*big.Int{big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4),
94 big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9)})
95 assert.Nil(t, err)
96 h2, err := HashSlice([]*big.Int{big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9),
97 big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4)})
98 assert.Nil(t, err)
99 assert.NotEqual(t, h1, h2)
100}
101
102func TestPoseidonHashArbitraryLenBrokenPadding(t *testing.T) {
103 h1, err := HashSlice([]*big.Int{big.NewInt(int64(1))})
104 assert.Nil(t, err)
105 h2, err := HashSlice([]*big.Int{big.NewInt(int64(1)), big.NewInt(int64(0))})
106 assert.Nil(t, err)
107 assert.NotEqual(t, h1, h2)
108}
109
110func BenchmarkPoseidonHashSmallValues(b *testing.B) {
111 b12 := big.NewInt(int64(12))
112 b45 := big.NewInt(int64(45))
113 b78 := big.NewInt(int64(78))
114 b41 := big.NewInt(int64(41))
115 bigArray4 := []*big.Int{b12, b45, b78, b41}
116
117 for i := 0; i < b.N; i++ {
118 HashSlice(bigArray4) //nolint:errcheck
119 }
120}
121
122func BenchmarkPoseidonHash(b *testing.B) {
123 b0 := big.NewInt(0)
124 b1 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061")
125 b2 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061")
126
127 bigArray4 := [T]*big.Int{b1, b2, b0, b0, b0, b0}
128
129 for i := 0; i < b.N; i++ {
130 Hash(bigArray4) //nolint:errcheck
131 }
132}