Skip to content

Commit

Permalink
Break down mmr into batches (#9)
Browse files Browse the repository at this point in the history
* refacto logic to build multiple mmr

* contracts updated

* refacto, onchain state decode from journ + publisher crate restructure

* full journal decode onchain

* fmt cairo

* misc fixes and cleanup
ametel01 authored Dec 3, 2024
1 parent 08f743d commit 768a1e9
Showing 48 changed files with 1,363 additions and 910 deletions.
Empty file added .cargo/config.toml
Empty file.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -23,6 +23,4 @@ scripts/katana/deploy.log

scripts/katana/katana.log

.cargo/

*.pb
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.8.5
starknet-foundry 0.33.0
starknet-foundry 0.34.0
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -101,14 +101,9 @@ Deploy the messaging infrastructure contracts:

Initialize the Light Client service:

1. Navigate to client implementation:
1. Execute client binary:
```bash
cd crates/client
```

2. Execute client binary:
```bash
cargo run --release
cargo run --bin client --release
```

## Terminal 5: Block Hash Relayer Process
2 changes: 1 addition & 1 deletion config/anvil.messaging.json
Original file line number Diff line number Diff line change
@@ -5,5 +5,5 @@
"sender_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"interval": 2,
"from_block": 21281491
"from_block": 21320622
}
5 changes: 2 additions & 3 deletions contracts/ethereum/src/L1MessageSender.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {Uint256Splitter} from "lib/U256Splitter.sol";
import {Uint256Splitter} from "../lib/U256Splitter.sol";

import {IStarknetMessaging} from "../src/StarknetMessagingLocal.sol";

@@ -22,7 +21,7 @@ contract L1MessageSender {
}

