From 95cba24cfe0179551fe911b448728790c95a6dd1 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Thu, 17 Oct 2024 18:11:08 +0700 Subject: [PATCH 1/2] feat: change to 128-bits of randomness/blinding --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6f85e8db..7151d417 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -496,7 +496,7 @@ pub fn verify_multiple_aggregate_signatures( &sigs, sigs_groupcheck.unwrap_or(false), &rands, - 64, + 128, ) == BLST_ERROR::BLST_SUCCESS } @@ -552,8 +552,8 @@ fn rand_non_zero(rng: &mut ThreadRng) -> u64 { /// copied from lighthouse: /// https://github.com/sigp/lighthouse/blob/9e12c21f268c80a3f002ae0ca27477f9f512eb6f/crypto/bls/src/impls/blst.rs#L52 -fn create_scalar(i: u64) -> blst_scalar { - let vals = [i, 0, 0, 0]; +fn create_scalar(i1: u64, i2: u64) -> blst_scalar { + let vals = [i1, i2, 0, 0]; let mut scalar = std::mem::MaybeUninit::::uninit(); unsafe { blst_scalar_from_uint64(scalar.as_mut_ptr(), vals.as_ptr()); @@ -565,14 +565,14 @@ fn create_scalar(i: u64) -> blst_scalar { fn create_rand_scalars(len: usize) -> Vec { let mut rng = rand::thread_rng(); (0..len) - .map(|_| create_scalar(rand_non_zero(&mut rng))) + .map(|_| create_scalar(rand_non_zero(&mut rng), rand_non_zero(&mut rng))) .collect() } /// Creates a vector of random bytes, length len * 8 fn create_rand_slice(len: usize) -> Vec { let mut rng = rand::thread_rng(); - (0..len) + (0..(2 * len)) .map(|_| rand_non_zero(&mut rng).to_ne_bytes()) .flatten() .collect() @@ -584,8 +584,8 @@ fn aggregate_with( sigs: &[min_pk::Signature], scalars: &[u8], ) -> (min_pk::PublicKey, min_pk::Signature) { - let pk = pks.mult(scalars, 64).to_public_key(); - let sig = sigs.mult(scalars, 64).to_signature(); + let pk = pks.mult(scalars, 128).to_public_key(); + let sig = sigs.mult(scalars, 128).to_signature(); (pk, sig) } From 7cb907aa96114ad2e41277ebfc262756a40f0a5c Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 22 Oct 2024 20:06:34 +0700 Subject: [PATCH 2/2] test: update tests to make sure MSM is being perf'd correctly --- .benchrc.yaml | 2 +- test/perf/functions.test.ts | 46 +++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.benchrc.yaml b/.benchrc.yaml index 9472182e..4abdbcd6 100644 --- a/.benchrc.yaml +++ b/.benchrc.yaml @@ -6,5 +6,5 @@ node-option: # benchmark opts threshold: 3 -maxMs: 60_000 +maxMs: 180_000 minRuns: 10 diff --git a/test/perf/functions.test.ts b/test/perf/functions.test.ts index cd674e16..9f34a318 100644 --- a/test/perf/functions.test.ts +++ b/test/perf/functions.test.ts @@ -26,7 +26,7 @@ describe("functions", () => { } }); describe("aggregateWithRandomness", () => { - for (const count of [1, 16, 128, 256, 512, 1024]) { + for (const count of [1, 8, 64, 512, 2048, 16_000]) { itBench({ id: `aggregateWithRandomness - ${count} sets`, before: () => { @@ -75,18 +75,36 @@ describe("functions", () => { } }); describe("verifyMultipleAggregateSignatures", () => { - for (const count of [1, 8, 32, 128, 256]) { + for (const count of [1, 8, 64, 512, 2048, 16_000]) { itBench({ id: `verifyMultipleAggregateSignatures - ${count} sets`, - beforeEach: () => arrayOfIndexes(0, count - 1).map((i) => getTestSet(i)), + beforeEach: () => + arrayOfIndexes(0, count - 1).map((i) => { + const set = getTestSet(i); + return { + msg: set.msg, + pk: set.pk, + sig: set.sig.toBytes(), + }; + }), fn: (sets) => { - blst.verifyMultipleAggregateSignatures(sets); + blst.verifyMultipleAggregateSignatures( + sets.map((set) => { + const sig = blst.Signature.fromBytes(set.sig); + sig.sigValidate(); + return { + msg: set.msg, + pk: set.pk, + sig, + }; + }) + ); }, }); } }); describe("verifyMultipleAggregateSignatures same message", () => { - for (const count of [1, 8, 32, 128, 256]) { + for (const count of [1, 8, 64, 512, 2048, 16_000]) { itBench({ id: `Same message - ${count} sets`, beforeEach: () => @@ -94,21 +112,15 @@ describe("functions", () => { .map((i) => getTestSetSameMessage(i)) .map((set) => { return { - message: set.msg, - secretKey: set.sk, - publicKey: set.pk, - signature: set.sig.toBytes(), + msg: set.msg, + sk: set.sk, + pk: set.pk, + sig: set.sig.toBytes(), }; }), fn: (sets) => { - const aggregatedPubkey = blst.aggregatePublicKeys(sets.map((set) => set.publicKey)); - const aggregatedSignature = blst.aggregateSignatures( - sets.map((set) => { - const sig = blst.Signature.fromBytes(set.signature, true, true); - return sig; - }) - ); - const isValid = blst.verify(sets[0].message, aggregatedPubkey, aggregatedSignature); + const {pk, sig} = blst.aggregateWithRandomness(sets); + const isValid = blst.verify(sets[0].msg, pk, sig); if (!isValid) throw Error("Invalid"); }, });