You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package poseidon
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/iden3/go-iden3-crypto/utils"
  7. "github.com/stretchr/testify/assert"
  8. "golang.org/x/crypto/blake2b"
  9. )
  10. func TestBlake2bVersion(t *testing.T) {
  11. h := blake2b.Sum256([]byte("poseidon_constants"))
  12. assert.Equal(t, "e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1", hex.EncodeToString(h[:]))
  13. }
  14. func TestPoseidonHash(t *testing.T) {
  15. b0 := big.NewInt(0)
  16. b1 := big.NewInt(1)
  17. b2 := big.NewInt(2)
  18. h, err := Hash([]*big.Int{b1})
  19. assert.Nil(t, err)
  20. assert.Equal(t, "11043376183861534927536506085090418075369306574649619885724436265926427398571", h.String())
  21. h, err = Hash([]*big.Int{b1, b2})
  22. assert.Nil(t, err)
  23. assert.Equal(t, "17117985411748610629288516079940078114952304104811071254131751175361957805920", h.String())
  24. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0})
  25. assert.Nil(t, err)
  26. assert.Equal(t, "3975478831357328722254985704342968745327876719981393787143845259590563829094", h.String())
  27. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
  28. assert.Nil(t, err)
  29. assert.Equal(t, "19772360636270345724087386688434825760738403416279047262510528378903625000110", h.String())
  30. b3 := big.NewInt(3)
  31. b4 := big.NewInt(4)
  32. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0})
  33. assert.Nil(t, err)
  34. assert.Equal(t, "3181200837746671699652342497997860344148947482942465819251904554707352676086", h.String())
  35. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0, b0})
  36. assert.Nil(t, err)
  37. assert.Equal(t, "8386348873272147968934270337233829407378789978142456170950021426339096575008", h.String())
  38. b5 := big.NewInt(5)
  39. b6 := big.NewInt(6)
  40. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6})
  41. assert.Nil(t, err)
  42. assert.Equal(t, "5202465217520500374834597824465244016759843635092906214933648999760272616044", h.String())
  43. }
  44. func TestErrorInputs(t *testing.T) {
  45. b0 := big.NewInt(0)
  46. b1 := big.NewInt(1)
  47. b2 := big.NewInt(2)
  48. _, err := Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
  49. assert.Nil(t, err)
  50. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0})
  51. assert.NotNil(t, err)
  52. assert.Equal(t, "invalid inputs length 7, max 7", err.Error())
  53. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0})
  54. assert.NotNil(t, err)
  55. assert.Equal(t, "invalid inputs length 8, max 7", err.Error())
  56. }
  57. func BenchmarkPoseidonHash(b *testing.B) {
  58. b0 := big.NewInt(0)
  59. b1 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061")
  60. b2 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061")
  61. bigArray4 := []*big.Int{b1, b2, b0, b0, b0, b0}
  62. for i := 0; i < b.N; i++ {
  63. Hash(bigArray4) //nolint:errcheck
  64. }
  65. }