arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
Resolve #4
arnaucube root@arnaucube.com
Tue, 03 Mar 2020 16:31:09 +0100
commit

83f87bfa46c4e64ea66d732c2bda3b4dd8a7e88e

parent

17bad7585348f5a3e3643276eb8e99404c5cabbb

3 files changed, 38 insertions(+), 11 deletions(-)

jump to
M constants/constants.go → 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)) + } }
M mimc7/mimc7.go → mimc7/mimc7.go

@@ -75,7 +75,7 @@ }

// 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 @@ }

// 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
M utils/utils.go → utils/utils.go

@@ -6,6 +6,9 @@ "encoding/hex"

"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 @@ }

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} +}