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.

130 lines
2.6 KiB

  1. package poseidon
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/iden3/go-iden3-crypto/poseidon"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const prime uint64 = 18446744069414584321
  9. func TestPoseidonHashCompare(t *testing.T) {
  10. b0 := uint64(0)
  11. b1 := uint64(1)
  12. bm1 := prime - 1
  13. bM := prime
  14. h, err := Hash([NROUNDSF]uint64{b0, b0, b0, b0, b0, b0, b0, b0},
  15. [CAPLEN]uint64{b0, b0, b0, b0})
  16. assert.Nil(t, err)
  17. assert.Equal(t,
  18. [CAPLEN]uint64{
  19. 4330397376401421145,
  20. 14124799381142128323,
  21. 8742572140681234676,
  22. 14345658006221440202,
  23. }, h,
  24. )
  25. h, err = Hash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1},
  26. [CAPLEN]uint64{b1, b1, b1, b1})
  27. assert.Nil(t, err)
  28. assert.Equal(t,
  29. [CAPLEN]uint64{
  30. 16428316519797902711,
  31. 13351830238340666928,
  32. 682362844289978626,
  33. 12150588177266359240,
  34. }, h,
  35. )
  36. h, err = Hash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1},
  37. [CAPLEN]uint64{b1, b1, b1, b1})
  38. assert.Nil(t, err)
  39. assert.Equal(t,
  40. [CAPLEN]uint64{
  41. 16428316519797902711,
  42. 13351830238340666928,
  43. 682362844289978626,
  44. 12150588177266359240,
  45. }, h,
  46. )
  47. h, err = Hash(
  48. [NROUNDSF]uint64{bm1, bm1, bm1, bm1, bm1, bm1, bm1, bm1},
  49. [CAPLEN]uint64{bm1, bm1, bm1, bm1},
  50. )
  51. assert.Nil(t, err)
  52. assert.Equal(t,
  53. [CAPLEN]uint64{
  54. 13691089994624172887,
  55. 15662102337790434313,
  56. 14940024623104903507,
  57. 10772674582659927682,
  58. }, h,
  59. )
  60. h, err = Hash([NROUNDSF]uint64{bM, bM, bM, bM, bM, bM, bM, bM},
  61. [CAPLEN]uint64{b0, b0, b0, b0})
  62. assert.Nil(t, err)
  63. assert.Equal(t,
  64. [CAPLEN]uint64{
  65. 4330397376401421145,
  66. 14124799381142128323,
  67. 8742572140681234676,
  68. 14345658006221440202,
  69. }, h,
  70. )
  71. h, err = Hash([NROUNDSF]uint64{
  72. uint64(923978),
  73. uint64(235763497586),
  74. uint64(9827635653498),
  75. uint64(112870),
  76. uint64(289273673480943876),
  77. uint64(230295874986745876),
  78. uint64(6254867324987),
  79. uint64(2087),
  80. }, [CAPLEN]uint64{b0, b0, b0, b0})
  81. assert.Nil(t, err)
  82. assert.Equal(t,
  83. [CAPLEN]uint64{
  84. 1892171027578617759,
  85. 984732815927439256,
  86. 7866041765487844082,
  87. 8161503938059336191,
  88. }, h,
  89. )
  90. }
  91. func BenchmarkPoseidonHash12Inputs(b *testing.B) {
  92. bigArray12 := []*big.Int{
  93. big.NewInt(1),
  94. big.NewInt(2),
  95. big.NewInt(3),
  96. big.NewInt(4),
  97. big.NewInt(5),
  98. big.NewInt(6),
  99. big.NewInt(7),
  100. big.NewInt(8),
  101. big.NewInt(9),
  102. big.NewInt(10),
  103. big.NewInt(11),
  104. big.NewInt(12),
  105. }
  106. for i := 0; i < b.N; i++ {
  107. poseidon.Hash(bigArray12) //nolint:errcheck,gosec
  108. }
  109. }
  110. func BenchmarkNeptuneHash(b *testing.B) {
  111. inp := [NROUNDSF]uint64{1, 2, 3, 4, 5, 6, 7, 8}
  112. cap := [CAPLEN]uint64{10, 11, 12, 13}
  113. for i := 0; i < b.N; i++ {
  114. Hash(inp, cap) //nolint:errcheck,gosec
  115. }
  116. }