-
Notifications
You must be signed in to change notification settings - Fork 310
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!: using WithHash<T>
in SharedMutable
+ fixing slot allocation
#11716
Merged
nventuro
merged 8 commits into
master
from
02-04-feat_using_withhash_t_in_sharedmutable_fixing_slotallocation
Feb 6, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36322d9
feat: using WithHash<T> in SharedMutable + fixing slotallocation
benesjan bcfd5f3
nuking redundant tests
benesjan ca1fa0e
testing storage slot allocation for SharedMutable
benesjan 9ab7cb0
migration notes
benesjan eb09a3d
improved tests
benesjan 98191fa
Update noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable.nr
benesjan 9f588c4
better num. generic name
benesjan 69aa267
comment
benesjan 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
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
55 changes: 55 additions & 0 deletions
55
noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable/shared_mutable_values.nr
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::state_vars::shared_mutable::{ | ||
scheduled_delay_change::{SCHEDULED_DELAY_CHANGE_PCKD_LEN, ScheduledDelayChange}, | ||
scheduled_value_change::ScheduledValueChange, | ||
}; | ||
use crate::utils::array; | ||
use dep::protocol_types::{traits::Packable, utils::arrays::array_concat}; | ||
use std::meta::derive; | ||
|
||
/// SharedMutableValues is just a wrapper around ScheduledValueChange and ScheduledDelayChange that then allows us | ||
/// to wrap both of these values in WithHash. WithHash allows for efficient read of values in private. | ||
/// | ||
/// Note that the WithHash optimization does not work in public (due to there being no unconstrained). But we also want | ||
/// to be able to read the values efficiently in public and we want to be able to read each value separately. For that | ||
/// reason we expose `get_delay_change_storage_slot` and `get_value_change_storage_slot` which point to the correct | ||
/// location in the storage. This is "hacky" as we pack and store the values together but there is no way around it. | ||
#[derive(Eq)] | ||
pub(crate) struct SharedMutableValues<T, let INITIAL_DELAY: u32> { | ||
svc: ScheduledValueChange<T>, | ||
sdc: ScheduledDelayChange<INITIAL_DELAY>, | ||
} | ||
|
||
impl<T, let INITIAL_DELAY: u32> SharedMutableValues<T, INITIAL_DELAY> { | ||
pub(crate) fn new( | ||
svc: ScheduledValueChange<T>, | ||
sdc: ScheduledDelayChange<INITIAL_DELAY>, | ||
) -> Self { | ||
SharedMutableValues { svc, sdc } | ||
} | ||
|
||
pub fn get_delay_change_storage_slot(shared_mutable_storage_slot: Field) -> Field { | ||
shared_mutable_storage_slot | ||
} | ||
|
||
pub fn get_value_change_storage_slot(shared_mutable_storage_slot: Field) -> Field { | ||
shared_mutable_storage_slot + (SCHEDULED_DELAY_CHANGE_PCKD_LEN as Field) | ||
} | ||
} | ||
|
||
// We pack to `2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN` fields because ScheduledValueChange contains T twice | ||
// + 1 field for block_of_change + SCHEDULED_DELAY_CHANGE_PCKD_LEN fields for ScheduledDelayChange | ||
impl<T, let INITIAL_DELAY: u32, let N: u32> Packable<2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN> for SharedMutableValues<T, INITIAL_DELAY> | ||
where | ||
T: Packable<N>, | ||
{ | ||
fn pack(self) -> [Field; 2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN] { | ||
array_concat(self.sdc.pack(), self.svc.pack()) | ||
} | ||
|
||
fn unpack(fields: [Field; 2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN]) -> Self { | ||
SharedMutableValues { | ||
sdc: Packable::unpack(array::subarray(fields, 0)), | ||
svc: Packable::unpack(array::subarray(fields, SCHEDULED_DELAY_CHANGE_PCKD_LEN)), | ||
} | ||
} | ||
} |
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.
Insted of defining packable for svc and sdc, we can create the struct that contains both and just implement packable for that. If we do this, we can combine sdc and svc, since svc has a block of change (32 bits) that can be merged into sdc. Then we'd have 2N+1 total instead of 2N+2, which makes public writes cheaper and private reads a bit cheaper (less hashing).
We can do this in a separate PR.
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.
We discussed 2 days ago that we want to be able to read svc and sdc individually in public. For this reason they each need to implement packable (
storage_read
requires it). Here is where we read it individually.But when writing both values get written at once as they are wrapped in
SharedMutableValues
.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.
If we wanted to be really nasty we can have a manual
storage_read
where we read a single field at the base slot plus offset of the field in which we combine these, and then manually unpack just that field. We do save a storaage write with this approach.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.
I don't understand