mirror of
https://github.com/logos-blockchain/logos-blockchain-pocs.git
synced 2026-02-03 12:43:08 +00:00
Reliable risc0 recursion (#53)
* introduce risc0_images and risc0_images_police * static instead of const for elfs * gen images in a loop to resolve id changes due to recursion * r0_proofs reference each other through risc0_images when recursing * update ledger to use risc0_images * remove debug panics
This commit is contained in:
parent
bc8c3b6f73
commit
58ee239fcc
@ -7,7 +7,9 @@ members = [
|
||||
"risc0_proofs",
|
||||
"bundle_risc0_proof",
|
||||
"tx_risc0_proof",
|
||||
"ledger_validity_proof"
|
||||
"ledger_validity_proof",
|
||||
"risc0_images",
|
||||
"risc0_images_police",
|
||||
]
|
||||
|
||||
# Always optimize; building and running the risc0_proofs takes much longer without optimization.
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "nomos_cl_bundle_risc0_proof"
|
||||
name = "nomos_mantle_bundle_risc0_proof"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ risc0-zkvm = { version = "1.0", default-features = false, features = ['std'] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
cl = { path = "../../cl" }
|
||||
ledger_proof_statements = { path = "../../ledger_proof_statements" }
|
||||
nomos_cl_tx_risc0_proof = { path = "../../tx_risc0_proof" }
|
||||
risc0_images = { path = "../../risc0_images" }
|
||||
|
||||
|
||||
[patch.crates-io]
|
||||
|
||||
@ -5,7 +5,11 @@ fn main() {
|
||||
let bundle_private: BundleWitness = env::read();
|
||||
|
||||
for tx in &bundle_private.txs {
|
||||
env::verify(nomos_cl_tx_risc0_proof::TX_ID, &serde::to_vec(&tx).unwrap()).unwrap();
|
||||
env::verify(
|
||||
risc0_images::nomos_mantle_tx_risc0_proof::TX_ID,
|
||||
&serde::to_vec(&tx).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
env::commit(&bundle_private.commit());
|
||||
|
||||
19
emmarin/cl/gen_risc0_images.sh
Executable file
19
emmarin/cl/gen_risc0_images.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
# We generate in a *loop* because some risc0 proofs are recursive, so if a child
|
||||
# proof's id changes, then the parent proof will also change, but we don't see the
|
||||
# parent's id change until the next run.
|
||||
|
||||
cargo run --bin gen_risc0_images > risc0_images/src/lib.rs.new
|
||||
|
||||
while ! cmp -s risc0_images/src/lib.rs.new risc0_images/src/lib.rs
|
||||
do
|
||||
mv risc0_images/src/lib.rs.new risc0_images/src/lib.rs
|
||||
cargo run --bin gen_risc0_images > risc0_images/src/lib.rs.new
|
||||
echo "-------- FINISHED UPDATE ITERATION --------"
|
||||
done
|
||||
|
||||
rm risc0_images/src/lib.rs.new
|
||||
|
||||
cargo test -p risc0_images_police
|
||||
@ -6,10 +6,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
cl = { path = "../cl" }
|
||||
ledger_proof_statements = { path = "../ledger_proof_statements" }
|
||||
nomos_mantle_risc0_proofs = { path = "../risc0_proofs" }
|
||||
nomos_cl_bundle_risc0_proof = { path = "../bundle_risc0_proof" }
|
||||
nomos_cl_tx_risc0_proof = { path = "../tx_risc0_proof" }
|
||||
ledger_validity_proof = { path = "../ledger_validity_proof" }
|
||||
risc0_images = { path = "../risc0_images" }
|
||||
risc0-zkvm = { version = "1.0", features = ["prove", "metal"] }
|
||||
risc0-groth16 = { version = "1.0" }
|
||||
rand = "0.8.5"
|
||||
|
||||
@ -23,7 +23,11 @@ impl ProvedBundle {
|
||||
|
||||
let opts = risc0_zkvm::ProverOpts::succinct();
|
||||
let prove_info = prover
|
||||
.prove_with_opts(env, nomos_cl_bundle_risc0_proof::BUNDLE_ELF, &opts)
|
||||
.prove_with_opts(
|
||||
env,
|
||||
risc0_images::nomos_mantle_bundle_risc0_proof::BUNDLE_ELF,
|
||||
&opts,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
println!(
|
||||
@ -46,7 +50,7 @@ impl ProvedBundle {
|
||||
|
||||
pub fn verify(&self) -> bool {
|
||||
self.risc0_receipt
|
||||
.verify(nomos_cl_bundle_risc0_proof::BUNDLE_ID)
|
||||
.verify(risc0_images::nomos_mantle_bundle_risc0_proof::BUNDLE_ID)
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ impl ProvedLedgerTransition {
|
||||
// This struct contains the receipt along with statistics about execution of the guest
|
||||
let opts = risc0_zkvm::ProverOpts::succinct();
|
||||
let prove_info = prover
|
||||
.prove_with_opts(env, ledger_validity_proof::LEDGER_ELF, &opts)
|
||||
.prove_with_opts(env, risc0_images::ledger_validity_proof::LEDGER_ELF, &opts)
|
||||
.unwrap();
|
||||
|
||||
println!(
|
||||
@ -99,7 +99,7 @@ impl ProvedLedgerTransition {
|
||||
|
||||
pub fn verify(&self) -> bool {
|
||||
self.risc0_receipt
|
||||
.verify(ledger_validity_proof::LEDGER_ID)
|
||||
.verify(risc0_images::ledger_validity_proof::LEDGER_ID)
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ impl StfProof {
|
||||
}
|
||||
|
||||
pub fn nop_stf() -> [u8; 32] {
|
||||
risc0_stf(nomos_mantle_risc0_proofs::STF_NOP_ID)
|
||||
risc0_stf(risc0_images::nomos_mantle_risc0_proofs::STF_NOP_ID)
|
||||
}
|
||||
|
||||
pub fn prove_nop(public: StfPublic) -> Self {
|
||||
@ -47,7 +47,11 @@ impl StfProof {
|
||||
|
||||
let opts = risc0_zkvm::ProverOpts::succinct();
|
||||
let prove_info = prover
|
||||
.prove_with_opts(env, nomos_mantle_risc0_proofs::STF_NOP_ELF, &opts)
|
||||
.prove_with_opts(
|
||||
env,
|
||||
risc0_images::nomos_mantle_risc0_proofs::STF_NOP_ELF,
|
||||
&opts,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
println!(
|
||||
@ -60,7 +64,7 @@ impl StfProof {
|
||||
let receipt = prove_info.receipt;
|
||||
|
||||
Self {
|
||||
risc0_id: nomos_mantle_risc0_proofs::STF_NOP_ID,
|
||||
risc0_id: risc0_images::nomos_mantle_risc0_proofs::STF_NOP_ID,
|
||||
public,
|
||||
risc0_receipt: receipt,
|
||||
}
|
||||
|
||||
@ -36,7 +36,11 @@ impl ProvedTx {
|
||||
// This struct contains the receipt along with statistics about execution of the guest
|
||||
let opts = risc0_zkvm::ProverOpts::succinct();
|
||||
let prove_info = prover
|
||||
.prove_with_opts(env, nomos_cl_tx_risc0_proof::TX_ELF, &opts)
|
||||
.prove_with_opts(
|
||||
env,
|
||||
risc0_images::nomos_mantle_tx_risc0_proof::TX_ELF,
|
||||
&opts,
|
||||
)
|
||||
.map_err(|_| Error::Risc0ProofFailed)?;
|
||||
|
||||
println!(
|
||||
@ -57,7 +61,7 @@ impl ProvedTx {
|
||||
|
||||
pub fn verify(&self) -> bool {
|
||||
self.risc0_receipt
|
||||
.verify(nomos_cl_tx_risc0_proof::TX_ID)
|
||||
.verify(risc0_images::nomos_mantle_tx_risc0_proof::TX_ID)
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ risc0-zkvm = { version = "1.0", default-features = false, features = ['std'] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
cl = { path = "../../cl" }
|
||||
ledger_proof_statements = { path = "../../ledger_proof_statements" }
|
||||
nomos_cl_bundle_risc0_proof = { path = "../../bundle_risc0_proof" }
|
||||
risc0_images = { path = "../../risc0_images" }
|
||||
|
||||
[patch.crates-io]
|
||||
# add RISC Zero accelerator support for all downstream usages of the following crates.
|
||||
|
||||
@ -23,7 +23,7 @@ fn main() {
|
||||
} in bundles
|
||||
{
|
||||
env::verify(
|
||||
nomos_cl_bundle_risc0_proof::BUNDLE_ID,
|
||||
risc0_images::nomos_mantle_bundle_risc0_proof::BUNDLE_ID,
|
||||
&serde::to_vec(&bundle).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
7
emmarin/cl/risc0_images/Cargo.toml
Normal file
7
emmarin/cl/risc0_images/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "risc0_images"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
binary_macros = "1.0.0"
|
||||
16
emmarin/cl/risc0_images/src/lib.rs
Normal file
16
emmarin/cl/risc0_images/src/lib.rs
Normal file
File diff suppressed because one or more lines are too long
14
emmarin/cl/risc0_images_police/Cargo.toml
Normal file
14
emmarin/cl/risc0_images_police/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "risc0_images_police"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
[dependencies]
|
||||
sha2 = "0.10"
|
||||
base64 = "0.22"
|
||||
risc0_images = { path = "../risc0_images" }
|
||||
nomos_mantle_risc0_proofs = { path = "../risc0_proofs" }
|
||||
nomos_mantle_bundle_risc0_proof = { path = "../bundle_risc0_proof" }
|
||||
nomos_mantle_tx_risc0_proof = { path = "../tx_risc0_proof" }
|
||||
ledger_validity_proof = { path = "../ledger_validity_proof" }
|
||||
25
emmarin/cl/risc0_images_police/src/bin/gen_risc0_images.rs
Normal file
25
emmarin/cl/risc0_images_police/src/bin/gen_risc0_images.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use base64::prelude::*;
|
||||
|
||||
macro_rules! gen_risc0_image {
|
||||
($module:ident, $id:ident, $elf:ident) => {
|
||||
println!("pub mod {} {{", stringify!($module));
|
||||
println!(
|
||||
" pub const {}: [u32; 8] = {:?};",
|
||||
stringify!($id),
|
||||
$module::$id
|
||||
);
|
||||
println!(
|
||||
" pub static {}: &[u8] = binary_macros::base64!({:?});",
|
||||
stringify!($elf),
|
||||
BASE64_STANDARD.encode(&$module::$elf)
|
||||
);
|
||||
println!("}}");
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
gen_risc0_image!(nomos_mantle_risc0_proofs, STF_NOP_ID, STF_NOP_ELF);
|
||||
gen_risc0_image!(nomos_mantle_bundle_risc0_proof, BUNDLE_ID, BUNDLE_ELF);
|
||||
gen_risc0_image!(nomos_mantle_tx_risc0_proof, TX_ID, TX_ELF);
|
||||
gen_risc0_image!(ledger_validity_proof, LEDGER_ID, LEDGER_ELF);
|
||||
}
|
||||
51
emmarin/cl/risc0_images_police/src/lib.rs
Normal file
51
emmarin/cl/risc0_images_police/src/lib.rs
Normal file
@ -0,0 +1,51 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn hash(x: impl AsRef<[u8]>) -> [u8; 32] {
|
||||
use sha2::{Digest, Sha256};
|
||||
Sha256::digest(x).into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ensure_images_are_correct() {
|
||||
assert_eq!(
|
||||
risc0_images::nomos_mantle_risc0_proofs::STF_NOP_ID,
|
||||
nomos_mantle_risc0_proofs::STF_NOP_ID,
|
||||
"STF_NOP_ID"
|
||||
);
|
||||
assert_eq!(
|
||||
hash(risc0_images::nomos_mantle_risc0_proofs::STF_NOP_ELF),
|
||||
hash(nomos_mantle_risc0_proofs::STF_NOP_ELF),
|
||||
"STF_NOP_ELF"
|
||||
);
|
||||
assert_eq!(
|
||||
risc0_images::nomos_mantle_bundle_risc0_proof::BUNDLE_ID,
|
||||
nomos_mantle_bundle_risc0_proof::BUNDLE_ID,
|
||||
"BUNDLE_ID"
|
||||
);
|
||||
assert_eq!(
|
||||
hash(risc0_images::nomos_mantle_bundle_risc0_proof::BUNDLE_ELF),
|
||||
hash(nomos_mantle_bundle_risc0_proof::BUNDLE_ELF),
|
||||
"BUNDLE_ELF"
|
||||
);
|
||||
assert_eq!(
|
||||
risc0_images::nomos_mantle_tx_risc0_proof::TX_ID,
|
||||
nomos_mantle_tx_risc0_proof::TX_ID,
|
||||
"TX_ID"
|
||||
);
|
||||
assert_eq!(
|
||||
hash(risc0_images::nomos_mantle_tx_risc0_proof::TX_ELF),
|
||||
hash(nomos_mantle_tx_risc0_proof::TX_ELF),
|
||||
"TX_ELF"
|
||||
);
|
||||
assert_eq!(
|
||||
risc0_images::ledger_validity_proof::LEDGER_ID,
|
||||
ledger_validity_proof::LEDGER_ID,
|
||||
"LEDGER_ID"
|
||||
);
|
||||
assert_eq!(
|
||||
hash(risc0_images::ledger_validity_proof::LEDGER_ELF),
|
||||
hash(ledger_validity_proof::LEDGER_ELF),
|
||||
"LEDGER_ELF"
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "nomos_cl_tx_risc0_proof"
|
||||
name = "nomos_mantle_tx_risc0_proof"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user