Browse Source

Resolve #4

feature/poseidon-opt-goff
arnaucube 4 years ago
parent
commit
83f87bfa46
3 changed files with 38 additions and 11 deletions
  1. +11
    -3
      constants/constants.go
  2. +2
    -2
      mimc7/mimc7.go
  3. +25
    -6
      utils/utils.go

+ 11
- 3
constants/constants.go

@ -1,12 +1,15 @@
package constants
import (
"github.com/iden3/go-iden3-crypto/utils"
"fmt"
"math/big"
"github.com/iden3/go-iden3-crypto/ff"
)
// Q is the order of the integer field (Zq) that fits inside the SNARK.
var Q *big.Int
var QE *ff.Element
// Zero is 0.
var Zero *big.Int
@ -21,6 +24,11 @@ func init() {
Zero = big.NewInt(0)
One = big.NewInt(1)
MinusOne = big.NewInt(-1)
Q = utils.NewIntFromString(
"21888242871839275222246405745257275088548364400416034343698204186575808495617")
qString := "21888242871839275222246405745257275088548364400416034343698204186575808495617"
var ok bool
Q, ok = new(big.Int).SetString(qString, 10)
if !ok {
panic(fmt.Sprintf("Bad base 10 string %s", qString))
}
}

+ 2
- 2
mimc7/mimc7.go

@ -75,7 +75,7 @@ func MIMC7HashGeneric(fqR field.Fq, xIn, k *big.Int, nRounds int) *big.Int {
// HashGeneric performs the MIMC7 hash over a *big.Int array, in a generic way, where it can be specified the Finite Field over R, and the number of rounds
func HashGeneric(iv *big.Int, arr []*big.Int, fqR field.Fq, nRounds int) (*big.Int, error) {
if !utils.CheckBigIntArrayInField(arr, constants.fqR.Q) {
if !utils.CheckBigIntArrayInField(arr) {
return nil, errors.New("inputs values not inside Finite Field")
}
r := iv
@ -108,7 +108,7 @@ func MIMC7Hash(xIn, k *big.Int) *big.Int {
// Hash performs the MIMC7 hash over a *big.Int array
func Hash(arr []*big.Int, key *big.Int) (*big.Int, error) {
if !utils.CheckBigIntArrayInField(arr, constants.fqR.Q) {
if !utils.CheckBigIntArrayInField(arr) {
return nil, errors.New("inputs values not inside Finite Field")
}
var r *big.Int

+ 25
- 6
utils/utils.go

@ -6,6 +6,9 @@ import (
"fmt"
"math/big"
"strings"
"github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-iden3-crypto/ff"
)
// NewIntFromString creates a new big.Int from a decimal integer encoded as a
@ -87,20 +90,36 @@ func HexDecodeInto(dst []byte, h []byte) error {
return nil
}
// CheckBigIntInField checks if given big.Int fits in a Field Q element
func CheckBigIntInField(a *big.Int, q *big.Int) bool {
if a.Cmp(q) != -1 {
// CheckBigIntInField checks if given *big.Int fits in a Field Q element
func CheckBigIntInField(a *big.Int) bool {
if a.Cmp(constants.Q) != -1 {
return false
}
return true
}
// CheckBigIntArrayInField checks if given big.Int fits in a Field Q element
func CheckBigIntArrayInField(arr []*big.Int, q *big.Int) bool {
// CheckBigIntArrayInField checks if given *big.Int fits in a Field Q element
func CheckBigIntArrayInField(arr []*big.Int) bool {
for _, a := range arr {
if !CheckBigIntInField(a, q) {
if !CheckBigIntInField(a) {
return false
}
}
return true
}
// CheckElementArrayInField checks if given *ff.Element fits in a Field Q element
func CheckElementArrayInField(arr []*ff.Element) bool {
for _, aE := range arr {
a := big.NewInt(0)
aE.ToBigIntRegular(a)
if !CheckBigIntInField(a) {
return false
}
}
return true
}
func NewElement() *ff.Element {
return &ff.Element{0, 0, 0, 0}
}

Loading…
Cancel
Save