-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move siloing to reset (#7871)
Please read [contributing guidelines](CONTRIBUTING.md) and remove this line.
- Loading branch information
Showing
59 changed files
with
2,083 additions
and
1,367 deletions.
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
22 changes: 8 additions & 14 deletions
22
...te-kernel-lib/src/components/previous_kernel_validator/previous_kernel_validator_hints.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 |
---|---|---|
@@ -1,32 +1,26 @@ | ||
use dep::types::{ | ||
abis::kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs, | ||
constants::{MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX}, hash::silo_note_hash, | ||
utils::arrays::{find_index, sort_get_order_hints_asc} | ||
abis::{kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs, note_hash::ScopedNoteHash}, | ||
constants::{MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX}, utils::arrays::find_index | ||
}; | ||
|
||
struct PreviousKernelValidatorHints { | ||
siloed_note_hashes: [Field; MAX_NOTE_HASHES_PER_TX], | ||
note_hash_indexes_for_nullifiers: [u32; MAX_NULLIFIERS_PER_TX], | ||
} | ||
|
||
unconstrained pub fn generate_previous_kernel_validator_hints(previous_kernel: PrivateKernelCircuitPublicInputs) -> PreviousKernelValidatorHints { | ||
let mut siloed_note_hashes = [0; MAX_NOTE_HASHES_PER_TX]; | ||
let unsilod_note_hashes = previous_kernel.end.note_hashes; | ||
let sorted_note_hash_hints = sort_get_order_hints_asc(unsilod_note_hashes); | ||
let tx_hash = previous_kernel.end.nullifiers[0].value(); // First nullifier is tx hash. | ||
for i in 0..unsilod_note_hashes.len() { | ||
siloed_note_hashes[i] = silo_note_hash(unsilod_note_hashes[i], tx_hash, sorted_note_hash_hints[i].sorted_index); | ||
} | ||
|
||
let mut note_hash_indexes_for_nullifiers = [0; MAX_NULLIFIERS_PER_TX]; | ||
let note_hashes = previous_kernel.end.note_hashes; | ||
let nullifiers = previous_kernel.end.nullifiers; | ||
for i in 0..nullifiers.len() { | ||
let nullified_note_hash = nullifiers[i].nullifier.note_hash; | ||
let note_hash_index = find_index(siloed_note_hashes, |n: Field| n == nullified_note_hash); | ||
let note_hash_index = find_index( | ||
note_hashes, | ||
|n: ScopedNoteHash| n.value() == nullified_note_hash | ||
); | ||
if (nullified_note_hash != 0) & (note_hash_index != MAX_NOTE_HASHES_PER_TX) { | ||
note_hash_indexes_for_nullifiers[i] = note_hash_index; | ||
} | ||
} | ||
|
||
PreviousKernelValidatorHints { siloed_note_hashes, note_hash_indexes_for_nullifiers } | ||
PreviousKernelValidatorHints { note_hash_indexes_for_nullifiers } | ||
} |
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
103 changes: 89 additions & 14 deletions
103
.../noir-protocol-circuits/crates/private-kernel-lib/src/components/reset_output_composer.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 |
---|---|---|
@@ -1,42 +1,117 @@ | ||
mod squash_transient_data; | ||
mod reset_output_hints; | ||
|
||
use crate::components::reset_output_composer::squash_transient_data::squash_transient_data; | ||
use crate::components::reset_output_composer::{reset_output_hints::{generate_reset_output_hints, ResetOutputHints}}; | ||
use dep::types::{ | ||
abis::{ | ||
kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs, log_hash::NoteLogHash, | ||
note_hash::ScopedNoteHash, nullifier::ScopedNullifier | ||
kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs, | ||
log_hash::{NoteLogHash, ScopedEncryptedLogHash}, note_hash::ScopedNoteHash, | ||
nullifier::ScopedNullifier | ||
}, | ||
constants::{MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX} | ||
address::AztecAddress, | ||
constants::{ | ||
MAX_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_HASHES_PER_TX, | ||
MAX_NULLIFIERS_PER_TX | ||
}, | ||
hash::{mask_encrypted_log_hash, silo_note_hash, silo_nullifier}, utils::arrays::sort_by_counters_asc | ||
}; | ||
|
||
struct PrivateKernelResetOutputs { | ||
note_hashes: [ScopedNoteHash; MAX_NOTE_HASHES_PER_TX], | ||
nullifiers: [ScopedNullifier; MAX_NULLIFIERS_PER_TX], | ||
note_encrypted_log_hashes: [NoteLogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], | ||
encrypted_log_hashes: [ScopedEncryptedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], | ||
} | ||
|
||
struct ResetOutputComposer { | ||
output: PrivateKernelResetOutputs, | ||
previous_kernel: PrivateKernelCircuitPublicInputs, | ||
note_hash_siloing_amount: u32, | ||
nullifier_siloing_amount: u32, | ||
encrypted_log_siloing_amount: u32, | ||
hints: ResetOutputHints, | ||
} | ||
|
||
impl ResetOutputComposer { | ||
pub fn new( | ||
previous_kernel: PrivateKernelCircuitPublicInputs, | ||
transient_nullifier_indexes_for_note_hashes: [u32; MAX_NOTE_HASHES_PER_TX], | ||
transient_note_hash_indexes_for_nullifiers: [u32; MAX_NULLIFIERS_PER_TX] | ||
transient_note_hash_indexes_for_nullifiers: [u32; MAX_NULLIFIERS_PER_TX], | ||
note_hash_siloing_amount: u32, | ||
nullifier_siloing_amount: u32, | ||
encrypted_log_siloing_amount: u32 | ||
) -> Self { | ||
let (note_hashes, nullifiers, note_encrypted_log_hashes) = squash_transient_data( | ||
previous_kernel.end.note_hashes, | ||
previous_kernel.end.nullifiers, | ||
previous_kernel.end.note_encrypted_logs_hashes, | ||
let hints = generate_reset_output_hints( | ||
previous_kernel, | ||
transient_nullifier_indexes_for_note_hashes, | ||
transient_note_hash_indexes_for_nullifiers | ||
); | ||
let output = PrivateKernelResetOutputs { note_hashes, nullifiers, note_encrypted_log_hashes }; | ||
ResetOutputComposer { output } | ||
ResetOutputComposer { previous_kernel, note_hash_siloing_amount, nullifier_siloing_amount, encrypted_log_siloing_amount, hints } | ||
} | ||
|
||
pub fn finish(self) -> PrivateKernelResetOutputs { | ||
self.output | ||
let note_hashes = if self.note_hash_siloing_amount == 0 { | ||
self.hints.kept_note_hashes | ||
} else { | ||
self.get_sorted_siloed_note_hashes() | ||
}; | ||
|
||
let nullifiers = if self.nullifier_siloing_amount == 0 { | ||
self.hints.kept_nullifiers | ||
} else { | ||
self.get_sorted_siloed_nullifiers() | ||
}; | ||
|
||
let note_encrypted_log_hashes = if self.note_hash_siloing_amount == 0 { | ||
self.hints.kept_note_encrypted_log_hashes | ||
} else { | ||
self.get_sorted_note_encrypted_log_hashes() | ||
}; | ||
|
||
let encrypted_log_hashes = if self.encrypted_log_siloing_amount == 0 { | ||
self.previous_kernel.end.encrypted_logs_hashes | ||
} else { | ||
self.get_sorted_masked_encrypted_log_hashes() | ||
}; | ||
|
||
PrivateKernelResetOutputs { note_hashes, nullifiers, note_encrypted_log_hashes, encrypted_log_hashes } | ||
} | ||
|
||
fn get_sorted_siloed_note_hashes(self) -> [ScopedNoteHash; MAX_NOTE_HASHES_PER_TX] { | ||
let mut note_hashes = sort_by_counters_asc(self.hints.kept_note_hashes); | ||
let first_nullifier = self.previous_kernel.end.nullifiers[0].value(); | ||
for i in 0..note_hashes.len() { | ||
note_hashes[i].note_hash.value = silo_note_hash( | ||
note_hashes[i], | ||
first_nullifier, | ||
i | ||
); | ||
note_hashes[i].contract_address = AztecAddress::zero(); | ||
} | ||
note_hashes | ||
} | ||
|
||
fn get_sorted_siloed_nullifiers(self) -> [ScopedNullifier; MAX_NULLIFIERS_PER_TX] { | ||
let mut nullifiers = sort_by_counters_asc(self.hints.kept_nullifiers); | ||
for i in 0..nullifiers.len() { | ||
nullifiers[i].nullifier.value = silo_nullifier(nullifiers[i]); | ||
nullifiers[i].contract_address = AztecAddress::zero(); | ||
} | ||
nullifiers | ||
} | ||
|
||
fn get_sorted_note_encrypted_log_hashes(self) -> [NoteLogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX] { | ||
let mut log_hashes = sort_by_counters_asc(self.hints.kept_note_encrypted_log_hashes); | ||
for i in 0..log_hashes.len() { | ||
log_hashes[i].note_hash_counter = 0; | ||
} | ||
log_hashes | ||
} | ||
|
||
fn get_sorted_masked_encrypted_log_hashes(self) -> [ScopedEncryptedLogHash; MAX_ENCRYPTED_LOGS_PER_TX] { | ||
let mut log_hashes = sort_by_counters_asc(self.previous_kernel.end.encrypted_logs_hashes); | ||
for i in 0..log_hashes.len() { | ||
log_hashes[i].contract_address = mask_encrypted_log_hash(log_hashes[i]); | ||
log_hashes[i].log_hash.randomness = 0; | ||
} | ||
log_hashes | ||
} | ||
} |
Oops, something went wrong.