mirror of
https://github.com/arnaucube/go-iden3-crypto.git
synced 2026-02-07 03:26:39 +01:00
Resolve #4
This commit is contained in:
@@ -1,12 +1,15 @@
|
|||||||
package constants
|
package constants
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/iden3/go-iden3-crypto/utils"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/iden3/go-iden3-crypto/ff"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Q is the order of the integer field (Zq) that fits inside the SNARK.
|
// Q is the order of the integer field (Zq) that fits inside the SNARK.
|
||||||
var Q *big.Int
|
var Q *big.Int
|
||||||
|
var QE *ff.Element
|
||||||
|
|
||||||
// Zero is 0.
|
// Zero is 0.
|
||||||
var Zero *big.Int
|
var Zero *big.Int
|
||||||
@@ -21,6 +24,11 @@ func init() {
|
|||||||
Zero = big.NewInt(0)
|
Zero = big.NewInt(0)
|
||||||
One = big.NewInt(1)
|
One = big.NewInt(1)
|
||||||
MinusOne = 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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// 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) {
|
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")
|
return nil, errors.New("inputs values not inside Finite Field")
|
||||||
}
|
}
|
||||||
r := iv
|
r := iv
|
||||||
@@ -108,7 +108,7 @@ func MIMC7Hash(xIn, k *big.Int) *big.Int {
|
|||||||
|
|
||||||
// Hash performs the MIMC7 hash over a *big.Int array
|
// Hash performs the MIMC7 hash over a *big.Int array
|
||||||
func Hash(arr []*big.Int, key *big.Int) (*big.Int, error) {
|
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")
|
return nil, errors.New("inputs values not inside Finite Field")
|
||||||
}
|
}
|
||||||
var r *big.Int
|
var r *big.Int
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"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
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBigIntInField checks if given big.Int fits in a Field Q element
|
// CheckBigIntInField checks if given *big.Int fits in a Field Q element
|
||||||
func CheckBigIntInField(a *big.Int, q *big.Int) bool {
|
func CheckBigIntInField(a *big.Int) bool {
|
||||||
if a.Cmp(q) != -1 {
|
if a.Cmp(constants.Q) != -1 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBigIntArrayInField checks if given big.Int fits in a Field Q element
|
// CheckBigIntArrayInField checks if given *big.Int fits in a Field Q element
|
||||||
func CheckBigIntArrayInField(arr []*big.Int, q *big.Int) bool {
|
func CheckBigIntArrayInField(arr []*big.Int) bool {
|
||||||
for _, a := range arr {
|
for _, a := range arr {
|
||||||
if !CheckBigIntInField(a, q) {
|
if !CheckBigIntInField(a) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
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}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user