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.

151 lines
3.0 KiB

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