Skip to content

Commit 7afa786

Browse files
committed
test: add verify of invalid singatur
1 parent f804e7e commit 7afa786

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

h2s2/src/holographic_homomorphic_signature_scheme.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub trait HolographicHomomorphicSignatureScheme<P: Pairing, D: Digest + Send + S
1010
type Signature;
1111
type Message;
1212
type Weight;
13-
type AggregateSignature;
13+
type AggregatedSignature;
1414

1515
/// Generate one G2 element and `n` G1 elements
1616
fn setup<R: Rng>(rng: &mut R, n: usize) -> Result<Self::Parameters, Box<dyn Error>>;
@@ -47,19 +47,18 @@ pub trait HolographicHomomorphicSignatureScheme<P: Pairing, D: Digest + Send + S
4747
signature: &Self::Signature,
4848
) -> Result<bool, Box<dyn Error>>;
4949

50-
// Verify aggregate `signature` matches `message_aggregate` with `tag` and `hash_aggregate` using `pp` parameter and `pk` public key
50+
/// Verify aggregate `signature` matches `message_aggregate`
51+
/// contained in [`AggregatedSignature`] with `tag` and `hash_aggregate` using `pp` parameter and `pk` public key
5152
fn verify_aggregate(
5253
pp: &Self::Parameters,
53-
pk: &Self::PublicKey,
5454
// tag: &[u8],
55-
message_aggregate: &[Self::Message],
5655
hash_aggregate: &P::G1,
57-
signature: &Self::AggregateSignature,
56+
signature: &Self::AggregatedSignature,
5857
) -> Result<bool, Box<dyn Error>>;
5958

6059
/// Aggregate `signatures` with `weights`
6160
fn evaluate(
6261
signatures: &[Self::Signature],
6362
weights: &[Self::Weight],
64-
) -> Result<Self::AggregateSignature, Box<dyn Error>>;
63+
) -> Result<Self::AggregatedSignature, Box<dyn Error>>;
6564
}

h2s2/src/ncs.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::ops::{Add, Mul, MulAssign};
22
use std::{error::Error, marker::PhantomData};
33

44
use crate::holographic_homomorphic_signature_scheme::HolographicHomomorphicSignatureScheme;
5-
use ark_bn254::{G1Projective, G2Projective};
65
use ark_ec::pairing::Pairing;
76
use ark_ec::AffineRepr;
87
use ark_ff::PrimeField;
@@ -47,7 +46,7 @@ pub struct Signature<P: Pairing> {
4746
}
4847

