moved examples to examples dir

This commit is contained in:
Sergio Chouhy 2025-07-16 17:25:03 -03:00
parent 8feb17db1a
commit d4a306d67a
5 changed files with 32 additions and 54 deletions

View File

@ -1,5 +1,7 @@
mod programs;
use outer_methods::{OUTER_ELF, OUTER_ID};
use rand::{rngs::OsRng, Rng};
use programs::TransferProgram;
use risc0_zkvm::{default_prover, ExecutorEnv, ProveInfo, Receipt};
use sparse_merkle_tree::SparseMerkleTree;
use toy_example_core::{
@ -9,14 +11,7 @@ use toy_example_core::{
types::{Address, AuthenticationPath, Commitment, Nonce, Nullifier},
};
use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};
use crate::program::{execute_and_prove, prove_privacy_execution, Program};
use crate::TransferProgram;
pub fn new_random_nonce() -> Nonce {
let mut rng = OsRng;
std::array::from_fn(|_| rng.gen())
}
use tuki::program::{prove_privacy_execution, Program};
fn mint_fresh_account(address: Address) -> Account {
let nonce = [0; 8];
@ -26,7 +21,7 @@ fn mint_fresh_account(address: Address) -> Account {
/// A private execution of the transfer function.
/// This actually "burns" a sender private account and "mints" two new private accounts:
/// one for the recipient with the transferred balance, and another owned by the sender with the remaining balance.
fn run_private_execution_of_transfer_program() {
fn main() {
// This is supposed to be an existing private account (UTXO) with balance equal to 150.
// And it is supposed to be a private account of the user running this private execution (hence the access to the private key)
let sender_private_key = [1, 2, 3, 4, 4, 3, 2, 1];
@ -74,13 +69,3 @@ fn run_private_execution_of_transfer_program() {
println!("nullifiers: {:?}", output.1);
println!("commitments: {:?}", output.2);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_private() {
run_private_execution_of_transfer_program();
}
}

View File

@ -0,0 +1,11 @@
use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};
use tuki::program::Program;
pub struct TransferProgram;
impl Program for TransferProgram {
const PROGRAM_ID: [u32; 8] = TRANSFER_ID;
const PROGRAM_ELF: &[u8] = TRANSFER_ELF;
// Amount to transfer
type InstructionData = u128;
}

View File

@ -1,16 +1,17 @@
mod programs;
use risc0_zkvm::{default_executor, ExecutorEnv};
use toy_example_core::account::Account;
use transfer_methods::TRANSFER_ELF;
use crate::{
program::{execute, Program},
TransferProgram,
};
use tuki::program::{execute, Program};
use crate::programs::TransferProgram;
/// A public execution.
/// This would be executed by the runtime after checking that
/// the initiating transaction includes the sender's signature.
pub fn run_public_execution_of_transfer_program() {
pub fn main() {
// Account fetched from the chain state with 150 in its balance.
let sender = {
let mut account = Account::new([5; 8], [98; 8]);
@ -38,13 +39,3 @@ pub fn run_public_execution_of_transfer_program() {
inputs_outputs[1], inputs_outputs[3],
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_public() {
run_public_execution_of_transfer_program();
}
}

View File

@ -1,15 +1,2 @@
mod private_execution;
mod program;
mod public_execution;
pub mod program;
use program::Program;
use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};
struct TransferProgram;
impl Program for TransferProgram {
const PROGRAM_ID: [u32; 8] = TRANSFER_ID;
const PROGRAM_ELF: &[u8] = TRANSFER_ELF;
// Amount to transfer
type InstructionData = u128;
}

View File

@ -1,13 +1,17 @@
use outer_methods::OUTER_ELF;
use rand::{rngs::OsRng, Rng};
use risc0_zkvm::{
default_executor, default_prover, ExecutorEnv, ExecutorEnvBuilder, ProveInfo, Receipt,
};
use serde::{Deserialize, Serialize};
use toy_example_core::{account::Account, input::InputVisibiility};
use toy_example_core::{account::Account, input::InputVisibiility, types::Nonce};
use crate::private_execution::new_random_nonce;
pub fn new_random_nonce() -> Nonce {
let mut rng = OsRng;
std::array::from_fn(|_| rng.gen())
}
pub(crate) trait Program {
pub trait Program {
const PROGRAM_ID: [u32; 8];
const PROGRAM_ELF: &[u8];
type InstructionData: Serialize + for<'de> Deserialize<'de>;
@ -45,7 +49,7 @@ pub(crate) fn execute_and_prove<P: Program>(
Ok((receipt, inputs_outputs))
}
pub(crate) fn execute<P: Program>(
pub fn execute<P: Program>(
input_accounts: &[Account],
instruction_data: &P::InstructionData,
) -> Result<Vec<Account>, ()> {
@ -64,7 +68,7 @@ pub(crate) fn execute<P: Program>(
Ok(inputs_outputs)
}
pub(crate) fn prove_privacy_execution<P: Program>(
pub fn prove_privacy_execution<P: Program>(
inputs: &[Account],
instruction_data: &P::InstructionData,
visibilities: &[InputVisibiility],