Skip to content

Commit 508ccb1

Browse files
committed
feat: verify blob merkle root
1 parent fed5508 commit 508ccb1

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

batcher/aligned-sdk/src/agg_mode.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ethers::{
66
providers::{Http, Middleware, Provider},
77
types::Filter,
88
};
9+
use sha3::{Digest, Keccak256};
910

1011
#[derive(Debug)]
1112
pub enum ProofVerificationAggModeError {
@@ -95,7 +96,7 @@ pub async fn is_proof_verified_in_aggregation_mode(
9596
.unwrap();
9697

9798
if proof_hashes.contains(&proof_hash_bytes) {
98-
return Ok(verify_merkle_root(proof_hashes, merkle_root));
99+
return Ok(verify_blob_merkle_root(proof_hashes, merkle_root));
99100
} else {
100101
continue;
101102
}
@@ -111,18 +112,15 @@ fn decoded_blob(blob_data: Vec<u8>) -> Vec<[u8; 32]> {
111112
let mut current_hash_count = 0;
112113
let mut total_bytes_count = 0;
113114

114-
let mut i = 0;
115-
116-
while i < blob_data.len() {
117-
// Every 32 bytes (or 64 characters) there is a 0x00 acting as padding, so we need to skip the byte (two iterations)
115+
while total_bytes_count < blob_data.len() {
116+
// Every 32 bytes there is a 0x0 acting as padding, so we need to skip the byte
118117
let is_pad = total_bytes_count % 32 == 0;
119118
if is_pad {
120-
i += 1;
121119
total_bytes_count += 1;
122120
continue;
123121
}
124122

125-
current_hash[current_hash_count] = blob_data[i];
123+
current_hash[current_hash_count] = blob_data[total_bytes_count];
126124

127125
if current_hash_count + 1 == 32 {
128126
if current_hash == [0u8; 32] {
@@ -131,18 +129,34 @@ fn decoded_blob(blob_data: Vec<u8>) -> Vec<[u8; 32]> {
131129
proof_hashes.push(current_hash);
132130
current_hash = [0u8; 32];
133131
current_hash_count = 0;
134-
continue;
135132
} else {
136133
current_hash_count += 1;
137134
}
138135

139-
i += 1;
140136
total_bytes_count += 1;
141137
}
142138

143139
proof_hashes
144140
}
145141

146-
fn verify_merkle_root(proof_hashes: Vec<[u8; 32]>, merkle_root: [u8; 32]) -> bool {
147-
true
142+
pub fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
143+
let mut hasher = Keccak256::new();
144+
hasher.update(hash_a);
145+
hasher.update(hash_b);
146+
hasher.finalize().into()
147+
}
148+
149+
fn verify_blob_merkle_root(mut proof_hashes: Vec<[u8; 32]>, merkle_root: [u8; 32]) -> bool {
150+
while proof_hashes.len() > 1 {
151+
proof_hashes = proof_hashes
152+
.chunks(2)
153+
.map(|chunk| match chunk {
154+
[a, b] => combine_hashes(a, b),
155+
[a] => combine_hashes(a, a),
156+
_ => panic!("Unexpected chunk size in leaves"),
157+
})
158+
.collect()
159+
}
160+
161+
proof_hashes[0] == merkle_root
148162
}

0 commit comments

Comments
 (0)