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.

97 lines
2.7 KiB

5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 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,
  13. "e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1",
  14. hex.EncodeToString(h[:]))
  15. }
  16. func TestPoseidonHash(t *testing.T) {
  17. b0 := big.NewInt(0)
  18. b1 := big.NewInt(1)
  19. b2 := big.NewInt(2)
  20. h, err := Hash([]*big.Int{b1})
  21. assert.Nil(t, err)
  22. assert.Equal(t,
  23. "11043376183861534927536506085090418075369306574649619885724436265926427398571",
  24. h.String())
  25. h, err = Hash([]*big.Int{b1, b2})
  26. assert.Nil(t, err)
  27. assert.Equal(t,
  28. "17117985411748610629288516079940078114952304104811071254131751175361957805920",
  29. h.String())
  30. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0})
  31. assert.Nil(t, err)
  32. assert.Equal(t,
  33. "3975478831357328722254985704342968745327876719981393787143845259590563829094",
  34. h.String())
  35. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
  36. assert.Nil(t, err)
  37. assert.Equal(t,
  38. "19772360636270345724087386688434825760738403416279047262510528378903625000110",
  39. h.String())
  40. b3 := big.NewInt(3)
  41. b4 := big.NewInt(4)
  42. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0})
  43. assert.Nil(t, err)
  44. assert.Equal(t,
  45. "3181200837746671699652342497997860344148947482942465819251904554707352676086",
  46. h.String())
  47. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0, b0})
  48. assert.Nil(t, err)
  49. assert.Equal(t,
  50. "8386348873272147968934270337233829407378789978142456170950021426339096575008",
  51. h.String())
  52. b5 := big.NewInt(5)
  53. b6 := big.NewInt(6)
  54. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6})
  55. assert.Nil(t, err)
  56. assert.Equal(t,
  57. "5202465217520500374834597824465244016759843635092906214933648999760272616044",
  58. h.String())
  59. }
  60. func TestErrorInputs(t *testing.T) {
  61. b0 := big.NewInt(0)
  62. b1 := big.NewInt(1)
  63. b2 := big.NewInt(2)
  64. _, err := Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
  65. assert.Nil(t, err)
  66. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0})
  67. assert.NotNil(t, err)
  68. assert.Equal(t, "invalid inputs length 7, max 7", err.Error())
  69. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0})
  70. assert.NotNil(t, err)
  71. assert.Equal(t, "invalid inputs length 8, max 7", err.Error())
  72. }
  73. func BenchmarkPoseidonHash(b *testing.B) {
  74. b0 := big.NewInt(0)
  75. b1 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061") //nolint:lll
  76. b2 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061") //nolint:lll
  77. bigArray4 := []*big.Int{b1, b2, b0, b0, b0, b0}
  78. for i := 0; i < b.N; i++ {
  79. Hash(bigArray4) //nolint:errcheck,gosec
  80. }
  81. }