Skip to content

Commit

Permalink
chore: Apply hash abstraction over aztec-nr (#2958)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Oct 23, 2023
1 parent 69c1d66 commit 52f01ae
Show file tree
Hide file tree
Showing 31 changed files with 118 additions and 104 deletions.
7 changes: 4 additions & 3 deletions yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use dep::aztec::{
get_secret_key::get_secret_key,
get_public_key::get_public_key,
},
hash::pedersen_hash,
context::PrivateContext,
};

Expand Down Expand Up @@ -40,11 +41,11 @@ impl AddressNote {
let siloed_note_hash = compute_siloed_note_hash(AddressNoteMethods, self);
let secret = get_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
dep::std::hash::pedersen([
pedersen_hash([
siloed_note_hash,
secret.low,
secret.high,
])[0]
],0)
}

pub fn set_header(&mut self, header: NoteHeader) {
Expand Down Expand Up @@ -80,7 +81,7 @@ fn serialize(note: AddressNote) -> [Field; ADDRESS_NOTE_LEN]{

fn compute_note_hash(note: AddressNote) -> Field {
// TODO(#1205) Should use a non-zero generator index.
dep::std::hash::pedersen(note.serialize())[0]
pedersen_hash(note.serialize(),0)
}

fn compute_nullifier(note: AddressNote) -> Field {
Expand Down
13 changes: 6 additions & 7 deletions yarn-project/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dep::std::hash::pedersen_with_separator;

use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
constants_gen::{EMPTY_NULLIFIED_COMMITMENT, GENERATOR_INDEX__SIGNATURE_PAYLOAD},
types::address::AztecAddress,
abi::hash_args,
hash::pedersen_hash,
};

global IS_VALID_SELECTOR = 0xe86ab4ff;
Expand All @@ -25,10 +24,10 @@ pub fn assert_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAdd
// Assert that `on_behalf_of` have authorized the current call with a valid authentication witness
pub fn assert_current_call_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAddress) {
// message_hash = H(caller, contract_this, selector, args_hash)
let message_hash = pedersen_with_separator(
let message_hash = pedersen_hash(
[context.msg_sender(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0];
);
assert_valid_authwit(context, on_behalf_of, message_hash);
}
// docs:end:assert_current_call_valid_authwit
Expand All @@ -46,10 +45,10 @@ pub fn assert_valid_authwit_public(context: &mut PublicContext, on_behalf_of: Az
// Assert that `on_behalf_of` have authorized the current call in a public context
pub fn assert_current_call_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress) {
// message_hash = H(caller, contract_this, selector, args_hash)
let message_hash = pedersen_with_separator(
let message_hash = pedersen_hash(
[context.msg_sender(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0];
);
assert_valid_authwit_public(context, on_behalf_of, message_hash);
}
// docs:end:assert_current_call_valid_authwit_public
Expand All @@ -63,6 +62,6 @@ pub fn compute_authwit_message_hash<N>(
args: [Field; N]
) -> Field {
let args_hash = hash_args(args);
pedersen_with_separator([caller.address, target.address, selector, args_hash], GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0]
pedersen_hash([caller.address, target.address, selector, args_hash], GENERATOR_INDEX__SIGNATURE_PAYLOAD)
}
// docs:end:compute_authwit_message_hash
7 changes: 3 additions & 4 deletions yarn-project/aztec-nr/authwit/src/entrypoint.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use dep::aztec::context::PrivateContext;
use dep::aztec::private_call_stack_item::PrivateCallStackItem;
use dep::aztec::public_call_stack_item::PublicCallStackItem;
use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD;

use dep::std::hash;
use dep::aztec::hash::pedersen_hash;

global ACCOUNT_MAX_CALLS: Field = 4;
// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC)
Expand Down Expand Up @@ -52,10 +51,10 @@ struct EntrypointPayload {

impl EntrypointPayload {
fn hash(self) -> Field {
hash::pedersen_with_separator(
pedersen_hash(
self.serialize(),
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0]
)
}

// Serializes the entrypoint struct
Expand Down
22 changes: 11 additions & 11 deletions yarn-project/aztec-nr/aztec/src/abi.nr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::constants_gen::{
use crate::oracle::debug_log;
use crate::types::vec::BoundedVec;
use crate::types::point::Point;

use crate::hash::pedersen_hash;

// docs:start:private-global-variables
struct PrivateGlobalVariables {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl ContractDeploymentData {
}

fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA)[0]
pedersen_hash(self.serialize(), GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ impl CallContext {
}

fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)[0]
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)
}
}

Expand Down Expand Up @@ -180,12 +180,12 @@ struct FunctionData {

impl FunctionData {
fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator([
pedersen_hash([
self.function_selector,
self.is_internal as Field,
self.is_private as Field,
self.is_constructor as Field,
], GENERATOR_INDEX__FUNCTION_DATA)[0]
], GENERATOR_INDEX__FUNCTION_DATA)
}
}

Expand Down Expand Up @@ -236,7 +236,7 @@ impl PrivateCircuitPublicInputs {
fields.push(self.chain_id);
fields.push(self.version);

dep::std::hash::pedersen_with_separator(fields.storage, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS)[0]
pedersen_hash(fields.storage, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS)
}

fn serialize(self) -> [Field; PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH] {
Expand Down Expand Up @@ -274,7 +274,7 @@ impl ContractStorageRead {
}

pub fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)[0]
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)
}

pub fn empty() -> Self {
Expand All @@ -294,7 +294,7 @@ impl ContractStorageUpdateRequest {
}

pub fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)[0]
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)
}

pub fn empty() -> Self {
Expand Down Expand Up @@ -342,7 +342,7 @@ impl PublicCircuitPublicInputs {
inputs.push_array(self.block_data.serialize());
inputs.push(self.prover_address);

dep::std::hash::pedersen_with_separator(inputs.storage, GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)[0]
pedersen_hash(inputs.storage, GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)
}

pub fn serialize(self) -> [Field; PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH] {
Expand Down Expand Up @@ -411,10 +411,10 @@ pub fn hash_args<N>(args: [Field; N]) -> Field {
chunk_args[j] = args[item_index];
}
}
chunk_hash = dep::std::hash::pedersen_with_separator(chunk_args, GENERATOR_INDEX__FUNCTION_ARGS)[0];
chunk_hash = pedersen_hash(chunk_args, GENERATOR_INDEX__FUNCTION_ARGS);
}
chunks_hashes[i] = chunk_hash;
}
dep::std::hash::pedersen_with_separator(chunks_hashes, GENERATOR_INDEX__FUNCTION_ARGS)[0]
pedersen_hash(chunks_hashes, GENERATOR_INDEX__FUNCTION_ARGS)
}
}
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/address.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::std::hash;
use crate::constants_gen::GENERATOR_INDEX__CONTRACT_ADDRESS;
use crate::hash::pedersen_hash;

