Skip to content

Commit c54ac50

Browse files
committed
feat: use lane_point aggregate in precompute instead of hash_scalar
use only first generator for signing and verifying messages, test with max_lanes amount of messages Signed-off-by: pedro bufulin <pedro@semiotic.ai>
1 parent e46b998 commit c54ac50

File tree

2 files changed

+88
-43
lines changed

2 files changed

+88
-43
lines changed

h2s2/src/holographic_homomorphic_signature_scheme.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub trait HolographicHomomorphicSignatureScheme<P: Pairing, D: Digest + Send + S
3535
) -> Result<Self::Signature, Box<dyn Error>>;
3636

3737
/// Verify a single `signature` matches `message` with `tag` at `index` using `pp` parameter and `pk` public key
38+
/// TODO: index should be restricted to a number from 1 to N (max number of lanes)
3839
fn verify(
3940
pp: &Self::Parameters,
4041
pk: &Self::PublicKey,
@@ -48,7 +49,7 @@ pub trait HolographicHomomorphicSignatureScheme<P: Pairing, D: Digest + Send + S
4849
fn verify_aggregate(
4950
pp: &Self::Parameters,
5051
pk: &Self::PublicKey,
51-
tag: &[u8],
52+
// tag: &[u8],
5253
message_aggregate: &[Self::Message],
5354
hash_aggregate: &P::G1,
5455
signature: &Self::Signature,

h2s2/src/ncs.rs

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
5656

5757
// Prepare the parameters without the secret/public keys
5858
let g1_generators: Vec<P::G1> = (0..=n).map(|_| P::G1::rand(rng)).collect();
59-
let mut pp = H2S2Parameters {
59+
let mut pp: H2S2Parameters<P> = H2S2Parameters {
6060
g1_generators,
6161
g2_generator,
6262
secret_key: Some(P::ScalarField::zero()), // Temporary placeholder
@@ -93,14 +93,12 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
9393
let mut input = Vec::from(tag);
9494
input.extend_from_slice(&lane_id.to_le_bytes());
9595

96-
// Hash the concatenated input to generate a scalar value
97-
let hash_scalar = P::ScalarField::from_le_bytes_mod_order(D::digest(&input).as_ref());
98-
99-
// Map the scalar to a G1 element
100-
let hash_point = pp.g1_generators[0].mul(hash_scalar);
96+
// Hash the concatenated input to map it to a G1 element
97+
let lane_point = hash_to_g1::<P, D>(input);
10198

10299
// Add the resulting point to the hash aggregate
103-
hash_aggregate += hash_point;
100+
//TODO: substitutue the hash_point by the lane_point being calculated
101+
hash_aggregate += lane_point;
104102
}
105103

106104
Ok(hash_aggregate)
@@ -139,7 +137,7 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
139137
let mut message_commitment = P::G1::zero();
140138
for (i, m) in message.iter().enumerate() {
141139
// Multiply each message part with its respective generator
142-
let mut message_point = pp.g1_generators[i].clone();
140+
let mut message_point = pp.g1_generators[0].clone();
143141
message_point = message_point.mul(*m);
144142
message_commitment += message_point;
145143
}
@@ -173,7 +171,7 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
173171
let mut message_commitment = P::G1::zero();
174172
for (i, m) in message.iter().enumerate() {
175173
// Multiply each message part with its respective generator
176-
let mut message_point = pp.g1_generators[i].clone();
174+
let mut message_point = pp.g1_generators[0].clone();
177175
message_point = message_point.mul(*m);
178176
message_commitment += message_point;
179177
}
@@ -192,17 +190,20 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
192190
fn verify_aggregate(
193191
pp: &Self::Parameters,
194192
pk: &Self::PublicKey,
195-
// TODO: is the tag necessary for the verify_aggregate?1
196-
tag: &[u8],
197193
message_aggregate: &[Self::Message],
198194
hash_aggregate: &P::G1,
199195
signature: &Self::Signature,
200196
) -> Result<bool, Box<dyn Error>> {
201-
// Compute the message commitment for the aggregated message
197+
// Validate that the aggregate message matches the expected format
198+
if message_aggregate.len() != 1 {
199+
return Err("Message aggregate must be a single scalar".into());
200+
}
201+
202+
// Compute the message commitment for the aggregated messages
202203
let mut message_commitment = P::G1::zero();
203204
for (i, m) in message_aggregate.iter().enumerate() {
204205
// Multiply each message part with its respective generator
205-
let mut message_point = pp.g1_generators[i].clone();
206+
let mut message_point = pp.g1_generators[0].clone();
206207
message_point = message_point.mul(*m);
207208
message_commitment += message_point;
208209
}
@@ -284,35 +285,52 @@ mod tests {
284285

285286
println!("Precomputed Hash Aggregate: {:?}", hash_aggregate);
286287
}
288+
287289
#[test]
288290
fn test_sign_and_verify() {
289291
let params = &*PARAMS;
290292
let allocation_id = b"example_allocation_id";
291-
let index = b"lane_1";
292-
let message: Vec<ark_ff::Fp<ark_ff::MontBackend<ark_bn254::FrConfig, 4>, 4>> =
293-
vec![ark_bn254::Fr::from(42u64), ark_bn254::Fr::from(7u64)];
294-
295293
let sk = params.secret_key.unwrap();
296294
let pk = params.public_key;
295+
let messages: Vec<ark_bn254::Fr> = (0..N)
296+
.map(|_| ark_bn254::Fr::rand(&mut test_rng()))
297+
.collect();
297298

298-
// Sign the message
299-
let signature =
300-
NCS::<Bn254, Blake2b512>::sign(&params, &sk, allocation_id, index, &message)
301-
.expect("Sign failed");
302-
303-
// Verify the signature
304-
let is_valid = NCS::<Bn254, Blake2b512>::verify(
305-
&params,
306-
&pk,
307-
allocation_id,
308-
index,
309-
&message,
310-
&signature,
311-
)
312-
.expect("Verify failed");
299+
// Iterate through indices and sign each message
300+
for index in 0..N {
301+
let index_bytes = &(index.to_le_bytes())[..];
302+
303+
// Sign the message
304+
let signature = NCS::<Bn254, Blake2b512>::sign(
305+
&params,
306+
&sk,
307+
allocation_id,
308+
index_bytes,
309+
&[messages[index]],
310+
)
311+
.expect("Sign failed");
312+
313+
let index_bytes = &(index.to_le_bytes())[..];
314+
315+
// Verify the signature
316+
let is_valid = NCS::<Bn254, Blake2b512>::verify(
317+
&params,
318+
&pk,
319+
allocation_id,
320+
index_bytes,
321+
&[messages[index]],
322+
&signature,
323+
)
324+
.expect("Verify failed");
325+
326+
assert!(
327+
is_valid,
328+
"Signature verification failed for index {}!",
329+
index
330+
);
331+
}
313332

314-
assert!(is_valid, "Signature verification failed!");
315-
println!("Signature successfully verified!");
333+
println!("All signatures successfully verified for indices 0..{N}!");
316334
}
317335

318336
#[test]
@@ -321,17 +339,44 @@ mod tests {
321339
let sk = params.secret_key.unwrap();
322340
let pk = params.public_key;
323341
let allocation_id = b"example_allocation_id";
324-
let index = b"lane_1";
325-
let messages: Vec<_> = vec![ark_bn254::Fr::from(42u64), ark_bn254::Fr::from(7u64)];
342+
let messages: Vec<ark_bn254::Fr> = (0..N)
343+
.map(|_| ark_bn254::Fr::rand(&mut test_rng()))
344+
.collect();
326345

327346
// Generate individual signatures
328347
let signatures: Vec<_> = (0..N)
329-
.map(|_| {
330-
NCS::<Bn254, Blake2b512>::sign(&params, &sk, allocation_id, index, &messages)
331-
.expect("Sign failed")
348+
.map(|index| {
349+
// Convert the index into a byte slice
350+
let index_bytes = &(index.to_le_bytes())[..];
351+
352+
// Sign the message using the index as part of the signing process
353+
NCS::<Bn254, Blake2b512>::sign(
354+
&params,
355+
&sk,
356+
allocation_id,
357+
index_bytes,
358+
&[messages[index]],
359+
)
360+
.expect("Sign failed")
332361
})
333362
.collect();
334363

364+
// Verify the signature
365+
366+
for i in 0..N {
367+
let index_bytes = &(i.to_le_bytes())[..];
368+
let is_valid = NCS::<Bn254, Blake2b512>::verify(
369+
&params,
370+
&pk,
371+
allocation_id,
372+
index_bytes,
373+
&[messages[i]],
374+
&signatures[i],
375+
)
376+
.expect("Verify failed");
377+
assert!(is_valid, "Invalid single signature!");
378+
}
379+
335380
// Generate weights (all set to 1)
336381
let weights: Vec<usize> = vec![1; N];
337382

@@ -343,14 +388,13 @@ mod tests {
343388
let hash_aggregate = NCS::<Bn254, Blake2b512>::precompute(&params, allocation_id, N)
344389
.expect("Precompute failed");
345390

346-
// Aggregate the messages (sum all messages into one scalar)
391+
// // Aggregate the messages (sum all messages into one scalar)
347392
let message_aggregate: ark_bn254::Fr = messages.iter().copied().sum();
348393

349-
// Verify the aggregated signature
394+
// // Verify the aggregated signature
350395
let is_valid = NCS::<Bn254, Blake2b512>::verify_aggregate(
351396
&params,
352397
&pk,
353-
allocation_id,
354398
&[message_aggregate],
355399
&hash_aggregate,
356400
&aggregated_signature,

0 commit comments

Comments
 (0)