Skip to content

Commit

Permalink
chore!: define key type in maps (#3841)
Browse files Browse the repository at this point in the history
Closes AztecProtocol/aztec-packages#2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

noir-lang/noir#3954
  • Loading branch information
superstar0402 committed Jan 18, 2024
1 parent 3194b03 commit 69fc33f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion authwit/src/account.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::auth::IS_VALID_SELECTOR;
struct AccountActions {
context: Context,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool,
approved_action: Map<PublicState<bool, BOOL_SERIALIZED_LEN>>,
approved_action: Map<Field, PublicState<bool, BOOL_SERIALIZED_LEN>>,
}

impl AccountActions {
Expand Down
15 changes: 9 additions & 6 deletions aztec/src/state_vars/map.nr
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use crate::context::{PrivateContext, PublicContext, Context};
use dep::std::option::Option;
use dep::protocol_types::hash::pedersen_hash;
use dep::protocol_types::{
hash::pedersen_hash,
traits::{ToField}
};

// docs:start:map
struct Map<V> {
struct Map<K, V> {
context: Context,
storage_slot: Field,
state_var_constructor: fn(Context, Field) -> V,
}
// docs:end:map

impl<V> Map<V> {
impl<K, V> Map<K, V> {
// docs:start:new
pub fn new(
context: Context,
storage_slot: Field,
state_var_constructor: fn(Context, Field) -> V,
) -> Map<V> {
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
Map {
context,
Expand All @@ -27,9 +30,9 @@ impl<V> Map<V> {
// docs:end:new

// docs:start:at
pub fn at(self, key: Field) -> V {
pub fn at(self, key: K) -> V where K: ToField {
// TODO(#1204): use a generator index for the storage slot
let derived_storage_slot = pedersen_hash([self.storage_slot, key],0);
let derived_storage_slot = pedersen_hash([self.storage_slot, key.to_field()],0);

let state_var_constructor = self.state_var_constructor;
state_var_constructor(self.context, derived_storage_slot)
Expand Down

0 comments on commit 69fc33f

Please sign in to comment.