Skip to content

Commit

Permalink
test: update multthread perf for added randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Feb 23, 2024
1 parent 35f78a0 commit 30dad3e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export function verifyMultipleAggregateSignatures(signatureSets: SignatureSet[])
* `rand` must not be exactly zero. Otherwise it would allow the verification of invalid signatures
* See https://github.com/ChainSafe/blst-ts/issues/45
*/
function randomBytesNonZero(BYTES_COUNT: number): Buffer {
export function randomBytesNonZero(BYTES_COUNT: number): Buffer {
const rand = crypto.randomBytes(BYTES_COUNT);
for (let i = 0; i < BYTES_COUNT; i++) {
if (rand[0] !== 0) return rand;
Expand Down
67 changes: 31 additions & 36 deletions test/perf/multithreading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@ import {itBench} from "@dapplion/benchmark";
import {expect} from "chai";
import {BlsMultiThreading, BlsPoolType, getGroupsOfBatchesOfSignatureSets} from "../utils";

describe("multithreading perf", function () {
const minutes = 10;
this.timeout(minutes * 60 * 1000);
const getGroupsInfo = (isSwig: boolean): Parameters<typeof getGroupsOfBatchesOfSignatureSets> => [
isSwig,
16,
128,
256,
256,
];
const minutes = 10;
const getGroupsInfo = (isSwig: boolean): Parameters<typeof getGroupsOfBatchesOfSignatureSets> => [
isSwig,
16,
128,
256,
256,
];

let libuvPool: BlsMultiThreading;
let workerPool: BlsMultiThreading;

describe("libuv", () => {
for (const addVerificationRandomness of [true, false]) {
describe.only(`multithreading perf - addVerificationRandomness ${addVerificationRandomness}`, function () {
this.timeout(minutes * 60 * 1000);
let libuvPool: BlsMultiThreading;
let workerPool: BlsMultiThreading;
let napiGroups: ReturnType<typeof getGroupsOfBatchesOfSignatureSets>;
let swigGroups: ReturnType<typeof getGroupsOfBatchesOfSignatureSets>;

before(async () => {
libuvPool = new BlsMultiThreading({blsPoolType: BlsPoolType.libuv});
libuvPool = new BlsMultiThreading({blsPoolType: BlsPoolType.libuv, addVerificationRandomness});
napiGroups = getGroupsOfBatchesOfSignatureSets(...getGroupsInfo(false));

workerPool = new BlsMultiThreading({blsPoolType: BlsPoolType.workers, addVerificationRandomness});
await workerPool.waitTillInitialized();
swigGroups = getGroupsOfBatchesOfSignatureSets(...getGroupsInfo(true));
});

itBench({
id: "libuv multithreading - napi",
id: `libuv multithreading - napi - addVerificationRandomness ${addVerificationRandomness}`,
fn: async () => {
const responses = [] as Promise<boolean>[];
for (const sets of napiGroups) {
Expand All @@ -33,20 +39,9 @@ describe("multithreading perf", function () {
expect(results.every((r) => r)).to.be.true;
},
});
});

describe("workers", () => {
let swigGroups: ReturnType<typeof getGroupsOfBatchesOfSignatureSets>;
before(async () => {
workerPool = new BlsMultiThreading({blsPoolType: BlsPoolType.workers});
await workerPool.waitTillInitialized();
swigGroups = getGroupsOfBatchesOfSignatureSets(...getGroupsInfo(true));
});
after(async () => {
await workerPool.close();
});
itBench({
id: "worker multithreading - swig",
id: `worker multithreading - swig - addVerificationRandomness ${addVerificationRandomness}`,
fn: async () => {
const responses = [] as Promise<any>[];
for (const sets of swigGroups) {
Expand All @@ -56,14 +51,14 @@ describe("multithreading perf", function () {
expect(results.every((r) => r)).to.be.true;
},
});
});

after(async () => {
console.log({
libuvPoolSize: libuvPool.blsPoolSize,
workerPoolSize: workerPool.blsPoolSize,
after(async () => {
console.log({
libuvPoolSize: libuvPool.blsPoolSize,
workerPoolSize: workerPool.blsPoolSize,
});
await libuvPool.close();
await workerPool.close();
});
await libuvPool.close();
await workerPool.close();
});
});
}
4 changes: 3 additions & 1 deletion test/utils/multithreading/blsMultiThreading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const MAX_JOBS_CAN_ACCEPT_WORK = 512;
export class BlsMultiThreading {
readonly blsPoolSize: number;

private readonly addVerificationRandomness: boolean;
private readonly blsVerifyAllInQueue: boolean;
private readonly blsPoolType: BlsPoolType;
private readonly workers: WorkerDescriptor[] = [];
Expand All @@ -63,6 +64,7 @@ export class BlsMultiThreading {
private workersBusy = 0;

constructor(options: BlsMultiThreadWorkerPoolOptions /*, modules: BlsMultiThreadWorkerPoolModules */) {
this.addVerificationRandomness = options.addVerificationRandomness ?? false;
this.blsVerifyAllInQueue = options.blsVerifyAllInQueue ?? false;
this.blsPoolType = options.blsPoolType ?? BlsPoolType.workers;

Expand Down Expand Up @@ -122,7 +124,7 @@ export class BlsMultiThreading {
resolve,
reject,
addedTimeMs: Date.now(),
opts,
opts: {...opts, addVerificationRandomness: this.addVerificationRandomness},
sets: setsChunk,
message,
});
Expand Down
46 changes: 43 additions & 3 deletions test/utils/multithreading/queuedJob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as swig from "../../../src";
import * as swig from "../../../src/lib";
import * as swigBindings from "../../../src/bindings";
import napi from "../../../rebuild/lib";
import {PublicKey} from "../types";
import {BlsWorkRequest, ISignatureSet, SignatureSetType, VerifySignatureOpts} from "./types";
Expand Down Expand Up @@ -47,6 +48,28 @@ export function prepareSwigWorkReqFromJob(job: QueuedJob): BlsWorkRequest {
};
}

if (job.opts.addVerificationRandomness) {
const pk_point = new swigBindings.blst.P1();
const sig_point = new swigBindings.blst.P2();
for (let i = 0; i < job.sets.length; i++) {
const randomness = swig.randomBytesNonZero(8);
pk_point.add((job.sets[i].publicKey as swig.PublicKey).jacobian.mult(randomness));
const sig = swig.Signature.fromBytes(job.sets[i].signature, swig.CoordType.affine);
sig.sigValidate();
sig_point.add(sig.jacobian.mult(randomness));
}
return {
opts: job.opts,
sets: [
{
pk: pk_point.serialize(),
sig: sig_point.serialize(),
msg: job.message,
},
],
};
}

const publicKey = swig.aggregatePubkeys(job.sets.map((set) => set.publicKey as swig.PublicKey));
const signature = swig.aggregateSignatures(
job.sets.map((set) => {
Expand Down Expand Up @@ -82,11 +105,28 @@ export function prepareNapiWorkReqFromJob(job: QueuedJob): BlsWorkRequest {
};
}

const publicKey = napi.aggregatePublicKeys(job.sets.map((set) => set.publicKey as napi.PublicKey));
const randomness: Uint8Array[] = [];
if (job.opts.addVerificationRandomness) {
for (let i = 0; i < job.sets.length; i++) {
randomness.push(swig.randomBytesNonZero(8));
}
}

const publicKey = napi.aggregatePublicKeys(
job.sets.map((set, i) => {
if (job.opts.addVerificationRandomness) {
return (set.publicKey as napi.PublicKey).multiplyBy(randomness[i]);
}
return set.publicKey as napi.PublicKey;
})
);
const signature = napi.aggregateSignatures(
job.sets.map((set) => {
job.sets.map((set, i) => {
const sig = napi.Signature.deserialize(set.signature, napi.CoordType.affine);
sig.sigValidate();
if (job.opts.addVerificationRandomness) {
return sig.multiplyBy(randomness[i]);
}
return sig;
})
);
Expand Down
2 changes: 2 additions & 0 deletions test/utils/multithreading/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export enum BlsPoolType {
export type BlsMultiThreadWorkerPoolOptions = {
blsVerifyAllInQueue?: boolean;
blsPoolType?: BlsPoolType;
addVerificationRandomness?: boolean;
};

export type BlsMultiThreadWorkerPoolModules = Record<string, never>;
Expand All @@ -89,6 +90,7 @@ export interface VerifySignatureOpts {
batchable?: boolean;
verifyOnMainThread?: boolean;
priority?: boolean;
addVerificationRandomness?: boolean;
}

export interface SerializedSwigSet {
Expand Down

0 comments on commit 30dad3e

Please sign in to comment.