diff --git a/curve/ecgfp5/scalar_field.go b/curve/ecgfp5/scalar_field.go index 6b977e8..05ada73 100644 --- a/curve/ecgfp5/scalar_field.go +++ b/curve/ecgfp5/scalar_field.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "math/big" + "github.com/elliottech/poseidon_crypto/field/goldilocks" gFp5 "github.com/elliottech/poseidon_crypto/field/goldilocks_quintic_extension" . "github.com/elliottech/poseidon_crypto/int" ) @@ -13,6 +14,18 @@ import ( // p = 1067993516717146951041484916571792702745057740581727230159139685185762082554198619328292418486241 type ECgFp5Scalar [5]uint64 +func (s ECgFp5Scalar) IsCanonical() bool { + for _, elem := range s { + if elem >= goldilocks.ORDER { + return false + } + } + if ToNonCanonicalBigInt(s).Cmp(ORDER) >= 0 { + return false + } + return true +} + var ( ORDER, _ = new(big.Int).SetString("1067993516717146951041484916571792702745057740581727230159139685185762082554198619328292418486241", 10) ZERO = ECgFp5Scalar{} diff --git a/signature/schnorr/schnorr.go b/signature/schnorr/schnorr.go index 8b3513c..910522d 100644 --- a/signature/schnorr/schnorr.go +++ b/signature/schnorr/schnorr.go @@ -15,6 +15,10 @@ type Signature struct { E curve.ECgFp5Scalar } +func (s Signature) IsCanonical() bool { + return s.E.IsCanonical() && s.S.IsCanonical() +} + var ZERO_SIG = Signature{ S: curve.ZERO, E: curve.ZERO, @@ -102,6 +106,10 @@ func Validate(pubKey, hashedMsg, sig []byte) error { } func IsSchnorrSignatureValid(pubKey, hashedMsg gFp5.Element, sig Signature) bool { + if !sig.IsCanonical() { + return false + } + pubKeyWs, ok := curve.DecodeFp5AsWeierstrass(pubKey) if !ok { return false