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.

124 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}, [CAPLEN]uint64{b0, b0, b0, b0})
  22. assert.Nil(t, err)
  23. assert.Equal(t,
  24. [CAPLEN]uint64{
  25. 4330397376401421145,
  26. 14124799381142128323,
  27. 8742572140681234676,
  28. 14345658006221440202,
  29. }, h,
  30. )
  31. h, err = compareHash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1}, [CAPLEN]uint64{b1, b1, b1, b1})
  32. assert.Nil(t, err)
  33. assert.Equal(t,
  34. [CAPLEN]uint64{
  35. 16428316519797902711,
  36. 13351830238340666928,
  37. 682362844289978626,
  38. 12150588177266359240,
  39. }, h,
  40. )
  41. h, err = compareHash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1}, [CAPLEN]uint64{b1, b1, b1, b1})
  42. assert.Nil(t, err)
  43. assert.Equal(t,
  44. [CAPLEN]uint64{
  45. 16428316519797902711,
  46. 13351830238340666928,
  47. 682362844289978626,
  48. 12150588177266359240,
  49. }, h,
  50. )
  51. h, err = compareHash(
  52. [NROUNDSF]uint64{bm1, bm1, bm1, bm1, bm1, bm1, bm1, bm1},
  53. [CAPLEN]uint64{bm1, bm1, bm1, bm1},
  54. )
  55. assert.Nil(t, err)
  56. assert.Equal(t,
  57. [CAPLEN]uint64{
  58. 13691089994624172887,
  59. 15662102337790434313,
  60. 14940024623104903507,
  61. 10772674582659927682,
  62. }, h,
  63. )
  64. h, err = compareHash([NROUNDSF]uint64{bM, bM, bM, bM, bM, bM, bM, bM}, [CAPLEN]uint64{b0, b0, b0, b0})
  65. assert.Nil(t, err)
  66. assert.Equal(t,
  67. [CAPLEN]uint64{
  68. 4330397376401421145,
  69. 14124799381142128323,
  70. 8742572140681234676,
  71. 14345658006221440202,
  72. }, h,
  73. )
  74. h, err = compareHash([NROUNDSF]uint64{
  75. uint64(923978),
  76. uint64(235763497586),
  77. uint64(9827635653498),
  78. uint64(112870),
  79. uint64(289273673480943876),
  80. uint64(230295874986745876),
  81. uint64(6254867324987),
  82. uint64(2087),
  83. }, [CAPLEN]uint64{b0, b0, b0, b0})
  84. assert.Nil(t, err)
  85. assert.Equal(t,
  86. [CAPLEN]uint64{
  87. 1892171027578617759,
  88. 984732815927439256,
  89. 7866041765487844082,
  90. 8161503938059336191,
  91. }, h,
  92. )
  93. }
  94. func BenchmarkPoseidonHash(b *testing.B) {
  95. inp := [NROUNDSF]uint64{1, 2, 3, 4, 5, 6, 7, 8}
  96. cap := [CAPLEN]uint64{10, 11, 12, 13}
  97. for i := 0; i < b.N; i++ {
  98. Hash(inp, cap) //nolint:errcheck,gosec
  99. }
  100. }
  101. func BenchmarkPoseidonNeptuneHash(b *testing.B) {
  102. inp := [NROUNDSF]uint64{1, 2, 3, 4, 5, 6, 7, 8}
  103. cap := [CAPLEN]uint64{10, 11, 12, 13}
  104. for i := 0; i < b.N; i++ {
  105. Hash(inp, cap) //nolint:errcheck,gosec
  106. }
  107. }