edwards: add signature IsEqual and Verify methods

This commit is contained in:
David Hill 2019-03-15 20:21:51 -04:00
parent f0ef1e4e2c
commit aa76fb64a1

View File

@ -8,6 +8,8 @@ package edwards
import (
"fmt"
"math/big"
"github.com/agl/ed25519"
)
// Signature is a type representing an ecdsa signature.
@ -39,6 +41,27 @@ func (sig Signature) Serialize() []byte {
return all
}
// IsEqual compares this Signature instance to the one passed, returning true
// if both Signatures are equivalent. A signature is equivalent to another, if
// they both have the same scalar value for R and S.
func (sig *Signature) IsEqual(otherSig *Signature) bool {
return sig.R.Cmp(otherSig.R) == 0 &&
sig.S.Cmp(otherSig.S) == 0
}
// Verify verifies a message 'hash' using the given public keys and signature.
func (sig *Signature) Verify(hash []byte, pubKey *PublicKey) bool {
if pubKey == nil || hash == nil {
return false
}
pubBytes := pubKey.Serialize()
sigBytes := sig.Serialize()
pubArray := copyBytes(pubBytes)
sigArray := copyBytes64(sigBytes)
return ed25519.Verify(pubArray, hash, sigArray)
}
// parseSig is the default method of parsing a serialized Ed25519 signature.
func parseSig(sigStr []byte, der bool) (*Signature, error) {
if der {