mirror of
https://github.com/logos-blockchain/lssa-zkvm-testing.git
synced 2026-01-02 13:23:08 +00:00
refactor
This commit is contained in:
parent
0b908c842a
commit
c322f93775
@ -1,16 +1,10 @@
|
||||
use risc0_zkvm::{
|
||||
serde::to_vec,
|
||||
sha::{Impl, Sha256},
|
||||
use crate::{
|
||||
hash,
|
||||
types::{Address, Commitment, Nonce},
|
||||
};
|
||||
use risc0_zkvm::{serde::to_vec, sha::Impl};
|
||||
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];
|
||||
pub type AuthenticationPath = [[u32; 8]; 32];
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Account {
|
||||
pub address: Address,
|
||||
@ -22,11 +16,7 @@ impl Account {
|
||||
/// Creates a new account with address = hash(private_key) and balance = 0
|
||||
pub fn new_from_private_key(private_key: Address, nonce: Nonce) -> Self {
|
||||
let address = hash(&private_key);
|
||||
Self {
|
||||
address,
|
||||
balance: 0,
|
||||
nonce,
|
||||
}
|
||||
Self::new(address, nonce)
|
||||
}
|
||||
|
||||
pub fn new(address: Address, nonce: Nonce) -> Self {
|
||||
@ -42,49 +32,3 @@ impl Account {
|
||||
hash(&to_vec(&self).unwrap())[0]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash(bytes: &[u32]) -> [u32; 8] {
|
||||
Impl::hash_words(bytes).as_words().try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn is_in_tree(commitment: Commitment, path: &AuthenticationPath, root: [u32; 8]) -> bool {
|
||||
const HASH_ONE: [u32; 8] = [
|
||||
789771595, 3310634292, 3140410939, 3820475020, 3591004369, 2777006897, 1021496535,
|
||||
2588247415,
|
||||
];
|
||||
|
||||
let mut hash = HASH_ONE;
|
||||
let mut current_index = commitment;
|
||||
for path_value in path.iter() {
|
||||
if current_index & 1 == 0 {
|
||||
hash = hash_two(&hash, path_value);
|
||||
} else {
|
||||
hash = hash_two(path_value, &hash);
|
||||
}
|
||||
current_index >>= 1;
|
||||
}
|
||||
root == hash
|
||||
}
|
||||
|
||||
/// Returns Hash(Commitment || private_key)
|
||||
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);
|
||||
hash(&bytes_to_hash)
|
||||
}
|
||||
|
||||
fn hash_two(left: &[u32; 8], right: &[u32; 8]) -> [u32; 8] {
|
||||
let mut bytes_to_hash = [0; 16];
|
||||
bytes_to_hash[..8].copy_from_slice(left);
|
||||
bytes_to_hash[8..].copy_from_slice(right);
|
||||
hash(&bytes_to_hash)
|
||||
}
|
||||
|
||||
pub fn bytes_to_words(bytes: &[u8; 32]) -> [u32; 8] {
|
||||
let mut words = [0; 8];
|
||||
for (i, chunk) in bytes.chunks(4).enumerate() {
|
||||
words[i] = u32::from_le_bytes(chunk.try_into().unwrap());
|
||||
}
|
||||
words
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::account::{AuthenticationPath, Key};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::types::{AuthenticationPath, Key};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum InputVisibiility {
|
||||
|
||||
@ -1,5 +1,53 @@
|
||||
pub mod account;
|
||||
pub mod input;
|
||||
pub mod types;
|
||||
|
||||
use crate::types::{AuthenticationPath, Commitment, Key, Nullifier};
|
||||
use risc0_zkvm::sha::{Impl, Sha256};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub fn hash(bytes: &[u32]) -> [u32; 8] {
|
||||
Impl::hash_words(bytes).as_words().try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn is_in_tree(commitment: Commitment, path: &AuthenticationPath, root: [u32; 8]) -> bool {
|
||||
const HASH_ONE: [u32; 8] = [
|
||||
789771595, 3310634292, 3140410939, 3820475020, 3591004369, 2777006897, 1021496535,
|
||||
2588247415,
|
||||
];
|
||||
|
||||
let mut hash = HASH_ONE;
|
||||
let mut current_index = commitment;
|
||||
for path_value in path.iter() {
|
||||
if current_index & 1 == 0 {
|
||||
hash = hash_two(&hash, path_value);
|
||||
} else {
|
||||
hash = hash_two(path_value, &hash);
|
||||
}
|
||||
current_index >>= 1;
|
||||
}
|
||||
root == hash
|
||||
}
|
||||
|
||||
/// Returns Hash(Commitment || private_key)
|
||||
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);
|
||||
hash(&bytes_to_hash)
|
||||
}
|
||||
|
||||
fn hash_two(left: &[u32; 8], right: &[u32; 8]) -> [u32; 8] {
|
||||
let mut bytes_to_hash = [0; 16];
|
||||
bytes_to_hash[..8].copy_from_slice(left);
|
||||
bytes_to_hash[8..].copy_from_slice(right);
|
||||
hash(&bytes_to_hash)
|
||||
}
|
||||
|
||||
pub fn bytes_to_words(bytes: &[u8; 32]) -> [u32; 8] {
|
||||
let mut words = [0; 8];
|
||||
for (i, chunk) in bytes.chunks(4).enumerate() {
|
||||
words[i] = u32::from_le_bytes(chunk.try_into().unwrap());
|
||||
}
|
||||
words
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use risc0_zkvm::{guest::env, serde::to_vec};
|
||||
use toy_example_core::{
|
||||
account::{compute_nullifier, hash, is_in_tree, Account, Nonce},
|
||||
input::InputVisibiility,
|
||||
account::Account, compute_nullifier, hash, input::InputVisibiility, is_in_tree, types::Nonce,
|
||||
};
|
||||
|
||||
/// Private execution logic.
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
mod default_hashes;
|
||||
|
||||
use default_hashes::DEFAULT_HASHES;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
mod default_hashes;
|
||||
use default_hashes::DEFAULT_HASHES;
|
||||
|
||||
const TREE_DEPTH: usize = 32;
|
||||
const ZERO_HASH: [u8; 32] = [
|
||||
110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63,
|
||||
|
||||
@ -3,8 +3,10 @@ use rand::{rngs::OsRng, Rng};
|
||||
use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
|
||||
use sparse_merkle_tree::SparseMerkleTree;
|
||||
use toy_example_core::{
|
||||
account::{bytes_to_words, Account, Address, AuthenticationPath, Commitment, Nonce, Nullifier},
|
||||
account::Account,
|
||||
bytes_to_words,
|
||||
input::InputVisibiility,
|
||||
types::{Address, AuthenticationPath, Commitment, Nonce, Nullifier},
|
||||
};
|
||||
use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user