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.

106 lines
3.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mimc7
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestKeccak256(t *testing.T) {
  10. res := crypto.Keccak256([]byte(SEED))
  11. assert.Equal(t,
  12. "b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e",
  13. hex.EncodeToString(res))
  14. c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
  15. assert.Equal(t,
  16. "82724731331859054037315113496710413141112897654334566532528783843265082629790",
  17. c.String())
  18. }
  19. func TestMIMC7Generic(t *testing.T) {
  20. b1 := big.NewInt(int64(1))
  21. b2 := big.NewInt(int64(2))
  22. b3 := big.NewInt(int64(3))
  23. bigArray := []*big.Int{b1, b2, b3}
  24. // Generic Hash
  25. mhg := MIMC7HashGeneric(b1, b2, 91)
  26. assert.Equal(t,
  27. "10594780656576967754230020536574539122676596303354946869887184401991294982664",
  28. mhg.String())
  29. hg, err := HashGeneric(big.NewInt(0), bigArray, 91)
  30. assert.Nil(t, err)
  31. assert.Equal(t,
  32. "6464402164086696096195815557694604139393321133243036833927490113253119343397",
  33. (*big.Int)(hg).String())
  34. }
  35. func TestMIMC7(t *testing.T) {
  36. b12 := big.NewInt(int64(12))
  37. b45 := big.NewInt(int64(45))
  38. b78 := big.NewInt(int64(78))
  39. b41 := big.NewInt(int64(41))
  40. // h1, hash of 1 elements
  41. bigArray1 := []*big.Int{b12}
  42. h1, err := Hash(bigArray1, nil)
  43. assert.Nil(t, err)
  44. // same hash value than the iden3js and circomlib tests:
  45. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h1).Bytes()),
  46. "0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd")
  47. // h2a, hash of 2 elements
  48. bigArray2a := []*big.Int{b78, b41}
  49. h2a, err := Hash(bigArray2a, nil)
  50. assert.Nil(t, err)
  51. // same hash value than the iden3js and circomlib tests:
  52. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2a).Bytes()),
  53. "0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70")
  54. // h2b, hash of 2 elements
  55. bigArray2b := []*big.Int{b12, b45}
  56. mh2b := MIMC7Hash(b12, b45)
  57. assert.Nil(t, err)
  58. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2b).Bytes()),
  59. "0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
  60. h2b, err := Hash(bigArray2b, nil)
  61. assert.Nil(t, err)
  62. // same hash value than the iden3js and circomlib tests:
  63. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2b).Bytes()),
  64. "0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879")
  65. // h4, hash of 4 elements
  66. bigArray4 := []*big.Int{b12, b45, b78, b41}
  67. h4, err := Hash(bigArray4, nil)
  68. assert.Nil(t, err)
  69. // same hash value than the iden3js and circomlib tests:
  70. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h4).Bytes()),
  71. "0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb")
  72. 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.") //nolint:lll
  73. hmsg := HashBytes(msg)
  74. assert.Equal(t,
  75. "16855787120419064316734350414336285711017110414939748784029922801367685456065",
  76. hmsg.String())
  77. }
  78. func BenchmarkMIMC7(b *testing.B) {
  79. b12 := big.NewInt(int64(12))
  80. b45 := big.NewInt(int64(45))
  81. b78 := big.NewInt(int64(78))
  82. b41 := big.NewInt(int64(41))
  83. bigArray4 := []*big.Int{b12, b45, b78, b41}
  84. for i := 0; i < b.N; i++ {
  85. Hash(bigArray4, nil) //nolint:errcheck,gosec
  86. }
  87. }