Skip to content

Commit 0518755

Browse files
committed
accept hash bytes directly in ecdsa verify, for easy use with different hash function
1 parent e1d70be commit 0518755

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/lib/provable/crypto/foreign-ecdsa.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ class EcdsaSignature {
104104
*/
105105
verify(message: Bytes, publicKey: FlexiblePoint): Bool {
106106
let msgHashBytes = Keccak.ethereum(message);
107-
let msgHash = keccakOutputToScalar(msgHashBytes, this.Constructor.Curve);
108-
return this.verifySignedHash(msgHash, publicKey);
107+
return this.verifySignedHash(msgHashBytes, publicKey);
109108
}
110109

111110
/**
@@ -155,22 +154,23 @@ class EcdsaSignature {
155154
...Bytes.fromString(String(message.length)).bytes, // message length as string
156155
...message.bytes, // actual message bytes
157156
]);
158-
159-
let msgHash = keccakOutputToScalar(msgHashBytes, this.Constructor.Curve);
160-
return this.verifySignedHash(msgHash, publicKey);
157+
return this.verifySignedHash(msgHashBytes, publicKey);
161158
}
162159

163160
/**
164161
* Verify the ECDSA signature given the message hash (a {@link Scalar}) and public key (a {@link Curve} point).
165162
*
166163
* This is a building block of {@link EcdsaSignature.verify}, where the input message is also hashed.
167-
* In contrast, this method just takes the message hash (a curve scalar) as input, giving you flexibility in
168-
* choosing the hashing algorithm.
164+
* In contrast, this method just takes the message hash (a curve scalar, or the output bytes of a hash function)
165+
* as input, giving you flexibility in choosing the hashing algorithm.
169166
*/
170167
verifySignedHash(
171-
msgHash: AlmostForeignField | bigint,
168+
msgHash: AlmostForeignField | bigint | Bytes,
172169
publicKey: FlexiblePoint
173170
): Bool {
171+
if (msgHash instanceof Bytes.Base)
172+
msgHash = keccakOutputToScalar(msgHash, this.Constructor.Curve);
173+
174174
let msgHash_ = this.Constructor.Curve.Scalar.from(msgHash);
175175
let publicKey_ = this.Constructor.Curve.from(publicKey);
176176
return Ecdsa.verify(
@@ -196,12 +196,15 @@ class EcdsaSignature {
196196
* Create an {@link EcdsaSignature} by signing a message hash with a private key.
197197
*
198198
* This is a building block of {@link EcdsaSignature.sign}, where the input message is also hashed.
199-
* In contrast, this method just takes the message hash (a curve scalar) as input, giving you flexibility in
200-
* choosing the hashing algorithm.
199+
* In contrast, this method just takes the message hash (a curve scalar, or the output bytes of a hash function)
200+
* as input, giving you flexibility in choosing the hashing algorithm.
201201
*
202-
* Note: This method is not provable, and only takes JS bigints as input.
202+
* Note: This method is not provable, and only takes JS bigints or constant Bytes as input.
203203
*/
204-
static signHash(msgHash: bigint, privateKey: bigint) {
204+
static signHash(msgHash: bigint | Bytes, privateKey: bigint) {
205+
if (msgHash instanceof Bytes.Base)
206+
msgHash = keccakOutputToScalar(msgHash, this.Curve).toBigInt();
207+
205208
let { r, s } = Ecdsa.sign(this.Curve.Bigint, msgHash, privateKey);
206209
return new this({ r, s });
207210
}

0 commit comments

Comments
 (0)