From 5e3e49fea4a68c12ec179d68ba1e0ab8867b3173 Mon Sep 17 00:00:00 2001 From: Gautam Botrel Date: Tue, 14 Jan 2025 08:59:16 -0600 Subject: [PATCH] perf: make SetBytes not allocate errors in non-fast path (#599) --- ecc/bls12-377/fp/element.go | 6 ++++-- ecc/bls12-377/fr/element.go | 6 ++++-- ecc/bls12-381/fp/element.go | 6 ++++-- ecc/bls12-381/fr/element.go | 6 ++++-- ecc/bls24-315/fp/element.go | 6 ++++-- ecc/bls24-315/fr/element.go | 6 ++++-- ecc/bls24-317/fp/element.go | 6 ++++-- ecc/bls24-317/fr/element.go | 6 ++++-- ecc/bn254/fp/element.go | 6 ++++-- ecc/bn254/fr/element.go | 6 ++++-- ecc/bw6-633/fp/element.go | 6 ++++-- ecc/bw6-633/fr/element.go | 6 ++++-- ecc/bw6-761/fp/element.go | 6 ++++-- ecc/bw6-761/fr/element.go | 6 ++++-- ecc/secp256k1/fp/element.go | 6 ++++-- ecc/secp256k1/fr/element.go | 6 ++++-- ecc/stark-curve/fp/element.go | 6 ++++-- ecc/stark-curve/fr/element.go | 6 ++++-- field/babybear/element.go | 6 ++++-- field/generator/internal/templates/element/conv.go | 6 ++++-- field/goldilocks/element.go | 6 ++++-- field/koalabear/element.go | 6 ++++-- 22 files changed, 88 insertions(+), 44 deletions(-) diff --git a/ecc/bls12-377/fp/element.go b/ecc/bls12-377/fp/element.go index 163bfa737b..d76fa8ac61 100644 --- a/ecc/bls12-377/fp/element.go +++ b/ecc/bls12-377/fp/element.go @@ -1214,6 +1214,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1231,7 +1233,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1265,7 +1267,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.LittleEndian.Uint64((*b)[40:48]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls12-377/fr/element.go b/ecc/bls12-377/fr/element.go index 85bd40bb35..712d80226f 100644 --- a/ecc/bls12-377/fr/element.go +++ b/ecc/bls12-377/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls12-381/fp/element.go b/ecc/bls12-381/fp/element.go index 0a6ab8e21f..2782c81aa3 100644 --- a/ecc/bls12-381/fp/element.go +++ b/ecc/bls12-381/fp/element.go @@ -1214,6 +1214,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1231,7 +1233,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1265,7 +1267,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.LittleEndian.Uint64((*b)[40:48]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls12-381/fr/element.go b/ecc/bls12-381/fr/element.go index 7fe789341e..6d22d7372b 100644 --- a/ecc/bls12-381/fr/element.go +++ b/ecc/bls12-381/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls24-315/fp/element.go b/ecc/bls24-315/fp/element.go index 5c9d2ad40c..9cab40d76a 100644 --- a/ecc/bls24-315/fp/element.go +++ b/ecc/bls24-315/fp/element.go @@ -1130,6 +1130,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1146,7 +1148,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1178,7 +1180,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.LittleEndian.Uint64((*b)[32:40]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls24-315/fr/element.go b/ecc/bls24-315/fr/element.go index 518f6908f4..72d4cb627f 100644 --- a/ecc/bls24-315/fr/element.go +++ b/ecc/bls24-315/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls24-317/fp/element.go b/ecc/bls24-317/fp/element.go index e061a5240c..88b9303564 100644 --- a/ecc/bls24-317/fp/element.go +++ b/ecc/bls24-317/fp/element.go @@ -1130,6 +1130,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1146,7 +1148,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1178,7 +1180,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.LittleEndian.Uint64((*b)[32:40]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bls24-317/fr/element.go b/ecc/bls24-317/fr/element.go index 01cbdc854f..6663dfff2d 100644 --- a/ecc/bls24-317/fr/element.go +++ b/ecc/bls24-317/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bn254/fp/element.go b/ecc/bn254/fp/element.go index 44aaa929bc..0d9cc8bd43 100644 --- a/ecc/bn254/fp/element.go +++ b/ecc/bn254/fp/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bn254/fr/element.go b/ecc/bn254/fr/element.go index a8f372c092..105442d6e3 100644 --- a/ecc/bn254/fr/element.go +++ b/ecc/bn254/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bw6-633/fp/element.go b/ecc/bw6-633/fp/element.go index e1cb5a8210..306ba9a694 100644 --- a/ecc/bw6-633/fp/element.go +++ b/ecc/bw6-633/fp/element.go @@ -1610,6 +1610,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1631,7 +1633,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[9] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1673,7 +1675,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[9] = binary.LittleEndian.Uint64((*b)[72:80]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bw6-633/fr/element.go b/ecc/bw6-633/fr/element.go index 286f0cbc97..065bfd5806 100644 --- a/ecc/bw6-633/fr/element.go +++ b/ecc/bw6-633/fr/element.go @@ -1130,6 +1130,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1146,7 +1148,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1178,7 +1180,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[4] = binary.LittleEndian.Uint64((*b)[32:40]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bw6-761/fp/element.go b/ecc/bw6-761/fp/element.go index b42b76e5a0..904b64e3bc 100644 --- a/ecc/bw6-761/fp/element.go +++ b/ecc/bw6-761/fp/element.go @@ -1844,6 +1844,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1867,7 +1869,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[11] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1913,7 +1915,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[11] = binary.LittleEndian.Uint64((*b)[88:96]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/bw6-761/fr/element.go b/ecc/bw6-761/fr/element.go index 71d980d3cc..074517a6db 100644 --- a/ecc/bw6-761/fr/element.go +++ b/ecc/bw6-761/fr/element.go @@ -1214,6 +1214,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1231,7 +1233,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1265,7 +1267,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[5] = binary.LittleEndian.Uint64((*b)[40:48]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/secp256k1/fp/element.go b/ecc/secp256k1/fp/element.go index 9c5f79500b..1829f9b676 100644 --- a/ecc/secp256k1/fp/element.go +++ b/ecc/secp256k1/fp/element.go @@ -1083,6 +1083,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1098,7 +1100,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1128,7 +1130,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/secp256k1/fr/element.go b/ecc/secp256k1/fr/element.go index 9f20f3b412..619f39e0ac 100644 --- a/ecc/secp256k1/fr/element.go +++ b/ecc/secp256k1/fr/element.go @@ -1083,6 +1083,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1098,7 +1100,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1128,7 +1130,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/stark-curve/fp/element.go b/ecc/stark-curve/fp/element.go index 6e911a9281..6c37af974e 100644 --- a/ecc/stark-curve/fp/element.go +++ b/ecc/stark-curve/fp/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fp.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fp.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/ecc/stark-curve/fr/element.go b/ecc/stark-curve/fr/element.go index c9d96f98b4..ce6fac740a 100644 --- a/ecc/stark-curve/fr/element.go +++ b/ecc/stark-curve/fr/element.go @@ -1055,6 +1055,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid fr.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -1070,7 +1072,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -1100,7 +1102,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[3] = binary.LittleEndian.Uint64((*b)[24:32]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid fr.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/field/babybear/element.go b/field/babybear/element.go index 8fc970bd41..7a8bdb77c2 100644 --- a/field/babybear/element.go +++ b/field/babybear/element.go @@ -768,6 +768,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid babybear.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -780,7 +782,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.BigEndian.Uint32((*b)[0:4]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid babybear.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -804,7 +806,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.LittleEndian.Uint32((*b)[0:4]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid babybear.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/field/generator/internal/templates/element/conv.go b/field/generator/internal/templates/element/conv.go index 70bffcf631..113b0b1972 100644 --- a/field/generator/internal/templates/element/conv.go +++ b/field/generator/internal/templates/element/conv.go @@ -327,6 +327,8 @@ type ByteOrder interface { } +var errInvalidEncoding = errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -345,7 +347,7 @@ func (bigEndian) Element(b *[Bytes]byte) ({{.ElementName}}, error) { {{- end}} if !z.smallerThanModulus() { - return {{.ElementName}}{}, errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding") + return {{.ElementName}}{}, errInvalidEncoding } z.toMont() @@ -382,7 +384,7 @@ func (littleEndian) Element(b *[Bytes]byte) ({{.ElementName}}, error) { {{- end}} if !z.smallerThanModulus() { - return {{.ElementName}}{}, errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding") + return {{.ElementName}}{}, errInvalidEncoding } z.toMont() diff --git a/field/goldilocks/element.go b/field/goldilocks/element.go index 349928655a..4e2a293242 100644 --- a/field/goldilocks/element.go +++ b/field/goldilocks/element.go @@ -801,6 +801,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid goldilocks.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -813,7 +815,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.BigEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid goldilocks.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -837,7 +839,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.LittleEndian.Uint64((*b)[0:8]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid goldilocks.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() diff --git a/field/koalabear/element.go b/field/koalabear/element.go index c18ddf14a7..399e7e7515 100644 --- a/field/koalabear/element.go +++ b/field/koalabear/element.go @@ -768,6 +768,8 @@ type ByteOrder interface { String() string } +var errInvalidEncoding = errors.New("invalid koalabear.Element encoding") + // BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder. var BigEndian bigEndian @@ -780,7 +782,7 @@ func (bigEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.BigEndian.Uint32((*b)[0:4]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid koalabear.Element encoding") + return Element{}, errInvalidEncoding } z.toMont() @@ -804,7 +806,7 @@ func (littleEndian) Element(b *[Bytes]byte) (Element, error) { z[0] = binary.LittleEndian.Uint32((*b)[0:4]) if !z.smallerThanModulus() { - return Element{}, errors.New("invalid koalabear.Element encoding") + return Element{}, errInvalidEncoding } z.toMont()