Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ZK constants in compile time #1613

Merged
merged 105 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
f61421e
Carry guest input read outside of run_sequencer_commitments_in_da_slot
yaziciahmet Dec 13, 2024
39e90d9
Get light client input as arg in circuit
yaziciahmet Dec 13, 2024
d9ec09e
Remove useless generic
yaziciahmet Dec 13, 2024
3de7c07
Add SEQ PUB KEYS and implement Fork str parser
yaziciahmet Dec 15, 2024
4c2b182
Fmt
yaziciahmet Dec 15, 2024
af6f754
One more fail-test
yaziciahmet Dec 15, 2024
fec0dcf
Rename
yaziciahmet Dec 16, 2024
b9ba404
Init const FORKS
yaziciahmet Dec 16, 2024
be04c37
Add doc
yaziciahmet Dec 16, 2024
39a4106
Lower count to 50
yaziciahmet Dec 16, 2024
b2e67e3
Implement fork list parser
yaziciahmet Dec 16, 2024
0036433
Rename
yaziciahmet Dec 16, 2024
8778b13
Fix list parser & add tests
yaziciahmet Dec 16, 2024
3d83afd
Validate fork list on parse
yaziciahmet Dec 16, 2024
9457a0d
Add light client consts
yaziciahmet Dec 16, 2024
c9433c6
Use specific const-hex v
yaziciahmet Dec 16, 2024
f1706d3
Update do not use comment
yaziciahmet Dec 16, 2024
73d4907
Merge branch 'nightly' into yaziciahmet/set-zk-constants-from-env
yaziciahmet Dec 16, 2024
9398b56
Merge branch 'nightly' into yaziciahmet/set-zk-constants-from-env
yaziciahmet Dec 16, 2024
6b942a4
Allow same activation heights for consecutive forks
yaziciahmet Dec 16, 2024
50d1164
Enforce env variables in guest build
yaziciahmet Dec 16, 2024
57222b4
Add env variables to mocks
yaziciahmet Dec 16, 2024
b07aa3c
Separate fork list verification
yaziciahmet Dec 16, 2024
4c4a05d
Add doc
yaziciahmet Dec 16, 2024
515d3b8
Allow non-zero start of forks
yaziciahmet Dec 17, 2024
da2b66d
Create Forks wrapper struct
yaziciahmet Dec 17, 2024
53f48ce
Add derive
yaziciahmet Dec 17, 2024
be23c03
Update guests accordingly
yaziciahmet Dec 17, 2024
479e0b4
Implement serde for Fork & Forks
yaziciahmet Dec 17, 2024
7c2fda0
Implement Forks::from_slice
yaziciahmet Dec 17, 2024
57fcb97
Read forks from rollup config
yaziciahmet Dec 17, 2024
ed2756f
Add lifetime to ForkManager
yaziciahmet Dec 17, 2024
84e0433
Separate fork_pos_from_block_number method as a helper
yaziciahmet Dec 17, 2024
8d73c8e
Improve verify_forks method
yaziciahmet Dec 17, 2024
ff3c60c
Make FORKS OnceLock static global
yaziciahmet Dec 17, 2024
e841a2a
Check first byte of da public key in compile time
yaziciahmet Dec 17, 2024
23fedae
Merge nightly
yaziciahmet Dec 17, 2024
d9d9d0a
Update test_helpers according to nightly
yaziciahmet Dec 17, 2024
e6114d9
Pass forks as arg
yaziciahmet Dec 17, 2024
aec4378
Have single SpecId
yaziciahmet Dec 18, 2024
1fb4c64
Take sequencer pub keys as arg to batch proof circuit
yaziciahmet Dec 18, 2024
487c84a
Get light client circuit constants as arg
yaziciahmet Dec 18, 2024
0b69f36
Lint
yaziciahmet Dec 18, 2024
41b15dd
Fix comment
yaziciahmet Dec 18, 2024
69a3f3b
Match DA public key env key for native and zk
yaziciahmet Dec 18, 2024
ebd3a8d
Make FORKS constant per environment
yaziciahmet Dec 18, 2024
cd89430
Remove forks from config
yaziciahmet Dec 18, 2024
f49bf57
Set everything from CITREA_NETWORK env
yaziciahmet Dec 18, 2024
539bcee
Remove unused Forks
yaziciahmet Dec 18, 2024
8bfd76d
Check fork ordering in compile time
yaziciahmet Dec 18, 2024
fdae8e1
Remove serde from Fork
yaziciahmet Dec 18, 2024
d755d0a
Newline
yaziciahmet Dec 18, 2024
ded4659
Rename sp1
yaziciahmet Dec 18, 2024
ed94e64
Fix sp1
yaziciahmet Dec 18, 2024
8a24f29
Update guest Dockerfile & Makefile
yaziciahmet Dec 18, 2024
4093f54
Fix udeps
yaziciahmet Dec 18, 2024
096a30d
Fix no-std
yaziciahmet Dec 18, 2024
76fbb91
Fix comment
yaziciahmet Dec 18, 2024
ee66672
Fix no-std warning
yaziciahmet Dec 18, 2024
9ee5ce0
Remove testing forks
yaziciahmet Dec 18, 2024
101e3cb
Merge nightly
yaziciahmet Dec 18, 2024
b3245e1
Match config env key and guest env keys
yaziciahmet Dec 18, 2024
d6dc818
Set mainnet keys to 0
yaziciahmet Dec 18, 2024
183c256
Add prover da pub key for regtest
yaziciahmet Dec 18, 2024
bcaaa87
Update latest guest spec to be Fork1
yaziciahmet Dec 18, 2024
952b80c
Run tests on Nightly
yaziciahmet Dec 19, 2024
9fbf384
Match e2e test keys with guest nightly
yaziciahmet Dec 19, 2024
1905fdd
Set genesis state roots
yaziciahmet Dec 19, 2024
0d9196c
Remove todo
yaziciahmet Dec 19, 2024
e6e0bc6
Set mock configs to be same with regtest
yaziciahmet Dec 19, 2024
48630e1
Set l2 genesis root for mock proof
yaziciahmet Dec 19, 2024
93f0557
Remove Simulate proof mode
yaziciahmet Dec 19, 2024
287a27d
Fix udeps
yaziciahmet Dec 19, 2024
abd9da4
Add sender_address back to rollup
yaziciahmet Dec 19, 2024
d76e286
Add todo
yaziciahmet Dec 19, 2024
e0d7d56
Update crates/sovereign-sdk/rollup-interface/src/network.rs
yaziciahmet Dec 19, 2024
090bd53
Merge nightly
yaziciahmet Dec 19, 2024
e701018
Fix CANCUN related test
yaziciahmet Dec 19, 2024
9ee8551
Match citrea-e2e pub keys
yaziciahmet Dec 19, 2024
5080e80
Separate batch and light client guests
yaziciahmet Dec 20, 2024
82f24b5
Update docker and make file
yaziciahmet Dec 20, 2024
c8264a5
Use batch proof method id from build in light client
yaziciahmet Dec 20, 2024
5d6c9e1
Merge branch 'nightly' into yaziciahmet/set-zk-constants-from-env
yaziciahmet Dec 20, 2024
4ba9399
Update build-release
yaziciahmet Dec 20, 2024
b9a0217
Use input public key for output
yaziciahmet Dec 20, 2024
0aeeef3
Merge branch 'nightly' into yaziciahmet/set-zk-constants-from-env
yaziciahmet Dec 20, 2024
5eb7228
Attempt to make preproven commitments test faster
yaziciahmet Dec 20, 2024
82fff65
Add env variable to release.yml workflow && rerun build on env change
yaziciahmet Dec 20, 2024
6d5bd92
SKIP_GUEST_BUILD in release.yml
yaziciahmet Dec 20, 2024
54876c3
Add genesis to fork1 l1 diff size change test
yaziciahmet Dec 20, 2024
8233e77
Remove test
yaziciahmet Dec 20, 2024
7f64d64
Merge branch 'nightly' into yaziciahmet/set-zk-constants-from-env
yaziciahmet Dec 20, 2024
1803b53
Make default evm in tests Fork1
yaziciahmet Dec 20, 2024
05cb5dc
Fix most sys tx tests
yaziciahmet Dec 20, 2024
daef7bd
Fix all sys tx tests
yaziciahmet Dec 20, 2024
1036e86
Fix most hooks tests
yaziciahmet Dec 20, 2024
d6d9d0f
Fix all hooks tests
yaziciahmet Dec 20, 2024
29fc600
Fix all genesis tests
yaziciahmet Dec 20, 2024
a67259e
Fix all call tests
yaziciahmet Dec 20, 2024
78588ee
Remove unnecessary fork setting
yaziciahmet Dec 20, 2024
31303bd
Fix fork tests
yaziciahmet Dec 20, 2024
1792171
Lint
yaziciahmet Dec 20, 2024
5ecdb1c
Recover l1 fee compression discount
yaziciahmet Dec 20, 2024
8918da8
Merge nightly
yaziciahmet Dec 21, 2024
443b7ed
Lint
yaziciahmet Dec 21, 2024
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
4 changes: 2 additions & 2 deletions crates/citrea-stf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ use sov_rollup_interface::da::DaVerifier;
use verifier::StateTransitionVerifier;

