From 5c73e593c718ab2ddeba8fb1108153c8007f27b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Men=C3=A9ndez?= Date: Mon, 18 Nov 2024 09:17:54 +0100 Subject: [PATCH] fixing endianess issues --- ff.go | 22 ++++++++++++++++++++++ hash.go | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 ff.go diff --git a/ff.go b/ff.go new file mode 100644 index 0000000..444e985 --- /dev/null +++ b/ff.go @@ -0,0 +1,22 @@ +package arbo + +import "math/big" + +var ( + // BN254BaseField is the base field for the BN254 curve. + BN254BaseField, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10) + // BLS12377BaseField is the base field for the BLS12377 curve. + BLS12377BaseField, _ = new(big.Int).SetString("25825498262808887005865186224201665565126143020923472090132963926938185026661", 10) +) + +// BigToFF function returns the finite field representation of the big.Int +// provided. It uses the curve scalar field to represent the provided number. +func BigToFF(baseField, iv *big.Int) *big.Int { + z := big.NewInt(0) + if c := iv.Cmp(baseField); c == 0 { + return z + } else if c != 1 && iv.Cmp(z) != -1 { + return iv + } + return z.Mod(iv, baseField) +} diff --git a/hash.go b/hash.go index ae498cc..3584d5f 100644 --- a/hash.go +++ b/hash.go @@ -144,9 +144,9 @@ func (f HashMiMC_BLS12_377) Len() int { func (f HashMiMC_BLS12_377) Hash(b ...[]byte) ([]byte, error) { h := mimc.NewMiMC() for i := 0; i < len(b); i++ { - if _, err := h.Write(b[i]); err != nil { + if _, err := h.Write(SwapEndianness(b[i])); err != nil { return nil, err } } - return h.Sum(nil), nil + return SwapEndianness(h.Sum(nil)), nil }