Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 6, 2024
1 parent 4eab954 commit a26def7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ library Constants {
uint256 internal constant ARGS_HASH_CHUNK_COUNT = 16;
uint256 internal constant MAX_ARGS_LENGTH = 256;
uint256 internal constant INITIALIZATION_SLOT_SEPARATOR = 1000000000;
uint256 internal constant MAP_STORAGE_SLOT_SEPARATOR = 1000000001;
uint256 internal constant INITIAL_L2_BLOCK_NUM = 1;
uint256 internal constant BLOB_SIZE_IN_BYTES = 126976;
uint256 internal constant MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 20000;
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ pub fn compute_note_hash_for_consumption<Note, let N: u32, let M: u32>(note: Not
// siloed by the kernel circuit. That being said, the siloed note hash computed above CAN be
// used for nullifier computation, and this achieves the (arguably unnecessary) property that
// nullifiers are computed from a note hash's fully-computed note hash tree leaf.
// --> This property is important when trying to detect wheter a note has been nullified just
// --> This property is important when trying to detect whether a note has been nullified just
// by watching for new note hashes and them hashing them all with nsk_app to derive future nullifiers.
// (See 'nsk_app and contract address are enough to detect note nullification' test case in e2e_keys.test.ts).
// Note: If we would drop the siloing here we would save ~1.2k gates per still a very unoptimized token
// Note: If we would drop the siloing here we would save ~1.2k gates per still-a-very-unoptimized token
// transfer so the savings would not be that great here.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct SharedMutable<T, let INITIAL_DELAY: u32, Context> {
storage_slot: Field,
}

// Randomly selected storage slot separators
global VALUE_CHANGE_SEPARATOR: u32 = 2583003158;
global DELAY_CHANGE_SEPARATOR: u32 = 3557441280;
global HASH_SEPARATOR: u32 = 2053124016;

// This will make the Aztec macros require that T implements the Serialize<N> trait, and allocate N storage slots to
// this state variable. This is incorrect, since what we actually store is:
// - a ScheduledValueChange<T>, which requires 1 + 2 * M storage slots, where M is the serialization length of T
Expand Down Expand Up @@ -76,15 +81,15 @@ impl<T, let INITIAL_DELAY: u32, Context> SharedMutable<T, INITIAL_DELAY, Context
// - a ScheduledDelaChange
// - the hash of both of these (via `hash_scheduled_data`)
fn get_value_change_storage_slot(self) -> Field {
poseidon2_hash_with_separator([self.storage_slot, 0], 0)
poseidon2_hash_with_separator([self.storage_slot], VALUE_CHANGE_SEPARATOR)
}

fn get_delay_change_storage_slot(self) -> Field {
poseidon2_hash_with_separator([self.storage_slot, 1], 0)
poseidon2_hash_with_separator([self.storage_slot], DELAY_CHANGE_SEPARATOR)
}

fn get_hash_storage_slot(self) -> Field {
poseidon2_hash_with_separator([self.storage_slot, 2], 0)
poseidon2_hash_with_separator([self.storage_slot], HASH_SEPARATOR)
}

// It may seem odd that we take a header and address instead of reading from e.g. a PrivateContext, but this lets us
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ global MAX_ARGS_LENGTH: u32 = ARGS_HASH_CHUNK_COUNT * ARGS_HASH_CHUNK_LENGTH;
// The initialization slot is computed by adding the constant below to the variable's storage slot. This constant has
// to be large enough so that it's ensured that it doesn't collide with storage slots of other variables.
global INITIALIZATION_SLOT_SEPARATOR: Field = 1000_000_000;
global MAP_STORAGE_SLOT_SEPARATOR: u32 = 1000_000_001;
global INITIAL_L2_BLOCK_NUM: Field = 1;
global BLOB_SIZE_IN_BYTES: Field = 31 * 4096;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{hash::poseidon2_hash_with_separator, traits::ToField};
use crate::{hash::poseidon2_hash_with_separator, constants::MAP_STORAGE_SLOT_SEPARATOR, traits::ToField};

pub fn derive_storage_slot_in_map<K>(storage_slot: Field, key: K) -> Field where K: ToField {
poseidon2_hash_with_separator([storage_slot, key.to_field()], 0)
poseidon2_hash_with_separator([storage_slot, key.to_field()], MAP_STORAGE_SLOT_SEPARATOR)
}

mod test {
Expand All @@ -15,7 +15,7 @@ mod test {
let slot = derive_storage_slot_in_map(map_slot, key);

// The following value was generated by `map_slot.test.ts`
let slot_from_typescript = 0x2499880e2b1b831785c17286f99a0d5122fee784ce7b1c04e380c4a991da819a;
let slot_from_typescript = 0x160e1bbd52a39bdb5ce2024c61d96dc2c1e9f0653e0b348e920828d29d334330;

assert_eq(slot, slot_from_typescript);
}
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const ARGS_HASH_CHUNK_LENGTH = 16;
export const ARGS_HASH_CHUNK_COUNT = 16;
export const MAX_ARGS_LENGTH = 256;
export const INITIALIZATION_SLOT_SEPARATOR = 1000000000;
export const MAP_STORAGE_SLOT_SEPARATOR = 1000000001;
export const INITIAL_L2_BLOCK_NUM = 1;
export const BLOB_SIZE_IN_BYTES = 126976;
export const MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 20000;
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/hash/map_slot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Map slot', () => {
const slot = deriveStorageSlotInMap(mapSlot, key);

expect(slot.toString()).toMatchInlineSnapshot(
`"0x2499880e2b1b831785c17286f99a0d5122fee784ce7b1c04e380c4a991da819a"`,
`"0x160e1bbd52a39bdb5ce2024c61d96dc2c1e9f0653e0b348e920828d29d334330"`,
);

// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/circuits.js/src/hash/map_slot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
import { type Fr } from '@aztec/foundation/fields';

import { MAP_STORAGE_SLOT_SEPARATOR } from '../constants.gen.js';

/**
* Computes the resulting storage slot for an entry in a map.
* @param mapSlot - The slot of the map within state.
Expand All @@ -14,5 +16,5 @@ export function deriveStorageSlotInMap(
toField: () => Fr;
},
): Fr {
return poseidon2HashWithSeparator([mapSlot, key.toField()], 0);
return poseidon2HashWithSeparator([mapSlot, key.toField()], MAP_STORAGE_SLOT_SEPARATOR);
}

0 comments on commit a26def7

Please sign in to comment.