Skip to content

Commit

Permalink
fix getCommitment and insert_from_public andd public simulator test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Aug 4, 2023
1 parent fd58bfb commit 70ddc7e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ export class ClientTxExecutionContext {
* @returns The commitment data.
*/
public async getCommitment(contractAddress: AztecAddress, commitment: ACVMField) {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): only works
// for noteHashes/commitments created by public functions! Once public kernel or
// base rollup circuit injects nonces, this can be used with commitments created by
// private functions as well.
const commitmentInputs = await this.db.getCommitmentOracle(contractAddress, fromACVMField(commitment));
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1029): support pending commitments here
this.readRequestPartialWitnesses.push(ReadRequestMembershipWitness.empty(commitmentInputs.index));
Expand Down
6 changes: 4 additions & 2 deletions yarn-project/acir-simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,13 @@ describe('ACIR public execution simulator', () => {
// Assert the commitment was created
expect(result.newCommitments.length).toEqual(1);

const expectedNewCommitmentValue = pedersenPlookupCommitInputs(
const expectedNoteHash = pedersenPlookupCommitInputs(
wasm,
params.map(a => a.toBuffer()),
);
expect(result.newCommitments[0].toBuffer()).toEqual(expectedNewCommitmentValue);
const storageSlot = new Fr(2); // matches storage.nr
const expectedInnerNoteHash = pedersenPlookupCommitInputs(wasm, [storageSlot.toBuffer(), expectedNoteHash]);
expect(result.newCommitments[0].toBuffer()).toEqual(expectedInnerNoteHash);
});

it('Should be able to create a L2 to L1 message from the public context', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ contract NonNativeToken {
// Create a commitment to the "amount" using the "secretHash"
// and insert it into the set of "pending_shields" and therefore
// (eventually) the private data tree.
let note = TransparentNote::new(amount, secretHash);
pending_shields.insert_from_public(note);
let mut note = TransparentNote::new(amount, secretHash);
pending_shields.insert_from_public(inputs, &mut note);
}

// The shield function takes a public balance, and creates a commitment containing the amount of tokens
Expand Down
19 changes: 19 additions & 0 deletions yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::abi::PublicContextInputs;
use crate::context::Context;
use crate::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_inner_note_hash,
};
use crate::oracle::create_commitment::create_commitment;
use crate::oracle::notes::{notify_created_note, notify_nullified_note};
use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT;
use crate::types::option::Option;
Expand All @@ -29,6 +31,23 @@ fn create_note<Note, N>(
context.push_new_note_hash(inner_note_hash);
}

fn create_note_hash_from_public<Note, N>(
inputs: PublicContextInputs,
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
) {
let mut inner_note_hash = 0;
let contract_address = inputs.call_context.storage_contract_address;

let header = NoteHeader { contract_address, storage_slot, nonce: 0 };
let set_header = note_interface.set_header;
set_header(note, header);
inner_note_hash = compute_inner_note_hash(note_interface, *note);

create_commitment(inner_note_hash);
}

fn destroy_note<Note, N>(
context: &mut Context,
storage_slot: Field,
Expand Down
23 changes: 11 additions & 12 deletions yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,25 @@ fn ensure_note_hash_exists<Note, N>(
// Get a note from oracle and early out if it doesn't exist.
let inner_note_hash = compute_inner_note_hash(note_interface, *note);

let raw_oracle_ret = oracle::get_commitment::get_commitment(inner_note_hash);
let deserialized_oracle_ret = make_commitment_getter_data(raw_oracle_ret, 0);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be
// unique_siloed_note_hash once public kernel applies nonces
let saved_siloed_note_hash = deserialized_oracle_ret.message;

assert(saved_siloed_note_hash != 0); // TODO(dbanks12): necessary?

// Only copy over the header to the original note to make sure the preimage is the same.
//let get_header = note_interface.get_header;
// Initialize header of note. Must be done before siloing.
let set_header = note_interface.set_header;
let note_header = NoteHeader {
contract_address: context.inputs.call_context.storage_contract_address,
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be
// nonce, not leaf index (once public kernel applies nonces)
nonce: deserialized_oracle_ret.leaf_index,
// real nonce (once public kernel applies nonces).
nonce: 0,
storage_slot
};
set_header(note, note_header);

let raw_oracle_ret = oracle::get_commitment::get_commitment(inner_note_hash);
let deserialized_oracle_ret = make_commitment_getter_data(raw_oracle_ret, 0);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be
// unique_siloed_note_hash once public kernel applies nonces
let saved_siloed_note_hash = deserialized_oracle_ret.message;

assert(saved_siloed_note_hash != 0); // TODO(dbanks12): necessary?

check_note_header(*context, storage_slot, note_interface, *note);

// Ensure that the note hash retrieved from oracle matches the one computed from note.
Expand Down
9 changes: 4 additions & 5 deletions yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::abi::PublicContextInputs;
use crate::context::Context;
use crate::note::lifecycle::{create_note, destroy_note};
use crate::note::lifecycle::{create_note, create_note_hash_from_public, destroy_note};
use crate::note::{
note_getter::{get_notes, ensure_note_exists, ensure_note_hash_exists},
note_getter_options::NoteGetterOptions,
note_interface::NoteInterface,
utils::compute_inner_note_hash,
};
use crate::oracle::create_commitment::create_commitment;
use crate::types::option::Option;

struct Set<Note, N> {
Expand All @@ -23,9 +23,8 @@ impl<Note, N> Set<Note, N> {
create_note(context, self.storage_slot, note, self.note_interface);
}

fn insert_from_public(self, note: Note) {
let note_hash = compute_inner_note_hash(self.note_interface, note);
create_commitment(note_hash);
fn insert_from_public(self, inputs: PublicContextInputs, note: &mut Note) {
create_note_hash_from_public(inputs, self.storage_slot, note, self.note_interface);
}

fn assert_contains(self, context: &mut Context, note: &mut Note) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export class WorldStateDB implements CommitmentsDB {

public async getCommitmentOracle(address: AztecAddress, innerCommitment: Fr): Promise<CommitmentDataOracleInputs> {
const siloedCommitment = siloCommitment(await CircuitsWasm.get(), address, innerCommitment);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): shoild be
// unique commitment that exists in tree (should be siloed and then made unique via
// nonce). Once public kernel or base rollup circuit injects nonces, this can be updated
// to use uniqueSiloedCommitment.
const index = (await this.db.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, siloedCommitment.toBuffer()))!;
const siblingPath = await this.db.getSiblingPath(MerkleTreeId.PRIVATE_DATA_TREE, index);

Expand Down

0 comments on commit 70ddc7e

Please sign in to comment.