-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: Updates singleton usage #4186
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,32 +20,11 @@ use crate::oracle::{ | |
notes::check_nullifier_exists, | ||
}; | ||
|
||
pub fn compute_singleton_initialization_nullifier( | ||
storage_slot: Field, | ||
owner: Option<AztecAddress>, | ||
context: Option<&mut PrivateContext> | ||
) -> Field { | ||
if owner.is_some() { | ||
let secret = if context.is_some() { | ||
context.unwrap_unchecked().request_nullifier_secret_key(owner.unwrap_unchecked()) | ||
} else { | ||
get_nullifier_secret_key(owner.unwrap_unchecked()) | ||
}; | ||
pedersen_hash( | ||
[storage_slot, secret.low, secret.high], | ||
GENERATOR_INDEX__INITIALIZATION_NULLIFIER | ||
) | ||
} else { | ||
pedersen_hash([storage_slot], GENERATOR_INDEX__INITIALIZATION_NULLIFIER) | ||
} | ||
} | ||
|
||
// docs:start:struct | ||
struct Singleton<Note, N> { | ||
context: Option<&mut PrivateContext>, | ||
storage_slot: Field, | ||
note_interface: NoteInterface<Note, N>, | ||
compute_initialization_nullifier: fn (Field, Option<AztecAddress>, Option<&mut PrivateContext>) -> Field, | ||
} | ||
// docs:end:struct | ||
|
||
|
@@ -57,19 +36,25 @@ impl<Note, N> Singleton<Note, N> { | |
note_interface: NoteInterface<Note, N>, | ||
) -> Self { | ||
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); | ||
Singleton { | ||
Self { | ||
context: context.private, | ||
storage_slot, | ||
note_interface, | ||
compute_initialization_nullifier: compute_singleton_initialization_nullifier, | ||
} | ||
} | ||
// docs:end:new | ||
|
||
// The following computation is leaky - the storage slot can easily be "guessed" by an adversary | ||
// by looking at the nullifier in the transaction data. | ||
// This is especially dangerous for `maps` because the storage slot is often also identifies the | ||
// actor that is executing the transaction. e.g, `map.at(msg.sender)` will leak `msg.sender`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, but also add a note like:
|
||
pub fn compute_initialization_nullifier(self) -> Field { | ||
pedersen_hash([self.storage_slot], GENERATOR_INDEX__INITIALIZATION_NULLIFIER) | ||
} | ||
|
||
// docs:start:is_initialized | ||
unconstrained pub fn is_initialized(self, owner: Option<AztecAddress>) -> bool { | ||
let compute_initialization_nullifier = self.compute_initialization_nullifier; | ||
let nullifier = compute_initialization_nullifier(self.storage_slot, owner, Option::none()); | ||
unconstrained pub fn is_initialized(self) -> bool { | ||
let nullifier = self.compute_initialization_nullifier(); | ||
check_nullifier_exists(nullifier) | ||
} | ||
// docs:end:is_initialized | ||
|
@@ -78,14 +63,12 @@ impl<Note, N> Singleton<Note, N> { | |
pub fn initialize( | ||
self, | ||
note: &mut Note, | ||
owner: Option<AztecAddress>, | ||
broadcast: bool, | ||
) { | ||
let context = self.context.unwrap(); | ||
|
||
// Nullify the storage slot. | ||
let compute_initialization_nullifier = self.compute_initialization_nullifier; | ||
let nullifier = compute_initialization_nullifier(self.storage_slot, owner, self.context); | ||
let nullifier = self.compute_initialization_nullifier(); | ||
context.push_new_nullifier(nullifier, 0); | ||
|
||
create_note(context, self.storage_slot, note, self.note_interface, broadcast); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.