Skip to content
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fe2045d
chore: bring in updated bls library
matthewkeil Jan 25, 2024
721debf
chore: update yarn.lock
matthewkeil Jan 25, 2024
05649df
fix: unit tests from switching library
matthewkeil Jan 25, 2024
127fe6f
feat: implement bls library with napi changes
matthewkeil Jan 28, 2024
21418cd
chore: update sha for bls library
matthewkeil Jan 28, 2024
66101d7
add updated bls commit with blst-ts changes
matthewkeil Feb 6, 2024
cef02a0
chore: force yarn resolution of correct version
matthewkeil Feb 6, 2024
e59afd0
chore: update bls sha
matthewkeil Feb 6, 2024
0ad6209
feat: add blsAddVerificationRandomness cli option
matthewkeil Mar 31, 2024
a3dc19f
feat: implement verification randomness for same message
matthewkeil Mar 31, 2024
f2d4799
feat: add randomBytesNonZero to utils
matthewkeil Mar 31, 2024
8a6c37a
chore: update to latest version of bls
matthewkeil Apr 1, 2024
c6a88e0
chore: fix broken yarn.lock
matthewkeil Apr 1, 2024
198ee64
feat: name anonymous functions in jobItem.ts
matthewkeil Apr 1, 2024
8510f0a
chore: update bls and add blst
matthewkeil Apr 10, 2024
26338dc
chore: add postinstall script to hack building types for branch import
matthewkeil Apr 10, 2024
0ff213c
fix: update for fixed types exported from bls rebuild-2
matthewkeil Apr 10, 2024
cb3046d
refactor: remove blst test that was moved to blst lib
matthewkeil Apr 10, 2024
05a50e6
chore: add bls@8.0.0
matthewkeil Apr 11, 2024
7892e42
Merge branch 'unstable' into mkeil/napi-rebuild-2-rebase-unstable-fin…
matthewkeil Apr 11, 2024
f4cbf54
refactor: rename flag to disableSameMessageVerificationRandomness
matthewkeil Apr 9, 2024
dde6eb4
feat: automatically set threadpool size in beaconHandler
matthewkeil Apr 4, 2024
72bc4e1
docs: add TODO about updating metrics
matthewkeil Apr 9, 2024
6a64d32
fix: make benchmark multi threading cross-compatible
matthewkeil Apr 9, 2024
3f39dfc
feat: add log about turning off verification randomness
matthewkeil Apr 11, 2024
8dda2ff
fix: ensure serialized objects cross worker boundary
matthewkeil Apr 12, 2024
e0b4bd2
fix: refactor runWork in bls
matthewkeil Apr 12, 2024
12debbc
feat: name anon functions in jobItem
matthewkeil Apr 12, 2024
39c0935
docs: add code comments per the PR comments
matthewkeil Apr 12, 2024
9d82670
fix: awaiting _runJob() for libuv pool type
twoeths Apr 15, 2024
fc3ce71
fix: stop runJob() if there is nothing in jobs queue
twoeths Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
"test-coverage:e2e": "c8 --config .c8rc.json --report-dir coverage/e2e/ --all npm run test:e2e",
"test-coverage:e2e-sim": "c8 --config .c8rc.json --report-dir coverage/e2e-sim/ --all npm run test:e2e:sim",
"test-coverage:spec": "c8 --config .c8rc.json --report-dir coverage/spec/ --all npm run test:spec",
"benchmark": "yarn benchmark:files 'packages/*/test/perf/**/*.test.ts'",
"benchmark:files": "NODE_OPTIONS='--max-old-space-size=4096 --loader=ts-node/esm' benchmark --config .benchrc.yaml --defaultBranch unstable",
"benchmark": "UV_THREADPOOL_SIZE=$(node -e 'console.log(require(`os`).availableParallelism())') yarn benchmark:files 'packages/*/test/perf/**/*.test.ts'",
"benchmark:files": "UV_THREADPOOL_SIZE=$(node -e 'console.log(require(`os`).availableParallelism())') NODE_OPTIONS='--max-old-space-size=4096 --loader=ts-node/esm' benchmark --config .benchrc.yaml --defaultBranch unstable",
"release:create-rc": "node scripts/release/create_rc.mjs",
"release:tag-rc": "node scripts/release/tag_rc.mjs",
"release:tag-stable": "node scripts/release/tag_stable.mjs",
"release:publish": "lerna publish from-package --yes --no-verify-access"
},
"dependencies": {
"@chainsafe/blst": "^1.0.0"
},
"devDependencies": {
"@actions/core": "^1.10.1",
"@chainsafe/eslint-plugin-node": "^11.2.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@
},
"dependencies": {
"@chainsafe/as-sha256": "^0.4.1",
"@chainsafe/bls": "7.1.3",
"@chainsafe/blst": "^0.2.10",
"@chainsafe/bls": "^8.0.0",
"@chainsafe/discv5": "^9.0.0",
"@chainsafe/enr": "^3.0.0",
"@chainsafe/libp2p-gossipsub": "^11.2.1",
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/bls/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type {IBlsVerifier} from "./interface.js";
export type {BlsMultiThreadWorkerPoolModules, JobQueueItemType} from "./multithread/index.js";
export {BlsPoolType} from "./multithread/types.js";
export {BlsMultiThreadWorkerPool} from "./multithread/index.js";
export {BlsSingleThreadVerifier} from "./singleThread.js";
12 changes: 10 additions & 2 deletions packages/beacon-node/src/chain/bls/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export type VerifySignatureOpts = {
priority?: boolean;
};

export interface QueuedVerificationOpts extends VerifySignatureOpts {
/**
* Include multiplying in randomization factor for verification. Only applicable for verification
* of same message batches. Applied via the worker pool options
*/
disableSameMessageVerificationRandomness?: boolean;
}

export interface IBlsVerifier {
/**
* Verify 1 or more signature sets. Sets may be verified on batch or not depending on their count
Expand All @@ -44,7 +52,7 @@ export interface IBlsVerifier {
* Signatures have already been checked for subgroup
* Signature checks above could be done here for convienence as well
*/
verifySignatureSets(sets: ISignatureSet[], opts?: VerifySignatureOpts): Promise<boolean>;
verifySignatureSets(sets: ISignatureSet[], opts?: QueuedVerificationOpts): Promise<boolean>;

/**
* Similar to verifySignatureSets but:
Expand All @@ -55,7 +63,7 @@ export interface IBlsVerifier {
verifySignatureSetsSameMessage(
sets: {publicKey: PublicKey; signature: Uint8Array}[],
messsage: Uint8Array,
opts?: Omit<VerifySignatureOpts, "verifyOnMainThread">
opts?: Omit<QueuedVerificationOpts, "verifyOnMainThread">
): Promise<boolean[]>;

/** For multithread pool awaits terminating all workers */
Expand Down
52 changes: 31 additions & 21 deletions packages/beacon-node/src/chain/bls/maybeBatch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import {CoordType, PublicKey} from "@chainsafe/bls/types";
import bls from "@chainsafe/bls";
import {WorkRequestSet} from "./multithread/types.js";
import {deserializeSet} from "./multithread/utils.js";

const MIN_SET_COUNT_TO_BATCH = 2;

export type SignatureSetDeserialized = {
publicKey: PublicKey;
message: Uint8Array;
signature: Uint8Array;
};

/**
* Verify signatures sets with batch verification or regular core verify depending on the set count.
* Abstracted in a separate file to be consumed by the threaded pool and the main thread implementation.
*/
export function verifySignatureSetsMaybeBatch(sets: SignatureSetDeserialized[]): boolean {
export function verifySignatureSetsMaybeBatch(sets: WorkRequestSet[]): boolean {
try {
const deserialized = sets.map(deserializeSet);
if (sets.length >= MIN_SET_COUNT_TO_BATCH) {
return bls.Signature.verifyMultipleSignatures(
sets.map((s) => ({
publicKey: s.publicKey,
message: s.message,
// true = validate signature
signature: bls.Signature.fromBytes(s.signature, CoordType.affine, true),
}))
);
return bls.verifyMultipleSignatures(deserialized);
}

// .every on an empty array returns true
Expand All @@ -32,11 +21,32 @@ export function verifySignatureSetsMaybeBatch(sets: SignatureSetDeserialized[]):
}

// If too few signature sets verify them without batching
return sets.every((set) => {
// true = validate signature
const sig = bls.Signature.fromBytes(set.signature, CoordType.affine, true);
return sig.verify(set.publicKey, set.message);
});
return deserialized.every(({message, publicKey, signature}) => bls.verify(publicKey, message, signature));
} catch (_) {
// A signature could be malformed, in that case fromBytes throws error
// blst-ts `verifyMultipleSignatures` is also a fallible operation if mul_n_aggregate fails
// see https://github.com/ChainSafe/blst-ts/blob/b1ba6333f664b08e5c50b2b0d18c4f079203962b/src/lib.ts#L291
return false;
}
}

export async function asyncVerifySignatureSetsMaybeBatch(sets: WorkRequestSet[]): Promise<boolean> {
Comment thread
matthewkeil marked this conversation as resolved.
Resolved
try {
const deserialized = sets.map(deserializeSet);
if (sets.length >= MIN_SET_COUNT_TO_BATCH) {
return await bls.asyncVerifyMultipleSignatures(deserialized);
}

// .every on an empty array returns true
if (sets.length === 0) {
throw Error("Empty signature sets");
}

const promises = await Promise.all(
deserialized.map(({message, publicKey, signature}) => bls.asyncVerify(publicKey, message, signature))
);
// If too few signature sets verify them without batching
return promises.every((isValid) => isValid);
} catch (_) {
// A signature could be malformed, in that case fromBytes throws error
// blst-ts `verifyMultipleSignatures` is also a fallible operation if mul_n_aggregate fails
Expand Down
Loading