Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/raito-assumevalid/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bitcoin::block::Header;
use stwo::core::vcs::blake2_merkle::Blake2sMerkleHasher;
use stwo_cairo_serialize::CairoSerialize;

use raito_cairo_serialize::{DigestString, U256String, U256StringLittleEndian};
use raito_cairo_serialize::{DigestString, U256String};
use raito_spv_mmr::sparse_roots::SparseRoots;
use raito_spv_verify::ChainState;

Expand Down Expand Up @@ -32,7 +32,7 @@ struct ChainStateView {

#[derive(CairoSerialize)]
pub struct SparseRootsView {
pub roots: Vec<U256StringLittleEndian>,
pub roots: Vec<U256String>,
}

/// View for a single block matching Cairo's Block structure
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn to_runner_args_hex(
roots: block_mmr
.roots
.iter()
.map(|root_hex| U256StringLittleEndian(hex_u256_to_decimal_string(root_hex)))
.map(|root_hex| U256String(hex_u256_to_decimal_string(root_hex)))
.collect(),
};

Expand Down
31 changes: 1 addition & 30 deletions crates/raito-cairo-serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use stwo_cairo_serialize::serialize::CairoSerialize;
pub struct U256String(pub String);
pub struct ByteArrayString(pub String);
pub struct DigestString(pub String);
pub struct U256StringLittleEndian(pub String);

impl CairoSerialize for U256String {
fn serialize(&self, output: &mut Vec<FieldElement>) {
Expand Down Expand Up @@ -43,34 +42,6 @@ impl CairoSerialize for U256String {
}
}

impl CairoSerialize for U256StringLittleEndian {
fn serialize(&self, output: &mut Vec<FieldElement>) {
// Accept decimal string only, produce 32-byte big-endian
let s = self.0.trim();
assert!(
!s.starts_with("0x") && !s.starts_with("0X"),
"Hex not supported for U256StringHiLo; use decimal",
);
let n = BigUint::from_str_radix(s, 10).expect("Invalid decimal string for U256");
let bytes = n.to_bytes_be();
assert!(bytes.len() <= 32, "U256 value exceeds 256 bits");
let mut be = [0u8; 32];
be[32 - bytes.len()..].copy_from_slice(&bytes);

// hi = most-significant 16 bytes, lo = least-significant 16 bytes
let (lo16, hi16) = be.split_at(16);

let mut hi_bytes = [0u8; 32];
hi_bytes[16..].copy_from_slice(hi16);
let mut lo_bytes = [0u8; 32];
lo_bytes[16..].copy_from_slice(lo16);

// Note: emit HI first, then LO to match Cairo MMR Serde (high, low)
output.push(FieldElement::from_bytes_be(&lo_bytes).unwrap());
output.push(FieldElement::from_bytes_be(&hi_bytes).unwrap());
}
}

impl CairoSerialize for ByteArrayString {
// Split into 31-byte chunks and save the remainder
fn serialize(&self, output: &mut Vec<FieldElement>) {
Expand Down Expand Up @@ -142,7 +113,7 @@ impl CairoSerialize for DigestString {

// Backwards-compatibility: preserve `serializer::...` path
pub mod serializer {
pub use super::{ByteArrayString, DigestString, U256String, U256StringLittleEndian};
pub use super::{ByteArrayString, DigestString, U256String};
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/raito-spv-mmr/src/sparse_roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ where
let lo_json_number = num_str_to_json_number::<S>(lo)?;
// Serialize as a dict with `hi` and `lo` keys (u256 in Cairo)
let mut dict = serde_json::Map::new();
dict.insert("hi".to_string(), hi_json_number.into());
dict.insert("lo".to_string(), lo_json_number.into());
dict.insert("hi".to_string(), hi_json_number.into());
seq.serialize_element(&dict)?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/assumevalid/tests/data/batch_100.json
Original file line number Diff line number Diff line change
Expand Up @@ -1214,8 +1214,8 @@
"block_mmr": {
"roots": [
{
"hi": 127393669198796545867873666634899495693,
"lo": 18179848941975845720653830328566420889
"lo": 18179848941975845720653830328566420889,
"hi": 127393669198796545867873666634899495693
},
{
"hi": 0,
Expand Down
6 changes: 3 additions & 3 deletions packages/assumevalid/tests/data/batch_200.json
Original file line number Diff line number Diff line change
Expand Up @@ -2414,9 +2414,9 @@
"block_mmr": {
"roots": [
{
"hi": 127393669198796545867873666634899495693,
"lo": 18179848941975845720653830328566420889
},
"lo": 18179848941975845720653830328566420889,
"hi": 127393669198796545867873666634899495693
},
{
"hi": 0,
"lo": 0
Expand Down
4 changes: 2 additions & 2 deletions packages/assumevalid/tests/data/batch_400.json
Original file line number Diff line number Diff line change
Expand Up @@ -4814,8 +4814,8 @@
"block_mmr": {
"roots": [
{
"hi": 127393669198796545867873666634899495693,
"lo": 18179848941975845720653830328566420889
"lo": 18179848941975845720653830328566420889,
"hi": 127393669198796545867873666634899495693
},
{
"hi": 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/assumevalid/tests/data/blocks_0_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"block_mmr": {
"roots": [
{
"hi": 127393669198796545867873666634899495693,
"lo": 18179848941975845720653830328566420889
"lo": 18179848941975845720653830328566420889,
"hi": 127393669198796545867873666634899495693
},
{
"hi": 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/assumevalid/tests/data/blocks_1_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"lo": 0
},
{
"hi": 214426372237413499259359749835690602779,
"lo": 60633371617111020730067693523639793590
"lo": 60633371617111020730067693523639793590,
"hi": 214426372237413499259359749835690602779
},
{
"hi": 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/mmr.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub impl MaybeBlake2sDigestSerde of Serde<Option<Blake2sDigest>> {
}

fn deserialize(ref serialized: Span<felt252>) -> Option<Option<Blake2sDigest>> {
let high: u128 = Serde::deserialize(ref serialized).expect('digest::high');
let low: u128 = Serde::deserialize(ref serialized).expect('digest::low');
let high: u128 = Serde::deserialize(ref serialized).expect('digest::high');
if high == 0 && low == 0 {
Some(None)
} else {
Expand Down