move code to account.rs

This commit is contained in:
Sergio Chouhy 2025-07-14 16:47:43 -03:00
parent 2175fcd497
commit 0431b640f0
6 changed files with 54 additions and 54 deletions

View File

@ -0,0 +1,47 @@
#![cfg_attr(not(test), no_std)]
use serde::{Serialize, Deserialize};
use risc0_zkvm::{sha::{Impl, Sha256}, serde::to_vec};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Account {
pub address: [u32; 8],
pub balance: u128,
pub nonce: [u32; 8]
}
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 {
let address = hash(&private_key);
Self { address, balance: 0, nonce }
}
pub fn new(address: [u32; 8], nonce: [u32; 8]) -> Self {
Self { address, balance: 0, nonce }
}
/// Returns Hash(Account)
pub fn commitment(&self) -> [u32; 8] {
hash(&to_vec(&self).unwrap())
}
}
pub fn hash(bytes: &[u32]) -> [u32; 8] {
Impl::hash_words(&bytes).as_words().try_into().unwrap()
}
pub fn is_in_commitment_tree(_commitment: [u32; 8], _tree_root: [u32; 8]) -> bool {
// Dummy implementation
true
}
/// Returns Hash(Commitment || private_key)
pub fn compute_nullifier(commitment: [u32; 8], private_key: [u32; 8]) -> [u32; 8] {
let mut bytes_to_hash = [0; 16];
bytes_to_hash[..8].copy_from_slice(&commitment);
bytes_to_hash[8..].copy_from_slice(&private_key);
hash(&bytes_to_hash)
}

View File

@ -1,48 +1 @@
#![cfg_attr(not(test), no_std)]
use serde::{Serialize, Deserialize};
use risc0_zkvm::{sha::{Impl, Sha256}, serde::to_vec};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Account {
pub address: [u32; 8],
pub balance: u128,
pub nonce: [u32; 8]
}
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 {
let address = hash(&private_key);
Self { address, balance: 0, nonce }
}
pub fn new(address: [u32; 8], nonce: [u32; 8]) -> Self {
Self { address, balance: 0, nonce }
}
/// Returns Hash(Account)
pub fn commitment(&self) -> [u32; 8] {
hash(&to_vec(&self).unwrap())
}
}
pub fn hash(bytes: &[u32]) -> [u32; 8] {
Impl::hash_words(&bytes).as_words().try_into().unwrap()
}
pub fn is_in_commitment_tree(_commitment: [u32; 8], _tree_root: [u32; 8]) -> bool {
// Dummy implementation
true
}
/// Returns Hash(Commitment || private_key)
pub fn compute_nullifier(commitment: [u32; 8], private_key: [u32; 8]) -> [u32; 8] {
let mut bytes_to_hash = [0; 16];
bytes_to_hash[..8].copy_from_slice(&commitment);
bytes_to_hash[8..].copy_from_slice(&private_key);
hash(&bytes_to_hash)
}
pub mod account;

View File

@ -1,5 +1,5 @@
use risc0_zkvm::{guest::env, sha::{Impl, Sha256}, serde::to_vec};
use toy_example_core::{Account, hash, compute_nullifier, is_in_commitment_tree};
use toy_example_core::account::{Account, hash, compute_nullifier, is_in_commitment_tree};
/// Private execution logic.
/// Circuit for proving correct execution of some program with program id

View File

@ -5,7 +5,7 @@ use outer_methods::{
OUTER_ELF, OUTER_ID
};
use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
use toy_example_core::Account;
use toy_example_core::account::Account;
/// A private execution of the transfer function.
/// This actually "burns" a sender private account and "mints" two new private accounts:
@ -97,4 +97,4 @@ mod tests {
fn test_private() {
run_private_execution_of_transfer_program();
}
}
}

View File

@ -1,5 +1,5 @@
use risc0_zkvm::{default_executor, ExecutorEnv};
use toy_example_core::Account;
use toy_example_core::account::Account;
use transfer_methods::TRANSFER_ELF;
/// A public execution.
@ -44,4 +44,4 @@ mod tests {
fn test_public() {
run_public_execution_of_transfer_program();
}
}
}

View File

@ -1,5 +1,5 @@
use risc0_zkvm::{guest::env, sha::{Impl, Sha256}, serde::to_vec};
use toy_example_core::Account;
use toy_example_core::account::Account;
/// A transfer of balance program.