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.

139 lines
4.4 KiB

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