Skip to content

Commit d977780

Browse files
committed
refactor: pedersen hash related cleanup in aztec.nr
1 parent d018335 commit d977780

File tree

23 files changed

+42
-52
lines changed

23 files changed

+42
-52
lines changed

l1-contracts/src/core/libraries/ConstantsGen.sol

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ library Constants {
9797
uint256 internal constant ARGS_HASH_CHUNK_COUNT = 16;
9898
uint256 internal constant MAX_ARGS_LENGTH = 256;
9999
uint256 internal constant INITIALIZATION_SLOT_SEPARATOR = 1000000000;
100+
uint256 internal constant MAP_STORAGE_SLOT_SEPARATOR = 1000000001;
100101
uint256 internal constant INITIAL_L2_BLOCK_NUM = 1;
101102
uint256 internal constant BLOB_SIZE_IN_BYTES = 126976;
102103
uint256 internal constant ETHEREUM_SLOT_DURATION = 12;

noir-projects/aztec-nr/authwit/src/account.nr

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use dep::aztec::context::{PrivateContext, PublicContext};
2-
use dep::aztec::protocol_types::{address::AztecAddress, abis::function_selector::FunctionSelector, hash::pedersen_hash};
1+
use dep::aztec::context::PrivateContext;
32

43
use crate::entrypoint::{app::AppPayload, fee::FeePayload};
54
use crate::auth::{IS_VALID_SELECTOR, compute_authwit_message_hash};

noir-projects/aztec-nr/aztec/src/hash.nr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use dep::protocol_types::{
66
},
77
traits::Hash,
88
hash::{
9-
pedersen_hash, compute_siloed_nullifier, sha256_to_field, pedersen_commitment,
10-
poseidon2_hash_with_separator
9+
pedersen_hash, compute_siloed_nullifier, sha256_to_field, poseidon2_hash_with_separator,
10+
pedersen_commitment
1111
}
1212
};
1313
// Note: pedersen_commitment is used only as a re-export here

noir-projects/aztec-nr/aztec/src/oracle/get_nullifier_membership_witness.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dep::protocol_types::{
22
abis::nullifier_leaf_preimage::{NullifierLeafPreimage, NULLIFIER_LEAF_PREIMAGE_LENGTH},
3-
constants::NULLIFIER_TREE_HEIGHT, hash::pedersen_hash, utils::arr_copy_slice
3+
constants::NULLIFIER_TREE_HEIGHT, utils::arr_copy_slice
44
};
55

66
// INDEX_LENGTH + NULLIFIER_LEAF_PREIMAGE_LENGTH + NULLIFIER_TREE_HEIGHT

noir-projects/aztec-nr/aztec/src/oracle/get_public_data_witness.nr

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use dep::protocol_types::{
2-
constants::PUBLIC_DATA_TREE_HEIGHT, hash::pedersen_hash,
3-
public_data_tree_leaf_preimage::PublicDataTreeLeafPreimage, traits::{Hash, Serialize},
2+
constants::PUBLIC_DATA_TREE_HEIGHT, public_data_tree_leaf_preimage::PublicDataTreeLeafPreimage,
43
utils::arr_copy_slice
54
};
65

noir-projects/aztec-nr/aztec/src/state_vars/map.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dep::protocol_types::{hash::pedersen_hash, storage::map::derive_storage_slot_in_map, traits::ToField};
1+
use dep::protocol_types::{storage::map::derive_storage_slot_in_map, traits::ToField};
22
use crate::state_vars::storage::Storage;
33

44
// docs:start:map

noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable/shared_mutable.nr

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct SharedMutable<T, let INITIAL_DELAY: u32, Context> {
1818
storage_slot: Field,
1919
}
2020