pub fn compute_address(pub_key_x: Field, pub_key_y: Field, partial_address: Field) -> Field {
hash::pedersen_with_separator([pub_key_x, pub_key_y, partial_address], GENERATOR_INDEX__CONTRACT_ADDRESS)[0]
pedersen_hash([pub_key_x, pub_key_y, partial_address], GENERATOR_INDEX__CONTRACT_ADDRESS)
}
6 changes: 5 additions & 1 deletion yarn-project/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ pub fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field {

pub fn compute_secret_hash(secret: Field) -> Field {
// TODO(#1205) This is probably not the right index to use
pedersen_with_separator([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)[0]
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
}

pub fn pedersen_hash<N>(inputs : [Field;N], hash_index : u32) -> Field {
pedersen_with_separator(inputs, hash_index)[0]
}
6 changes: 3 additions & 3 deletions yarn-project/aztec-nr/aztec/src/messaging/l1_to_l2_message.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::constants_gen::{
GENERATOR_INDEX__NULLIFIER,
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
};
use crate::hash::{sha256_to_field};
use crate::hash::{sha256_to_field, pedersen_hash};

struct L1ToL2Message {
sender: Field,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl L1ToL2Message {
}

pub fn validate_message_secret(self: Self) {
let recomputed_hash = dep::std::hash::pedersen_with_separator([self.secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)[0];
let recomputed_hash = pedersen_hash([self.secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET);
assert(self.secret_hash == recomputed_hash);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ impl L1ToL2Message {
// docs:start:l1_to_l2_message_compute_nullifier
pub fn compute_nullifier(self: Self) -> Field {
let message_hash = self.message_hash();
dep::std::hash::pedersen_with_separator([message_hash, self.secret, self.tree_index], GENERATOR_INDEX__NULLIFIER)[0]
pedersen_hash([message_hash, self.secret, self.tree_index], GENERATOR_INDEX__NULLIFIER)
}
// docs:end:l1_to_l2_message_compute_nullifier
}
8 changes: 4 additions & 4 deletions yarn-project/aztec-nr/aztec/src/note/note_hash.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use dep::std::hash::{pedersen, pedersen_with_separator};
use crate::hash::pedersen_hash;
use crate::constants_gen::{GENERATOR_INDEX__UNIQUE_COMMITMENT, GENERATOR_INDEX__SILOED_COMMITMENT};

pub fn compute_inner_hash(storage_slot: Field, note_hash: Field) -> Field {
// TODO(#1205) Do we need a generator index here?
pedersen([storage_slot, note_hash])[0]
pedersen_hash([storage_slot, note_hash],0)
}

pub fn compute_siloed_hash(contract_address: Field, inner_note_hash: Field) -> Field {
let inputs = [contract_address, inner_note_hash];
pedersen_with_separator(inputs, GENERATOR_INDEX__SILOED_COMMITMENT)[0]
pedersen_hash(inputs, GENERATOR_INDEX__SILOED_COMMITMENT)
}

pub fn compute_unique_hash(nonce: Field, siloed_note_hash: Field) -> Field {
let inputs = [nonce, siloed_note_hash];
pedersen_with_separator(inputs, GENERATOR_INDEX__UNIQUE_COMMITMENT)[0]
pedersen_hash(inputs, GENERATOR_INDEX__UNIQUE_COMMITMENT)
}
5 changes: 3 additions & 2 deletions yarn-project/aztec-nr/aztec/src/private_call_stack_item.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::abi::FunctionData;
use crate::abi::PrivateCircuitPublicInputs;
use crate::constants_gen::GENERATOR_INDEX__CALL_STACK_ITEM;
use crate::hash::pedersen_hash;

struct PrivateCallStackItem {
contract_address: Field,
Expand All @@ -11,10 +12,10 @@ struct PrivateCallStackItem {

impl PrivateCallStackItem {
pub fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator([
pedersen_hash([
self.contract_address,
self.function_data.hash(),
self.public_inputs.hash(),
], GENERATOR_INDEX__CALL_STACK_ITEM)[0]
], GENERATOR_INDEX__CALL_STACK_ITEM)
}
}
5 changes: 3 additions & 2 deletions yarn-project/aztec-nr/aztec/src/public_call_stack_item.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
abi,
hash::pedersen_hash,
abi::{
PublicCircuitPublicInputs,
FunctionData,
Expand All @@ -24,11 +25,11 @@ struct PublicCallStackItem {

impl PublicCallStackItem {
pub fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator([
pedersen_hash([
self.contract_address,
self.function_data.hash(),
self.public_inputs.hash(),
], GENERATOR_INDEX__CALL_STACK_ITEM)[0]
], GENERATOR_INDEX__CALL_STACK_ITEM)
}
}

3 changes: 2 additions & 1 deletion yarn-project/aztec-nr/aztec/src/state_vars/map.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::context::{PrivateContext, PublicContext, Context};
use dep::std::option::Option;
use crate::hash::pedersen_hash;

// docs:start:map
struct Map<V> {
Expand Down Expand Up @@ -28,7 +29,7 @@ impl<V> Map<V> {
// docs:start:at
pub fn at(self, key: Field) -> V {
// TODO(#1204): use a generator index for the storage slot
let derived_storage_slot = dep::std::hash::pedersen([self.storage_slot, key])[0];
let derived_storage_slot = pedersen_hash([self.storage_slot, key],0);

let state_var_constructor = self.state_var_constructor;
state_var_constructor(self.context, derived_storage_slot)
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ use crate::oracle::{
get_secret_key::get_secret_key,
notes::check_nullifier_exists,
};
use dep::std::hash::pedersen_with_separator;
use crate::hash::pedersen_hash;

pub fn compute_singleton_initialization_nullifier(storage_slot: Field, owner: Option<Field>) -> Field {
if owner.is_some() {
let secret = get_secret_key(owner.unwrap_unchecked());
pedersen_with_separator(
pedersen_hash(
[storage_slot, secret.low, secret.high],
GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
)[0]
)
} else {
pedersen_with_separator(
pedersen_hash(
[storage_slot],
GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
)[0]
)
}
}

Expand Down
9 changes: 5 additions & 4 deletions yarn-project/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use dep::aztec::{
get_public_key::get_public_key,
},
log::emit_encrypted_log,
hash::pedersen_hash,
context::PrivateContext,
};

Expand Down Expand Up @@ -51,11 +52,11 @@ impl ValueNote {

pub fn compute_note_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
dep::std::hash::pedersen([
pedersen_hash([
self.value,
self.owner,
self.randomness,
])[0]
],0)
}

// docs:start:nullifier
Expand All @@ -64,11 +65,11 @@ impl ValueNote {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(ValueNoteMethods, self);
let secret = get_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
dep::std::hash::pedersen([
pedersen_hash([
note_hash_for_nullify,
secret.low,
secret.high,
])[0]
],0)
}

// docs:end:nullifier
Expand Down
Loading

0 comments on commit 52f01ae

Please sign in to comment.