|
| 1 | +package adaptor |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcec/v2" |
| 7 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 8 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 9 | + "github.com/decred/dcrd/dcrec/secp256k1/v4" |
| 10 | +) |
| 11 | + |
| 12 | +// Verify verifies the provided schnorr adaptor signature against the given adaptor point |
| 13 | +func Verify(sigBytes []byte, msg []byte, pubKeyBytes []byte, adaptorPointBytes []byte) bool { |
| 14 | + _, err := schnorr.ParseSignature(sigBytes) |
| 15 | + if err != nil { |
| 16 | + return false |
| 17 | + } |
| 18 | + |
| 19 | + pubKey, err := schnorr.ParsePubKey(pubKeyBytes) |
| 20 | + if err != nil { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + adaptorPoint, err := btcec.ParsePubKey(adaptorPointBytes) |
| 25 | + if err != nil { |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + return verifySchnorrAdaptorSignature(NewSignature(sigBytes), msg, pubKey, adaptorPoint) == nil |
| 30 | +} |
| 31 | + |
| 32 | +// verifySchnorrAdaptorSignature verifies the given schnorr adaptor signature |
| 33 | +// |
| 34 | +// The algorithm is based on the verifier for schnorr signature |
| 35 | +// Specifically, step 3 and 6 are added, step 7 and 10 are modified |
| 36 | +// |
| 37 | +// Annotation: |
| 38 | +// AP: adaptor point |
| 39 | +// AR: adapted R |
| 40 | +func verifySchnorrAdaptorSignature(sig *Signature, hash []byte, pubKey *secp256k1.PublicKey, adaptorPoint *secp256k1.PublicKey) error { |
| 41 | + // 1. Fail if m is not 32 bytes |
| 42 | + // 2. P = lift_x(int(pk)). |
| 43 | + // 3. AP = lift_x(int(ap)). |
| 44 | + // 4. r = int(sig[0:32]); fail is r >= p. |
| 45 | + // 5. s = int(sig[32:64]); fail if s >= n. |
| 46 | + // 6. AR = R + AP. |
| 47 | + // 7. e = int(tagged_hash("BIP0340/challenge", bytes(AR) || bytes(P) || M)) mod n. |
| 48 | + // 8. R = s*G - e*P |
| 49 | + // 9. Fail if is_infinite(R) |
| 50 | + // 10. Fail if not has_even_y(R+AP) |
| 51 | + // 11. Fail is x(R) != r. |
| 52 | + // 12. Return success iff not failure occured before reaching this |
| 53 | + // point. |
| 54 | + |
| 55 | + // Step 1. |
| 56 | + // |
| 57 | + // Fail if m is not 32 bytes |
| 58 | + if len(hash) != scalarSize { |
| 59 | + str := fmt.Sprintf("wrong size for message (got %v, want %v)", |
| 60 | + len(hash), scalarSize) |
| 61 | + return fmt.Errorf("invalid hash length: %s", str) |
| 62 | + } |
| 63 | + |
| 64 | + // Step 2. |
| 65 | + // |
| 66 | + // P = lift_x(int(pk)) |
| 67 | + // |
| 68 | + // Fail if P is not a point on the curve |
| 69 | + if !pubKey.IsOnCurve() { |
| 70 | + str := "pubkey point is not on curve" |
| 71 | + return fmt.Errorf("invalid pub key: %s", str) |
| 72 | + } |
| 73 | + |
| 74 | + // Step 3. |
| 75 | + // |
| 76 | + // AP = lift_x(int(ap)) |
| 77 | + // |
| 78 | + // Fail if AP is not a point on the curve |
| 79 | + if !adaptorPoint.IsOnCurve() { |
| 80 | + str := "adaptor point is not on curve" |
| 81 | + return fmt.Errorf("invalid adaptor point: %s", str) |
| 82 | + } |
| 83 | + |
| 84 | + // Step 4. |
| 85 | + // |
| 86 | + // Fail if r >= p |
| 87 | + // |
| 88 | + // Note this is already handled by the fact r is a field element. |
| 89 | + |
| 90 | + // Step 5. |
| 91 | + // |
| 92 | + // Fail if s >= n |
| 93 | + // |
| 94 | + // Note this is already handled by the fact s is a mod n scalar. |
| 95 | + |
| 96 | + // Step 6. |
| 97 | + // |
| 98 | + // AR = R + AP |
| 99 | + var rBytes [32]byte |
| 100 | + sig.r.PutBytesUnchecked(rBytes[:]) |
| 101 | + |
| 102 | + rPoint, err := schnorr.ParsePubKey(rBytes[:]) |
| 103 | + if err != nil { |
| 104 | + str := "failed to parse r" |
| 105 | + return fmt.Errorf("invalid r: %s", str) |
| 106 | + } |
| 107 | + |
| 108 | + var R, AP, AR btcec.JacobianPoint |
| 109 | + rPoint.AsJacobian(&R) |
| 110 | + adaptorPoint.AsJacobian(&AP) |
| 111 | + btcec.AddNonConst(&R, &AP, &AR) |
| 112 | + |
| 113 | + // Step 7. |
| 114 | + // |
| 115 | + // e = int(tagged_hash("BIP0340/challenge", bytes(ar) || bytes(P) || M)) mod n. |
| 116 | + AR.ToAffine() |
| 117 | + var arBytes [32]byte |
| 118 | + AR.X.PutBytesUnchecked(arBytes[:]) |
| 119 | + |
| 120 | + pBytes := schnorr.SerializePubKey(pubKey) |
| 121 | + |
| 122 | + commitment := chainhash.TaggedHash( |
| 123 | + chainhash.TagBIP0340Challenge, arBytes[:], pBytes, hash, |
| 124 | + ) |
| 125 | + |
| 126 | + var e btcec.ModNScalar |
| 127 | + if overflow := e.SetBytes((*[32]byte)(commitment)); overflow != 0 { |
| 128 | + str := "hash of (r || P || m) too big" |
| 129 | + return fmt.Errorf("invalid schnorr hash: %s", str) |
| 130 | + } |
| 131 | + |
| 132 | + // Negate e here so we can use AddNonConst below to subtract the s*G |
| 133 | + // point from e*P. |
| 134 | + e.Negate() |
| 135 | + |
| 136 | + // Step 8. |
| 137 | + // |
| 138 | + // R = s*G - e*P |
| 139 | + var P, sG, eP btcec.JacobianPoint |
| 140 | + pubKey.AsJacobian(&P) |
| 141 | + btcec.ScalarBaseMultNonConst(&sig.s, &sG) |
| 142 | + btcec.ScalarMultNonConst(&e, &P, &eP) |
| 143 | + btcec.AddNonConst(&sG, &eP, &R) |
| 144 | + |
| 145 | + // Step 9. |
| 146 | + // |
| 147 | + // Fail if R is the point at infinity |
| 148 | + if (R.X.IsZero() && R.Y.IsZero()) || R.Z.IsZero() { |
| 149 | + str := "calculated R point is the point at infinity" |
| 150 | + return fmt.Errorf("invalid signature: %s", str) |
| 151 | + } |
| 152 | + |
| 153 | + // Step 10. |
| 154 | + // |
| 155 | + // Fail if (R+AP).y is odd |
| 156 | + // |
| 157 | + // Note that R+AP must be in affine coordinates for this check. |
| 158 | + btcec.AddNonConst(&R, &AP, &AR) |
| 159 | + AR.ToAffine() |
| 160 | + if AR.Y.IsOdd() { |
| 161 | + str := "calculated AR y-value is odd" |
| 162 | + return fmt.Errorf("invalid signature: %s", str) |
| 163 | + } |
| 164 | + |
| 165 | + // Step 11. |
| 166 | + // |
| 167 | + // Verified if R.x == r |
| 168 | + // |
| 169 | + // Note that R must be in affine coordinates for this check. |
| 170 | + if !sig.r.Equals(&R.X) { |
| 171 | + str := "calculated R point was not given R" |
| 172 | + return fmt.Errorf("invalid signature: %s", str) |
| 173 | + } |
| 174 | + |
| 175 | + // Step 12. |
| 176 | + // |
| 177 | + // Return success iff not failure occured before reaching this |
| 178 | + return nil |
| 179 | +} |
0 commit comments