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.

128 lines
2.6 KiB

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