Skip to content

Commit 0984bd7

Browse files
fix: merkle tree and blob data exceeding BLS_MODULUS (#1848)
1 parent 559833d commit 0984bd7

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

aggregation_mode/src/backend/merkle_tree.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ pub fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
1010

1111
/// Returns (merkle_root, leaves)
1212
pub fn compute_proofs_merkle_root(proofs: &[AlignedProof]) -> ([u8; 32], Vec<[u8; 32]>) {
13-
let leaves: Vec<[u8; 32]> = proofs
14-
.chunks(2)
15-
.map(|chunk| match chunk {
16-
[a, b] => combine_hashes(&a.hash(), &b.hash()),
17-
[a] => combine_hashes(&a.hash(), &a.hash()),
18-
_ => panic!("Unexpected chunk leaves"),
19-
})
20-
.collect();
13+
let leaves: Vec<[u8; 32]> = proofs.iter().map(|proof| proof.hash()).collect();
2114

2215
let mut root = leaves.clone();
2316

aggregation_mode/src/backend/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ mod merkle_tree;
44
mod s3;
55
mod types;
66

7-
use crate::aggregators::{lib::{AggregatedProof, ProofAggregationError}, sp1_aggregator::{aggregate_proofs, SP1AggregationInput}, AlignedProof, ZKVMEngine};
8-
7+
use crate::aggregators::{
8+
lib::{AggregatedProof, ProofAggregationError},
9+
sp1_aggregator::{aggregate_proofs, SP1AggregationInput},
10+
AlignedProof, ZKVMEngine,
11+
};
912

1013
use alloy::{
1114
consensus::{Blob, BlobTransactionSidecar},
@@ -25,7 +28,6 @@ use std::str::FromStr;
2528
use tracing::{error, info, warn};
2629
use types::{AlignedProofAggregationService, AlignedProofAggregationServiceContract};
2730

28-
2931
#[derive(Debug)]
3032
pub enum AggregatedProofSubmissionError {
3133
Aggregation(ProofAggregationError),
@@ -122,8 +124,7 @@ impl ProofAggregator {
122124
merkle_root,
123125
};
124126

125-
aggregate_proofs(input)
126-
.map_err(AggregatedProofSubmissionError::Aggregation)?
127+
aggregate_proofs(input).map_err(AggregatedProofSubmissionError::Aggregation)?
127128
}
128129
};
129130
info!("Proof aggregation program finished");
@@ -184,8 +185,17 @@ impl ProofAggregator {
184185
let data: Vec<u8> = leaves.iter().flat_map(|arr| arr.iter().copied()).collect();
185186
let mut blob_data: [u8; BYTES_PER_BLOB] = [0u8; BYTES_PER_BLOB];
186187

187-
for (i, byte) in data.iter().enumerate() {
188-
blob_data[i] = *byte;
188+
// We pad the data with 0x0 byte every 31 bytes so that the field elements
189+
// constructed from the bytes are less than BLS_MODULUS.
190+
//
191+
// See https://github.com/ethereum/consensus-specs/blob/86fb82b221474cc89387fa6436806507b3849d88/specs/deneb/polynomial-commitments.md#bytes_to_bls_field
192+
let mut offset = 0;
193+
for chunk in data.chunks(31) {
194+
blob_data[offset] = 0x00;
195+
let start = offset + 1;
196+
let end = start + chunk.len();
197+
blob_data[start..end].copy_from_slice(chunk);
198+
offset += 32;
189199
}
190200

191201
// calculate kzg commitments for blob

0 commit comments

Comments
 (0)