schnorr: Rename error codes to better match reality.

This renames some error codes to more closely match the failure as
follows:

- ErrBadInputSize -> ErrInvalidHashLen
- ErrInputValue -> ErrPrivateKeyIsZero
- ErrPointNotOnCurve -> ErrPubKeyNotOnCurve
- ErrBadSigRYValue -> ErrSigRYIsOdd
- ErrBadSigRNotOnCurve -> ErrSigRNotOnCurve
This commit is contained in:
Dave Collins 2020-04-02 00:09:57 -05:00
parent 0cd04a0b0c
commit 277e4e4574
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
4 changed files with 75 additions and 74 deletions

View File

@ -1,5 +1,5 @@
// Copyright (c) 2014 Conformal Systems LLC.
// Copyright (c) 2015-2016 The Decred developers
// Copyright (c) 2015-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
@ -16,27 +16,28 @@ type ErrorCode int
// These constants are used to identify a specific RuleError.
const (
// ErrBadInputSize indicates that input to a signature was of the wrong size.
ErrBadInputSize ErrorCode = iota
// ErrInvalidHashLen indicates that the input hash to sign or verify is not
// the required length.
ErrInvalidHashLen ErrorCode = iota
// ErrInputValue indicates that the value of an input was wrong (e.g. zero).
ErrInputValue
// ErrPrivateKeyIsZero indicates an attempt was made to sign a message with
// a private key that is equal to zero.
ErrPrivateKeyIsZero
// ErrSchnorrHashValue indicates that the hash of (R || m) was too large
// and so a new k value (nonce) should be used.
// ErrSchnorrHashValue indicates that the hash of (R || m) was too large and
// so a new nonce should be used.
ErrSchnorrHashValue
// ErrPointNotOnCurve indicates that a point was not on the given
// elliptic curve.
ErrPointNotOnCurve
// ErrPubKeyNotOnCurve indicates that a point was not on the given elliptic
// curve.
ErrPubKeyNotOnCurve
// ErrBadSigRYValue indicates that the calculated Y value of R was odd,
// which is not allowed.
ErrBadSigRYValue
// ErrSigRYIsOdd indicates that the calculated Y value of R was odd.
ErrSigRYIsOdd
// ErrBadSigRNotOnCurve indicates that the calculated or given point R for some
// ErrSigRNotOnCurve indicates that the calculated or given point R for some
// signature was not on the curve.
ErrBadSigRNotOnCurve
ErrSigRNotOnCurve
// ErrUnequalRValues indicates that the calculated point R for some
// signature was not the same as the given R value for the signature.
@ -65,17 +66,17 @@ const (
// Map of ErrorCode values back to their constant names for pretty printing.
var errorCodeStrings = map[ErrorCode]string{
ErrBadInputSize: "ErrBadInputSize",
ErrInputValue: "ErrInputValue",
ErrSchnorrHashValue: "ErrSchnorrHashValue",
ErrPointNotOnCurve: "ErrPointNotOnCurve",
ErrBadSigRYValue: "ErrBadSigRYValue",
ErrBadSigRNotOnCurve: "ErrBadSigRNotOnCurve",
ErrUnequalRValues: "ErrUnequalRValues",
ErrSigTooShort: "ErrSigTooShort",
ErrSigTooLong: "ErrSigTooLong",
ErrSigRTooBig: "ErrSigRTooBig",
ErrSigSTooBig: "ErrSigSTooBig",
ErrInvalidHashLen: "ErrInvalidHashLen",
ErrPrivateKeyIsZero: "ErrPrivateKeyIsZero",
ErrSchnorrHashValue: "ErrSchnorrHashValue",
ErrPubKeyNotOnCurve: "ErrPubKeyNotOnCurve",
ErrSigRYIsOdd: "ErrSigRYIsOdd",
ErrSigRNotOnCurve: "ErrSigRNotOnCurve",
ErrUnequalRValues: "ErrUnequalRValues",
ErrSigTooShort: "ErrSigTooShort",
ErrSigTooLong: "ErrSigTooLong",
ErrSigRTooBig: "ErrSigRTooBig",
ErrSigSTooBig: "ErrSigSTooBig",
}
// String returns the ErrorCode as a human-readable name.

View File

@ -15,12 +15,12 @@ func TestErrorCodeStringer(t *testing.T) {
in ErrorCode
want string
}{
{ErrBadInputSize, "ErrBadInputSize"},
{ErrInputValue, "ErrInputValue"},
{ErrInvalidHashLen, "ErrInvalidHashLen"},
{ErrPrivateKeyIsZero, "ErrPrivateKeyIsZero"},
{ErrSchnorrHashValue, "ErrSchnorrHashValue"},
{ErrPointNotOnCurve, "ErrPointNotOnCurve"},
{ErrBadSigRYValue, "ErrBadSigRYValue"},
{ErrBadSigRNotOnCurve, "ErrBadSigRNotOnCurve"},
{ErrPubKeyNotOnCurve, "ErrPubKeyNotOnCurve"},
{ErrSigRYIsOdd, "ErrSigRYIsOdd"},
{ErrSigRNotOnCurve, "ErrSigRNotOnCurve"},
{ErrUnequalRValues, "ErrUnequalRValues"},
{ErrSigTooShort, "ErrSigTooShort"},
{ErrSigTooLong, "ErrSigTooLong"},
@ -76,53 +76,53 @@ func TestErrorCodeIsAs(t *testing.T) {
wantMatch bool
wantAs ErrorCode
}{{
name: "ErrBadInputSize == ErrBadInputSize",
err: ErrBadInputSize,
target: ErrBadInputSize,
name: "ErrInvalidHashLen == ErrInvalidHashLen",
err: ErrInvalidHashLen,
target: ErrInvalidHashLen,
wantMatch: true,
wantAs: ErrBadInputSize,
wantAs: ErrInvalidHashLen,
}, {
name: "Error.ErrBadInputSize == ErrBadInputSize",
err: signatureError(ErrBadInputSize, ""),
target: ErrBadInputSize,
name: "Error.ErrInvalidHashLen == ErrInvalidHashLen",
err: signatureError(ErrInvalidHashLen, ""),
target: ErrInvalidHashLen,
wantMatch: true,
wantAs: ErrBadInputSize,
wantAs: ErrInvalidHashLen,
}, {
name: "ErrBadInputSize == Error.ErrBadInputSize",
err: ErrBadInputSize,
target: signatureError(ErrBadInputSize, ""),
name: "ErrInvalidHashLen == Error.ErrInvalidHashLen",
err: ErrInvalidHashLen,
target: signatureError(ErrInvalidHashLen, ""),
wantMatch: true,
wantAs: ErrBadInputSize,
wantAs: ErrInvalidHashLen,
}, {
name: "Error.ErrBadInputSize == Error.ErrBadInputSize",
err: signatureError(ErrBadInputSize, ""),
target: signatureError(ErrBadInputSize, ""),
name: "Error.ErrInvalidHashLen == Error.ErrInvalidHashLen",
err: signatureError(ErrInvalidHashLen, ""),
target: signatureError(ErrInvalidHashLen, ""),
wantMatch: true,
wantAs: ErrBadInputSize,
wantAs: ErrInvalidHashLen,
}, {
name: "ErrInputValue != ErrBadInputSize",
err: ErrInputValue,
target: ErrBadInputSize,
name: "ErrPrivateKeyIsZero != ErrInvalidHashLen",
err: ErrPrivateKeyIsZero,
target: ErrInvalidHashLen,
wantMatch: false,
wantAs: ErrInputValue,
wantAs: ErrPrivateKeyIsZero,
}, {
name: "Error.ErrInputValue != ErrBadInputSize",
err: signatureError(ErrInputValue, ""),
target: ErrBadInputSize,
name: "Error.ErrPrivateKeyIsZero != ErrInvalidHashLen",
err: signatureError(ErrPrivateKeyIsZero, ""),
target: ErrInvalidHashLen,
wantMatch: false,
wantAs: ErrInputValue,
wantAs: ErrPrivateKeyIsZero,
}, {
name: "ErrInputValue != Error.ErrBadInputSize",
err: ErrInputValue,
target: signatureError(ErrBadInputSize, ""),
name: "ErrPrivateKeyIsZero != Error.ErrInvalidHashLen",
err: ErrPrivateKeyIsZero,
target: signatureError(ErrInvalidHashLen, ""),
wantMatch: false,
wantAs: ErrInputValue,
wantAs: ErrPrivateKeyIsZero,
}, {
name: "Error.ErrInputValue != Error.ErrBadInputSize",
err: signatureError(ErrInputValue, ""),
target: signatureError(ErrBadInputSize, ""),
name: "Error.ErrPrivateKeyIsZero != Error.ErrInvalidHashLen",
err: signatureError(ErrPrivateKeyIsZero, ""),
target: signatureError(ErrInvalidHashLen, ""),
wantMatch: false,
wantAs: ErrInputValue,
wantAs: ErrPrivateKeyIsZero,
}}
for _, test := range tests {

View File

@ -142,7 +142,7 @@ func schnorrVerify(sig *Signature, hash []byte, pubKey *secp256k1.PublicKey) err
if len(hash) != scalarSize {
str := fmt.Sprintf("wrong size for message (got %v, want %v)",
len(hash), scalarSize)
return signatureError(ErrBadInputSize, str)
return signatureError(ErrInvalidHashLen, str)
}
// Step 2.
@ -151,7 +151,7 @@ func schnorrVerify(sig *Signature, hash []byte, pubKey *secp256k1.PublicKey) err
curve := secp256k1.S256()
if !curve.IsOnCurve(pubKey.X(), pubKey.Y()) {
str := "pubkey point is not on curve"
return signatureError(ErrPointNotOnCurve, str)
return signatureError(ErrPubKeyNotOnCurve, str)
}
// Step 3.
@ -199,7 +199,7 @@ func schnorrVerify(sig *Signature, hash []byte, pubKey *secp256k1.PublicKey) err
// Fail if R is the point at infinity
if (R.X.IsZero() && R.Y.IsZero()) || R.Z.IsZero() {
str := "calculated R point is the point at infinity"
return signatureError(ErrBadSigRNotOnCurve, str)
return signatureError(ErrSigRNotOnCurve, str)
}
// Step 9.
@ -210,7 +210,7 @@ func schnorrVerify(sig *Signature, hash []byte, pubKey *secp256k1.PublicKey) err
R.ToAffine()
if R.Y.IsOdd() {
str := "calculated R y-value is odd"
return signatureError(ErrBadSigRYValue, str)
return signatureError(ErrSigRYIsOdd, str)
}
// Step 10.
@ -366,7 +366,7 @@ func Sign(privKey *secp256k1.PrivateKey, hash []byte) (*Signature, error) {
if len(hash) != scalarSize {
str := fmt.Sprintf("wrong size for message hash (got %v, want %v)",
len(hash), scalarSize)
return nil, signatureError(ErrBadInputSize, str)
return nil, signatureError(ErrInvalidHashLen, str)
}
// Step 2.
@ -375,7 +375,7 @@ func Sign(privKey *secp256k1.PrivateKey, hash []byte) (*Signature, error) {
privKeyScalar := &privKey.Key
if privKeyScalar.IsZero() {
str := "private key is zero"
return nil, signatureError(ErrInputValue, str)
return nil, signatureError(ErrPrivateKeyIsZero, str)
}
var privKeyBytes [scalarSize]byte

View File

@ -412,7 +412,7 @@ func TestVerifyErrors(t *testing.T) {
hash: "c301ba9de5d6053caad9f5eb46523f007702add2c62fa39de03146a36b8026b700",
pubX: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
pubY: "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
err: ErrBadInputSize,
err: ErrInvalidHashLen,
}, {
// Signature created from private key 0x01, blake256(0x40) and removing
// the leading zero byte. It is otherwise valid.
@ -422,7 +422,7 @@ func TestVerifyErrors(t *testing.T) {
hash: "0e0f08e2ee912478b77004ec62845b5e01418f03837b76cbdc8b1fb0480322",
pubX: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
pubY: "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
err: ErrBadInputSize,
err: ErrInvalidHashLen,
}, {
// Signature created from private key 0x01, blake256(0x01020304) over
// the secp256r1 curve (note the r1 instead of k1).
@ -432,7 +432,7 @@ func TestVerifyErrors(t *testing.T) {
hash: "c301ba9de5d6053caad9f5eb46523f007702add2c62fa39de03146a36b8026b7",
pubX: "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
pubY: "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
err: ErrPointNotOnCurve,
err: ErrPubKeyNotOnCurve,
}, {
// Signature invented since finding a signature with an r value that is
// exactly the field prime prior to the modular reduction is not
@ -490,7 +490,7 @@ func TestVerifyErrors(t *testing.T) {
hash: "c301ba9de5d6053caad9f5eb46523f007702add2c62fa39de03146a36b8026b7",
pubX: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
pubY: "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
err: ErrBadSigRNotOnCurve,
err: ErrSigRNotOnCurve,
}, {
// Signature created from private key 0x01, blake256(0x01020304050607).
// It is otherwise valid.
@ -500,7 +500,7 @@ func TestVerifyErrors(t *testing.T) {
hash: "ccf8c53a7631aad469d412963d495c729ff219dd2ae9a0c4de4bd1b4c777d49c",
pubX: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
pubY: "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
err: ErrBadSigRYValue,
err: ErrSigRYIsOdd,
}, {
// Signature created from private key 0x01, blake256(0x01020304). Thus,
// it is valid for that message. Attempting to verify wrong message