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.

137 lines
4.5 KiB

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