-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Secp256r1 #1885
Merged
Merged
Support Secp256r1 #1885
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e52e7d
bindings
mitschabaude 58e4355
add `a` parameter to ec methods
mitschabaude 43f9c15
support *r1 in ec circuit unit tests
mitschabaude b82396f
add to ecdsa unit test
mitschabaude ed19610
changelog
mitschabaude 34e86be
minor
mitschabaude a0812d7
test ecdsa against web crypto
mitschabaude ec38e56
node 18 -> 20 in CI
mitschabaude d28034b
Merge branch 'main' into feature/secp256r1
mitschabaude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule bindings
updated
5 files
+16 −12 | crypto/elliptic-curve-endomorphism.ts | |
+13 −0 | crypto/elliptic-curve-examples.ts | |
+130 −39 | crypto/elliptic-curve.ts | |
+58 −19 | crypto/elliptic-curve.unit-test.ts | |
+4 −0 | crypto/finite-field-examples.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,57 @@ import { | |
} from '../../testing/equivalent.js'; | ||
import { Bool } from '../bool.js'; | ||
import { Random } from '../../testing/random.js'; | ||
import { bytesToBigInt } from '../../../bindings/crypto/bigint-helpers.js'; | ||
import { expect } from 'expect'; | ||
|
||
// quick tests | ||
const Secp256k1 = createCurveAffine(CurveParams.Secp256k1); | ||
const Secp256r1 = createCurveAffine(CurveParams.Secp256r1); | ||
const Pallas = createCurveAffine(CurveParams.Pallas); | ||
const Vesta = createCurveAffine(CurveParams.Vesta); | ||
let curves = [Secp256k1, Pallas, Vesta]; | ||
|
||
// secp256r1 test against web crypto API | ||
{ | ||
// generate a key pair | ||
let { privateKey, publicKey } = await crypto.subtle.generateKey( | ||
{ name: 'ECDSA', namedCurve: 'P-256' }, | ||
true, | ||
['sign'] | ||
); | ||
|
||
// extract public and private key to bigints | ||
let pkBuffer = await crypto.subtle.exportKey('raw', publicKey); | ||
let x = bytesToBigIntBE(pkBuffer.slice(1, 33)); | ||
let y = bytesToBigIntBE(pkBuffer.slice(33)); | ||
let pk = { x, y }; | ||
|
||
var skJwk = await crypto.subtle.exportKey('jwk', privateKey); | ||
let sk = bytesToBigIntBE(fromBase64Url(skJwk.d!)); | ||
|
||
// sanity check: we extracted keys correctly | ||
expect(Secp256r1.from(pk)).toEqual(Secp256r1.scale(Secp256r1.one, sk)); | ||
|
||
// sign a message using web crypto | ||
let message = new TextEncoder().encode('hello world'); | ||
|
||
let sigBuffer = await crypto.subtle.sign( | ||
{ name: 'ECDSA', hash: 'SHA-256' }, | ||
privateKey, | ||
message | ||
); | ||
let r = bytesToBigIntBE(sigBuffer.slice(0, 32)); | ||
let s = bytesToBigIntBE(sigBuffer.slice(32)); | ||
let signature = Ecdsa.Signature.from({ r, s }); | ||
|
||
// check that we can verify the signature on the message hash | ||
let msgHash = await crypto.subtle.digest('SHA-256', message); | ||
let m = Field3.from(Secp256r1.Scalar.mod(bytesToBigIntBE(msgHash))); | ||
|
||
let ok = Ecdsa.verify(Secp256r1, signature, m, Point.from(pk)); | ||
assert(ok, 'web crypto signature verifies'); | ||
} | ||
|
||
let curves = [Secp256k1, Secp256r1, Pallas, Vesta]; | ||
|
||
for (let Curve of curves) { | ||
// prepare test inputs | ||
|
@@ -189,3 +234,49 @@ console.timeEnd('ecdsa verify (prove)'); | |
|
||
assert(await program.verify(proof), 'proof verifies'); | ||
proof.publicOutput.assertTrue('signature verifies'); | ||
|
||
// check constraints w/o endomorphism | ||
|
||
let programNoEndo = ZkProgram({ | ||
name: 'ecdsa-secp256r1', | ||
publicOutput: Bool, | ||
methods: { | ||
ecdsa: { | ||
privateInputs: [], | ||
async method() { | ||
let signature_ = Provable.witness(Ecdsa.Signature, () => signature); | ||
let msgHash_ = Provable.witness(Field3, () => msgHash); | ||
let publicKey_ = Provable.witness(Point, () => publicKey); | ||
|
||
return { | ||
publicOutput: Ecdsa.verify( | ||
Secp256r1, | ||
signature_, | ||
msgHash_, | ||
publicKey_, | ||
config | ||
), | ||
}; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
console.time('ecdsa verify, no endomorphism (build constraint system)'); | ||
let csNoEndo = (await programNoEndo.analyzeMethods()).ecdsa; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add some test vectors to verify this implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes! I'll do that |
||
console.timeEnd('ecdsa verify, no endomorphism (build constraint system)'); | ||
|
||
console.log(csNoEndo.summary()); | ||
|
||
// helpers | ||
|
||
function bytesToBigIntBE(bytes: ArrayBuffer | Uint8Array | number[]) { | ||
return bytesToBigInt([...new Uint8Array(bytes)].reverse()); | ||
} | ||
|
||
function fromBase64Url(b64url: string): Uint8Array { | ||
let b64 = | ||
b64url.replace(/-/g, '+').replace(/_/g, '/') + | ||
'===='.slice(0, (4 - (b64url.length % 4)) % 4); | ||
return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In PR310 this
m
is computed with a single modular operation, meaning:let m = mod((3n * x * x + a) * d, p);
. Cannot we remove onemod
from this line?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question! I think using
mod()
twice is better actually, because both x and d are large field elements. so x^2 is already a bigint of 2x the length, and it's more efficient to first reduce it (withmod()
) back to 1x the length before doing another bigint multiplication with d. In the code you mention, we get an intermediate result of 2x * 1x = 3x the length