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.

141 lines
4.7 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 TestUtils(t *testing.T) {
  11. b1 := big.NewInt(int64(1))
  12. b2 := big.NewInt(int64(2))
  13. b3 := big.NewInt(int64(3))
  14. arrBigInt := []*big.Int{b1, b2, b3}
  15. // *big.Int array to RElem array
  16. rElems, err := BigIntsToRElems(arrBigInt)
  17. assert.Nil(t, err)
  18. // RElem array to *big.Int array
  19. bElems := RElemsToBigInts(rElems)
  20. assert.Equal(t, arrBigInt, bElems)
  21. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  22. assert.True(t, ok)
  23. // greater or equal than R will give error when passing bigInts to RElems, to fit in the R Finite Field
  24. overR, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495618", 10)
  25. assert.True(t, ok)
  26. _, err = BigIntsToRElems([]*big.Int{b1, overR, b2})
  27. assert.True(t, err != nil)
  28. _, err = BigIntsToRElems([]*big.Int{b1, r, b2})
  29. assert.True(t, err != nil)
  30. // smaller than R will not give error when passing bigInts to RElems, to fit in the R Finite Field
  31. underR, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495616", 10)
  32. assert.True(t, ok)
  33. _, err = BigIntsToRElems([]*big.Int{b1, underR, b2})
  34. assert.Nil(t, err)
  35. }
  36. func TestKeccak256(t *testing.T) {
  37. res := crypto.Keccak256([]byte(SEED))
  38. assert.Equal(t, "b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e", hex.EncodeToString(res))
  39. c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
  40. assert.Equal(t, "82724731331859054037315113496710413141112897654334566532528783843265082629790", c.String())
  41. }
  42. func TestMIMC7Generic(t *testing.T) {
  43. b1 := big.NewInt(int64(1))
  44. b2 := big.NewInt(int64(2))
  45. b3 := big.NewInt(int64(3))
  46. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  47. assert.True(t, ok)
  48. fqR := field.NewFq(r)
  49. bigArray := []*big.Int{b1, b2, b3}
  50. elementsArray, err := BigIntsToRElems(bigArray)
  51. assert.Nil(t, err)
  52. // Generic Hash
  53. mhg := MIMC7HashGeneric(fqR, b1, b2, 91)
  54. assert.Nil(t, err)
  55. assert.Equal(t, "10594780656576967754230020536574539122676596303354946869887184401991294982664", mhg.String())
  56. hg, err := HashGeneric(fqR.Zero(), elementsArray, fqR, 91)
  57. assert.Nil(t, err)
  58. assert.Equal(t, "6464402164086696096195815557694604139393321133243036833927490113253119343397", (*big.Int)(hg).String())
  59. }
  60. func TestMIMC7(t *testing.T) {
  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. // h1, hash of 1 elements
  66. bigArray1 := []*big.Int{b12}
  67. elementsArray1, err := BigIntsToRElems(bigArray1)
  68. assert.Nil(t, err)
  69. h1 := Hash(elementsArray1, nil)
  70. assert.Nil(t, err)
  71. // same hash value than the iden3js and circomlib tests:
  72. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h1).Bytes()), "0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd")
  73. // h2a, hash of 2 elements
  74. bigArray2a := []*big.Int{b78, b41}
  75. elementsArray2a, err := BigIntsToRElems(bigArray2a)
  76. assert.Nil(t, err)
  77. h2a := Hash(elementsArray2a, nil)
  78. assert.Nil(t, err)
  79. // same hash value than the iden3js and circomlib tests:
  80. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2a).Bytes()), "0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70")
  81. // h2b, hash of 2 elements
  82. bigArray2b := []*big.Int{b12, b45}
  83. elementsArray2b, err := BigIntsToRElems(bigArray2b)
  84. assert.Nil(t, err)
  85. mh2b := MIMC7Hash(b12, b45)
  86. assert.Nil(t, err)
  87. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2b).Bytes()), "0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
  88. h2b := Hash(elementsArray2b, nil)
  89. assert.Nil(t, err)
  90. // same hash value than the iden3js and circomlib tests:
  91. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2b).Bytes()), "0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879")
  92. // h4, hash of 4 elements
  93. bigArray4 := []*big.Int{b12, b45, b78, b41}
  94. elementsArray4, err := BigIntsToRElems(bigArray4)
  95. assert.Nil(t, err)
  96. h4 := Hash(elementsArray4, nil)
  97. assert.Nil(t, err)
  98. // same hash value than the iden3js and circomlib tests:
  99. assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h4).Bytes()), "0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb")
  100. }
  101. func BenchmarkMIMC7(b *testing.B) {
  102. b12 := big.NewInt(int64(12))
  103. b45 := big.NewInt(int64(45))
  104. b78 := big.NewInt(int64(78))
  105. b41 := big.NewInt(int64(41))
  106. bigArray4 := []*big.Int{b12, b45, b78, b41}
  107. elementsArray4, err := BigIntsToRElems(bigArray4)
  108. assert.Nil(b, err)
  109. var h4 RElem
  110. for i := 0; i < b.N; i++ {
  111. h4 = Hash(elementsArray4, nil)
  112. }
  113. println(h4)
  114. }