Skip to content

Commit 6288136

Browse files
committed
Deserialize, Serialize for XmssPubKey / naming
1 parent f07a751 commit 6288136

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rec_aggregation/src/benchmark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use xmss::{XmssPublicKey, XmssSignature};
1010
use utils::ansi as s;
1111

1212
use crate::compilation::{get_aggregation_bytecode, init_aggregation_bytecode};
13-
use crate::{AggregatedSigs, AggregationTopology, aggregate, count_signers};
13+
use crate::{AggregatedXMSS, AggregationTopology, aggregate, count_signers};
1414

1515
fn count_nodes(topology: &AggregationTopology) -> usize {
1616
1 + topology.children.iter().map(count_nodes).sum::<usize>()
@@ -225,7 +225,7 @@ fn build_aggregation(
225225
signatures: &[XmssSignature],
226226
overlap: usize,
227227
tracing: bool,
228-
) -> (AggregatedSigs, f64) {
228+
) -> (AggregatedXMSS, f64) {
229229
let message = message_for_benchmark();
230230
let slot = BENCHMARK_SLOT;
231231
let raw_count = topology.raw_xmss;

crates/rec_aggregation/src/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ pub(crate) fn count_signers(topology: &AggregationTopology, overlap: usize) -> u
3838
topology.raw_xmss + child_count - overlap * n_overlaps
3939
}
4040

41-
pub fn hash_pubkeys(pub_keys: &[Digest]) -> Digest {
41+
pub fn hash_pubkeys(pub_keys: &[XmssPublicKey]) -> Digest {
4242
let iv = [F::ZERO; DIGEST_LEN];
4343
let flat: Vec<F> = iv
4444
.iter()
4545
.copied()
46-
.chain(pub_keys.iter().flat_map(|pk| pk.0.iter().copied()))
46+
.chain(pub_keys.iter().flat_map(|pk| pk.merkle_root.iter().copied()))
4747
.collect();
4848
Digest(poseidon_compress_slice(&flat))
4949
}
@@ -95,16 +95,16 @@ fn encode_xmss_signature(sig: &XmssSignature) -> Vec<F> {
9595
}
9696

9797
#[derive(Debug, Serialize, Deserialize)]
98-
pub struct AggregatedSigs {
99-
pub pub_keys: Vec<Digest>,
98+
pub struct AggregatedXMSS {
99+
pub pub_keys: Vec<XmssPublicKey>,
100100
pub proof: PrunedProof<F>,
101101
pub bytecode_point: Option<MultilinearPoint<EF>>,
102102
// benchmark / debug purpose
103103
#[serde(skip, default)]
104104
pub metadata: Option<ExecutionMetadata>,
105105
}
106106

107-
impl AggregatedSigs {
107+
impl AggregatedXMSS {
108108
pub fn raw_proof(&self) -> Option<RawProof<F>> {
109109
self.proof.clone().restore().map(|p| p.into_raw_proof())
110110
}
@@ -152,31 +152,31 @@ impl AggregatedSigs {
152152
}
153153

154154
pub fn verify_aggregation(
155-
sigs: &AggregatedSigs,
155+
agg_sig: &AggregatedXMSS,
156156
message: &[F; MESSAGE_LEN_FE],
157157
slot: u32,
158158
) -> Result<ProofVerificationDetails, ProofError> {
159-
if !sigs.pub_keys.is_sorted() {
159+
if !agg_sig.pub_keys.is_sorted() {
160160
return Err(ProofError::InvalidProof);
161161
}
162-
let public_input = sigs.public_input(message, slot);
162+
let public_input = agg_sig.public_input(message, slot);
163163
let bytecode = get_aggregation_bytecode();
164164
verify_execution(
165165
bytecode,
166166
&public_input,
167-
sigs.raw_proof().ok_or(ProofError::InvalidProof)?,
167+
agg_sig.raw_proof().ok_or(ProofError::InvalidProof)?,
168168
)
169169
}
170170

171171
/// panics if one of the sub-proof (children) is invalid
172172
#[instrument(skip_all)]
173173
pub fn aggregate(
174-
children: &[AggregatedSigs],
174+
children: &[AggregatedXMSS],
175175
mut raw_xmss: Vec<(XmssPublicKey, XmssSignature)>,
176176
message: &[F; MESSAGE_LEN_FE],
177177
slot: u32,
178178
log_inv_rate: usize,
179-
) -> AggregatedSigs {
179+
) -> AggregatedXMSS {
180180
raw_xmss.sort_by_key(|(a, _)| Digest(a.merkle_root));
181181
raw_xmss.dedup_by(|(a, _), (b, _)| a.merkle_root == b.merkle_root);
182182

@@ -189,7 +189,7 @@ pub fn aggregate(
189189
let bytecode_claim_size = (bytecode_point_n_vars + 1) * DIMENSION;
190190

191191
// Build global_pub_keys as sorted deduplicated union
192-
let mut global_pub_keys: Vec<Digest> = raw_xmss.iter().map(|(pk, _)| Digest(pk.merkle_root)).collect();
192+
let mut global_pub_keys: Vec<XmssPublicKey> = raw_xmss.iter().map(|(pk, _)| pk.clone()).collect();
193193
for child in children.iter() {
194194
assert!(child.pub_keys.is_sorted(), "child pub_keys must be sorted");
195195
global_pub_keys.extend_from_slice(&child.pub_keys);
@@ -297,8 +297,8 @@ pub fn aggregate(
297297
let pubkeys_start = public_memory.len() + header_size;
298298

299299
// Build source blocks (also discovers duplicate pub_keys)
300-
let mut claimed: HashSet<Digest> = HashSet::new();
301-
let mut dup_pub_keys: Vec<Digest> = Vec::new();
300+
let mut claimed: HashSet<XmssPublicKey> = HashSet::new();
301+
let mut dup_pub_keys: Vec<XmssPublicKey> = Vec::new();
302302
let mut source_blocks: Vec<Vec<F>> = vec![];
303303

304304
// Build XMSS signatures (one Vec<F> per signature, consumed by hint_xmss)
@@ -308,10 +308,9 @@ pub fn aggregate(
308308
{
309309
let mut block = vec![F::from_usize(raw_count)];
310310
for (pk, _) in &raw_xmss {
311-
let key = Digest(pk.merkle_root);
312-
let pos = global_pub_keys.binary_search(&key).unwrap();
311+
let pos = global_pub_keys.binary_search(pk).unwrap();
313312
block.push(F::from_usize(pos));
314-
claimed.insert(key);
313+
claimed.insert(pk.clone());
315314
}
316315
source_blocks.push(block);
317316
}
@@ -320,12 +319,12 @@ pub fn aggregate(
320319
for (i, child) in children.iter().enumerate() {
321320
let mut block = vec![F::from_usize(child.pub_keys.len())];
322321
for key in &child.pub_keys {
323-
if claimed.insert(*key) {
322+
if claimed.insert(key.clone()) {
324323
let pos = global_pub_keys.binary_search(key).unwrap();
325324
block.push(F::from_usize(pos));
326325
} else {
327326
block.push(F::from_usize(n_sigs + dup_pub_keys.len()));
328-
dup_pub_keys.push(*key);
327+
dup_pub_keys.push(key.clone());
329328
}
330329
}
331330

@@ -364,10 +363,10 @@ pub fn aggregate(
364363
assert_eq!(private_input.len(), header_size);
365364

366365
for pk in &global_pub_keys {
367-
private_input.extend_from_slice(&pk.0);
366+
private_input.extend_from_slice(&pk.merkle_root);
368367
}
369368
for pk in &dup_pub_keys {
370-
private_input.extend_from_slice(&pk.0);
369+
private_input.extend_from_slice(&pk.merkle_root);
371370
}
372371
for block in &source_blocks {
373372
private_input.extend_from_slice(block);
@@ -397,7 +396,7 @@ pub fn aggregate(
397396
};
398397
let execution_proof = prove_execution(bytecode, &non_reserved_public_input, &witness, &whir_config, false);
399398

400-
AggregatedSigs {
399+
AggregatedXMSS {
401400
pub_keys: global_pub_keys,
402401
proof: execution_proof.proof,
403402
bytecode_point,

crates/xmss/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ workspace = true
1010

1111
rand.workspace = true
1212
utils.workspace = true
13-
backend.workspace = true
13+
backend.workspace = true
14+
serde.workspace = true

crates/xmss/src/xmss.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use backend::*;
22
use rand::{CryptoRng, Rng, SeedableRng, rngs::StdRng};
3+
use serde::{Deserialize, Serialize};
34

45
use crate::*;
56

@@ -19,7 +20,7 @@ pub struct XmssSignature {
1920
pub merkle_proof: Vec<Digest>,
2021
}
2122

22-
#[derive(Debug, Clone)]
23+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
2324
pub struct XmssPublicKey {
2425
pub merkle_root: Digest,
2526
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use backend::*;
22

33
pub use backend::ProofError;
4-
pub use rec_aggregation::{AggregatedSigs, AggregationTopology, Digest, aggregate, verify_aggregation};
4+
pub use rec_aggregation::{AggregatedXMSS, AggregationTopology, Digest, aggregate, verify_aggregation};
55
pub use xmss::{MESSAGE_LEN_FE, XmssPublicKey, XmssSecretKey, XmssSignature, xmss_key_gen, xmss_sign, xmss_verify};
66

77
pub type F = KoalaBear;
@@ -70,7 +70,7 @@ mod tests {
7070

7171
let serialized_final = aggregated_final.serialize();
7272
println!("Serialized aggregated final: {} KiB", serialized_final.len() / 1024);
73-
let deserialized_final = AggregatedSigs::deserialize(&serialized_final).unwrap();
73+
let deserialized_final = AggregatedXMSS::deserialize(&serialized_final).unwrap();
7474

7575
verify_aggregation(&deserialized_final, &message, slot).unwrap();
7676
}

0 commit comments

Comments
 (0)