21+
// Separators separating storage slot of different values within the same state variable
22+
global VALUE_CHANGE_SEPARATOR: u32 = 0;
23+
global DELAY_CHANGE_SEPARATOR: u32 = 1;
24+
global HASH_SEPARATOR: u32 = 2;
25+
2126
// This will make the Aztec macros require that T implements the Serialize<N> trait, and allocate N storage slots to
2227
// this state variable. This is incorrect, since what we actually store is:
2328
// - a ScheduledValueChange<T>, which requires 1 + 2 * M storage slots, where M is the serialization length of T
@@ -76,15 +81,15 @@ impl<T, let INITIAL_DELAY: u32, Context> SharedMutable<T, INITIAL_DELAY, Context
7681
// - a ScheduledDelaChange
7782
// - the hash of both of these (via `hash_scheduled_data`)
7883
fn get_value_change_storage_slot(self) -> Field {
79-
pedersen_hash([self.storage_slot, 0], 0)
84+
pedersen_hash([self.storage_slot], VALUE_CHANGE_SEPARATOR)
8085
}
8186

8287
fn get_delay_change_storage_slot(self) -> Field {
83-
pedersen_hash([self.storage_slot, 1], 0)
88+
pedersen_hash([self.storage_slot], DELAY_CHANGE_SEPARATOR)
8489
}
8590

8691
fn get_hash_storage_slot(self) -> Field {
87-
pedersen_hash([self.storage_slot, 2], 0)
92+
pedersen_hash([self.storage_slot], HASH_SEPARATOR)
8893
}
8994

9095
// It may seem odd that we take a header and address instead of reading from e.g. a PrivateContext, but this lets us

noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable/shared_mutable_private_getter.nr

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
use dep::protocol_types::{
2-
hash::{pedersen_hash, poseidon2_hash}, traits::{FromField, ToField}, address::AztecAddress,
3-
header::Header
4-
};
1+
use dep::protocol_types::{traits::{FromField, ToField}, address::AztecAddress, header::Header};
52

6-
use crate::context::PrivateContext;
7-
use crate::state_vars::{
8-
storage::Storage,
9-
shared_mutable::{
10-
shared_mutable::SharedMutable, scheduled_delay_change::ScheduledDelayChange,
11-
scheduled_value_change::ScheduledValueChange
12-
}
13-
};
3+
use crate::{context::PrivateContext, state_vars::shared_mutable::shared_mutable::SharedMutable};
144

155
struct SharedMutablePrivateGetter<T, INITIAL_DELAY> {
166
context: &mut PrivateContext,

noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ contract AvmTest {
3030
use dep::aztec::state_vars::PublicMutable;
3131
use dep::aztec::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH, point::Point, scalar::Scalar};
3232
use dep::aztec::oracle::get_contract_instance::{get_contract_instance_avm, get_contract_instance_internal_avm};
33-
use dep::aztec::protocol_types::abis::function_selector::FunctionSelector;
33+
use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, storage::map::derive_storage_slot_in_map};
3434
use dep::aztec::context::gas::GasOpts;
3535
use dep::compressed_string::CompressedString;
3636

