Skip to content

Commit 448bea3

Browse files
committed
refactor: add only base generator
also refactor curve and hasher types in test Signed-off-by: pedro bufulin <pedro@semiotic.ai>
1 parent 6176415 commit 448bea3

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

h2s2/src/ncs.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
7676

7777
// Generate a deterministic set of G1 generators based on the hardcoded G1 generator
7878
let g1_base_generator = P::G1::generator();
79-
let g1_generators: Vec<P::G1> = (0..=n)
80-
.map(|i| g1_base_generator.mul(&P::ScalarField::from(i as u64)))
81-
.collect();
79+
// In practice, we only ever use the first g1 generator
80+
// so it is going to be generated only g1[0]
81+
let g1_generators = vec![g1_base_generator];
8282

8383
// Initialize parameters without secret/public keys
8484
let pp: H2S2Parameters<P> = H2S2Parameters {
@@ -214,18 +214,25 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
214214
mod tests {
215215
use super::*;
216216
use ark_bn254::Bn254;
217+
//we could also use the ark_bls12_381 curve which was intended to substitute this one:
218+
//https://docs.rs/ark-bls12-381/latest/ark_bls12_381/
219+
// Ethereum is reviewing using it:
220+
// https://eips.ethereum.org/EIPS/eip-2537
221+
// use ark_bls12_381::Bn254;
217222
use ark_std::test_rng;
218-
use blake2::Blake2b512; // Use 512-bit Blake2b for digest
219223
use once_cell::sync::Lazy;
220-
224+
type Curve = Bn254;
225+
type Fr = ark_bn254::Fr;
226+
type Hasher = blake2::Blake2b512;
221227
static N: usize = 10; // Define the number of generators
222-
static PARAMS: Lazy<H2S2Parameters<Bn254>> = Lazy::new(|| {
228+
229+
static PARAMS: Lazy<H2S2Parameters<Curve>> = Lazy::new(|| {
223230
let mut rng = test_rng();
224231

225-
let mut params = NCS::<Bn254, Blake2b512>::setup(N).expect("Setup failed");
232+
let mut params = NCS::<Curve, Hasher>::setup(N).expect("Setup failed");
226233

227234
// Generate the secret and public keys using keygen
228-
let (pk, sk) = NCS::<Bn254, Blake2b512>::keygen(&params, &mut rng).expect("Keygen failed");
235+
let (pk, sk) = NCS::<Curve, Hasher>::keygen(&params, &mut rng).expect("Keygen failed");
229236

230237
params.secret_key = Some(sk);
231238
params.public_key = pk;
@@ -234,19 +241,21 @@ mod tests {
234241

235242
#[test]
236243
fn test_setup_and_keygen() {
244+
// Use the correct WBConfig implementation for G1
245+
237246
let mut rng = test_rng();
238247
let n = 10;
239248

240-
let params = NCS::<Bn254, Blake2b512>::setup(n).expect("Setup failed");
249+
let params = &*PARAMS; // Explicit reference to PARAMS
241250

242-
let (pk, sk) = NCS::<Bn254, Blake2b512>::keygen(&params, &mut rng).expect("Keygen failed");
251+
let (pk, sk) = NCS::<Curve, Hasher>::keygen(&params, &mut rng).expect("Keygen failed");
243252

244253
assert_eq!(
245254
params.g1_generators.len(),
246-
n + 1,
255+
1,
247256
"Incorrect number of G1 generators"
248257
);
249-
assert_eq!(params.max_lanes, n, "Max lanes value mismatch");
258+
assert_eq!(params.max_lanes, n, "Max lanes value 'mismatch");
250259

251260
// Verify the public key matches the secret key and G2 generator relationship
252261
let calculated_public_key = params.g2_generator.mul(sk);
@@ -263,7 +272,7 @@ mod tests {
263272
let params = &*PARAMS;
264273
let mut rng = test_rng();
265274
let (hash_aggregate, alloc_id) =
266-
NCS::<Bn254, Blake2b512>::precompute(&params, &mut rng, N).expect("Precompute failed");
275+
NCS::<Curve, Hasher>::precompute(&params, &mut rng, N).expect("Precompute failed");
267276

268277
println!("Precomputed Hash Aggregate: {:?}", hash_aggregate);
269278
println!("allocation_id {:?}", alloc_id);
@@ -276,20 +285,20 @@ mod tests {
276285

277286
// Precompute the hash aggregate and allocation ID
278287
let (_, allocation_id) =
279-
NCS::<Bn254, Blake2b512>::precompute(&params, &mut rng, N).expect("Precompute failed");
288+
NCS::<Curve, Hasher>::precompute(&params, &mut rng, N).expect("Precompute failed");
280289

281290
// Generate messages for each lane/index
282-
let messages: Vec<ark_bn254::Fr> = (0..N).map(|_| ark_bn254::Fr::rand(&mut rng)).collect();
291+
let messages: Vec<Fr> = (0..N).map(|_| Fr::rand(&mut rng)).collect();
283292

284293
// Iterate through indices and sign each message
285294
for index in 0..N {
286295
// Sign the message with the current index
287296
let signature =
288-
NCS::<Bn254, Blake2b512>::sign(&params, allocation_id, index, messages[index])
297+
NCS::<Curve, Hasher>::sign(&params, allocation_id, index, messages[index])
289298
.expect("Sign failed");
290299

291300
// Verify the signature with the same index
292-
let is_valid = NCS::<Bn254, Blake2b512>::verify(
301+
let is_valid = NCS::<Curve, Hasher>::verify(
293302
&params,
294303
allocation_id,
295304
index,
@@ -314,23 +323,23 @@ mod tests {
314323
let params = &*PARAMS;
315324

316325
// Generate random messages for each lane/index
317-
let messages: Vec<ark_bn254::Fr> = (0..N).map(|_| ark_bn254::Fr::rand(&mut rng)).collect();
326+
let messages: Vec<Fr> = (0..N).map(|_| Fr::rand(&mut rng)).collect();
318327

319328
// Precompute the hash aggregate and allocation ID
320329
let (hash_aggregate, allocation_id) =
321-
NCS::<Bn254, Blake2b512>::precompute(&params, &mut rng, N).expect("Precompute failed");
330+
NCS::<Curve, Hasher>::precompute(&params, &mut rng, N).expect("Precompute failed");
322331

323332
// Generate individual signatures for each message
324333
let mut signatures: Vec<_> = (0..N)
325334
.map(|index| {
326-
NCS::<Bn254, Blake2b512>::sign(&params, allocation_id, index, messages[index])
335+
NCS::<Curve, Hasher>::sign(&params, allocation_id, index, messages[index])
327336
.expect("Sign failed")
328337
})
329338
.collect();
330339

331340
// Verify each individual signature
332341
for (index, signature) in signatures.iter().enumerate() {
333-
let is_valid = NCS::<Bn254, Blake2b512>::verify(
342+
let is_valid = NCS::<Curve, Hasher>::verify(
334343
&params,
335344
allocation_id,
336345
index,
@@ -346,15 +355,12 @@ mod tests {
346355

347356
// Aggregate the signatures
348357
let aggregated_signature =
349-
NCS::<Bn254, Blake2b512>::evaluate(&signatures, &weights).expect("Evaluate failed");
358+
NCS::<Curve, Hasher>::evaluate(&signatures, &weights).expect("Evaluate failed");
350359

351360
// Verify the aggregated signature
352-
let is_valid = NCS::<Bn254, Blake2b512>::verify_aggregate(
353-
&params,
354-
&hash_aggregate,
355-
&aggregated_signature,
356-
)
357-
.expect("Verify failed");
361+
let is_valid =
362+
NCS::<Curve, Hasher>::verify_aggregate(&params, &hash_aggregate, &aggregated_signature)
363+
.expect("Verify failed");
358364

359365
assert!(
360366
is_valid,
@@ -376,10 +382,10 @@ mod tests {
376382

377383
// Aggregate the signatures, including the duplicate
378384
let tampered_aggregate_signature =
379-
NCS::<Bn254, Blake2b512>::evaluate(&signatures, &weights).expect("Evaluate failed");
385+
NCS::<Curve, Hasher>::evaluate(&signatures, &weights).expect("Evaluate failed");
380386

381387
// Verify the aggregated signature with the tampered signature table
382-
let is_valid = NCS::<Bn254, Blake2b512>::verify_aggregate(
388+
let is_valid = NCS::<Curve, Hasher>::verify_aggregate(
383389
&params,
384390
&hash_aggregate,
385391
&tampered_aggregate_signature,

0 commit comments

Comments
 (0)