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.

94 lines
3.1 KiB

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