Skip to content

Commit

Permalink
chore: update pedersen 💀
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Oct 31, 2023
1 parent d61721f commit eb15a49
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
1 change: 1 addition & 0 deletions l1-contracts/lib/allo-v2
Submodule allo-v2 added at d41643
27 changes: 22 additions & 5 deletions yarn-project/aztec-nr/slow-updates-tree/src/slow_map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::types::type_serialization::TypeSerializationInterface;
use dep::aztec::oracle::storage::{storage_read, storage_write};
use dep::aztec::oracle::debug_log::debug_log_format;

use dep::std::option::Option;
use dep::std::hash::pedersen_hash;

use dep::std::merkle::compute_merkle_root;
use dep::std::option::Option;

// The epoch lenght is just a random number for now.
// The epoch length is just a random number for now.
global EPOCH_LENGTH: u120 = 100;

fn compute_next_change(time: Field) -> Field {
Expand Down Expand Up @@ -49,7 +50,7 @@ struct SlowUpdateInner<N> {
}

// The slow update proof. Containing two merkle paths
// One for the the before and one for the after trees.
// One for the before and one for the after trees.
// M = 2 * N + 4
struct SlowUpdateProof<N, M> {
index: Field,
Expand Down Expand Up @@ -208,7 +209,7 @@ impl<N,M> SlowMap<N,M> {
}

// A variation of `update_at` that skips the merkle-membership checks.
// To be used by a contract wich has already checked the merkle-membership.
// To be used by a contract which has already checked the merkle-membership.
// This allows us to check the merkle-memberships in private and then update
// in public, limiting the cost of the update.
pub fn update_unsafe_at(self: Self, index: Field, leaf_value: Field, new_root: Field) {
Expand Down Expand Up @@ -251,3 +252,19 @@ impl<N,M> SlowMap<N,M> {
}

}

pub fn compute_merkle_root<N>(leaf: Field, index: Field, hash_path: [Field; N]) -> Field {
let n = hash_path.len();
let index_bits = index.to_le_bits(n as u32);
let mut current = leaf;
for i in 0..n {
let path_bit = index_bits[i] as bool;
let (hash_left, hash_right) = if path_bit {
(hash_path[i], current)
} else {
(current, hash_path[i])
};
current = pedersen_hash([hash_left, hash_right]);
};
current
}
36 changes: 4 additions & 32 deletions yarn-project/end-to-end/src/e2e_slow_tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { CheatCodes, Fr, Wallet } from '@aztec/aztec.js';
import { CheatCodes, Fr, Wallet, sleep } from '@aztec/aztec.js';
import { CircuitsWasm } from '@aztec/circuits.js';
import { pedersenPlookupCommitInputs } from '@aztec/circuits.js/barretenberg';
import { DebugLogger } from '@aztec/foundation/log';
import { SparseTree, newTree } from '@aztec/merkle-tree';
import { Pedersen, SparseTree, newTree } from '@aztec/merkle-tree';
import { SlowTreeContract } from '@aztec/noir-contracts/types';
import { Hasher } from '@aztec/types';

import { default as levelup } from 'levelup';
import { type MemDown, default as memdown } from 'memdown';
Expand All @@ -28,35 +26,9 @@ describe('e2e_slow_tree', () => {

afterAll(() => teardown());

/**
* Pedersen hasher for the slow tree to match noir hashing.
*/
class PedersenHasher implements Hasher {
private readonly circuitsWasm: CircuitsWasm;

constructor(circuitsWasm: CircuitsWasm) {
this.circuitsWasm = circuitsWasm;
}
compressInputs(_inputs: Buffer[]): Buffer {
throw new Error('Method not implemented.');
}
hashToField(_data: Uint8Array): Buffer {
throw new Error('Method not implemented.');
}
hashToTree(_leaves: Buffer[]): Promise<Buffer[]> {
throw new Error('Method not implemented.');
}

public compress(lhs: Buffer, rhs: Buffer): Buffer {
return pedersenPlookupCommitInputs(this.circuitsWasm, [lhs, rhs]);
}
}

it('Messing around with noir slow tree', async () => {
const circuitsWasm = await CircuitsWasm.get();
const hasher = new PedersenHasher(circuitsWasm);

const db = levelup(createMemDown());
const hasher = new Pedersen(await CircuitsWasm.get());
const depth = 254;
const tree = await newTree(SparseTree, db, hasher, 'test', depth);
const root = tree.getRoot(true);
Expand Down Expand Up @@ -106,7 +78,7 @@ describe('e2e_slow_tree', () => {

logger(`Updating tree[${key}] to 1 from public`);
await contract.methods
.update_at(await getUpdateProof(1n, key))
.update_at_public(await getUpdateProof(1n, key))
.send()
.wait();
await tree.updateLeaf(new Fr(1).toBuffer(), key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// This is made as a separate contract for one thing mainly. Making it a simpler to use.
contract SlowTree {
use dep::std::option::Option;
use dep::std::merkle::compute_merkle_root;
use dep::value_note::{
balance_utils,
utils::{increment, decrement},
Expand All @@ -23,11 +22,11 @@ contract SlowTree {
},
};
use dep::slow_updates_tree::slow_map::{
SlowMap, Leaf, SlowUpdateProof
SlowMap, Leaf, SlowUpdateProof, compute_merkle_root
};

global TREE_HEIGHT: Field = 254;
global MEMBERSHIP_SIZE: Field = 256; // TREE_HEIGTH + 2
global MEMBERSHIP_SIZE: Field = 256; // TREE_HEIGHT + 2
global UPDATE_SIZE: Field = 512; // TREE_HEIGHT * 2 + 4

struct Storage {
Expand Down Expand Up @@ -122,7 +121,7 @@ contract SlowTree {
}

#[aztec(public)]
fn update_at(p: SlowUpdateProof<TREE_HEIGHT, UPDATE_SIZE>) {
fn update_at_public(p: SlowUpdateProof<TREE_HEIGHT, UPDATE_SIZE>) {
storage.trees.at(context.msg_sender()).update_at(p);
}

Expand Down

0 comments on commit eb15a49

Please sign in to comment.