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.

79 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
  1. package poseidon
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/iden3/go-iden3-crypto/utils"
  7. "github.com/stretchr/testify/assert"
  8. "golang.org/x/crypto/blake2b"
  9. )
  10. func TestBlake2bVersion(t *testing.T) {
  11. h := blake2b.Sum256([]byte("poseidon_constants"))
  12. assert.Equal(t, "e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1", hex.EncodeToString(h[:]))
  13. }
  14. func TestPoseidon(t *testing.T) {
  15. b1 := big.NewInt(int64(1))
  16. b2 := big.NewInt(int64(2))
  17. h, err := Hash([]*big.Int{b1, b2})
  18. assert.Nil(t, err)
  19. assert.Equal(t, "12242166908188651009877250812424843524687801523336557272219921456462821518061", h.String())
  20. b3 := big.NewInt(int64(3))
  21. b4 := big.NewInt(int64(4))
  22. h, err = Hash([]*big.Int{b3, b4})
  23. assert.Nil(t, err)
  24. assert.Equal(t, "17185195740979599334254027721507328033796809509313949281114643312710535000993", h.String())
  25. msg := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
  26. n := 31
  27. msgElems := make([]*big.Int, 0, len(msg)/n+1)
  28. for i := 0; i < len(msg)/n; i++ {
  29. v := new(big.Int)
  30. utils.SetBigIntFromLEBytes(v, msg[n*i:n*(i+1)])
  31. msgElems = append(msgElems, v)
  32. }
  33. if len(msg)%n != 0 {
  34. v := new(big.Int)
  35. utils.SetBigIntFromLEBytes(v, msg[(len(msg)/n)*n:])
  36. msgElems = append(msgElems, v)
  37. }
  38. hmsg, err := Hash(msgElems)
  39. assert.Nil(t, err)
  40. assert.Equal(t, "11821124228916291136371255062457365369197326845706357273715164664419275913793", hmsg.String())
  41. msg2 := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet.")
  42. msg2Elems := make([]*big.Int, 0, len(msg2)/n+1)
  43. for i := 0; i < len(msg2)/n; i++ {
  44. v := new(big.Int)
  45. utils.SetBigIntFromLEBytes(v, msg2[n*i:n*(i+1)])
  46. msg2Elems = append(msg2Elems, v)
  47. }
  48. if len(msg2)%n != 0 {
  49. v := new(big.Int)
  50. utils.SetBigIntFromLEBytes(v, msg2[(len(msg2)/n)*n:])
  51. msg2Elems = append(msg2Elems, v)
  52. }
  53. hmsg2, err := Hash(msg2Elems)
  54. assert.Nil(t, err)
  55. assert.Equal(t, "10747013384255785702102976082726575658403084163954725275481577373644732938016", hmsg2.String())
  56. hmsg2, err = HashBytes(msg2)
  57. assert.Nil(t, err)
  58. assert.Equal(t, "10747013384255785702102976082726575658403084163954725275481577373644732938016", hmsg2.String())
  59. }
  60. func BenchmarkPoseidon(b *testing.B) {
  61. b12 := big.NewInt(int64(12))
  62. b45 := big.NewInt(int64(45))
  63. b78 := big.NewInt(int64(78))
  64. b41 := big.NewInt(int64(41))
  65. bigArray4 := []*big.Int{b12, b45, b78, b41}
  66. for i := 0; i < b.N; i++ {
  67. Hash(bigArray4)
  68. }
  69. }