Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 23, 2024
1 parent fc57845 commit 1c5f67d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
6 changes: 1 addition & 5 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::protocol_types::{
address::{AztecAddress, EthAddress},
constants::{
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT,
GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, GENERATOR_INDEX__NOTE_SECRET_HASH
GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH
},
traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field}
};
Expand All @@ -11,10 +11,6 @@ pub fn compute_message_secret_hash(secret: Field) -> Field {
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
}

pub fn compute_note_secret_hash(secret: Field) -> Field {
pedersen_hash([secret], GENERATOR_INDEX__NOTE_SECRET_HASH)
}

pub fn compute_message_hash(
sender: EthAddress,
chain_id: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract Test {

use dep::aztec::{
context::{Context, inputs::private_context_inputs::PrivateContextInputs},
hash::{pedersen_hash, compute_note_secret_hash, ArgsHasher},
hash::{pedersen_hash, poseidon2_hash, ArgsHasher},
note::{
lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes},
note_getter_options::NoteStatus
Expand Down Expand Up @@ -350,7 +350,7 @@ contract Test {
// Adapted from TokenContract#redeem_shield but without an initcheck so it can be run in simulator/src/client/private_execution.test.ts
fn consume_note_from_secret(secret: Field) {
let notes_set = storage.example_set;
let secret_hash = compute_note_secret_hash(secret);
let secret_hash = poseidon2_hash([secret, 92543]); // global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;
let mut options = NoteGetterOptions::new();
options = options.select(FieldNote::properties().value, secret_hash, Option::none()).set_limit(1);
let notes = notes_set.get_notes(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ contract TokenBlacklist {

use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress};
use dep::aztec::{
note::note_getter_options::NoteGetterOptions,
note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader},
hash::compute_note_secret_hash,
state_vars::{Map, PublicMutable, PrivateSet, SharedMutable}
state_vars::{Map, PublicMutable, PrivateSet, SharedMutable, SharedImmutable}
};

use dep::field_note::field_note::FieldNote;
Expand Down Expand Up @@ -257,4 +257,4 @@ contract TokenBlacklist {
unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field {
storage.public_balances.at(owner).read().to_field()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use dep::aztec::{
};

global TRANSPARENT_NOTE_LEN: Field = 2;
// Defined here as it's not a protocol constant. Copied over to private execution test.
global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;

// Transparent note represents a note that is created in the clear (public execution), but can only be spent by those
// that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ contract Token {

use dep::compressed_string::FieldCompressedString;

use dep::aztec::prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress};
use dep::aztec::hash::compute_note_secret_hash;
use dep::aztec::{
hash::poseidon2_hash,
prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}
};

// docs:start:import_authwit
use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}};
// docs:end:import_authwit

use crate::types::{transparent_note::TransparentNote, token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap};
use crate::types::{
transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE},
token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap
};
// docs:end::imports

// docs:start:storage_struct
Expand Down Expand Up @@ -239,7 +244,7 @@ contract Token {
#[aztec(private)]
fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) {
let pending_shields = storage.pending_shields;
let secret_hash = compute_note_secret_hash(secret);
let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]);
// Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash
// stored in field with index 1 (select(1, secret_hash)).
let mut options = NoteGetterOptions::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use dep::aztec::{
};

global TRANSPARENT_NOTE_LEN: Field = 2;
// Defined here as it's not a protocol constant. Copied over to private execution test and test contract.
global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;

// Transparent note represents a note that is created in the clear (public execution), but can only be spent by those
// that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,3 @@ global GENERATOR_INDEX__OVSK_M = 49;
global GENERATOR_INDEX__TSK_M = 50;
global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51;
global GENERATOR_INDEX__NOTE_NULLIFIER = 52;
global GENERATOR_INDEX__NOTE_SECRET_HASH = 53;
10 changes: 5 additions & 5 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) {
}

/**
* Given a secret, it computes its pedersen hash - used to send l1 to l2 messages
* @param secret - the secret to hash - secret could be generated however you want e.g. `Fr.random()`
* @returns the hash
* Computes a hash of a secret as is done in the L1 to L2 message flow.
* @param secret - The secret to hash (could be generated however you want e.g. `Fr.random()`)
* @returns The hash
*/
export function computeMessageSecretHash(secretMessage: Fr) {
return pedersenHash([secretMessage], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET);
export function computeMessageSecretHash(secret: Fr) {
return pedersenHash([secret], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET);
}

export function computeL1ToL2MessageNullifier(
Expand Down
7 changes: 5 additions & 2 deletions yarn-project/simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
nonEmptySideEffects,
sideEffectArrayToValueArray,
} from '@aztec/circuits.js';
import { computeCommitmentNonce, computeMessageSecretHash, computeVarArgsHash } from '@aztec/circuits.js/hash';
import { computeCommitmentNonce, computeVarArgsHash } from '@aztec/circuits.js/hash';
import { makeHeader } from '@aztec/circuits.js/testing';
import {
type FunctionArtifact,
Expand Down Expand Up @@ -62,6 +62,9 @@ import { AcirSimulator } from './simulator.js';

jest.setTimeout(60_000);

// Copied over from `transparent_note.nr` - not placed in constants.nr as it's not a protocol constant
const GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;

describe('Private Execution test suite', () => {
let oracle: MockProxy<DBOracle>;
let node: MockProxy<AztecNode>;
Expand Down Expand Up @@ -727,7 +730,7 @@ describe('Private Execution test suite', () => {
it('Should be able to consume a dummy public to private message', async () => {
const artifact = getFunctionArtifact(TestContractArtifact, 'consume_note_from_secret');
const secret = new Fr(1n);
const secretHash = computeMessageSecretHash(secret);
const secretHash = poseidon2Hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]);
const note = new Note([secretHash]);
const storageSlot = new Fr(5);
oracle.getNotes.mockResolvedValue([
Expand Down

0 comments on commit 1c5f67d

Please sign in to comment.