signature.mx raw

   1  // Copyright (c) 2013-2022 The btcsuite developers
   2  
   3  package schnorr
   4  
   5  import (
   6  	"fmt"
   7  
   8  	"smesh.lol/pkg/nostr/ec"
   9  	"smesh.lol/pkg/nostr/ec/chainhash"
  10  	"smesh.lol/pkg/nostr/ec/secp256k1"
  11  	"smesh.lol/pkg/lol/chk"
  12  )
  13  
  14  const (
  15  	// SignatureSize is the size of an encoded Schnorr signature.
  16  	SignatureSize = 64
  17  	// scalarSize is the size of an encoded big endian scalar.
  18  	scalarSize = 32
  19  )
  20  
  21  var (
  22  	// rfc6979ExtraDataV0 is the extra data to feed to RFC6979 when generating
  23  	// the deterministic nonce for the BIP-340 scheme. This ensures the same
  24  	// nonce is not generated for the same message and key as for other signing
  25  	// algorithms such as ECDSA.
  26  	//
  27  	// It is equal to SHA-256(by("BIP-340")).
  28  	rfc6979ExtraDataV0 = [32]uint8{
  29  		0xa3, 0xeb, 0x4c, 0x18, 0x2f, 0xae, 0x7e, 0xf4,
  30  		0xe8, 0x10, 0xc6, 0xee, 0x13, 0xb0, 0xe9, 0x26,
  31  		0x68, 0x6d, 0x71, 0xe8, 0x7f, 0x39, 0x4f, 0x79,
  32  		0x9c, 0x00, 0xa5, 0x21, 0x03, 0xcb, 0x4e, 0x17,
  33  	}
  34  )
  35  
  36  // Signature is a type representing a Schnorr signature.
  37  type Signature struct {
  38  	r btcec.FieldVal
  39  	s btcec.ModNScalar
  40  }
  41  
  42  // NewSignature instantiates a new signature given some r and s values.
  43  func NewSignature(r *btcec.FieldVal, s *btcec.ModNScalar) *Signature {
  44  	var sig Signature
  45  	sig.r.Set(r).Normalize()
  46  	sig.s.Set(s)
  47  	return &sig
  48  }
  49  
  50  // Serialize returns the Schnorr signature in a stricter format.
  51  //
  52  // The signatures are encoded as
  53  //
  54  //		sig[0:32]
  55  //	 	x coordinate of the point R, encoded as a big-endian uint256
  56  //		sig[32:64]
  57  //			s, encoded also as big-endian uint256
  58  func (sig Signature) Serialize() []byte {
  59  	// Total length of returned signature is the length of r and s.
  60  	var b [SignatureSize]byte
  61  	sig.r.PutBytesUnchecked(b[0:32])
  62  	sig.s.PutBytesUnchecked(b[32:64])
  63  	return b[:]
  64  }
  65  
  66  // ParseSignature parses a signature according to the BIP-340 specification and
  67  // enforces the following additional restrictions specific to secp256k1:
  68  //
  69  // - The r component must be in the valid range for secp256k1 field elements
  70  //
  71  // - The s component must be in the valid range for secp256k1 scalars
  72  func ParseSignature(sig []byte) (*Signature, error) {
  73  	// The signature must be the correct length.
  74  	sigLen := len(sig)
  75  	if sigLen < SignatureSize {
  76  		str := fmt.Sprintf(
  77  			"malformed signature: too short: %d < %d", sigLen,
  78  			SignatureSize,
  79  		)
  80  		return nil, signatureError(ErrSigTooShort, str)
  81  	}
  82  	if sigLen > SignatureSize {
  83  		str := fmt.Sprintf(
  84  			"malformed signature: too long: %d > %d", sigLen,
  85  			SignatureSize,
  86  		)
  87  		return nil, signatureError(ErrSigTooLong, str)
  88  	}
  89  	// The signature is validly encoded at this point, however, enforce
  90  	// additional restrictions to ensure r is in the range [0, p-1], and s is in
  91  	// the range [0, n-1] since valid Schnorr signatures are required to be in
  92  	// that range per spec.
  93  	var r btcec.FieldVal
  94  	if overflow := r.SetByteSlice(sig[0:32]); overflow {
  95  		str := "invalid signature: r >= field prime"
  96  		return nil, signatureError(ErrSigRTooBig, str)
  97  	}
  98  	var s btcec.ModNScalar
  99  	s.SetByteSlice(sig[32:64])
 100  	// Return the signature.
 101  	return NewSignature(&r, &s), nil
 102  }
 103  
 104  // IsEqual compares this Signature instance to the one passed, returning true if
 105  // both Signatures are equivalent. A signature is equivalent to another if they
 106  // both have the same scalar value for R and S.
 107  func (sig Signature) IsEqual(otherSig *Signature) bool {
 108  	return sig.r.Equals(&otherSig.r) && sig.s.Equals(&otherSig.s)
 109  }
 110  
 111  // schnorrVerify attempt to verify the signature for the provided hash and
 112  // secp256k1 public key and either returns nil if successful or a specific error
 113  // indicating why it failed if not successful.
 114  //
 115  // This differs from the exported Verify method in that it returns a specific
 116  // error to support better testing, while the exported method simply returns a
 117  // bool indicating success or failure.
 118  func schnorrVerify(sig *Signature, hash []byte, pubKeyBytes []byte) error {
 119  	// The algorithm for producing a BIP-340 signature is described in
 120  	// README.md and is reproduced here for reference:
 121  	//
 122  	// 1. Fail if m is not 32 bytes
 123  	// 2. P = lift_x(int(pk)).
 124  	// 3. r = int(sig[0:32]); fail is r >= p.
 125  	// 4. s = int(sig[32:64]); fail if s >= n.
 126  	// 5. e = int(tagged_hash("BIP0340/challenge", bytes(r) || bytes(P) || M)) mod n.
 127  	// 6. R = s*G - e*P
 128  	// 7. Fail if is_infinite(R)
 129  	// 8. Fail if not hash_even_y(R)
 130  	// 9. Fail is x(R) != r.
 131  	// 10. Return success iff not failure occured before reachign this
 132  	// point.
 133  
 134  	// // Step 1.
 135  	// //
 136  	// // Fail if m is not 32 bytes
 137  	// if len(hash) != scalarSize {
 138  	// 	str := fmt.Sprintf("wrong size for message (got %v, want %v)",
 139  	// 		len(hash), scalarSize)
 140  	// 	return signatureError(schnorr.ErrInvalidHashLen, str)
 141  	// }
 142  
 143  	// Step 2.
 144  	//
 145  	// P = lift_x(int(pk))
 146  	//
 147  	// Fail if P is not a point on the curve
 148  	pubKey, err := ParsePubKey(pubKeyBytes)
 149  	if chk.E(err) {
 150  		return err
 151  	}
 152  	if !pubKey.IsOnCurve() {
 153  		str := "pubkey point is not on curve"
 154  		return signatureError(ErrPubKeyNotOnCurve, str)
 155  	}
 156  	// Step 3.
 157  	//
 158  	// Fail if r >= p
 159  	//
 160  	// Note this is already handled by the fact r is a field element.
 161  	//
 162  	// Step 4.
 163  	//
 164  	// Fail if s >= n
 165  	//
 166  	// Note this is already handled by the fact s is a mod n scalar.
 167  	//
 168  	// Step 5.
 169  	//
 170  	// e = int(tagged_hash("BIP0340/challenge", bytes(r) || bytes(P) || M)) mod n.
 171  	var rBytes [32]byte
 172  	sig.r.PutBytesUnchecked(rBytes[:])
 173  	pBytes := SerializePubKey(pubKey)
 174  	commitment := chainhash.TaggedHash(
 175  		chainhash.TagBIP0340Challenge, rBytes[:], pBytes, hash,
 176  	)
 177  	var e btcec.ModNScalar
 178  	e.SetBytes((*[32]byte)(commitment))
 179  	// Negate e here so we can use AddNonConst below to subtract the s*G
 180  	// point from e*P.
 181  	e.Negate()
 182  	// Step 6.
 183  	//
 184  	// R = s*G - e*P
 185  	var P, R, sG, eP btcec.JacobianPoint
 186  	pubKey.AsJacobian(&P)
 187  	btcec.ScalarBaseMultNonConst(&sig.s, &sG)
 188  	btcec.ScalarMultNonConst(&e, &P, &eP)
 189  	btcec.AddNonConst(&sG, &eP, &R)
 190  	// Step 7.
 191  	//
 192  	// Fail if R is the point at infinity
 193  	if (R.X.IsZero() && R.Y.IsZero()) || R.Z.IsZero() {
 194  		str := "calculated R point is the point at infinity"
 195  		return signatureError(ErrSigRNotOnCurve, str)
 196  	}
 197  	// Step 8.
 198  	//
 199  	// Fail if R.y is odd
 200  	//
 201  	// Note that R must be in affine coordinates for this check.
 202  	R.ToAffine()
 203  	if R.Y.IsOdd() {
 204  		str := "calculated R y-value is odd"
 205  		return signatureError(ErrSigRYIsOdd, str)
 206  	}
 207  	// Step 9.
 208  	//
 209  	// Verified if R.x == r
 210  	//
 211  	// Note that R must be in affine coordinates for this check.
 212  	if !sig.r.Equals(&R.X) {
 213  		str := "calculated R point was not given R"
 214  		return signatureError(ErrUnequalRValues, str)
 215  	}
 216  	// Step 10.
 217  	//
 218  	// Return success iff not failure occured before reachign this
 219  	return nil
 220  }
 221  
 222  // Verify returns whether or not the signature is valid for the provided hash
 223  // and secp256k1 public key.
 224  func (sig *Signature) Verify(hash []byte, pubKey *btcec.PublicKey) bool {
 225  	pubkeyBytes := SerializePubKey(pubKey)
 226  	return schnorrVerify(sig, hash, pubkeyBytes) == nil
 227  }
 228  
 229  // zeroArray zeroes the memory of a scalar array.
 230  func zeroArray(a *[scalarSize]byte) {
 231  	for i := 0; i < scalarSize; i++ {
 232  		a[i] = 0x00
 233  	}
 234  }
 235  
 236  // schnorrSign generates an BIP-340 signature over the secp256k1 curve for the
 237  // provided hash (which should be the result of hashing a larger message) using
 238  // the given nonce and secret key. The produced signature is deterministic (the
 239  // same message, nonce, and key yield the same signature) and canonical.
 240  //
 241  // WARNING: The hash MUST be 32 bytes, and both the nonce and secret keys must
 242  // NOT be 0. Since this is an internal use function, these preconditions MUST be
 243  // satisified by the caller.
 244  func schnorrSign(
 245  	privKey, nonce *btcec.ModNScalar, pubKey *btcec.PublicKey,
 246  	hash []byte, opts *signOptions,
 247  ) (*Signature, error) {
 248  
 249  	// The algorithm for producing a BIP-340 signature is described in
 250  	// README.md and is reproduced here for reference:
 251  	//
 252  	// G = curve generator
 253  	// n = curve order
 254  	// d = secret key
 255  	// m = message
 256  	// a = input randmoness
 257  	// r, s = signature
 258  	//
 259  	// 1. d' = int(d)
 260  	// 2. Fail if m is not 32 bytes
 261  	// 3. Fail if d = 0 or d >= n
 262  	// 4. P = d'*G
 263  	// 5. Negate d if P.y is odd
 264  	// 6. t = bytes(d) xor tagged_hash("BIP0340/aux", t || bytes(P) || m)
 265  	// 7. rand = tagged_hash("BIP0340/nonce", a)
 266  	// 8. k' = int(rand) mod n
 267  	// 9. Fail if k' = 0
 268  	// 10. R = 'k*G
 269  	// 11. Negate k if R.y id odd
 270  	// 12. e = tagged_hash("BIP0340/challenge", bytes(R) || bytes(P) || m) mod n
 271  	// 13. sig = bytes(R) || bytes((k + e*d)) mod n
 272  	// 14. If Verify(bytes(P), m, sig) fails, abort.
 273  	// 15. return sig.
 274  	//
 275  	// Note that the set of functional options passed in may modify the
 276  	// above algorithm. Namely if CustomNonce is used, then steps 6-8 are
 277  	// replaced with a process that generates the nonce using rfc6979. If
 278  	// FastSign is passed, then we skip set 14.
 279  
 280  	// NOTE: Steps 1-9 are performed by the caller.
 281  
 282  	//
 283  	// Step 10.
 284  	//
 285  	// R = kG
 286  	var R btcec.JacobianPoint
 287  	k := *nonce
 288  	btcec.ScalarBaseMultNonConst(&k, &R)
 289  	// Step 11.
 290  	//
 291  	// Negate nonce k if R.y is odd (R.y is the y coordinate of the point R)
 292  	//
 293  	// Note that R must be in affine coordinates for this check.
 294  	R.ToAffine()
 295  	if R.Y.IsOdd() {
 296  		k.Negate()
 297  	}
 298  	// Step 12.
 299  	//
 300  	// e = tagged_hash("BIP0340/challenge", bytes(R) || bytes(P) || m) mod n
 301  	var rBytes [32]byte
 302  	r := &R.X
 303  	r.PutBytesUnchecked(rBytes[:])
 304  	pBytes := SerializePubKey(pubKey)
 305  	commitment := chainhash.TaggedHash(
 306  		chainhash.TagBIP0340Challenge, rBytes[:], pBytes, hash,
 307  	)
 308  	var e btcec.ModNScalar
 309  	if overflow := e.SetBytes((*[32]byte)(commitment)); overflow != 0 {
 310  		k.Zero()
 311  		str := "hash of (r || P || m) too big"
 312  		return nil, signatureError(ErrSchnorrHashValue, str)
 313  	}
 314  	// Step 13.
 315  	//
 316  	// s = k + e*d mod n
 317  	var sVal btcec.ModNScalar
 318  	s := sVal.Mul2(&e, privKey).Add(&k)
 319  	k.Zero()
 320  	sig := NewSignature(r, s)
 321  	// Step 14.
 322  	//
 323  	// If Verify(bytes(P), m, sig) fails, abort.
 324  	if !opts.fastSign {
 325  		if err := schnorrVerify(sig, hash, pBytes); chk.T(err) {
 326  			return nil, err
 327  		}
 328  	}
 329  	// Step 15.
 330  	//
 331  	// Return (r, s)
 332  	return sig, nil
 333  }
 334  
 335  // SignOption is a functional option argument that allows callers to modify the
 336  // way we generate BIP-340 schnorr signatures.
 337  type SignOption func(*signOptions)
 338  
 339  // signOptions houses the set of functional options that can be used to modify
 340  // the method used to generate the BIP-340 signature.
 341  type signOptions struct {
 342  	// fastSign determines if we'll skip the check at the end of the routine
 343  	// where we attempt to verify the produced signature.
 344  	fastSign bool
 345  	// authNonce allows the user to pass in their own nonce information, which
 346  	// is useful for schemes like mu-sig.
 347  	authNonce *[32]byte
 348  }
 349  
 350  // defaultSignOptions returns the default set of signing operations.
 351  func defaultSignOptions() *signOptions { return &signOptions{} }
 352  
 353  // FastSign forces signing to skip the extra verification step at the end.
 354  // Peformance sensitive applications may opt to use this option to speed up the
 355  // signing operation.
 356  func FastSign() SignOption {
 357  	return func(o *signOptions) { o.fastSign = true }
 358  }
 359  
 360  // CustomNonce allows users to pass in a custom set of auxData that's used as
 361  // input randomness to generate the nonce used during signing. Users may want
 362  // to specify this custom value when using multi-signatures schemes such as
 363  // Mu-Sig2. If this option isn't set, then rfc6979 will be used to generate the
 364  // nonce material.
 365  func CustomNonce(auxData [32]byte) SignOption {
 366  	return func(o *signOptions) { o.authNonce = &auxData }
 367  }
 368  
 369  // Sign generates an BIP-340 signature over the secp256k1 curve for the provided
 370  // hash (which should be the result of hashing a larger message) using the given
 371  // secret key. The produced signature is deterministic (the same message and the
 372  // same key yield the same signature) and canonical.
 373  //
 374  // Note that the current signing implementation has a few remaining variable
 375  // time aspects which make use of the secret key and the generated nonce, which
 376  // can expose the signer to constant time attacks. As a result, this function
 377  // should not be used in situations where there is the possibility of someone
 378  // having EM field/cache/etc access.
 379  func Sign(
 380  	privKey *btcec.SecretKey, hash []byte,
 381  	signOpts ...SignOption,
 382  ) (*Signature, error) {
 383  	// First, parse the set of optional signing options.
 384  	opts := defaultSignOptions()
 385  	for _, option := range signOpts {
 386  		option(opts)
 387  	}
 388  	// The algorithm for producing a BIP-340 signature is described in README.md
 389  	// and is reproduced here for reference:
 390  	//
 391  	// G = curve generator
 392  	// n = curve order
 393  	// d = secret key
 394  	// m = message
 395  	// a = input randmoness
 396  	// r, s = signature
 397  	//
 398  	// 1. d' = int(d)
 399  	// 2. Fail if m is not 32 bytes
 400  	// 3. Fail if d = 0 or d >= n
 401  	// 4. P = d'*G
 402  	// 5. Negate d if P.y is odd
 403  	// 6. t = bytes(d) xor tagged_hash("BIP0340/aux", t || bytes(P) || m)
 404  	// 7. rand = tagged_hash("BIP0340/nonce", a)
 405  	// 8. k' = int(rand) mod n
 406  	// 9. Fail if k' = 0
 407  	// 10. R = 'k*G
 408  	// 11. Negate k if R.y id odd
 409  	// 12. e = tagged_hash("BIP0340/challenge", bytes(R) || bytes(P) || mod) mod n
 410  	// 13. sig = bytes(R) || bytes((k + e*d)) mod n
 411  	// 14. If Verify(bytes(P), m, sig) fails, abort.
 412  	// 15. return sig.
 413  	//
 414  	// Note that the set of functional options passed in may modify the above
 415  	// algorithm. Namely if CustomNonce is used, then steps 6-8 are replaced
 416  	// with a process that generates the nonce using rfc6979. If FastSign is
 417  	// passed, then we skip set 14.
 418  	//
 419  	// Step 1.
 420  	//
 421  	// d' = int(d)
 422  	var privKeyScalar btcec.ModNScalar
 423  	privKeyScalar.Set(&privKey.Key)
 424  
 425  	// Step 2.
 426  	//
 427  	// Fail if m is not 32 bytes
 428  	// if len(hash) != scalarSize {
 429  	// 	str := fmt.Sprintf("wrong size for message hash (got %v, want %v)",
 430  	// 		len(hash), scalarSize)
 431  	// 	return nil, signatureError(schnorr.ErrInvalidHashLen, str)
 432  	// }
 433  	//
 434  	// Step 3.
 435  	//
 436  	// Fail if d = 0 or d >= n
 437  	if privKeyScalar.IsZero() {
 438  		str := "secret key is zero"
 439  		return nil, signatureError(ErrSecretKeyIsZero, str)
 440  	}
 441  	// Step 4.
 442  	//
 443  	// P = 'd*G
 444  	pub := privKey.PubKey()
 445  	// Step 5.
 446  	//
 447  	// Negate d if P.y is odd.
 448  	pubKeyBytes := pub.SerializeCompressed()
 449  	if pubKeyBytes[0] == secp256k1.PubKeyFormatCompressedOdd {
 450  		privKeyScalar.Negate()
 451  	}
 452  	// At this point, we check to see if a CustomNonce has been passed in, and
 453  	// if so, then we'll deviate from the main routine here by generating the
 454  	// nonce value as specified by BIP-0340.
 455  	if opts.authNonce != nil {
 456  		// Step 6.
 457  		//
 458  		// t = bytes(d) xor tagged_hash("BIP0340/aux", a)
 459  		privBytes := privKeyScalar.Bytes()
 460  		t := chainhash.TaggedHash(
 461  			chainhash.TagBIP0340Aux, (*opts.authNonce)[:],
 462  		)
 463  		for i := 0; i < len(t); i++ {
 464  			t[i] ^= privBytes[i]
 465  		}
 466  		// Step 7.
 467  		//
 468  		// rand = tagged_hash("BIP0340/nonce", t || bytes(P) || m)
 469  		//
 470  		// We snip off the first byte of the serialized pubkey, as we only need
 471  		// the x coordinate and not the market byte.
 472  		rand := chainhash.TaggedHash(
 473  			chainhash.TagBIP0340Nonce, t[:], pubKeyBytes[1:], hash,
 474  		)
 475  		// Step 8.
 476  		//
 477  		// k'= int(rand) mod n
 478  		var kPrime btcec.ModNScalar
 479  		kPrime.SetBytes((*[32]byte)(rand))
 480  		// Step 9.
 481  		//
 482  		// Fail if k' = 0
 483  		if kPrime.IsZero() {
 484  			str := fmt.Sprintf("generated nonce is zero")
 485  			return nil, signatureError(ErrSchnorrHashValue, str)
 486  		}
 487  		sig, err := schnorrSign(&privKeyScalar, &kPrime, pub, hash, opts)
 488  		kPrime.Zero()
 489  		if err != nil {
 490  			return nil, err
 491  		}
 492  		return sig, nil
 493  	}
 494  	var privKeyBytes [scalarSize]byte
 495  	privKeyScalar.PutBytes(&privKeyBytes)
 496  	defer zeroArray(&privKeyBytes)
 497  	for iteration := uint32(0); ; iteration++ {
 498  		var k *secp256k1.ModNScalar
 499  		// Step 6-9.
 500  		//
 501  		// Use RFC6979 to generate a deterministic nonce k in [1, n-1]
 502  		// parameterized by the secret key, message being signed, extra data
 503  		// that identifies the scheme, and an iteration count
 504  		k = btcec.NonceRFC6979(
 505  			privKeyBytes[:], hash, rfc6979ExtraDataV0[:], nil, iteration,
 506  		)
 507  		// Steps 10-15.
 508  		sig, err := schnorrSign(&privKeyScalar, k, pub, hash, opts)
 509  		k.Zero()
 510  		if err != nil {
 511  			// Try again with a new nonce.
 512  			continue
 513  		}
 514  		return sig, nil
 515  	}
 516  }
 517