-
Notifications
You must be signed in to change notification settings - Fork 11
feat: mt verify with benchmarks #231
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
Changes from all commits
b69a81a
e3947c5
900d77a
0cccfd3
9b03f38
8f01ec6
f4e7aa9
36b5d38
846333c
9b191fc
ed21a61
76447b9
e923570
d7760d2
80d2833
e5c9294
ebb9def
1ec9d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| //TODO(bing): The ts benchmarks are here really to ensure perf is up to par on the zig side. | ||
| // Remove once we are happy | ||
| import crypto from "node:crypto"; | ||
| import {bench, describe} from "@chainsafe/benchmark"; | ||
| import { | ||
| SecretKey as SecretKeyTS, | ||
| type Signature as SignatureTS, | ||
| aggregatePublicKeys as aggregatePublicKeysTS, | ||
| aggregateSignatures as aggregateSignaturesTS, | ||
| aggregateVerify as aggregateVerifyTS, | ||
| verifyMultipleAggregateSignatures as verifyTS, | ||
| } from "@chainsafe/blst"; | ||
| import { | ||
| SecretKey as SecretKeyZig, | ||
| type Signature as SignatureZig, | ||
| aggregatePublicKeys as aggregatePublicKeysZig, | ||
| aggregateSignatures as aggregateSignaturesZig, | ||
| aggregateVerify as aggregateVerifyZig, | ||
| verifyMultipleAggregateSignatures as verifyZig, | ||
| } from "../src/blst.js"; | ||
|
|
||
| interface SignatureSetZig { | ||
| msg: Uint8Array; | ||
| pk: InstanceType<typeof SecretKeyZig> extends {toPublicKey(): infer P} ? P : never; | ||
| sig: InstanceType<typeof SignatureZig>; | ||
| } | ||
|
|
||
| interface SignatureSetTS { | ||
| msg: Uint8Array; | ||
| pk: ReturnType<InstanceType<typeof SecretKeyTS>["toPublicKey"]>; | ||
| sig: InstanceType<typeof SignatureTS>; | ||
| } | ||
|
|
||
| function generateZigSets(count: number): SignatureSetZig[] { | ||
| return Array.from({length: count}, () => { | ||
| const msg = crypto.randomBytes(32); | ||
| const sk = SecretKeyZig.fromKeygen(crypto.randomBytes(32)); | ||
| const pk = sk.toPublicKey(); | ||
| const sig = sk.sign(msg); | ||
| return {msg, pk, sig}; | ||
| }); | ||
| } | ||
|
|
||
| function generateTSSets(count: number): SignatureSetTS[] { | ||
| return Array.from({length: count}, () => { | ||
| const msg = crypto.randomBytes(32); | ||
| const sk = SecretKeyTS.fromKeygen(crypto.randomBytes(32)); | ||
| const pk = sk.toPublicKey(); | ||
| const sig = sk.sign(msg); | ||
| return {msg, pk, sig}; | ||
| }); | ||
| } | ||
|
|
||
| describe("aggregatePublicKeys", () => { | ||
| for (const count of [1, 8, 32, 128, 256]) { | ||
| bench({ | ||
| beforeEach: () => generateZigSets(count).map((s) => s.pk), | ||
| fn: (publicKeys) => { | ||
| aggregatePublicKeysZig(publicKeys); | ||
| }, | ||
| id: `aggregatePublicKeys lodestar-z ${count} keys`, | ||
| }); | ||
|
|
||
| bench({ | ||
| beforeEach: () => generateTSSets(count).map((s) => s.pk), | ||
| fn: (publicKeys) => { | ||
| aggregatePublicKeysTS(publicKeys); | ||
| }, | ||
| id: `aggregatePublicKeys @chainsafe/blst ${count} keys`, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe("aggregateSignatures", () => { | ||
| for (const count of [1, 8, 32, 128, 256]) { | ||
| bench({ | ||
| beforeEach: () => generateZigSets(count).map((s) => s.sig), | ||
| fn: (signatures) => { | ||
| aggregateSignaturesZig(signatures); | ||
| }, | ||
| id: `aggregateSignatures lodestar-z ${count} sigs`, | ||
| }); | ||
|
|
||
| bench({ | ||
| beforeEach: () => generateTSSets(count).map((s) => s.sig), | ||
| fn: (signatures) => { | ||
| aggregateSignaturesTS(signatures); | ||
| }, | ||
| id: `aggregateSignatures @chainsafe/blst ${count} sigs`, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe("aggregateVerify", () => { | ||
| for (const count of [3, 8, 32, 64, 128]) { | ||
| bench({ | ||
| beforeEach: () => { | ||
| const sets = generateZigSets(count); | ||
| return { | ||
| messages: sets.map((s) => s.msg), | ||
| publicKeys: sets.map((s) => s.pk), | ||
| signature: aggregateSignaturesZig(sets.map((s) => s.sig)), | ||
| }; | ||
| }, | ||
| fn: ({messages, publicKeys, signature}) => { | ||
| const isValid = aggregateVerifyZig(messages, publicKeys, signature); | ||
| if (!isValid) throw Error("Invalid"); | ||
| }, | ||
| id: `aggregateVerify lodestar-z ${count} sets`, | ||
| }); | ||
|
|
||
| bench({ | ||
| beforeEach: () => { | ||
| const sets = generateTSSets(count); | ||
| return { | ||
| messages: sets.map((s) => s.msg), | ||
| publicKeys: sets.map((s) => s.pk), | ||
| signature: aggregateSignaturesTS(sets.map((s) => s.sig)), | ||
| }; | ||
| }, | ||
| fn: ({messages, publicKeys, signature}) => { | ||
| const isValid = aggregateVerifyTS(messages, publicKeys, signature); | ||
| if (!isValid) throw Error("Invalid"); | ||
| }, | ||
| id: `aggregateVerify @chainsafe/blst ${count} sets`, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe("verifyMultipleAggregateSignatures", () => { | ||
| for (const count of [3, 8, 32, 64, 128]) { | ||
| bench({ | ||
| beforeEach: () => generateZigSets(count), | ||
| fn: (sets) => { | ||
| const isValid = verifyZig(sets); | ||
| if (!isValid) throw Error("Invalid"); | ||
| }, | ||
| id: `lodestar-z ${count} sets`, | ||
| }); | ||
|
|
||
| bench({ | ||
| beforeEach: () => generateTSSets(count), | ||
| fn: (sets) => { | ||
| const isValid = verifyTS(sets); | ||
| if (!isValid) throw Error("Invalid"); | ||
| }, | ||
| id: `@chainsafe/blst ${count} sets`, | ||
| }); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |||||
| } | ||||||
| }, | ||||||
| "scripts": { | ||||||
| "prepare": "zig build build-lib:bindings -Doptimize=ReleaseSafe", | ||||||
| "prepare": "zig build build-lib:bindings -Doptimize=ReleaseFast", | ||||||
|
Contributor
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. Changing the optimization level to
Suggested change
References
Collaborator
Author
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. The underlying
spiral-ladder marked this conversation as resolved.
|
||||||
| "lint": "biome check", | ||||||
| "test": "NODE_OPTIONS='--expose-gc' vitest run bindings/test/*.test.ts" | ||||||
| }, | ||||||
|
|
@@ -47,7 +47,9 @@ | |||||
| }, | ||||||
| "devDependencies": { | ||||||
| "@biomejs/biome": "^2.3.11", | ||||||
| "@chainsafe/benchmark": "^2.0.1", | ||||||
| "@chainsafe/biomejs-config": "^1.0.0", | ||||||
| "@chainsafe/blst": "^2.2.0", | ||||||
| "@lodestar/config": "^1.39.1", | ||||||
| "@lodestar/era": "^1.39.1", | ||||||
| "@lodestar/state-transition": "^1.39.1", | ||||||
|
|
||||||
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 this test file we have both benchmarks for blst-z and blst-ts but this is really just for the PRs sake, we should remove the typescript benchmarks once we're happy with results.