@@ -76,15 +76,15 @@ contract AvmTest {
7676
fn set_storage_map(to: AztecAddress, amount: u32) -> Field {
7777
storage.map.at(to).write(amount);
7878
// returns storage slot for key
79-
std::hash::pedersen_hash([storage.map.storage_slot, to.to_field()])
79+
derive_storage_slot_in_map(storage.map.storage_slot, to)
8080
}
8181

8282
#[aztec(public)]
8383
fn add_storage_map(to: AztecAddress, amount: u32) -> Field {
8484
let new_balance = storage.map.at(to).read().add(amount);
8585
storage.map.at(to).write(new_balance);
8686
// returns storage slot for key
87-
std::hash::pedersen_hash([storage.map.storage_slot, to.to_field()])
87+
derive_storage_slot_in_map(storage.map.storage_slot, to)
8888
}
8989

9090
#[aztec(public)]

noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use dep::aztec::{
2-
note::{note_header::NoteHeader, note_interface::NoteInterface}, hash::pedersen_hash,
3-
context::PrivateContext
4-
};
1+
use dep::aztec::{note::{note_header::NoteHeader, note_interface::NoteInterface}, context::PrivateContext};
52

63
global TEST_NOTE_LEN: Field = 1;
74
// TEST_NOTE_LENGTH * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)

noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dep::aztec::prelude::{AztecAddress, NoteGetterOptions, NoteViewerOptions, NoteHeader, NoteInterface, PrivateSet, Map};
22
use dep::aztec::{
3-
context::{PrivateContext, UnconstrainedContext}, hash::pedersen_hash,
3+
context::{PrivateContext, UnconstrainedContext},
44
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
55
note::{note_getter::view_notes, note_getter_options::SortOrder, note_emission::OuterNoteEmission}
66
};

noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dep::aztec::prelude::{AztecAddress, NoteGetterOptions, NoteViewerOptions, NoteHeader, NoteInterface, PrivateSet, Map};
22
use dep::aztec::{
3-
context::{PrivateContext, UnconstrainedContext}, hash::pedersen_hash,
3+
context::{PrivateContext, UnconstrainedContext},
44
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
55
note::{
66
note_getter::view_notes, note_getter_options::SortOrder,

noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/types/balances_map.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dep::aztec::prelude::{AztecAddress, NoteGetterOptions, NoteViewerOptions, NoteHeader, NoteInterface, PrivateSet, Map};
22
use dep::aztec::{
3-
context::{PrivateContext, UnconstrainedContext}, hash::pedersen_hash,
3+
context::{PrivateContext, UnconstrainedContext},
44
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
55
note::{
66
note_getter::view_notes, note_getter_options::SortOrder,

noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::{
22
abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress},
3-
constants::{GAS_LENGTH, FIXED_DA_GAS, FIXED_L2_GAS}, hash::pedersen_hash,
4-
traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered, utils::reader::Reader,
5-
abis::gas_fees::GasFees
3+
constants::{GAS_LENGTH, FIXED_DA_GAS, FIXED_L2_GAS}, traits::{Deserialize, Hash, Serialize, Empty},
4+
abis::side_effect::Ordered, utils::reader::Reader, abis::gas_fees::GasFees
65
};
76
use std::ops::{Add, Sub};
87

noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_fees.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress},
3-
constants::GAS_FEES_LENGTH, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty},
3+
constants::GAS_FEES_LENGTH, traits::{Deserialize, Hash, Serialize, Empty},
44
abis::side_effect::Ordered, utils::reader::Reader
55
};
66

noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_settings.nr

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::{
55
GAS_SETTINGS_LENGTH, DEFAULT_GAS_LIMIT, DEFAULT_TEARDOWN_GAS_LIMIT, DEFAULT_MAX_FEE_PER_GAS,
66
DEFAULT_INCLUSION_FEE
77
},
8-
hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered,
9-
utils::reader::Reader
8+
traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered, utils::reader::Reader
109
};
1110

1211
struct GasSettings {

noir-projects/noir-protocol-circuits/crates/types/src/address/eth_address.nr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
constants::ETH_ADDRESS_LENGTH, hash::pedersen_hash,
3-
traits::{Empty, ToField, Serialize, Deserialize}, utils
4-
};
1+
use crate::{constants::ETH_ADDRESS_LENGTH, traits::{Empty, ToField, Serialize, Deserialize}, utils};
52