4948
#[derive(Clone)]
50-
pub struct AggregateSignature<P: Pairing> {
49+
pub struct AggregatedSignature<P: Pairing> {
5150
pub signature: P::G1,
5251
pub total_value: P::ScalarField,
5352
}
@@ -67,7 +66,7 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
6766
type Signature = Signature<P>;
6867
type Message = P::ScalarField;
6968
type Weight = usize;
70-
type AggregateSignature = AggregateSignature<P>;
69+
type AggregatedSignature = AggregatedSignature<P>;
7170

7271
// n represents the max_lanes amount
7372
fn setup<R: Rng>(rng: &mut R, n: usize) -> Result<Self::Parameters, Box<dyn Error>> {
@@ -175,10 +174,8 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
175174

176175
fn verify_aggregate(
177176
pp: &Self::Parameters,
178-
pk: &Self::PublicKey,
179-
message_aggregate: &[<P as Pairing>::ScalarField],
180177
hash_aggregate: &P::G1,
181-
signature: &Self::AggregateSignature,
178+
signature: &Self::AggregatedSignature,
182179
) -> Result<bool, Box<dyn Error>> {
183180
let lane_point = hash_aggregate;
184181
let mut value_point = pp.g1_generators[0].clone();
@@ -192,15 +189,15 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
192189
fn evaluate(
193190
signatures: &[Self::Signature],
194191
_weights: &[Self::Weight],
195-
) -> Result<Self::AggregateSignature, Box<dyn Error>> {
192+
) -> Result<Self::AggregatedSignature, Box<dyn Error>> {
196193
let mut aggregate_signature = P::G1::zero();
197194
let mut total_value = P::ScalarField::zero();
198195
for sig in signatures {
199196
aggregate_signature += sig.signature;
200197
total_value += sig.value;
201198
}
202199

203-
Ok(AggregateSignature {
200+
Ok(AggregatedSignature {
204201
signature: aggregate_signature,
205202
total_value,
206203
})
@@ -255,12 +252,9 @@ mod tests {
255252
let params = &*PARAMS;
256253

257254
// Precompute the hash aggregate and allocation ID
258-
let (hash_aggregate, allocation_id) =
255+
let (_, allocation_id) =
259256
NCS::<Bn254, Blake2b512>::precompute(&params, &mut rng, N).expect("Precompute failed");
260257

261-
let sk = params.secret_key.unwrap();
262-
let pk = params.public_key;
263-
264258
// Generate messages for each lane/index
265259
let messages: Vec<ark_bn254::Fr> = (0..N).map(|_| ark_bn254::Fr::rand(&mut rng)).collect();
266260

@@ -295,8 +289,8 @@ mod tests {
295289
fn test_aggregate() {
296290
let mut rng = test_rng();
297291
let params = &*PARAMS;
298-
let sk = params.secret_key.unwrap();
299-
let pk = params.public_key;
292+
// let sk = params.secret_key.unwrap();
293+
// let pk = params.public_key;
300294

301295
// Generate random messages for each lane/index
302296
let messages: Vec<ark_bn254::Fr> = (0..N).map(|_| ark_bn254::Fr::rand(&mut rng)).collect();
@@ -306,7 +300,7 @@ mod tests {
306300
NCS::<Bn254, Blake2b512>::precompute(&params, &mut rng, N).expect("Precompute failed");
307301

308302
// Generate individual signatures for each message
309-
let signatures: Vec<_> = (0..N)
303+
let mut signatures: Vec<_> = (0..N)
310304
.map(|index| {
311305
NCS::<Bn254, Blake2b512>::sign(&params, allocation_id, index, messages[index])
312306
.expect("Sign failed")
@@ -334,13 +328,11 @@ mod tests {
334328
NCS::<Bn254, Blake2b512>::evaluate(&signatures, &weights).expect("Evaluate failed");
335329

336330
// Compute the aggregate message (sum of all messages)
337-
let message_aggregate: ark_bn254::Fr = messages.iter().copied().sum();
331+
// let message_aggregate: ark_bn254::Fr = messages.iter().copied().sum();
338332

339333
// Verify the aggregated signature
340334
let is_valid = NCS::<Bn254, Blake2b512>::verify_aggregate(
341335
&params,
342-
&pk,
343-
&[message_aggregate],
344336
&hash_aggregate,
345337
&aggregated_signature,
346338
)
@@ -355,5 +347,34 @@ mod tests {
355347
"Aggregated signature successfully verified for all {} messages!",
356348
N
357349
);
350+
351+
// this next signature aggregation test should fail
352+
// Introduce a duplicate signature to simulate a lying indexer
353+
let random_index = rng.gen_range(0..N);
354+
let duplicate_signature = signatures[random_index].clone();
355+
signatures.push(duplicate_signature);
356+
357+
// Aggregate the signatures, including the duplicate
358+
let tampered_aggregate_signature =
359+
NCS::<Bn254, Blake2b512>::evaluate(&signatures, &weights).expect("Evaluate failed");
360+
361+
// Compute the aggregate message (should fail because of duplicate signature)
362+
// let tampered_message_aggregate: ark_bn254::Fr = messages.iter().copied().sum();
363+
364+
// Verify the aggregated signature with the tampered signature table
365+
let is_valid = NCS::<Bn254, Blake2b512>::verify_aggregate(
366+
&params,
367+
&hash_aggregate,
368+
&tampered_aggregate_signature,
369+
)
370+
.expect("Verify failed");
371+
372+
// Assert that verification fails
373+
assert!(
374+
!is_valid,
375+
"Aggregated signature verification should fail with a tampered signature table!"
376+
);
377+
378+
println!("Tampered aggregated signature verification correctly failed as expected!");
358379
}
359380
}

0 commit comments

Comments
 (0)