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.

134 lines
3.9 KiB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 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. "github.com/stretchr/testify/require"
  9. "golang.org/x/crypto/blake2b"
  10. )
  11. func TestBlake2bVersion(t *testing.T) {
  12. h := blake2b.Sum256([]byte("poseidon_constants"))
  13. assert.Equal(t,
  14. "e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1",
  15. hex.EncodeToString(h[:]))
  16. }
  17. func TestPoseidonHash(t *testing.T) {
  18. b0 := big.NewInt(0)
  19. b1 := big.NewInt(1)
  20. b2 := big.NewInt(2)
  21. b3 := big.NewInt(3)
  22. b4 := big.NewInt(4)
  23. b5 := big.NewInt(5)
  24. b6 := big.NewInt(6)
  25. b7 := big.NewInt(7)
  26. b8 := big.NewInt(8)
  27. b9 := big.NewInt(9)
  28. b10 := big.NewInt(10)
  29. b11 := big.NewInt(11)
  30. b12 := big.NewInt(12)
  31. b13 := big.NewInt(13)
  32. b14 := big.NewInt(14)
  33. b15 := big.NewInt(15)
  34. b16 := big.NewInt(16)
  35. h, err := Hash([]*big.Int{b1})
  36. assert.Nil(t, err)
  37. assert.Equal(t,
  38. "18586133768512220936620570745912940619677854269274689475585506675881198879027",
  39. h.String())
  40. h, err = Hash([]*big.Int{b1, b2})
  41. assert.Nil(t, err)
  42. assert.Equal(t,
  43. "7853200120776062878684798364095072458815029376092732009249414926327459813530",
  44. h.String())
  45. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0})
  46. assert.Nil(t, err)
  47. assert.Equal(t,
  48. "1018317224307729531995786483840663576608797660851238720571059489595066344487",
  49. h.String())
  50. h, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
  51. assert.Nil(t, err)
  52. assert.Equal(t,
  53. "15336558801450556532856248569924170992202208561737609669134139141992924267169",
  54. h.String())
  55. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0})
  56. assert.Nil(t, err)
  57. assert.Equal(t,
  58. "5811595552068139067952687508729883632420015185677766880877743348592482390548",
  59. h.String())
  60. h, err = Hash([]*big.Int{b3, b4, b0, b0, b0, b0})
  61. assert.Nil(t, err)
  62. assert.Equal(t,
  63. "12263118664590987767234828103155242843640892839966517009184493198782366909018",
  64. h.String())
  65. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6})
  66. assert.Nil(t, err)
  67. assert.Equal(t,
  68. "20400040500897583745843009878988256314335038853985262692600694741116813247201",
  69. h.String())
  70. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14})
  71. assert.Nil(t, err)
  72. assert.Equal(t,
  73. "8354478399926161176778659061636406690034081872658507739535256090879947077494",
  74. h.String())
  75. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b0, b0, b0, b0})
  76. assert.Nil(t, err)
  77. assert.Equal(t,
  78. "5540388656744764564518487011617040650780060800286365721923524861648744699539",
  79. h.String())
  80. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b0, b0, b0, b0, b0, b0})
  81. assert.Nil(t, err)
  82. assert.Equal(t,
  83. "11882816200654282475720830292386643970958445617880627439994635298904836126497",
  84. h.String())
  85. h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16})
  86. assert.Nil(t, err)
  87. assert.Equal(t,
  88. "9989051620750914585850546081941653841776809718687451684622678807385399211877",
  89. h.String())
  90. }
  91. func TestErrorInputs(t *testing.T) {
  92. b0 := big.NewInt(0)
  93. b1 := big.NewInt(1)
  94. b2 := big.NewInt(2)
  95. var err error
  96. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
  97. require.Nil(t, err)
  98. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
  99. require.NotNil(t, err)
  100. assert.Equal(t, "invalid inputs length 17, max 16", err.Error())
  101. _, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
  102. require.NotNil(t, err)
  103. assert.Equal(t, "invalid inputs length 18, max 16", err.Error())
  104. }
  105. func BenchmarkPoseidonHash(b *testing.B) {
  106. b0 := big.NewInt(0)
  107. b1 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061") //nolint:lll
  108. b2 := utils.NewIntFromString("12242166908188651009877250812424843524687801523336557272219921456462821518061") //nolint:lll
  109. bigArray4 := []*big.Int{b1, b2, b0, b0, b0, b0}
  110. for i := 0; i < b.N; i++ {
  111. Hash(bigArray4) //nolint:errcheck,gosec
  112. }
  113. }