63
struct EthAddress{
74
inner : Field

noir-projects/noir-protocol-circuits/crates/types/src/constants.nr

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ global MAX_ARGS_LENGTH: u32 = ARGS_HASH_CHUNK_COUNT * ARGS_HASH_CHUNK_LENGTH;
127127
// The initialization slot is computed by adding the constant below to the variable's storage slot. This constant has
128128
// to be large enough so that it's ensured that it doesn't collide with storage slots of other variables.
129129
global INITIALIZATION_SLOT_SEPARATOR: Field = 1000_000_000;
130+
global MAP_STORAGE_SLOT_SEPARATOR: u32 = 1000_000_001;
130131
global INITIAL_L2_BLOCK_NUM: Field = 1;
131132
global BLOB_SIZE_IN_BYTES: Field = 31 * 4096;
132133
global ETHEREUM_SLOT_DURATION: u32 = 12;

noir-projects/noir-protocol-circuits/crates/types/src/storage/map.nr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::{hash::pedersen_hash, traits::ToField};
1+
use crate::{hash::pedersen_hash, constants::MAP_STORAGE_SLOT_SEPARATOR, traits::ToField};
22

33
pub fn derive_storage_slot_in_map<K>(storage_slot: Field, key: K) -> Field where K: ToField {
4-
pedersen_hash([storage_slot, key.to_field()], 0)
4+
pedersen_hash([storage_slot, key.to_field()], MAP_STORAGE_SLOT_SEPARATOR)
55
}
66

77
mod test {
@@ -15,7 +15,7 @@ mod test {
1515
let slot = derive_storage_slot_in_map(map_slot, key);
1616

1717
// The following value was generated by `map_slot.test.ts`
18-
let slot_from_typescript = 0x2499880e2b1b831785c17286f99a0d5122fee784ce7b1c04e380c4a991da819a;
18+
let slot_from_typescript = 0x160e1bbd52a39bdb5ce2024c61d96dc2c1e9f0653e0b348e920828d29d334330;
1919

2020
assert_eq(slot, slot_from_typescript);
2121
}

yarn-project/circuits.js/src/constants.gen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const ARGS_HASH_CHUNK_LENGTH = 16;
8383
export const ARGS_HASH_CHUNK_COUNT = 16;
8484
export const MAX_ARGS_LENGTH = 256;
8585
export const INITIALIZATION_SLOT_SEPARATOR = 1000000000;
86+
export const MAP_STORAGE_SLOT_SEPARATOR = 1000000001;
8687
export const INITIAL_L2_BLOCK_NUM = 1;
8788
export const BLOB_SIZE_IN_BYTES = 126976;
8889
export const ETHEREUM_SLOT_DURATION = 12;

yarn-project/circuits.js/src/hash/map_slot.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Map slot', () => {
1212
const slot = deriveStorageSlotInMap(mapSlot, key);
1313

1414
expect(slot.toString()).toMatchInlineSnapshot(
15-
`"0x2499880e2b1b831785c17286f99a0d5122fee784ce7b1c04e380c4a991da819a"`,
15+
`"0x160e1bbd52a39bdb5ce2024c61d96dc2c1e9f0653e0b348e920828d29d334330"`,
1616
);
1717

1818
// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data

yarn-project/circuits.js/src/hash/map_slot.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { pedersenHash } from '@aztec/foundation/crypto';
21
import { type Fr } from '@aztec/foundation/fields';
32

3+
import { MAP_STORAGE_SLOT_SEPARATOR } from '../constants.gen.js';
4+
import { pedersenHash } from '@aztec/foundation/crypto';
5+
46
/**
57
* Computes the resulting storage slot for an entry in a map.
68
* @param mapSlot - The slot of the map within state.
@@ -14,5 +16,5 @@ export function deriveStorageSlotInMap(
1416
toField: () => Fr;
1517
},
1618
): Fr {
17-
return pedersenHash([mapSlot, key.toField()]);
19+
return pedersenHash([mapSlot, key.toField()], MAP_STORAGE_SLOT_SEPARATOR);
1820
}

yarn-project/end-to-end/src/e2e_keys.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ describe('Key Registry', () => {
5050
afterAll(() => teardown());
5151

5252
describe('using nsk_app to detect nullification', () => {
53-
// This test checks that it possible to detect that a note has been nullified just by using nsk_app. Note that
54-
// this only works for non-transient notes as transient ones never emit a note hash which makes it impossible
55-
// to brute force their nullifier.
53+
// This test checks that it is possible to detect that a note has been nullified just by using nsk_app. Note
54+
// that this only works for non-transient notes as transient ones never emit a note hash which makes it
55+
// impossible to brute force their nullifier.
5656
// This might seem to make the scheme useless in practice. This could not be the case because if you have
5757
// a note of funds, when you create the transient you are nullifying that note. So even if I cannot see when you
5858
// nullified the transient ones, I can see that you nullified the first.

0 commit comments

Comments
 (0)