Skip to content

Commit

Permalink
Revert "feat: switch bls to napi rebuild (#6616)"
Browse files Browse the repository at this point in the history
This reverts commit 97d9aa8.
  • Loading branch information
matthewkeil committed Jun 3, 2024
1 parent d8f1f29 commit 0bcdee9
Show file tree
Hide file tree
Showing 34 changed files with 554 additions and 322 deletions.
1 change: 0 additions & 1 deletion .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ orchestrator
osx
overriden
params
peerDependency
pid
plaintext
pre
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"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": "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",
"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",
"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",
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
},
"dependencies": {
"@chainsafe/as-sha256": "^0.4.1",
"@chainsafe/bls": "^8.1.0",
"@chainsafe/blst": "^1.0.0",
"@chainsafe/bls": "7.1.3",
"@chainsafe/blst": "^0.2.10",
"@chainsafe/discv5": "^9.0.0",
"@chainsafe/enr": "^3.0.0",
"@chainsafe/libp2p-gossipsub": "^13.0.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/chain/bls/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type {IBlsVerifier} from "./interface.js";
export type {JobQueueItemType} from "./jobItem.js";
export type {BlsMultiThreadWorkerPoolModules} from "./multiThread.js";
export {BlsMultiThreadWorkerPool} from "./multiThread.js";
export type {BlsMultiThreadWorkerPoolModules, JobQueueItemType} from "./multithread/index.js";
export {BlsMultiThreadWorkerPool} from "./multithread/index.js";
export {BlsSingleThreadVerifier} from "./singleThread.js";
46 changes: 46 additions & 0 deletions packages/beacon-node/src/chain/bls/maybeBatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {CoordType, PublicKey} from "@chainsafe/bls/types";
import bls from "@chainsafe/bls";

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 {
try {
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),
}))
);
}

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

// 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);
});
} 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;
}
}
Loading

0 comments on commit 0bcdee9

Please sign in to comment.