From e8f660c2c7588729e9e5bc290db06793284af566 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 8 Oct 2025 20:27:09 -0300 Subject: [PATCH 1/7] wip --- nssa/Cargo.toml | 3 +++ nssa/build.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++ nssa/src/lib.rs | 4 +++ nssa/src/program.rs | 2 +- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 nssa/build.rs diff --git a/nssa/Cargo.toml b/nssa/Cargo.toml index 93553a8..f1a055e 100644 --- a/nssa/Cargo.toml +++ b/nssa/Cargo.toml @@ -15,6 +15,9 @@ rand = "0.8" borsh = "1.5.7" hex = "0.4.3" +[build-dependencies] +risc0-build = "3.0.3" + [dev-dependencies] test-program-methods = { path = "test_program_methods" } hex-literal = "1.0.0" diff --git a/nssa/build.rs b/nssa/build.rs new file mode 100644 index 0000000..f04ba25 --- /dev/null +++ b/nssa/build.rs @@ -0,0 +1,66 @@ +use std::{env, fs, path::Path, process::Command}; +use risc0_build::a; + +fn main() { + // 1️⃣ Crate root and OUT_DIR + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let out_dir = env::var("OUT_DIR").unwrap(); + + // 2️⃣ Directory to write generated module + let mod_dir = Path::new(&out_dir).join("nssa_programs"); + let mod_file = mod_dir.join("mod.rs"); + + println!("cargo:rerun-if-changed=program_methods/guest"); + + // 3️⃣ Build the Risc0 guest program + let guest_manifest = Path::new(&manifest_dir) + .join("program_methods/guest/Cargo.toml"); + + let status = Command::new("cargo") + .arg("risczero") + .arg("build") + .arg("--manifest-path") + .arg(&guest_manifest) + .status() + .expect("failed to run risczero build"); + assert!(status.success(), "Risc0 deterministic build failed"); + + // 4️⃣ Target directory where the Risc0 build produces .bin files + let target_dir = Path::new(&manifest_dir) + .join("program_methods/guest/target/riscv32im-risc0-zkvm-elf/docker/"); + + println!("cargo:warning=Looking for binaries in {}", target_dir.display()); + + // 5️⃣ Collect all .bin files + let bins = fs::read_dir(&target_dir) + .expect("failed to read external target dir") + .filter_map(Result::ok) + .filter(|e| e.path().extension().map(|ext| ext == "bin").unwrap_or(false)) + .collect::>(); + + if bins.is_empty() { + panic!("No .bin files found in {:?}", target_dir); + } + + println!("cargo:warning=Found {} binaries:", bins.len()); + for b in &bins { + println!("cargo:warning= - {}", b.path().display()); + } + + // 6️⃣ Generate Rust module + fs::create_dir_all(&mod_dir).unwrap(); + let mut src = String::new(); + for entry in bins { + let path = entry.path(); + let name = path.file_stem().unwrap().to_string_lossy(); + src.push_str(&format!( + "pub const {}_ELF: &[u8] = include_bytes!(r#\"{}\"#);\n", + name.to_uppercase(), + path.display() + )); + } + + fs::write(&mod_file, src).unwrap(); + println!("cargo:warning=Generated module at {}", mod_file.display()); +} + diff --git a/nssa/src/lib.rs b/nssa/src/lib.rs index 21defa9..cdf45a2 100644 --- a/nssa/src/lib.rs +++ b/nssa/src/lib.rs @@ -1,3 +1,7 @@ +pub mod programs { + include!(concat!(env!("OUT_DIR"), "/nssa_programs/mod.rs")); +} + pub mod encoding; pub mod error; mod merkle_tree; diff --git a/nssa/src/program.rs b/nssa/src/program.rs index 5abc153..fc13865 100644 --- a/nssa/src/program.rs +++ b/nssa/src/program.rs @@ -2,7 +2,7 @@ use nssa_core::{ account::{Account, AccountWithMetadata}, program::{InstructionData, ProgramId, ProgramOutput}, }; -use program_methods::{ +use crate::programs::{ AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID, PINATA_ELF, PINATA_ID, TOKEN_ELF, TOKEN_ID, }; From 69b610269b6b708ee8739accae5c5d6a6584f2a4 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Thu, 9 Oct 2025 21:27:43 -0300 Subject: [PATCH 2/7] use risc0 method for image_id computaiton --- ci_scripts/test-ubuntu.sh | 10 +-- nssa/Cargo.toml | 4 +- nssa/build.rs | 77 +++++++++---------- nssa/src/lib.rs | 8 +- .../privacy_preserving_transaction/circuit.rs | 2 +- nssa/src/program.rs | 2 +- 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/ci_scripts/test-ubuntu.sh b/ci_scripts/test-ubuntu.sh index 5c19b36..b563e6b 100755 --- a/ci_scripts/test-ubuntu.sh +++ b/ci_scripts/test-ubuntu.sh @@ -4,16 +4,16 @@ curl -L https://risczero.com/install | bash /home/runner/.risc0/bin/rzup install source env.sh -RISC0_DEV_MODE=1 cargo test --release +RISC0_DEV_MODE=1 cargo test --release --features no_docker cd integration_tests export NSSA_WALLET_HOME_DIR=$(pwd)/configs/debug/wallet/ export RUST_LOG=info -cargo run $(pwd)/configs/debug all +cargo run $(pwd)/configs/debug all --features no_docker echo "Try test valid proof at least once" -cargo run $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account +cargo run $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account --features no_docker echo "Continuing in dev mode" -RISC0_DEV_MODE=1 cargo run $(pwd)/configs/debug all +RISC0_DEV_MODE=1 cargo run $(pwd)/configs/debug all --features no_docker cd .. -cd nssa/program_methods/guest && cargo test --release +cd nssa/program_methods/guest && cargo test --release --features no_docker diff --git a/nssa/Cargo.toml b/nssa/Cargo.toml index f1a055e..a86bd98 100644 --- a/nssa/Cargo.toml +++ b/nssa/Cargo.toml @@ -7,7 +7,7 @@ edition = "2024" thiserror = "2.0.12" risc0-zkvm = { version = "3.0.3", features = ['std'] } nssa-core = { path = "core", features = ["host"] } -program-methods = { path = "program_methods" } +program-methods = { path = "program_methods", optional = true } serde = "1.0.219" sha2 = "0.10.9" secp256k1 = "0.31.1" @@ -17,6 +17,7 @@ hex = "0.4.3" [build-dependencies] risc0-build = "3.0.3" +risc0-binfmt = "3.0.2" [dev-dependencies] test-program-methods = { path = "test_program_methods" } @@ -24,3 +25,4 @@ hex-literal = "1.0.0" [features] default = [] +no_docker = ["program-methods"] diff --git a/nssa/build.rs b/nssa/build.rs index f04ba25..cc4608e 100644 --- a/nssa/build.rs +++ b/nssa/build.rs @@ -1,66 +1,63 @@ -use std::{env, fs, path::Path, process::Command}; -use risc0_build::a; - fn main() { - // 1️⃣ Crate root and OUT_DIR - let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let out_dir = env::var("OUT_DIR").unwrap(); + if cfg!(feature = "no_docker") { + println!("cargo:warning=NO_DOCKER feature enabled – deterministic build skipped"); + return; + } - // 2️⃣ Directory to write generated module - let mod_dir = Path::new(&out_dir).join("nssa_programs"); + build_deterministic().expect("Deterministic build failed"); +} + +fn build_deterministic() -> Result<(), Box> { + use std::{env, fs, path::PathBuf, process::Command}; + + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); + let out_dir = PathBuf::from(env::var("OUT_DIR")?); + let mod_dir = out_dir.join("program_methods"); let mod_file = mod_dir.join("mod.rs"); - println!("cargo:rerun-if-changed=program_methods/guest"); + println!("cargo:rerun-if-changed=program_methods/guest/src"); + println!("cargo:rerun-if-changed=program_methods/guest/Cargo.toml"); - // 3️⃣ Build the Risc0 guest program - let guest_manifest = Path::new(&manifest_dir) - .join("program_methods/guest/Cargo.toml"); + let guest_manifest = manifest_dir.join("program_methods/guest/Cargo.toml"); let status = Command::new("cargo") - .arg("risczero") - .arg("build") - .arg("--manifest-path") + .args(["risczero", "build", "--manifest-path"]) .arg(&guest_manifest) - .status() - .expect("failed to run risczero build"); - assert!(status.success(), "Risc0 deterministic build failed"); + .status()?; + if !status.success() { + return Err("Risc0 deterministic build failed".into()); + } - // 4️⃣ Target directory where the Risc0 build produces .bin files - let target_dir = Path::new(&manifest_dir) - .join("program_methods/guest/target/riscv32im-risc0-zkvm-elf/docker/"); + let target_dir = + manifest_dir.join("program_methods/guest/target/riscv32im-risc0-zkvm-elf/docker/"); - println!("cargo:warning=Looking for binaries in {}", target_dir.display()); - - // 5️⃣ Collect all .bin files - let bins = fs::read_dir(&target_dir) - .expect("failed to read external target dir") + let bins = fs::read_dir(&target_dir)? .filter_map(Result::ok) - .filter(|e| e.path().extension().map(|ext| ext == "bin").unwrap_or(false)) + .filter(|e| e.path().extension().is_some_and(|ext| ext == "bin")) .collect::>(); if bins.is_empty() { - panic!("No .bin files found in {:?}", target_dir); + return Err(format!("No .bin files found in {:?}", target_dir).into()); } - println!("cargo:warning=Found {} binaries:", bins.len()); - for b in &bins { - println!("cargo:warning= - {}", b.path().display()); - } - - // 6️⃣ Generate Rust module - fs::create_dir_all(&mod_dir).unwrap(); + fs::create_dir_all(&mod_dir)?; let mut src = String::new(); for entry in bins { let path = entry.path(); let name = path.file_stem().unwrap().to_string_lossy(); + let bytecode = fs::read(&path)?; + let image_id: [u32; 8] = risc0_binfmt::compute_image_id(&bytecode)?.into(); src.push_str(&format!( - "pub const {}_ELF: &[u8] = include_bytes!(r#\"{}\"#);\n", + "pub const {}_ELF: &[u8] = include_bytes!(r#\"{}\"#);\n\ + pub const {}_ID: [u32; 8] = {:?};\n", name.to_uppercase(), - path.display() + path.display(), + name.to_uppercase(), + image_id )); } - - fs::write(&mod_file, src).unwrap(); + fs::write(&mod_file, src)?; println!("cargo:warning=Generated module at {}", mod_file.display()); -} + Ok(()) +} diff --git a/nssa/src/lib.rs b/nssa/src/lib.rs index cdf45a2..5227875 100644 --- a/nssa/src/lib.rs +++ b/nssa/src/lib.rs @@ -1,7 +1,11 @@ -pub mod programs { - include!(concat!(env!("OUT_DIR"), "/nssa_programs/mod.rs")); +#[cfg(not(feature = "no_docker"))] +pub mod program_methods { + include!(concat!(env!("OUT_DIR"), "/program_methods/mod.rs")); } +#[cfg(feature = "no_docker")] +use program_methods; + pub mod encoding; pub mod error; mod merkle_tree; diff --git a/nssa/src/privacy_preserving_transaction/circuit.rs b/nssa/src/privacy_preserving_transaction/circuit.rs index 3a98723..9ce0610 100644 --- a/nssa/src/privacy_preserving_transaction/circuit.rs +++ b/nssa/src/privacy_preserving_transaction/circuit.rs @@ -8,7 +8,7 @@ use risc0_zkvm::{ExecutorEnv, InnerReceipt, Receipt, default_prover}; use crate::{error::NssaError, program::Program}; -use program_methods::{PRIVACY_PRESERVING_CIRCUIT_ELF, PRIVACY_PRESERVING_CIRCUIT_ID}; +use crate::program_methods::{PRIVACY_PRESERVING_CIRCUIT_ELF, PRIVACY_PRESERVING_CIRCUIT_ID}; /// Proof of the privacy preserving execution circuit #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/nssa/src/program.rs b/nssa/src/program.rs index fc13865..b229241 100644 --- a/nssa/src/program.rs +++ b/nssa/src/program.rs @@ -2,7 +2,7 @@ use nssa_core::{ account::{Account, AccountWithMetadata}, program::{InstructionData, ProgramId, ProgramOutput}, }; -use crate::programs::{ +use crate::program_methods::{ AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID, PINATA_ELF, PINATA_ID, TOKEN_ELF, TOKEN_ID, }; From f200f39779d57c568804628d57295df322a36a8c Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Fri, 10 Oct 2025 16:48:12 -0300 Subject: [PATCH 3/7] minor fix --- ci_scripts/test-ubuntu.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ci_scripts/test-ubuntu.sh b/ci_scripts/test-ubuntu.sh index b563e6b..65d219e 100755 --- a/ci_scripts/test-ubuntu.sh +++ b/ci_scripts/test-ubuntu.sh @@ -9,11 +9,11 @@ RISC0_DEV_MODE=1 cargo test --release --features no_docker cd integration_tests export NSSA_WALLET_HOME_DIR=$(pwd)/configs/debug/wallet/ export RUST_LOG=info -cargo run $(pwd)/configs/debug all --features no_docker +cargo run --features no_docker $(pwd)/configs/debug all echo "Try test valid proof at least once" -cargo run $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account --features no_docker +cargo run --features no_docker $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account echo "Continuing in dev mode" -RISC0_DEV_MODE=1 cargo run $(pwd)/configs/debug all --features no_docker +RISC0_DEV_MODE=1 cargo run --features no_docker $(pwd)/configs/debug all cd .. cd nssa/program_methods/guest && cargo test --release --features no_docker From d7240e073a961889b47702df671f5f97a0c36797 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Fri, 10 Oct 2025 17:51:22 -0300 Subject: [PATCH 4/7] fix integration tests --- ci_scripts/test-ubuntu.sh | 8 ++++---- integration_tests/Cargo.toml | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ci_scripts/test-ubuntu.sh b/ci_scripts/test-ubuntu.sh index 65d219e..d62cbb3 100755 --- a/ci_scripts/test-ubuntu.sh +++ b/ci_scripts/test-ubuntu.sh @@ -9,11 +9,11 @@ RISC0_DEV_MODE=1 cargo test --release --features no_docker cd integration_tests export NSSA_WALLET_HOME_DIR=$(pwd)/configs/debug/wallet/ export RUST_LOG=info -cargo run --features no_docker $(pwd)/configs/debug all +cargo run $(pwd)/configs/debug all echo "Try test valid proof at least once" -cargo run --features no_docker $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account +cargo run $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account echo "Continuing in dev mode" -RISC0_DEV_MODE=1 cargo run --features no_docker $(pwd)/configs/debug all +RISC0_DEV_MODE=1 cargo run $(pwd)/configs/debug all cd .. -cd nssa/program_methods/guest && cargo test --release --features no_docker +cd nssa/program_methods/guest && cargo test --release diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index db2dfeb..f7bd131 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -36,3 +36,4 @@ path = "../common" [dependencies.nssa] path = "../nssa" +features = ["no_docker"] From ba35fafad4c2c65cdc0819f618336dee586b447c Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Mon, 13 Oct 2025 15:09:50 -0300 Subject: [PATCH 5/7] fmt --- nssa/src/lib.rs | 1 + nssa/src/program.rs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/nssa/src/lib.rs b/nssa/src/lib.rs index 5227875..a635c0f 100644 --- a/nssa/src/lib.rs +++ b/nssa/src/lib.rs @@ -4,6 +4,7 @@ pub mod program_methods { } #[cfg(feature = "no_docker")] +#[allow(clippy::single_component_path_imports)] use program_methods; pub mod encoding; diff --git a/nssa/src/program.rs b/nssa/src/program.rs index b229241..f6df8fe 100644 --- a/nssa/src/program.rs +++ b/nssa/src/program.rs @@ -1,11 +1,11 @@ -use nssa_core::{ - account::{Account, AccountWithMetadata}, - program::{InstructionData, ProgramId, ProgramOutput}, -}; use crate::program_methods::{ AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID, PINATA_ELF, PINATA_ID, TOKEN_ELF, TOKEN_ID, }; +use nssa_core::{ + account::{Account, AccountWithMetadata}, + program::{InstructionData, ProgramId, ProgramOutput}, +}; use risc0_zkvm::{ExecutorEnv, ExecutorEnvBuilder, default_executor, serde::to_vec}; use serde::Serialize; From cf9d296d085bcd6bf02f06f6707185c909f5200c Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Mon, 13 Oct 2025 15:26:14 -0300 Subject: [PATCH 6/7] remove redundant test in ci --- ci_scripts/test-ubuntu.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/ci_scripts/test-ubuntu.sh b/ci_scripts/test-ubuntu.sh index d62cbb3..2aeba86 100755 --- a/ci_scripts/test-ubuntu.sh +++ b/ci_scripts/test-ubuntu.sh @@ -9,7 +9,6 @@ RISC0_DEV_MODE=1 cargo test --release --features no_docker cd integration_tests export NSSA_WALLET_HOME_DIR=$(pwd)/configs/debug/wallet/ export RUST_LOG=info -cargo run $(pwd)/configs/debug all echo "Try test valid proof at least once" cargo run $(pwd)/configs/debug test_success_private_transfer_to_another_owned_account echo "Continuing in dev mode" From 6274addad92a75511fd99a47b0db658274c151d0 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Mon, 13 Oct 2025 16:25:59 -0300 Subject: [PATCH 7/7] Remove unused Nonce alias --- common/src/transaction.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/common/src/transaction.rs b/common/src/transaction.rs index 3a2bda1..5332e5c 100644 --- a/common/src/transaction.rs +++ b/common/src/transaction.rs @@ -5,12 +5,6 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, digest::FixedOutput}; -use elliptic_curve::{ - consts::{B0, B1}, - generic_array::GenericArray, -}; -use sha2::digest::typenum::{UInt, UTerm}; - #[derive(Debug, Clone, PartialEq, Eq)] pub enum NSSATransaction { Public(nssa::PublicTransaction), @@ -32,7 +26,6 @@ impl From for NSSATransaction { use crate::TreeHashType; pub type CipherText = Vec; -pub type Nonce = GenericArray, B1>, B0>, B0>>; pub type Tag = u8; #[derive(