diff --git a/Cargo.lock b/Cargo.lock index d5f3c265..03ae9fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -935,6 +935,26 @@ dependencies = [ "serde", ] +[[package]] +name = "bincode" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740" +dependencies = [ + "bincode_derive", + "serde", + "unty", +] + +[[package]] +name = "bincode_derive" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09" +dependencies = [ + "virtue", +] + [[package]] name = "bit-set" version = "0.8.0" @@ -6588,6 +6608,7 @@ name = "scroll-zkvm-batch-circuit" version = "0.5.0" dependencies = [ "alloy-primitives", + "bincode 2.0.1", "bitcode", "c-kzg", "halo2curves-axiom 0.7.0", @@ -6647,6 +6668,7 @@ name = "scroll-zkvm-bundle-circuit" version = "0.5.0" dependencies = [ "alloy-primitives", + "bincode 2.0.1", "openvm", "openvm-algebra-guest", "openvm-keccak256-guest", @@ -6660,6 +6682,7 @@ dependencies = [ name = "scroll-zkvm-chunk-circuit" version = "0.5.0" dependencies = [ + "bincode 2.0.1", "k256 0.13.4 (git+https://github.com/openvm-org/openvm.git?rev=5368d4756993fc1e51092499a816867cf4808de0)", "openvm", "openvm-algebra-complex-macros", @@ -6722,7 +6745,7 @@ version = "0.5.0" dependencies = [ "alloy-primitives", "base64", - "bincode", + "bincode 1.3.3", "c-kzg", "eyre", "git-version", @@ -6759,7 +6782,7 @@ name = "scroll-zkvm-types" version = "0.5.0" dependencies = [ "base64", - "bincode", + "bincode 1.3.3", "c-kzg", "openvm-continuations", "openvm-native-recursion", @@ -6794,6 +6817,7 @@ name = "scroll-zkvm-types-batch" version = "0.5.0" dependencies = [ "alloy-primitives", + "bincode 2.0.1", "c-kzg", "halo2curves-axiom 0.7.0", "itertools 0.14.0", @@ -6814,6 +6838,7 @@ name = "scroll-zkvm-types-bundle" version = "0.5.0" dependencies = [ "alloy-primitives", + "bincode 2.0.1", "itertools 0.14.0", "rkyv", "scroll-zkvm-types-base", @@ -6826,6 +6851,7 @@ name = "scroll-zkvm-types-chunk" version = "0.5.0" dependencies = [ "alloy-primitives", + "bincode 2.0.1", "itertools 0.14.0", "openvm", "openvm-custom-insn", @@ -6860,7 +6886,7 @@ name = "scroll-zkvm-verifier" version = "0.5.0" dependencies = [ "base64", - "bincode", + "bincode 1.3.3", "eyre", "itertools 0.14.0", "once_cell", @@ -7227,7 +7253,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a338d065044702bf751e87cf353daac63e2fc4c53a3e323cbcd98c603ee6e66c" dependencies = [ "ark-std 0.3.0", - "bincode", + "bincode 1.3.3", "ethereum-types", "getset", "halo2-base", @@ -7250,7 +7276,7 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03046db52868c1b60e8acffa0777ef6dc11ec1bbbb10b9eb612a871f69c8d3f6" dependencies = [ - "bincode", + "bincode 1.3.3", "serde", "sp1-primitives", ] @@ -7261,7 +7287,7 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6939d6b2f63e54e5fbd208a0293027608f22511741b62fe32b6f67f6c144e0c0" dependencies = [ - "bincode", + "bincode 1.3.3", "blake3", "cfg-if", "hex", @@ -7851,6 +7877,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "unty" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae" + [[package]] name = "url" version = "2.5.4" @@ -7896,6 +7928,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "virtue" +version = "0.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1" + [[package]] name = "vm-zstd" version = "0.1.1" @@ -8061,9 +8099,9 @@ dependencies = [ [[package]] name = "windows-link" -version = "0.1.3" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" [[package]] name = "windows-result" diff --git a/Cargo.toml b/Cargo.toml index d8a88434..70304397 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ alloy-primitives = { version = "1.2", default-features = false, features = ["std alloy-serde = { version = "1.0.13", default-features = false } bitcode = { version = "0.6.5", default-features = false, features = ["serde", "derive"] } bincode_v1 = { version = "1.3", package = "bincode"} -bincode = { version = "2.0" } +bincode = { version = "2.0", features = ["serde"] } cargo_metadata = "0.20" c-kzg = { version = "2.0" } derivative = "2.2.0" diff --git a/Makefile b/Makefile index c28fb8ba..2538ea01 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,9 @@ clean-guest: build-guest: sh build-guest.sh +build-guest-local: + bash build-guest-actions-entrypoint.sh + clean-build-guest: clean-guest build-guest clean-test-cache: diff --git a/crates/circuits/batch-circuit/Cargo.toml b/crates/circuits/batch-circuit/Cargo.toml index de32cbf3..aae02382 100644 --- a/crates/circuits/batch-circuit/Cargo.toml +++ b/crates/circuits/batch-circuit/Cargo.toml @@ -6,6 +6,7 @@ repository.workspace = true version = "0.5.0" [dependencies] +bincode.workspace = true scroll-zkvm-types-circuit.workspace = true scroll-zkvm-types-batch.workspace = true diff --git a/crates/circuits/batch-circuit/src/circuit.rs b/crates/circuits/batch-circuit/src/circuit.rs index 8283674a..37bb5a7b 100644 --- a/crates/circuits/batch-circuit/src/circuit.rs +++ b/crates/circuits/batch-circuit/src/circuit.rs @@ -1,5 +1,5 @@ use alloy_primitives::B256; -use scroll_zkvm_types_batch::ArchivedBatchWitness; +use scroll_zkvm_types_batch::BatchWitness; use scroll_zkvm_types_circuit::{ AggCircuit, AggregationInput, Circuit, ProgramCommitment, io::read_witnesses, @@ -25,7 +25,7 @@ openvm::init!(); pub struct BatchCircuit; impl Circuit for BatchCircuit { - type Witness = ArchivedBatchWitness; + type Witness = BatchWitness; type PublicInputs = VersionedBatchInfo; @@ -34,12 +34,16 @@ impl Circuit for BatchCircuit { } fn deserialize_witness(witness_bytes: &[u8]) -> &Self::Witness { - rkyv::access::(witness_bytes) - .expect("BatchCircuit: rkyc deserialisation of witness bytes failed") + let config = bincode::config::standard(); + let (witness, _): (Self::Witness, _) = + bincode::serde::decode_from_slice(witness_bytes, config).unwrap(); + Box::leak(Box::new(witness)) + // rkyv::access::(witness_bytes) + // .expect("BatchCircuit: rkyc deserialisation of witness bytes failed") } fn validate(witness: &Self::Witness) -> Self::PublicInputs { - (BatchInfo::from(witness), (&witness.fork_name).into()) + (BatchInfo::from(witness), (witness.fork_name.clone())) } } @@ -64,11 +68,11 @@ impl AggCircuit for BatchCircuit { } fn aggregated_public_inputs(witness: &Self::Witness) -> Vec { - let fork_name = (&witness.fork_name).into(); + let fork_name = (witness.fork_name).clone(); witness .chunk_infos .iter() - .map(|archived| (archived.into(), fork_name)) + .map(|archived| (archived.clone(), fork_name)) .collect() } diff --git a/crates/circuits/bundle-circuit/Cargo.toml b/crates/circuits/bundle-circuit/Cargo.toml index a51da15b..7bdce203 100644 --- a/crates/circuits/bundle-circuit/Cargo.toml +++ b/crates/circuits/bundle-circuit/Cargo.toml @@ -6,6 +6,7 @@ repository.workspace = true version = "0.5.0" [dependencies] +bincode.workspace = true scroll-zkvm-types-circuit.workspace = true scroll-zkvm-types-bundle.workspace = true diff --git a/crates/circuits/bundle-circuit/src/circuit.rs b/crates/circuits/bundle-circuit/src/circuit.rs index 34449cc8..2d97e81b 100644 --- a/crates/circuits/bundle-circuit/src/circuit.rs +++ b/crates/circuits/bundle-circuit/src/circuit.rs @@ -1,5 +1,5 @@ use alloy_primitives::B256; -use scroll_zkvm_types_bundle::ArchivedBundleWitness; +use scroll_zkvm_types_bundle::BundleWitness; use scroll_zkvm_types_circuit::{ AggCircuit, AggregationInput, Circuit, ProgramCommitment, io::read_witnesses, @@ -18,7 +18,7 @@ use openvm_keccak256_guest; pub struct BundleCircuit; impl Circuit for BundleCircuit { - type Witness = ArchivedBundleWitness; + type Witness = BundleWitness; type PublicInputs = VersionedBundleInfo; @@ -27,12 +27,16 @@ impl Circuit for BundleCircuit { } fn deserialize_witness(witness_bytes: &[u8]) -> &Self::Witness { - rkyv::access::(witness_bytes) - .expect("BundleCircuit: rkyv deserialization of witness bytes failed") + let config = bincode::config::standard(); + let (witness, _): (Self::Witness, _) = + bincode::serde::decode_from_slice(witness_bytes, config).unwrap(); + Box::leak(Box::new(witness)) + // rkyv::access::(witness_bytes) + // .expect("BundleCircuit: rkyv deserialization of witness bytes failed") } fn validate(witness: &Self::Witness) -> Self::PublicInputs { - (BundleInfo::from(witness), (&witness.fork_name).into()) + (BundleInfo::from(witness), (witness.fork_name).clone()) } } @@ -57,11 +61,11 @@ impl AggCircuit for BundleCircuit { } fn aggregated_public_inputs(witness: &Self::Witness) -> Vec { - let fork_name = (&witness.fork_name).into(); + let fork_name = (witness.fork_name).clone(); witness .batch_infos .iter() - .map(|archived| (archived.into(), fork_name)) + .map(|archived| (archived.clone(), fork_name)) .collect() } diff --git a/crates/circuits/chunk-circuit/Cargo.toml b/crates/circuits/chunk-circuit/Cargo.toml index 7fd535ff..407292c1 100644 --- a/crates/circuits/chunk-circuit/Cargo.toml +++ b/crates/circuits/chunk-circuit/Cargo.toml @@ -6,6 +6,7 @@ repository.workspace = true version = "0.5.0" [dependencies] +bincode = { workspace = true, features = ["serde"] } scroll-zkvm-types-circuit = { workspace = true } scroll-zkvm-types-chunk = { workspace = true } sbv-precompile = { workspace = true } diff --git a/crates/circuits/chunk-circuit/src/circuit.rs b/crates/circuits/chunk-circuit/src/circuit.rs index 8b7cdeb5..32eac019 100644 --- a/crates/circuits/chunk-circuit/src/circuit.rs +++ b/crates/circuits/chunk-circuit/src/circuit.rs @@ -1,5 +1,5 @@ use openvm::init; -use scroll_zkvm_types_chunk::ArchivedChunkWitness; +use scroll_zkvm_types_chunk::{ChunkWitness, execute}; use scroll_zkvm_types_circuit::{ Circuit, io::read_witnesses, @@ -22,7 +22,7 @@ init!(); pub struct ChunkCircuit; impl Circuit for ChunkCircuit { - type Witness = ArchivedChunkWitness; + type Witness = ChunkWitness; type PublicInputs = VersionedChunkInfo; fn read_witness_bytes() -> Vec { @@ -30,14 +30,16 @@ impl Circuit for ChunkCircuit { } fn deserialize_witness(witness_bytes: &[u8]) -> &Self::Witness { - rkyv::access::(witness_bytes) - .expect("ChunkCircuit: rkyv deserialisation of witness bytes failed") + let config = bincode::config::standard(); + let (witness, _): (Self::Witness, _) = + bincode::serde::decode_from_slice(witness_bytes, config).unwrap(); + Box::leak(Box::new(witness)) + // rkyv::access::(witness_bytes) + // .expect("ChunkCircuit: rkyv deserialisation of witness bytes failed") } fn validate(witness: &Self::Witness) -> Self::PublicInputs { - ( - ChunkInfo::try_from(witness).expect("failed to execute chunk"), - (&witness.fork_name).into(), - ) + let info = execute(witness).unwrap(); + (info, (witness.fork_name.clone())) } } diff --git a/crates/integration/src/lib.rs b/crates/integration/src/lib.rs index 7dd1d012..a57235a2 100644 --- a/crates/integration/src/lib.rs +++ b/crates/integration/src/lib.rs @@ -72,10 +72,10 @@ static DIR_TESTRUN: OnceCell = OnceCell::new(); /// Circuit that implements functionality required to run e2e tests in specified phase (chunk/batch/bundle). pub trait ProverTester { /// Tester witness type - type Witness: rkyv::Archive + PartialProvingTask; + type Witness: PartialProvingTask;// + rkyv::Archive; /// Tester metadata type - type Metadata: for<'a> TryFrom<&'a ::Archived>; + type Metadata;//: for<'a> TryFrom<&'a ::Archived>; /// Naming for tester const NAME: &str; diff --git a/crates/integration/src/testers/batch.rs b/crates/integration/src/testers/batch.rs index 4faeff4f..e96a7c59 100644 --- a/crates/integration/src/testers/batch.rs +++ b/crates/integration/src/testers/batch.rs @@ -24,7 +24,7 @@ impl PartialProvingTask for BatchWitness { } fn write_guest_input(&self, stdin: &mut openvm_sdk::StdIn) -> Result<(), rkyv::rancor::Error> { - let b = rkyv::to_bytes::(self)?; + let b = self.bincode_serialize(None).unwrap(); stdin.write_bytes(b.as_slice()); Ok(()) } diff --git a/crates/integration/src/testers/bundle.rs b/crates/integration/src/testers/bundle.rs index 76cb5255..e6bfc42a 100644 --- a/crates/integration/src/testers/bundle.rs +++ b/crates/integration/src/testers/bundle.rs @@ -28,7 +28,7 @@ impl PartialProvingTask for BundleWitness { } fn write_guest_input(&self, stdin: &mut openvm_sdk::StdIn) -> Result<(), rkyv::rancor::Error> { - let b = self.rkyv_serialize(None)?; + let b = self.bincode_serialize(None).unwrap(); stdin.write_bytes(b.as_slice()); Ok(()) } diff --git a/crates/integration/src/testers/chunk.rs b/crates/integration/src/testers/chunk.rs index 07eb8c53..d013a1e8 100644 --- a/crates/integration/src/testers/chunk.rs +++ b/crates/integration/src/testers/chunk.rs @@ -50,7 +50,8 @@ impl PartialProvingTask for ChunkWitness { } fn write_guest_input(&self, stdin: &mut openvm_sdk::StdIn) -> Result<(), rkyv::rancor::Error> { - stdin.write_bytes(self.rkyv_serialize(None)?.as_slice()); + let b = self.bincode_serialize(None).unwrap(); + stdin.write_bytes(b.as_slice()); Ok(()) } diff --git a/crates/integration/src/utils/mod.rs b/crates/integration/src/utils/mod.rs index 9232d1f6..19d158ab 100644 --- a/crates/integration/src/utils/mod.rs +++ b/crates/integration/src/utils/mod.rs @@ -1,7 +1,6 @@ use crate::testing_hardfork; use sbv_primitives::{ - B256, - types::{BlockWitness, Transaction, eips::Encodable2718, reth::primitives::TransactionSigned}, + B256, U256, types::{BlockWitness, Transaction, eips::Encodable2718, reth::primitives::TransactionSigned}, }; use scroll_zkvm_types::{ batch::{ @@ -105,26 +104,17 @@ impl From<&BatchHeaderV7> for LastHeader { } pub fn metadata_from_chunk_witnesses(witness: &ChunkWitness) -> eyre::Result { - use scroll_zkvm_types::chunk::ArchivedChunkWitness; - let bytes = witness.rkyv_serialize(None)?; - let archieved_wit = rkyv::access::(&bytes)?; - archieved_wit + witness .try_into() .map_err(|e| eyre::eyre!("get chunk metadata fail {e}")) } pub fn metadata_from_batch_witnesses(witness: &BatchWitness) -> eyre::Result { - use scroll_zkvm_types::batch::ArchivedBatchWitness; - let bytes = rkyv::to_bytes::(witness)?; - let archieved_wit = rkyv::access::(&bytes)?; - Ok(archieved_wit.into()) + Ok(witness.into()) } pub fn metadata_from_bundle_witnesses(witness: &BundleWitness) -> eyre::Result { - use scroll_zkvm_types::bundle::ArchivedBundleWitness; - let bytes = witness.rkyv_serialize(None)?; - let archieved_wit = rkyv::access::(&bytes)?; - Ok(archieved_wit.into()) + Ok(witness.into()) } pub fn build_batch_witnesses( diff --git a/crates/integration/tests/chunk_circuit.rs b/crates/integration/tests/chunk_circuit.rs index 09bbbda7..d1fa1d01 100644 --- a/crates/integration/tests/chunk_circuit.rs +++ b/crates/integration/tests/chunk_circuit.rs @@ -62,7 +62,7 @@ fn test_execute() -> eyre::Result<()> { let (exec_result, total_gas_used) = exec_chunk(&prover, &wit)?; let cycle_per_gas = exec_result.total_cycle / total_gas_used; assert_ne!(cycle_per_gas, 0); - assert!(cycle_per_gas <= 35); + assert!(cycle_per_gas <= 40); Ok(()) } diff --git a/crates/integration/tests/onchain_verifier.rs b/crates/integration/tests/onchain_verifier.rs index f5812d9b..e02ef0a1 100644 --- a/crates/integration/tests/onchain_verifier.rs +++ b/crates/integration/tests/onchain_verifier.rs @@ -60,8 +60,6 @@ fn export_onchain_verifier() -> eyre::Result<()> { format!("/home/lispczz/zkvm-prover/releases/{version}/verifier/verifier.bin"); show_deployed_codehash(&code_path)?; } - show_deployed_codehash("/home/lispczz/zkvm-prover/2.bin")?; - show_deployed_codehash("/home/lispczz/zkvm-prover/x.bin")?; Ok(()) } diff --git a/crates/types/base/src/public_inputs/batch.rs b/crates/types/base/src/public_inputs/batch.rs index 139c8b0f..be93c409 100644 --- a/crates/types/base/src/public_inputs/batch.rs +++ b/crates/types/base/src/public_inputs/batch.rs @@ -43,21 +43,6 @@ pub struct BatchInfo { pub post_msg_queue_hash: B256, } -impl From<&ArchivedBatchInfo> for BatchInfo { - fn from(archived: &ArchivedBatchInfo) -> Self { - Self { - parent_state_root: archived.parent_state_root.into(), - parent_batch_hash: archived.parent_batch_hash.into(), - state_root: archived.state_root.into(), - batch_hash: archived.batch_hash.into(), - chain_id: archived.chain_id.into(), - withdraw_root: archived.withdraw_root.into(), - prev_msg_queue_hash: archived.prev_msg_queue_hash.into(), - post_msg_queue_hash: archived.post_msg_queue_hash.into(), - } - } -} - impl BatchInfo { /// Public input hash for a batch (euclidv1 or da-codec@v6) is defined as /// diff --git a/crates/types/base/src/public_inputs/chunk.rs b/crates/types/base/src/public_inputs/chunk.rs index 907713b8..14e775d9 100644 --- a/crates/types/base/src/public_inputs/chunk.rs +++ b/crates/types/base/src/public_inputs/chunk.rs @@ -38,18 +38,6 @@ pub struct BlockContextV2 { pub num_l1_msgs: u16, } -impl From<&ArchivedBlockContextV2> for BlockContextV2 { - fn from(archived: &ArchivedBlockContextV2) -> Self { - Self { - timestamp: archived.timestamp.into(), - base_fee: archived.base_fee.into(), - gas_limit: archived.gas_limit.into(), - num_txs: archived.num_txs.into(), - num_l1_msgs: archived.num_l1_msgs.into(), - } - } -} - impl From<&[u8]> for BlockContextV2 { fn from(bytes: &[u8]) -> Self { assert_eq!(bytes.len(), SIZE_BLOCK_CTX); @@ -214,28 +202,6 @@ impl ChunkInfo { } } -impl From<&ArchivedChunkInfo> for ChunkInfo { - fn from(archived: &ArchivedChunkInfo) -> Self { - Self { - chain_id: archived.chain_id.into(), - prev_state_root: archived.prev_state_root.into(), - post_state_root: archived.post_state_root.into(), - withdraw_root: archived.withdraw_root.into(), - data_hash: archived.data_hash.into(), - tx_data_digest: archived.tx_data_digest.into(), - prev_msg_queue_hash: archived.prev_msg_queue_hash.into(), - post_msg_queue_hash: archived.post_msg_queue_hash.into(), - tx_data_length: archived.tx_data_length.into(), - initial_block_number: archived.initial_block_number.into(), - block_ctxs: archived - .block_ctxs - .iter() - .map(BlockContextV2::from) - .collect(), - } - } -} - pub type VersionedChunkInfo = (ChunkInfo, ForkName); impl MultiVersionPublicInputs for ChunkInfo { diff --git a/crates/types/batch/Cargo.toml b/crates/types/batch/Cargo.toml index 95e82405..bced00ba 100644 --- a/crates/types/batch/Cargo.toml +++ b/crates/types/batch/Cargo.toml @@ -9,6 +9,7 @@ version.workspace = true [dependencies] alloy-primitives = { workspace = true } +bincode = { workspace = true } rkyv.workspace = true serde.workspace = true itertools.workspace = true diff --git a/crates/types/batch/src/header/mod.rs b/crates/types/batch/src/header/mod.rs index eaae2130..fcf9c373 100644 --- a/crates/types/batch/src/header/mod.rs +++ b/crates/types/batch/src/header/mod.rs @@ -25,7 +25,7 @@ pub trait BatchHeader { /// Reference header indicate the version of batch header base on which batch hash /// should be calculated. -#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] +#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, serde::Deserialize, serde::Serialize)] #[rkyv(derive(Debug))] pub enum ReferenceHeader { /// Represents DA-codec v6. diff --git a/crates/types/batch/src/header/v6.rs b/crates/types/batch/src/header/v6.rs index 9a95c835..f583a123 100644 --- a/crates/types/batch/src/header/v6.rs +++ b/crates/types/batch/src/header/v6.rs @@ -95,65 +95,3 @@ impl BatchHeader for BatchHeaderV6 { } } -impl BatchHeader for ArchivedBatchHeaderV6 { - fn version(&self) -> u8 { - self.version - } - - fn index(&self) -> u64 { - self.batch_index.into() - } - - fn parent_batch_hash(&self) -> B256 { - self.parent_batch_hash.into() - } - - fn batch_hash(&self) -> B256 { - let batch_index: u64 = self.batch_index.into(); - let l1_message_popped: u64 = self.l1_message_popped.into(); - let total_l1_message_popped: u64 = self.total_l1_message_popped.into(); - let data_hash: B256 = self.data_hash.into(); - let blob_versioned_hash: B256 = self.blob_versioned_hash.into(); - let parent_batch_hash: B256 = self.parent_batch_hash.into(); - let last_block_timestamp: u64 = self.last_block_timestamp.into(); - let blob_data_proof: [B256; 2] = self.blob_data_proof.map(|h| h.into()); - keccak256( - std::iter::empty() - .chain(vec![self.version].as_slice()) - .chain(batch_index.to_be_bytes().as_slice()) - .chain(l1_message_popped.to_be_bytes().as_slice()) - .chain(total_l1_message_popped.to_be_bytes().as_slice()) - .chain(data_hash.as_slice()) - .chain(blob_versioned_hash.as_slice()) - .chain(parent_batch_hash.as_slice()) - .chain(last_block_timestamp.to_be_bytes().as_slice()) - .chain(blob_data_proof[0].as_slice()) - .chain(blob_data_proof[1].as_slice()) - .cloned() - .collect::>(), - ) - } - - fn blob_versioned_hash(&self) -> B256 { - self.blob_versioned_hash.into() - } -} - -impl From<&ArchivedBatchHeaderV6> for BatchHeaderV6 { - fn from(archived: &ArchivedBatchHeaderV6) -> Self { - Self { - version: archived.version, - batch_index: archived.batch_index.into(), - l1_message_popped: archived.l1_message_popped.into(), - total_l1_message_popped: archived.total_l1_message_popped.into(), - parent_batch_hash: archived.parent_batch_hash.into(), - last_block_timestamp: archived.last_block_timestamp.into(), - data_hash: archived.data_hash.into(), - blob_versioned_hash: archived.blob_versioned_hash.into(), - blob_data_proof: [ - archived.blob_data_proof[0].into(), - archived.blob_data_proof[1].into(), - ], - } - } -} diff --git a/crates/types/batch/src/header/v7.rs b/crates/types/batch/src/header/v7.rs index e70600c9..f2a73aed 100644 --- a/crates/types/batch/src/header/v7.rs +++ b/crates/types/batch/src/header/v7.rs @@ -69,46 +69,3 @@ impl BatchHeader for BatchHeaderV7 { } } -impl BatchHeader for ArchivedBatchHeaderV7 { - fn version(&self) -> u8 { - self.version - } - - fn index(&self) -> u64 { - self.batch_index.into() - } - - fn parent_batch_hash(&self) -> B256 { - self.parent_batch_hash.into() - } - - fn batch_hash(&self) -> B256 { - let batch_index: u64 = self.batch_index.into(); - let blob_versioned_hash: B256 = self.blob_versioned_hash.into(); - let parent_batch_hash: B256 = self.parent_batch_hash.into(); - keccak256( - std::iter::empty() - .chain(vec![self.version].as_slice()) - .chain(batch_index.to_be_bytes().as_slice()) - .chain(blob_versioned_hash.as_slice()) - .chain(parent_batch_hash.as_slice()) - .cloned() - .collect::>(), - ) - } - - fn blob_versioned_hash(&self) -> B256 { - self.blob_versioned_hash.into() - } -} - -impl From<&ArchivedBatchHeaderV7> for BatchHeaderV7 { - fn from(archived: &ArchivedBatchHeaderV7) -> Self { - Self { - version: archived.version, - batch_index: archived.batch_index.into(), - parent_batch_hash: archived.parent_batch_hash.into(), - blob_versioned_hash: archived.blob_versioned_hash.into(), - } - } -} diff --git a/crates/types/batch/src/witness.rs b/crates/types/batch/src/witness.rs index e6f75a9a..102e174e 100644 --- a/crates/types/batch/src/witness.rs +++ b/crates/types/batch/src/witness.rs @@ -13,21 +13,44 @@ use crate::{ /// Simply rewrap byte48 to avoid unnecessary dep pub type Bytes48 = [u8; 48]; +mod array48 { + use serde::{Deserialize, Deserializer, Serializer}; + + pub fn serialize(array: &[u8; 48], serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(array) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; 48], D::Error> + where + D: Deserializer<'de>, + { + let bytes: &[u8] = serde::Deserialize::deserialize(deserializer)?; + bytes.try_into().map_err(|_| { + let msg = format!("expected a byte array of length 48 but got {}", bytes.len()); + serde::de::Error::custom(msg) + }) + } +} /// Witness required by applying point evaluation -#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] +#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, serde::Deserialize, serde::Serialize)] #[rkyv(derive(Debug))] pub struct PointEvalWitness { /// kzg commitment #[rkyv()] + #[serde(with = "array48")] pub kzg_commitment: Bytes48, /// kzg proof #[rkyv()] + #[serde(with = "array48")] pub kzg_proof: Bytes48, } /// Witness to the batch circuit. -#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] +#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, serde::Deserialize, serde::Serialize)] #[rkyv(derive(Debug))] pub struct BatchWitness { /// Flattened root proofs from all chunks in the batch. @@ -49,30 +72,30 @@ pub struct BatchWitness { pub fork_name: ForkName, } -impl ProofCarryingWitness for ArchivedBatchWitness { +impl BatchWitness { + pub fn bincode_serialize( + &self, + guest_version: Option, + ) -> Result, bincode::error::EncodeError> { + let config = bincode::config::standard(); + bincode::serde::encode_to_vec(&self, config) + } +} + +impl ProofCarryingWitness for BatchWitness { fn get_proofs(&self) -> Vec { - self.chunk_proofs - .iter() - .map(|archived| AggregationInput { - public_values: archived - .public_values - .iter() - .map(|u32_le| u32_le.to_native()) - .collect(), - commitment: ProgramCommitment::from(&archived.commitment), - }) - .collect() + self.chunk_proofs.clone() } } -impl From<&ArchivedBatchWitness> for BatchInfo { - fn from(witness: &ArchivedBatchWitness) -> Self { - let chunk_infos: Vec = witness.chunk_infos.iter().map(|ci| ci.into()).collect(); +impl From<&BatchWitness> for BatchInfo { + fn from(witness: &BatchWitness) -> Self { + let chunk_infos: Vec = witness.chunk_infos.iter().map(|ci| ci.clone()).collect(); match &witness.reference_header { - ArchivedReferenceHeader::V6(header) => { + ReferenceHeader::V6(header) => { let args = BuilderArgsV6 { - header: header.into(), + header: header.clone(), chunk_infos, blob_bytes: witness.blob_bytes.to_vec(), kzg_commitment: None, @@ -80,9 +103,9 @@ impl From<&ArchivedBatchWitness> for BatchInfo { }; BatchInfoBuilderV6::build(args) } - ArchivedReferenceHeader::V7(header) => { + ReferenceHeader::V7(header) => { let args = BuilderArgsV7 { - header: header.into(), + header: header.clone(), chunk_infos, blob_bytes: witness.blob_bytes.to_vec(), kzg_commitment: Some(witness.point_eval_witness.kzg_commitment), @@ -90,9 +113,9 @@ impl From<&ArchivedBatchWitness> for BatchInfo { }; BatchInfoBuilderV7::build(args) } - ArchivedReferenceHeader::V8(header) => { + ReferenceHeader::V8(header) => { let args = BuilderArgsV8 { - header: header.into(), + header: header.clone(), chunk_infos, blob_bytes: witness.blob_bytes.to_vec(), kzg_commitment: Some(witness.point_eval_witness.kzg_commitment), diff --git a/crates/types/bundle/Cargo.toml b/crates/types/bundle/Cargo.toml index 04d57428..f176a274 100644 --- a/crates/types/bundle/Cargo.toml +++ b/crates/types/bundle/Cargo.toml @@ -9,6 +9,7 @@ version.workspace = true [dependencies] alloy-primitives = { workspace = true } +bincode = { workspace = true } rkyv.workspace = true serde.workspace = true itertools.workspace = true diff --git a/crates/types/bundle/src/witness.rs b/crates/types/bundle/src/witness.rs index 2e1c129c..c9b13e4b 100644 --- a/crates/types/bundle/src/witness.rs +++ b/crates/types/bundle/src/witness.rs @@ -18,7 +18,7 @@ pub struct BundleWitnessEuclid { } /// The witness for the bundle circuit. -#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] +#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, serde::Deserialize, serde::Serialize)] #[rkyv(derive(Debug))] pub struct BundleWitness { /// Batch proofs being aggregated in the bundle. @@ -40,6 +40,15 @@ impl BundleWitness { batch_infos: self.batch_infos, } } + + pub fn bincode_serialize( + &self, + guest_version: Option, + ) -> Result, bincode::error::EncodeError> { + let config = bincode::config::standard(); + bincode::serde::encode_to_vec(&self, config) + } + /// See ChunkWitnessEuclid::rkyv_serialize for details. pub fn rkyv_serialize( &self, @@ -56,24 +65,14 @@ impl BundleWitness { } } -impl ProofCarryingWitness for ArchivedBundleWitness { +impl ProofCarryingWitness for BundleWitness { fn get_proofs(&self) -> Vec { - self.batch_proofs - .iter() - .map(|archived| AggregationInput { - public_values: archived - .public_values - .iter() - .map(|u32_le| u32_le.to_native()) - .collect(), - commitment: ProgramCommitment::from(&archived.commitment), - }) - .collect() + self.batch_proofs.clone() } } -impl From<&ArchivedBundleWitness> for BundleInfo { - fn from(witness: &ArchivedBundleWitness) -> Self { +impl From<&BundleWitness> for BundleInfo { + fn from(witness: &BundleWitness) -> Self { assert!( !witness.batch_infos.is_empty(), "at least one batch in a bundle" diff --git a/crates/types/chunk/Cargo.toml b/crates/types/chunk/Cargo.toml index 7a6980f5..b5bcd7f4 100644 --- a/crates/types/chunk/Cargo.toml +++ b/crates/types/chunk/Cargo.toml @@ -10,6 +10,7 @@ version.workspace = true [dependencies] revm-precompile = { git = "https://github.com/scroll-tech/revm", branch = "feat/reth-v74", default-features=false, features = ["kzg-rs", "libsecp256k1"] } alloy-primitives = { workspace = true, features = ["native-keccak"] } +bincode = { workspace = true } rkyv.workspace = true sbv-trie = { workspace = true } sbv-core = { workspace = true } diff --git a/crates/types/chunk/src/execute.rs b/crates/types/chunk/src/execute.rs index b19647c0..f1c6f2c2 100644 --- a/crates/types/chunk/src/execute.rs +++ b/crates/types/chunk/src/execute.rs @@ -1,6 +1,6 @@ use crate::{ - ArchivedChunkWitness, BlockHashProvider, CodeDb, NodesProvider, make_providers, - manually_drop_on_zkvm, witness::ArchivedStateCommitMode, + BlockHashProvider, ChunkWitness, CodeDb, NodesProvider, make_providers, manually_drop_on_zkvm, + witness::StateCommitMode, }; use alloy_primitives::B256; use itertools::Itertools; @@ -15,14 +15,14 @@ use sbv_primitives::{ ext::{BlockWitnessChunkExt, BlockWitnessRethExt as _, TxBytesHashExt}, hardforks::SCROLL_DEV_HARDFORKS, types::{ - reth::primitives::{Block, RecoveredBlock}, - scroll::ChunkInfoBuilder, - }, + reth::primitives::{Block, RecoveredBlock}, + scroll::ChunkInfoBuilder, + } }; use std::sync::Arc; use types_base::{fork_name::ForkName, public_inputs::chunk::ChunkInfo}; -type Witness = ArchivedChunkWitness; +type Witness = ChunkWitness; /// `compression_ratios` can be `None` in host mode. /// But in guest mode, it must be provided. @@ -47,8 +47,8 @@ pub fn execute(witness: &Witness) -> Result { ); let pre_state_root = witness.blocks[0].pre_state_root; - let fork_name = ForkName::from(&witness.fork_name); - let chain = Chain::from_id(witness.blocks[0].chain_id()); + let fork_name = ForkName::from(witness.fork_name.clone()); + let chain = Chain::from_id(witness.blocks[0].chain_id); // SCROLL_DEV_HARDFORKS will enable all forks let mut hardforks = (*SCROLL_DEV_HARDFORKS).clone(); @@ -72,19 +72,15 @@ pub fn execute(witness: &Witness) -> Result { let code_db = manually_drop_on_zkvm!(code_db); let nodes_provider = manually_drop_on_zkvm!(nodes_provider); - let prev_state_root = witness.blocks[0].pre_state_root(); + let prev_state_root = witness.blocks[0].pre_state_root; let state_commit_mode = &witness.state_commit_mode; println!("state_commit_mode: {:?}", state_commit_mode); - let compression_ratios = witness - .compression_ratios - .iter() - .map(|b| b.iter().map(|c| c.into()).collect()) - .collect::>>(); + let compression_ratios = witness.compression_ratios.clone(); let (post_state_root, withdraw_root) = match state_commit_mode { - ArchivedStateCommitMode::Chunk | ArchivedStateCommitMode::Block => execute_inner( + StateCommitMode::Chunk | StateCommitMode::Block => execute_inner( fork_name, &code_db, &nodes_provider, @@ -93,9 +89,9 @@ pub fn execute(witness: &Witness) -> Result { &blocks, chain_spec.clone(), compression_ratios, - matches!(state_commit_mode, ArchivedStateCommitMode::Chunk), + matches!(state_commit_mode, StateCommitMode::Chunk), )?, - ArchivedStateCommitMode::Auto => match execute_inner( + StateCommitMode::Auto => match execute_inner( fork_name, &code_db, &nodes_provider, diff --git a/crates/types/chunk/src/witness.rs b/crates/types/chunk/src/witness.rs index e929a9ca..6c4392a6 100644 --- a/crates/types/chunk/src/witness.rs +++ b/crates/types/chunk/src/witness.rs @@ -122,6 +122,13 @@ impl ChunkWitness { fork_name: self.fork_name, } } + pub fn bincode_serialize( + &self, + guest_version: Option, + ) -> Result, bincode::error::EncodeError> { + let config = bincode::config::standard(); + bincode::serde::encode_to_vec(&self, config) + } /// `guest_version` is related to the guest program. /// It is not always same with the evm hardfork. /// For example, a `Feynman` guest program can execute `EuclidV2` blocks. @@ -158,10 +165,10 @@ impl ChunkWitness { } } -impl TryFrom<&ArchivedChunkWitness> for ChunkInfo { +impl TryFrom<&ChunkWitness> for ChunkInfo { type Error = String; - fn try_from(value: &ArchivedChunkWitness) -> Result { + fn try_from(value: &ChunkWitness) -> Result { crate::execute(value) } } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 31de76e6..bdd8cc7a 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -3,57 +3,18 @@ pub mod bundle { pub use types_base::public_inputs::bundle::BundleInfo; pub use types_bundle::*; - - pub struct ToArchievedWitness(pub rkyv::util::AlignedVec); - impl ToArchievedWitness { - pub fn create(witness: &BundleWitness) -> Result { - rkyv::to_bytes::(witness) - .map_err(|e| format!("failed to serialize bundle witness: {e}")) - .map(Self) - } - pub fn access(&self) -> Result<&ArchivedBundleWitness, String> { - rkyv::access::(&self.0) - .map_err(|e| format!("rkyv deserialisation of bundle witness bytes failed: {e}")) - } - } } pub mod batch { - pub use types_base::public_inputs::batch::{ArchivedBatchInfo, BatchInfo, VersionedBatchInfo}; + pub use types_base::public_inputs::batch::{BatchInfo, VersionedBatchInfo}; pub use types_batch::*; - - pub struct ToArchievedWitness(pub rkyv::util::AlignedVec); - impl ToArchievedWitness { - pub fn create(witness: &BatchWitness) -> Result { - rkyv::to_bytes::(witness) - .map_err(|e| format!("failed to serialize batch witness: {e}")) - .map(Self) - } - pub fn access(&self) -> Result<&ArchivedBatchWitness, String> { - rkyv::access::(&self.0) - .map_err(|e| format!("rkyv deserialisation of batch witness bytes failed: {e}")) - } - } } pub mod chunk { pub use types_base::public_inputs::chunk::{ - ArchivedChunkInfo, BlockContextV2, ChunkInfo, SIZE_BLOCK_CTX, VersionedChunkInfo, + BlockContextV2, ChunkInfo, SIZE_BLOCK_CTX, VersionedChunkInfo, }; pub use types_chunk::*; - - pub struct ToArchievedWitness(pub rkyv::util::AlignedVec); - impl ToArchievedWitness { - pub fn create(witness: &ChunkWitness) -> Result { - rkyv::to_bytes::(witness) - .map_err(|e| format!("failed to serialize chunk witness: {e}")) - .map(Self) - } - pub fn access(&self) -> Result<&ArchivedChunkWitness, String> { - rkyv::access::(&self.0) - .map_err(|e| format!("rkyv deserialisation of chunk witness bytes failed: {e}")) - } - } } pub use types_base::{aggregation as types_agg, environ, public_inputs};