name types

This commit is contained in:
Sergio Chouhy 2025-07-15 09:26:34 -03:00
parent caa00a667f
commit 5c954d3d45
4 changed files with 18 additions and 16 deletions

View File

@ -5,17 +5,21 @@ use risc0_zkvm::{
use serde::{Deserialize, Serialize};
pub type Commitment = u32;
pub type Nullifier = [u32; 8];
pub type Address = [u32; 8];
pub type Nonce = [u32; 8];
pub type Key = [u32; 8];
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Account {
pub address: [u32; 8],
pub address: Address,
pub balance: u128,
pub nonce: [u32; 8],
pub nonce: Nonce,
}
impl Account {
/// Creates a new account with address = hash(private_key) and balance = 0
pub fn new_from_private_key(private_key: [u32; 8], nonce: [u32; 8]) -> Self {
pub fn new_from_private_key(private_key: Address, nonce: Nonce) -> Self {
let address = hash(&private_key);
Self {
address,
@ -24,7 +28,7 @@ impl Account {
}
}
pub fn new(address: [u32; 8], nonce: [u32; 8]) -> Self {
pub fn new(address: Address, nonce: Nonce) -> Self {
Self {
address,
balance: 0,
@ -48,7 +52,7 @@ pub fn is_in_commitment_tree(_commitment: Commitment, _tree_root: [u32; 8]) -> b
}
/// Returns Hash(Commitment || private_key)
pub fn compute_nullifier(commitment: &Commitment, private_key: &[u32; 8]) -> [u32; 8] {
pub fn compute_nullifier(commitment: &Commitment, private_key: &Key) -> Nullifier {
let mut bytes_to_hash = [0; 9]; // <- 1 word for the commitment, 8 words for the private key
bytes_to_hash[..1].copy_from_slice(&[*commitment]);
bytes_to_hash[1..].copy_from_slice(private_key);

View File

@ -1,12 +1,10 @@
use crate::account::Key;
use serde::{Deserialize, Serialize};
pub type PrivateKey = [u32; 8];
#[derive(Serialize, Deserialize)]
pub enum InputVisibiility {
// A public account
Public,
// A private account
Private(Option<PrivateKey>),
Private(Option<Key>),
}

View File

@ -1,6 +1,6 @@
use risc0_zkvm::{guest::env, serde::to_vec};
use toy_example_core::{
account::{compute_nullifier, hash, is_in_commitment_tree, Account},
account::{compute_nullifier, hash, is_in_commitment_tree, Account, Nonce},
input::InputVisibiility,
};
@ -30,7 +30,7 @@ fn main() {
assert_eq!(input_visibilities.len() as u32, num_inputs);
// Read nonces for outputs
let output_nonces: Vec<[u32; 8]> = env::read();
let output_nonces: Vec<Nonce> = env::read();
assert_eq!(output_nonces.len() as u32, num_inputs);
let commitment_tree_root: [u32; 8] = env::read();

View File

@ -2,19 +2,19 @@ use outer_methods::{OUTER_ELF, OUTER_ID};
use rand::{rngs::OsRng, Rng};
use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
use toy_example_core::{
account::{Account, Commitment},
account::{Account, Address, Commitment, Nonce, Nullifier},
input::InputVisibiility,
};
use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};
const COMMITMENT_TREE_ROOT: [u32; 8] = [0xdd, 0xee, 0xaa, 0xdd, 0xbb, 0xee, 0xee, 0xff];
pub fn new_random_nonce() -> [u32; 8] {
pub fn new_random_nonce() -> Nonce {
let mut rng = OsRng;
std::array::from_fn(|_| rng.gen())
}
fn mint_fresh_account(address: [u32; 8]) -> Account {
fn mint_fresh_account(address: Address) -> Account {
let nonce = new_random_nonce();
Account::new(address, nonce)
}
@ -25,7 +25,7 @@ fn mint_fresh_account(address: [u32; 8]) -> Account {
fn run_private_execution_of_transfer_program() {
// 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 = [0; 8];
let sender_private_key = [1, 2, 3, 4, 4, 3, 2, 1];
let sender = {
// Creating it now but it's supposed to be already created by other previous transactions.
let mut account = Account::new_from_private_key(sender_private_key, [1; 8]);
@ -75,7 +75,7 @@ fn run_private_execution_of_transfer_program() {
// Sanity check
receipt.verify(OUTER_ID).unwrap();
let output: (Vec<Account>, Vec<[u32; 8]>, Vec<Commitment>) = receipt.journal.decode().unwrap();
let output: (Vec<Account>, Vec<Nullifier>, Vec<Commitment>) = receipt.journal.decode().unwrap();
println!("public_outputs: {:?}", output.0);
println!("nullifiers: {:?}", output.1);
println!("commitments: {:?}", output.2);