Skip to content

Commit

Permalink
add trait for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Yithis committed Feb 19, 2024
1 parent 82b8021 commit 9746310
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
13 changes: 6 additions & 7 deletions relations/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use halo2_base::{gates::GateChip, utils::BigPrimeField, AssignedValue, Context};

use crate::operation::{CircuitOperation, Operation};
use crate::{
operation::{CircuitOperation, Operation},
CloneToVec,
};

pub trait Account<F: BigPrimeField> {
pub trait Account<F: BigPrimeField>: CloneToVec<F> {
type CircuitAccount: CircuitAccount<F>;
type Op: Operation<F>;

fn update(&self, op: &Self::Op) -> Self;

fn to_array(&self) -> Vec<F>;

fn load(&self, ctx: &mut Context<F>) -> Self::CircuitAccount;
}

pub trait CircuitAccount<F: BigPrimeField> {
pub trait CircuitAccount<F: BigPrimeField>: CloneToVec<AssignedValue<F>> {
type Op: CircuitOperation<F>;

fn update(&self, op: Self::Op, ctx: &mut Context<F>, gate: &GateChip<F>) -> Self;

fn to_array(&self) -> Vec<AssignedValue<F>>;
}
4 changes: 4 additions & 0 deletions relations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub enum Token {
USDT,
}

pub trait CloneToVec<T> {
fn clone_to_vec(&self) -> Vec<T>;
}

pub mod poseidon_consts {
/// Has to be greater than 1 and equal to RATE + 1, due to the outer Poseidon implementation.
pub const T_WIDTH: usize = RATE + 1;
Expand Down
18 changes: 11 additions & 7 deletions relations/src/note.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use halo2_base::{utils::ScalarField, AssignedValue, Context};

use crate::CloneToVec;

#[derive(Clone, Copy, Debug)]
pub struct Note<F: ScalarField> {
pub zk_id: F,
Expand All @@ -18,10 +20,6 @@ impl<F: ScalarField> Note<F> {
}
}

pub fn to_array(&self) -> [F; 4] {
[self.zk_id, self.trapdoor, self.nullifier, self.account_hash]
}

pub fn load(&self, ctx: &mut Context<F>) -> CircuitNote<F> {
CircuitNote {
zk_id: ctx.load_witness(self.zk_id),
Expand All @@ -32,6 +30,12 @@ impl<F: ScalarField> Note<F> {
}
}

impl<F: ScalarField> CloneToVec<F> for Note<F> {
fn clone_to_vec(&self) -> Vec<F> {
vec![self.zk_id, self.trapdoor, self.nullifier, self.account_hash]
}
}

#[derive(Clone, Copy, Debug)]
pub struct CircuitNote<F: ScalarField> {
pub zk_id: AssignedValue<F>,
Expand All @@ -40,8 +44,8 @@ pub struct CircuitNote<F: ScalarField> {
pub account_hash: AssignedValue<F>,
}

impl<F: ScalarField> CircuitNote<F> {
pub fn to_array(&self) -> [AssignedValue<F>; 4] {
[self.zk_id, self.trapdoor, self.nullifier, self.account_hash]
impl<F: ScalarField> CloneToVec<AssignedValue<F>> for CircuitNote<F> {
fn clone_to_vec(&self) -> Vec<AssignedValue<F>> {
vec![self.zk_id, self.trapdoor, self.nullifier, self.account_hash]
}
}
2 changes: 1 addition & 1 deletion relations/src/relations/update_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn verify_account_circuit<F, A>(
F: BigPrimeField,
A: CircuitAccount<F>,
{
let inner_account_hash = poseidon.hash_fix_len_array(ctx, gate, &account.to_array());
let inner_account_hash = poseidon.hash_fix_len_array(ctx, gate, &account.clone_to_vec());
let eq = gate.is_equal(ctx, account_hash, inner_account_hash);
gate.assert_is_const(ctx, &eq, &F::ONE);
}
Expand Down
5 changes: 3 additions & 2 deletions relations/src/relations/update_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
note::{CircuitNote, Note},
operation::{CircuitOperation, Operation},
poseidon_consts::{RATE, R_F, R_P, T_WIDTH},
CloneToVec,
};

type OpFor<A, F> = <<A as Account<F>>::CircuitAccount as CircuitAccount<F>>::Op;
Expand Down Expand Up @@ -96,7 +97,7 @@ fn verify_note_circuit<F>(
) where
F: BigPrimeField,
{
let inner_note_hash = poseidon.hash_fix_len_array(ctx, gate, &note.to_array());
let inner_note_hash = poseidon.hash_fix_len_array(ctx, gate, &note.clone_to_vec());
let eq = gate.is_equal(ctx, note_hash, inner_note_hash);
gate.assert_is_const(ctx, &eq, &F::ONE);
}
Expand Down Expand Up @@ -127,7 +128,7 @@ pub fn update_note_circuit<F, A, const TREE_HEIGHT: usize>(

verify_note_circuit(ctx, &gate, &mut poseidon, &input.new_note, new_note_hash);

let old_note_hash = poseidon.hash_fix_len_array(ctx, &gate, &input.old_note.to_array());
let old_note_hash = poseidon.hash_fix_len_array(ctx, &gate, &input.old_note.clone_to_vec());

let merkle_proof = input.merkle_proof;

Expand Down

0 comments on commit 9746310

Please sign in to comment.