arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
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 TestPoseidon(t *testing.T) {
 19	b1 := big.NewInt(1)
 20	b2 := big.NewInt(2)
 21	h, err := Hash([]*big.Int{b1, b2})
 22	assert.Nil(t, err)
 23	assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154855", h.String())
 24
 25	b3 := big.NewInt(3)
 26	b4 := big.NewInt(4)
 27	h, err = Hash([]*big.Int{b3, b4})
 28	assert.Nil(t, err)
 29	assert.Equal(t, "4635491972858758537477743930622086396911540895966845494943021655521913507504", h.String())
 30
 31	b5 := big.NewInt(5)
 32	b6 := big.NewInt(6)
 33	b7 := big.NewInt(7)
 34	b8 := big.NewInt(8)
 35	b9 := big.NewInt(9)
 36	b10 := big.NewInt(10)
 37	b11 := big.NewInt(11)
 38	b12 := big.NewInt(12)
 39	h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12})
 40	assert.Nil(t, err)
 41	assert.Equal(t, "15278801138972282646981503374384603641625274360649669926363020545395022098027", h.String())
 42
 43	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.")
 44	n := 31
 45	msgElems := make([]*big.Int, 0, len(msg)/n+1)
 46	for i := 0; i < len(msg)/n; i++ {
 47		v := new(big.Int)
 48		utils.SetBigIntFromLEBytes(v, msg[n*i:n*(i+1)])
 49		msgElems = append(msgElems, v)
 50	}
 51	if len(msg)%n != 0 {
 52		v := new(big.Int)
 53		utils.SetBigIntFromLEBytes(v, msg[(len(msg)/n)*n:])
 54		msgElems = append(msgElems, v)
 55	}
 56	hmsg, err := Hash(msgElems)
 57	assert.Nil(t, err)
 58	assert.Equal(t, "16019700159595764790637132363672701294192939959594423814006267756172551741065", hmsg.String())
 59
 60	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.")
 61	msg2Elems := make([]*big.Int, 0, len(msg2)/n+1)
 62	for i := 0; i < len(msg2)/n; i++ {
 63		v := new(big.Int)
 64		utils.SetBigIntFromLEBytes(v, msg2[n*i:n*(i+1)])
 65		msg2Elems = append(msg2Elems, v)
 66	}
 67	if len(msg2)%n != 0 {
 68		v := new(big.Int)
 69		utils.SetBigIntFromLEBytes(v, msg2[(len(msg2)/n)*n:])
 70		msg2Elems = append(msg2Elems, v)
 71	}
 72	hmsg2, err := Hash(msg2Elems)
 73	assert.Nil(t, err)
 74	assert.Equal(t, "2978613163687734485261639854325792381691890647104372645321246092227111432722", hmsg2.String())
 75
 76	hmsg2, err = HashBytes(msg2)
 77	assert.Nil(t, err)
 78	assert.Equal(t, "2978613163687734485261639854325792381691890647104372645321246092227111432722", hmsg2.String())
 79}
 80
 81func TestPoseidonBrokenChunks(t *testing.T) {
 82	h1, err := Hash([]*big.Int{big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4),
 83		big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9)})
 84	assert.Nil(t, err)
 85	h2, err := Hash([]*big.Int{big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9),
 86		big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4)})
 87	assert.Nil(t, err)
 88	assert.NotEqual(t, h1, h2)
 89}
 90
 91func TestPoseidonBrokenPadding(t *testing.T) {
 92	h1, err := Hash([]*big.Int{big.NewInt(int64(1))})
 93	assert.Nil(t, err)
 94	h2, err := Hash([]*big.Int{big.NewInt(int64(1)), big.NewInt(int64(0))})
 95	assert.Nil(t, err)
 96	assert.NotEqual(t, h1, h2)
 97}
 98
 99func BenchmarkPoseidon(b *testing.B) {
100	b12 := big.NewInt(int64(12))
101	b45 := big.NewInt(int64(45))
102	b78 := big.NewInt(int64(78))
103	b41 := big.NewInt(int64(41))
104	bigArray4 := []*big.Int{b12, b45, b78, b41}
105
106	for i := 0; i < b.N; i++ {
107		Hash(bigArray4)
108	}
109}
110
111func BenchmarkPoseidonLarge(b *testing.B) {
112	b12 := utils.NewIntFromString("11384336176656855268977457483345535180380036354188103142384839473266348197733")
113	b45 := utils.NewIntFromString("11384336176656855268977457483345535180380036354188103142384839473266348197733")
114	b78 := utils.NewIntFromString("11384336176656855268977457483345535180380036354188103142384839473266348197733")
115	b41 := utils.NewIntFromString("11384336176656855268977457483345535180380036354188103142384839473266348197733")
116
117	bigArray4 := []*big.Int{b12, b45, b78, b41}
118
119	for i := 0; i < b.N; i++ {
120		Hash(bigArray4)
121	}
122}