/// Alias for StateTransitionVerifier.
pub type StfVerifier<DA, Vm, ZkContext, RT> =
StateTransitionVerifier<StfBlueprint<ZkContext, <DA as DaVerifier>::Spec, RT>, DA, Vm>;
pub type StfVerifier<DA, ZkContext, RT> =
StateTransitionVerifier<StfBlueprint<ZkContext, <DA as DaVerifier>::Spec, RT>, DA>;
28 changes: 8 additions & 20 deletions crates/citrea-stf/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
use std::marker::PhantomData;

use sov_rollup_interface::da::{BlockHeaderTrait, DaNamespace, DaVerifier};
use sov_rollup_interface::stf::{ApplySequencerCommitmentsOutput, StateTransitionFunction};
use sov_rollup_interface::zk::{BatchProofCircuitInput, BatchProofCircuitOutput, Zkvm, ZkvmGuest};
use sov_rollup_interface::zk::{BatchProofCircuitInput, BatchProofCircuitOutput};

/// Verifies a state transition
pub struct StateTransitionVerifier<ST, Da, Zk>
pub struct StateTransitionVerifier<ST, Da>
where
Da: DaVerifier,
Zk: Zkvm,
ST: StateTransitionFunction<Da::Spec>,
{
app: ST,
da_verifier: Da,
phantom: PhantomData<Zk>,
}