function sendFinalizedBlockHashToL2(uint256 l2RecipientAddr) external payable {
uint256 finalizedBlockNumber = block.number - 192;
uint256 finalizedBlockNumber = block.number - 96;
bytes32 parentHash = blockhash(finalizedBlockNumber);
uint256 blockNumber = uint256(finalizedBlockNumber);
_sendBlockHashToL2(parentHash, blockNumber, l2RecipientAddr);
12 changes: 6 additions & 6 deletions contracts/starknet/Scarb.lock
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ dependencies = [

[[package]]
name = "garaga"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/garaga.git?rev=65a2fad#65a2fad47069c67d15e6745845b542bba8d61b92"
version = "0.14.0"
source = "git+https://github.com/keep-starknet-strange/garaga.git?rev=5f3b232#5f3b23252a04f1714838415e69e318ed8e097c15"

[[package]]
name = "l1_message_proxy"
@@ -23,15 +23,15 @@ dependencies = [

[[package]]
name = "snforge_scarb_plugin"
version = "0.33.0"
version = "0.34.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:b4dd6088372decd367652827091e0589bbf6bc550dfc3957baa3e9c61d6eb449"
checksum = "sha256:56f2b06ff2f0d8bbdfb7fb6c211fba7e4da6e5334ea70ba849af329a739faf11"

[[package]]
name = "snforge_std"
version = "0.33.0"
version = "0.34.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:f7dc3349f8a6ef4915c93df447a00bd5a53a31129fd0990a00afa0ad31d91b06"
checksum = "sha256:bd20964bde07e6fd0f7adb50d41216f05d66abd422ed82241030369333385876"
dependencies = [
"snforge_scarb_plugin",
]
4 changes: 3 additions & 1 deletion contracts/starknet/Scarb.toml
Original file line number Diff line number Diff line change
@@ -4,10 +4,12 @@ members = ["l1_message_proxy", "store", "verifier"]
[workspace.dependencies]
starknet = "2.8.5"
assert_macros = "2.8.5"
snforge_std = "0.33.0"
snforge_std = "0.34.0"
cairo_test = "2.8.5"

[dev-dependencies]
snforge_std.workspace = true
cairo_test.workspace = true

[[target.starknet-contract]]
casm = true
61 changes: 30 additions & 31 deletions contracts/starknet/store/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -3,39 +3,40 @@ pub trait IFossilStore<TContractState> {
fn store_latest_blockhash_from_l1(ref self: TContractState, block_number: u64, blockhash: u256);
fn update_mmr_state(
ref self: TContractState,
batch_index: u64,
latest_mmr_block: u64,
mmr_root: u256,
elements_count: u64,
leaves_count: u64,
mmr_root: u256,
);
fn get_latest_blockhash_from_l1(self: @TContractState) -> (u64, u256);
fn get_mmr_state(self: @TContractState) -> Store::MMRSnapshot;
fn get_mmr_state(self: @TContractState, batch_index: u64) -> Store::MMRSnapshot;
fn get_latest_mmr_block(self: @TContractState) -> u64;
}

#[starknet::contract]
mod Store {
use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess,};
use core::starknet::storage::{
Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess
};

#[starknet::storage_node]
pub(crate) struct MMRState {
latest_block_number: u64,
root_hash: u256,
elements_count: u64,
pub(crate) struct MMRBatch {
leaves_count: u64,
root_hash: u256,
}

#[derive(Copy, Drop, Serde, Debug)]
pub struct MMRSnapshot {
latest_block_number: u64,
batch_index: u64,
root_hash: u256,
elements_count: u64,
leaves_count: u64,
}

#[storage]
struct Storage {
latest_blockhash_from_l1: (u64, u256),
mmr_state: MMRState,
latest_mmr_block: u64,
mmr_batches: Map<u64, MMRBatch>,
}

#[event]
@@ -53,10 +54,9 @@ mod Store {

#[derive(Drop, starknet::Event)]
struct MmrStateUpdated {
latest_mmr_block: u64,
root_hash: u256,
elements_count: u64,
batch_index: u64,
leaves_count: u64,
root_hash: u256,
}

#[abi(embed_v0)]
@@ -74,33 +74,32 @@ mod Store {

fn update_mmr_state(
ref self: ContractState,
batch_index: u64,
latest_mmr_block: u64,
mmr_root: u256,
elements_count: u64,
leaves_count: u64,
mmr_root: u256,
) {
let mut curr_state = self.mmr_state;
curr_state.latest_block_number.write(latest_mmr_block);
curr_state.root_hash.write(mmr_root);
curr_state.elements_count.write(elements_count);
let mut curr_state = self.mmr_batches.entry(batch_index);

curr_state.leaves_count.write(leaves_count);
curr_state.root_hash.write(mmr_root);

self.latest_mmr_block.write(latest_mmr_block);

self
.emit(
MmrStateUpdated {
latest_mmr_block, root_hash: mmr_root, elements_count, leaves_count
}
);
self.emit(MmrStateUpdated { batch_index, leaves_count, root_hash: mmr_root });
}

fn get_mmr_state(self: @ContractState) -> MMRSnapshot {
let curr_state = self.mmr_state;
fn get_mmr_state(self: @ContractState, batch_index: u64) -> MMRSnapshot {
let curr_state = self.mmr_batches.entry(batch_index);
MMRSnapshot {
latest_block_number: curr_state.latest_block_number.read(),
root_hash: curr_state.root_hash.read(),
elements_count: curr_state.elements_count.read(),
batch_index,
leaves_count: curr_state.leaves_count.read(),
root_hash: curr_state.root_hash.read(),
}
}

fn get_latest_mmr_block(self: @ContractState) -> u64 {
self.latest_mmr_block.read()
}
}
}
3 changes: 2 additions & 1 deletion contracts/starknet/verifier/Scarb.toml
Original file line number Diff line number Diff line change
@@ -6,13 +6,14 @@ edition = "2023_11"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
garaga = { git = "https://github.com/keep-starknet-strange/garaga.git", rev = "65a2fad" }
garaga = { git = "https://github.com/keep-starknet-strange/garaga.git", rev = "5f3b232" }
starknet.workspace = true
fossil_store = { path = "../store" }

[dev-dependencies]
snforge_std.workspace = true
assert_macros.workspace = true
cairo_test.workspace = true

[tool]
fmt.workspace = true
47 changes: 19 additions & 28 deletions contracts/starknet/verifier/src/fossil_verifier.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#[starknet::interface]
pub trait IFossilVerifier<TContractState> {
fn verify_mmr_proof(
ref self: TContractState,
latest_mmr_block: u64,
new_mmr_root: u256,
new_elements_count: u64,
new_leaves_count: u64,
proof: Span<felt252>,
) -> bool;
fn verify_mmr_proof(ref self: TContractState, proof: Span<felt252>,) -> bool;
fn get_verifier_address(self: @TContractState) -> starknet::ContractAddress;
fn get_fossil_store_address(self: @TContractState) -> starknet::ContractAddress;
}

#[starknet::contract]
mod FossilVerifier {
use fossil_store::{IFossilStoreDispatcher, IFossilStoreDispatcherTrait};
use verifier::decode_journal;
use verifier::groth16_verifier::{
IRisc0Groth16VerifierBN254Dispatcher, IRisc0Groth16VerifierBN254DispatcherTrait
};
@@ -33,10 +27,10 @@ mod FossilVerifier {

#[derive(Drop, starknet::Event)]
struct MmrProofVerified {
batch_index: u64,
latest_mmr_block: u64,
new_mmr_root: u256,
new_elements_count: u64,
new_leaves_count: u64,
new_mmr_root: u256,
}

#[constructor]
@@ -52,28 +46,25 @@ mod FossilVerifier {
}

#[external(v0)]
fn verify_mmr_proof(
ref self: ContractState,
latest_mmr_block: u64,
new_mmr_root: u256,
new_elements_count: u64,
new_leaves_count: u64,
proof: Span<felt252>,
) -> bool {
let verified = self.bn254_verifier.read().verify_groth16_proof_bn254(proof);
fn verify_mmr_proof(ref self: ContractState, proof: Span<felt252>,) -> bool {
let (verified, journal) = self.bn254_verifier.read().verify_groth16_proof_bn254(proof);
println!("journal: {:?}", journal);

let (new_mmr_root, new_leaves_count, batch_index, latest_mmr_block) = decode_journal(
journal
);

if verified {
self.fossil_store.read().update_mmr_state(
latest_mmr_block, new_mmr_root, new_elements_count, new_leaves_count
);
self
.fossil_store
.read()
.update_mmr_state(batch_index, latest_mmr_block, new_leaves_count, new_mmr_root);
}

self.emit(MmrProofVerified {
latest_mmr_block,
new_mmr_root,
new_elements_count,
new_leaves_count,
});
self
.emit(
MmrProofVerified { batch_index, latest_mmr_block, new_leaves_count, new_mmr_root }
);

verified
}
6 changes: 3 additions & 3 deletions contracts/starknet/verifier/src/groth16_verifier.cairo
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use super::groth16_verifier_constants::{N_FREE_PUBLIC_INPUTS, vk, ic, precompute
pub trait IRisc0Groth16VerifierBN254<TContractState> {
fn verify_groth16_proof_bn254(
self: @TContractState, full_proof_with_hints: Span<felt252>,
) -> bool;
) -> (bool, Span<u8>);
}

#[starknet::contract]
@@ -32,7 +32,7 @@ mod Risc0Groth16VerifierBN254 {
impl IRisc0Groth16VerifierBN254 of super::IRisc0Groth16VerifierBN254<ContractState> {
fn verify_groth16_proof_bn254(
self: @ContractState, full_proof_with_hints: Span<felt252>,
) -> bool {
) -> (bool, Span<u8>) {
// DO NOT EDIT THIS FUNCTION UNLESS YOU KNOW WHAT YOU ARE DOING.
// This function returns an Option for the public inputs if the proof is valid.
// If the proof is invalid, the execution will either fail or return None.
@@ -93,7 +93,7 @@ mod Risc0Groth16VerifierBN254 {
small_Q
);

result
(result, journal)
}
}
}
Loading

0 comments on commit 768a1e9

Please sign in to comment.