impl<Stf, Da, Zk> StateTransitionVerifier<Stf, Da, Zk>
impl<Stf, Da> StateTransitionVerifier<Stf, Da>
where
Da: DaVerifier,
Zk: ZkvmGuest,
Stf: StateTransitionFunction<Da::Spec>,
{
/// Create a [`StateTransitionVerifier`]
pub fn new(app: Stf, da_verifier: Da) -> Self {
Self {
app,
da_verifier,
phantom: Default::default(),
}
Self { app, da_verifier }
}

/// Verify the next block
pub fn run_sequencer_commitments_in_da_slot(
&mut self,
zkvm: Zk,
data: BatchProofCircuitInput<Stf::StateRoot, Stf::Witness, Da::Spec, Stf::Transaction>,
kpp marked this conversation as resolved.
Show resolved Hide resolved
pre_state: Stf::PreState,
) -> Result<(), Da::Error> {
) -> Result<BatchProofCircuitOutput<Da::Spec, Stf::StateRoot>, Da::Error> {
println!("Running sequencer commitments in DA slot");
let data: BatchProofCircuitInput<Stf::StateRoot, _, Da::Spec, Stf::Transaction> =
zkvm.read_from_host();

if !data.da_block_header_of_commitments.verify_hash() {
panic!("Invalid hash of DA block header of commitments");
Expand Down Expand Up @@ -88,7 +77,7 @@ where

println!("out of apply_soft_confirmations_from_sequencer_commitments");

let out: BatchProofCircuitOutput<Da::Spec, _> = BatchProofCircuitOutput {
let out = BatchProofCircuitOutput {
initial_state_root: data.initial_state_root,
final_state_root,
final_soft_confirmation_hash,
Expand All @@ -102,7 +91,6 @@ where
last_l2_height,
};

zkvm.commit(&out);
Ok(())
Ok(out)
}
}
4 changes: 1 addition & 3 deletions crates/light-client-prover/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ pub enum LightClientVerificationError {

pub fn run_circuit<DaV: DaVerifier, G: ZkvmGuest>(
da_verifier: DaV,
guest: &G,
input: LightClientCircuitInput<DaV::Spec>,
) -> Result<LightClientCircuitOutput<DaV::Spec>, LightClientVerificationError> {
let input: LightClientCircuitInput<DaV::Spec> = guest.read_from_host();

// Extract previous light client proof output
let previous_light_client_proof_output =
if let Some(journal) = input.previous_light_client_proof_journal {
Expand Down
84 changes: 26 additions & 58 deletions crates/light-client-prover/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod test_utils;

use sov_mock_da::{MockBlockHeader, MockDaSpec, MockDaVerifier};
use sov_mock_da::{MockBlockHeader, MockDaVerifier};
use sov_mock_zkvm::MockZkGuest;
use sov_rollup_interface::zk::LightClientCircuitInput;
use test_utils::{create_mock_blob, create_prev_lcp_serialized};
Expand All @@ -18,7 +18,7 @@ fn test_light_client_circuit_valid_da_valid_data() {

let block_header_1 = MockBlockHeader::from_height(1);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -30,11 +30,7 @@ fn test_light_client_circuit_valid_da_valid_data() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let mut guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition actually happened
assert_eq!(output_1.state_root, [3; 32]);
Expand All @@ -49,7 +45,7 @@ fn test_light_client_circuit_valid_da_valid_data() {

let mock_output_1_serialized = create_prev_lcp_serialized(output_1, true);

let input_2 = LightClientCircuitInput::<MockDaSpec> {
let input_2 = LightClientCircuitInput {
previous_light_client_proof_journal: Some(mock_output_1_serialized),
da_block_header: block_header_2,
da_data: vec![blob_3, blob_4],
Expand All @@ -61,11 +57,7 @@ fn test_light_client_circuit_valid_da_valid_data() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input_2 = borsh::to_vec(&input_2).expect("should serialize");

guest.input = serialized_input_2;

let output_2 = run_circuit(da_verifier, &guest).unwrap();
let output_2 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input_2).unwrap();

// Check that the state transition actually happened
assert_eq!(output_2.state_root, [5; 32]);
Expand All @@ -83,7 +75,7 @@ fn test_wrong_order_da_blocks_should_still_work() {

let block_header_1 = MockBlockHeader::from_height(1);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -95,11 +87,7 @@ fn test_wrong_order_da_blocks_should_still_work() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition actually happened
assert_eq!(output_1.state_root, [3; 32]);
Expand All @@ -117,7 +105,7 @@ fn create_unchainable_outputs_then_chain_them_on_next_block() {
let blob_1 = create_mock_blob([2u8; 32], [3u8; 32], 3, true);
let blob_2 = create_mock_blob([3u8; 32], [4u8; 32], 4, true);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -129,11 +117,7 @@ fn create_unchainable_outputs_then_chain_them_on_next_block() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let mut guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition has not happened because we are missing 1->2
assert_eq!(output_1.state_root, [1; 32]);
Expand All @@ -159,17 +143,19 @@ fn create_unchainable_outputs_then_chain_them_on_next_block() {

let mock_output_1_ser = create_prev_lcp_serialized(output_1, true);

let input_2 = LightClientCircuitInput::<MockDaSpec> {
let input_2 = LightClientCircuitInput {
previous_light_client_proof_journal: Some(mock_output_1_ser),
light_client_proof_method_id,
da_block_header: block_header_2,
da_data: vec![blob_1],
inclusion_proof: [1u8; 32],
completeness_proof: (),
l2_genesis_state_root: None,
..input
batch_proof_method_id: light_client_proof_method_id,
batch_prover_da_pub_key: [9; 32].to_vec(),
};

guest.input = borsh::to_vec(&input_2).unwrap();

let output_2 = run_circuit(da_verifier, &guest).unwrap();
let output_2 = run_circuit::<_, MockZkGuest>(da_verifier, input_2).unwrap();

// Check that the state transition actually happened from 1-4 now

Expand All @@ -188,7 +174,7 @@ fn test_header_chain_proof_height_and_hash() {

let block_header_1 = MockBlockHeader::from_height(1);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -200,11 +186,7 @@ fn test_header_chain_proof_height_and_hash() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let mut guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition actually happened
assert_eq!(output_1.state_root, [3; 32]);
Expand All @@ -219,7 +201,7 @@ fn test_header_chain_proof_height_and_hash() {

let prev_lcp_out = create_prev_lcp_serialized(output_1, true);

let input_2 = LightClientCircuitInput::<MockDaSpec> {
let input_2 = LightClientCircuitInput {
previous_light_client_proof_journal: Some(prev_lcp_out),
da_block_header: block_header_2,
da_data: vec![blob_3, blob_4],
Expand All @@ -231,12 +213,8 @@ fn test_header_chain_proof_height_and_hash() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input_2 = borsh::to_vec(&input_2).expect("should serialize");

guest.input = serialized_input_2;

// Header chain verification must fail because the l1 block 3 was given before l1 block 2
let res = run_circuit(da_verifier, &guest);
let res = run_circuit::<_, MockZkGuest>(da_verifier, input_2);
assert!(matches!(
res,
Err(LightClientVerificationError::HeaderChainVerificationFailed)
Expand All @@ -254,7 +232,7 @@ fn test_unverifiable_batch_proofs() {

let block_header_1 = MockBlockHeader::from_height(1);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -266,11 +244,7 @@ fn test_unverifiable_batch_proofs() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition actually happened but only for verified batch proof
// and assert the unverified is ignored, so it is not even in the unchained outputs
Expand All @@ -291,7 +265,7 @@ fn test_unverifiable_prev_light_client_proof() {

let block_header_1 = MockBlockHeader::from_height(1);

let input = LightClientCircuitInput::<MockDaSpec> {
let input = LightClientCircuitInput {
previous_light_client_proof_journal: None,
light_client_proof_method_id,
da_block_header: block_header_1,
Expand All @@ -303,11 +277,7 @@ fn test_unverifiable_prev_light_client_proof() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

let serialized_input = borsh::to_vec(&input).expect("should serialize");

let mut guest = MockZkGuest::new(serialized_input);

let output_1 = run_circuit(da_verifier.clone(), &guest).unwrap();
let output_1 = run_circuit::<_, MockZkGuest>(da_verifier.clone(), input).unwrap();

// Check that the state transition actually happened but only for verified batch proof
// and assert the unverified is ignored, so it is not even in the unchained outputs
Expand All @@ -320,7 +290,7 @@ fn test_unverifiable_prev_light_client_proof() {

let prev_lcp_out = create_prev_lcp_serialized(output_1, false);

let input_2 = LightClientCircuitInput::<MockDaSpec> {
let input_2 = LightClientCircuitInput {
previous_light_client_proof_journal: Some(prev_lcp_out),
da_block_header: block_header_2,
da_data: vec![],
Expand All @@ -332,9 +302,7 @@ fn test_unverifiable_prev_light_client_proof() {
batch_prover_da_pub_key: [9; 32].to_vec(),
};

guest.input = borsh::to_vec(&input_2).unwrap();

let res = run_circuit(da_verifier, &guest);
let res = run_circuit::<_, MockZkGuest>(da_verifier, input_2);
assert!(matches!(
res,
Err(LightClientVerificationError::InvalidPreviousLightClientProof)
Expand Down
6 changes: 2 additions & 4 deletions crates/prover-services/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use citrea_stf::verifier::StateTransitionVerifier;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::ZkvmHost;

mod parallel;
pub use parallel::*;

pub enum ProofGenMode<Da, Vm, Stf>
pub enum ProofGenMode<Da, Stf>
where
Da: DaService,
Vm: ZkvmHost,
Stf: StateTransitionFunction<Da::Spec>,
{
/// Skips proving.
Skip,
/// The simulator runs the rollup verifier logic without even emulating the zkVM
Simulate(StateTransitionVerifier<Stf, Da::Verifier, Vm::Guest>),
Simulate(StateTransitionVerifier<Stf, Da::Verifier>),
/// The executor runs the rollup verification logic in the zkVM, but does not actually
/// produce a zk proof
Execute,
Expand Down
Loading