-
Notifications
You must be signed in to change notification settings - Fork 607
feat: log decryption in Noir #12596
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
Merged
Merged
feat: log decryption in Noir #12596
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2721ba1
WIP
benesjan ecba8e7
cute imports
benesjan 80b4bce
test fix
benesjan 597e5e6
fixes
benesjan 8837569
e2e_offchain_note_delivery test fix
benesjan 5ea3014
fix
benesjan 2124fb9
sf
benesjan 0fc4a70
moving non-oracle stuff to utils
benesjan 1184bbb
linking a todo
benesjan 43b7c79
more TODOs
benesjan 6a55c08
nuking random_with_max_byte_size
benesjan 18c3f8e
more TODOs
benesjan b0dfb2a
Apply suggestions from code review
benesjan fc7f0a8
Update noir-projects/aztec-nr/aztec/src/encrypted_logs/log_assembly_s…
benesjan 69ea4b2
Update noir-projects/aztec-nr/aztec/src/oracle/shared_secret.nr
benesjan 538e90e
Update noir-projects/aztec-nr/aztec/src/oracle/shared_secret.nr
benesjan 8ce8276
Update noir-projects/aztec-nr/aztec/src/oracle/aes128_decrypt.nr
benesjan e1de1aa
typo fix
benesjan f09095b
fixes after rebase
benesjan 89abba9
process_private_note_log --> attempt_note_discovery
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 hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
...ec/src/encrypted_logs/log_assembly_strategies/default_aes128/arithmetic_generics_utils.nr
This file contains hidden or 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,85 @@ | ||
| /********************************************************/ | ||
|
nventuro marked this conversation as resolved.
|
||
| // Disgusting arithmetic on generics | ||
| /********************************************************/ | ||
|
|
||
| // In this section, instead of initialising arrays with very complicated generic | ||
| // arithmetic, such as: | ||
| // let my_arr: [u8; (((PT + (16 - (PT % 16))) + 49) + ((((((PT + (16 - (PT % 16))) + 49) + 30) / 31) * 31) - ((PT + (16 - (PT % 16))) + 49)))] = [0; (((PT + (16 - (PT % 16))) + 49) + ((((((PT + (16 - (PT % 16))) + 49) + 30) / 31) * 31) - ((PT + (16 - (PT % 16))) + 49)))]; | ||
| //... we instead do the arithmetic a little bit at a time, so that the computation | ||
| // can be audited and understood. Now, we can't do arithmetic on generics in the body | ||
| // of a function, so we abusing functions in the following way: | ||
|
|
||
| // |full_pt| = |pt| = (N * 32) + 64 | ||
| fn get_arr_of_size__full_plaintext<let PT: u32>() -> [u8; PT] { | ||
| [0; PT] | ||
| } | ||
|
|
||
| // |pt_aes_padding| = 16 - (|full_pt| % 16) | ||
| fn get_arr_of_size__plaintext_aes_padding<let FULL_PT: u32>( | ||
| _full_pt: [u8; FULL_PT], | ||
| ) -> [u8; 16 - (FULL_PT % 16)] { | ||
| [0; 16 - (FULL_PT % 16)] | ||
| } | ||
|
|
||
| // |ct| = |full_pt| + |pt_aes_padding| | ||
| fn get_arr_of_size__ciphertext<let FULL_PT: u32, let PT_AES_PADDING: u32>( | ||
| _full_pt: [u8; FULL_PT], | ||
| _pt_aes_padding: [u8; PT_AES_PADDING], | ||
| ) -> [u8; FULL_PT + PT_AES_PADDING] { | ||
| [0; FULL_PT + PT_AES_PADDING] | ||
| } | ||
|
|
||
| // Ok, so we have the following bytes: | ||
| // eph_pk_sign, header_ciphertext, ciphertext: | ||
| // Let lbwop = 1 + 48 + |ct| // aka log bytes without padding | ||
| fn get_arr_of_size__log_bytes_without_padding<let CT: u32>(_ct: [u8; CT]) -> [u8; 1 + 48 + CT] { | ||
| [0; 1 + 48 + CT] | ||
| } | ||
|
|
||
| // Recall: | ||
| // lbwop := 1 + 48 + |ct| // aka log bytes without padding | ||
| // We now want to pad b to the next multiple of 31, so as to "fill" fields. | ||
| // Let p be that padding. | ||
| // p = 31 * ceil(lbwop / 31) - lbwop | ||
| // = 31 * ((lbwop + 30) // 31) - lbwop | ||
| // (because ceil(x / y) = (x + y - 1) // y ). | ||
| fn get_arr_of_size__log_bytes_padding<let LBWOP: u32>( | ||
| _lbwop: [u8; LBWOP], | ||
| ) -> [u8; (31 * ((LBWOP + 30) / 31)) - LBWOP] { | ||
| [0; (31 * ((LBWOP + 30) / 31)) - LBWOP] | ||
| } | ||
|
|
||
| // |log_bytes| = 1 + 48 + |ct| + p // aka log bytes (with padding) | ||
| // Recall: | ||
| // lbwop := 1 + 48 + |ct| | ||
| // p is the padding | ||
| fn get_arr_of_size__log_bytes<let LBWOP: u32, let P: u32>( | ||
| _lbwop: [u8; LBWOP], | ||
| _p: [u8; P], | ||
| ) -> [u8; LBWOP + P] { | ||
| [0; LBWOP + P] | ||
| } | ||
|
|
||
| // The return type is pasted from the LSP's expectation, because it was too difficult | ||
| // to match its weird way of doing algebra. It doesn't know all rules of arithmetic. | ||
| // PT is the plaintext length. | ||
| pub(crate) fn get_arr_of_size__log_bytes_padding__from_PT<let PT: u32>() -> [u8; ((((((PT + (16 - (PT % 16))) + 49) + 30) / 31) * 31) - ((PT + (16 - (PT % 16))) + 49))] { | ||
| let full_pt = get_arr_of_size__full_plaintext::<PT>(); | ||
| let pt_aes_padding = get_arr_of_size__plaintext_aes_padding(full_pt); | ||
| let ct = get_arr_of_size__ciphertext(full_pt, pt_aes_padding); | ||
| let lbwop = get_arr_of_size__log_bytes_without_padding(ct); | ||
| let p = get_arr_of_size__log_bytes_padding(lbwop); | ||
| p | ||
| } | ||
|
|
||
| // The return type is pasted from the LSP's expectation, because it was too difficult | ||
| // to match its weird way of doing algebra. It doesn't know all rules of arithmetic. | ||
| pub(crate) fn get_arr_of_size__log_bytes__from_PT<let PT: u32>() -> [u8; (((PT + (16 - (PT % 16))) + 49) + ((((((PT + (16 - (PT % 16))) + 49) + 30) / 31) * 31) - ((PT + (16 - (PT % 16))) + 49)))] { | ||
| let full_pt = get_arr_of_size__full_plaintext::<PT>(); | ||
| let pt_aes_padding = get_arr_of_size__plaintext_aes_padding(full_pt); | ||
| let ct = get_arr_of_size__ciphertext(full_pt, pt_aes_padding); | ||
| let lbwop = get_arr_of_size__log_bytes_without_padding(ct); | ||
| let p = get_arr_of_size__log_bytes_padding(lbwop); | ||
| let log_bytes = get_arr_of_size__log_bytes(lbwop, p); | ||
| log_bytes | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,21 +2,24 @@ use crate::{ | |
| context::PrivateContext, | ||
| encrypted_logs::{ | ||
| encrypt::aes128::derive_aes_symmetric_key_and_iv_from_ecdh_shared_secret_using_sha256, | ||
| log_assembly_strategies::default_aes128::note::{ | ||
| get_arr_of_size__log_bytes__from_PT, get_arr_of_size__log_bytes_padding__from_PT, | ||
| HEADER_CIPHERTEXT_SIZE_IN_BYTES, | ||
| log_assembly_strategies::default_aes128::{ | ||
| arithmetic_generics_utils::{ | ||
| get_arr_of_size__log_bytes__from_PT, get_arr_of_size__log_bytes_padding__from_PT, | ||
| }, | ||
| note::encryption::HEADER_CIPHERTEXT_SIZE_IN_BYTES, | ||
| }, | ||
| }, | ||
| event::event_interface::EventInterface, | ||
| keys::{ | ||
| ecdh_shared_secret::derive_ecdh_shared_secret_using_aztec_address, | ||
| ephemeral::generate_ephemeral_key_pair, | ||
| }, | ||
| oracle::{ | ||
| notes::{get_app_tag_as_sender, increment_app_tagging_secret_index_as_sender}, | ||
| random::{get_random_bytes, random}, | ||
| oracle::notes::{get_app_tag_as_sender, increment_app_tagging_secret_index_as_sender}, | ||
| utils::{ | ||
| conversion::{bytes_to_fields::bytes_to_fields, fields_to_bytes::fields_to_bytes}, | ||
| point::get_sign_of_point, | ||
| random::get_random_bytes, | ||
| }, | ||
| utils::{conversion::bytes_to_fields::bytes_to_fields, point::get_sign_of_point}, | ||
| }; | ||
| use dep::protocol_types::{ | ||
| address::AztecAddress, | ||
|
|
@@ -60,31 +63,26 @@ use std::aes128::aes128_encrypt; | |
| /// This particular log assembly strategy (AES 128) requires the event (and the | ||
| /// event_type_id) to be converted into bytes, because the aes function | ||
| /// operates on bytes; not fields. | ||
| /// NB: The extra `+ 32` bytes is for the event_type_id: | ||
| fn compute_event_plaintext_for_this_strategy<Event, let N: u32>(event: Event) -> [u8; N * 32 + 32] | ||
| /// NB: The extra `+ 1` is for the event_type_id: | ||
| fn compute_event_plaintext_for_this_strategy<Event, let N: u32>(event: Event) -> [u8; (N + 1) * 32] | ||
| where | ||
| Event: EventInterface<N>, | ||
| { | ||
| let serialized_event = Serialize::<N>::serialize(event); | ||
|
|
||
| let event_type_id_bytes: [u8; 32] = Event::get_event_type_id().to_field().to_be_bytes(); | ||
|
|
||
| let mut plaintext_bytes = [0 as u8; N * 32 + 32]; | ||
|
|
||
| for i in 0..32 { | ||
| plaintext_bytes[i] = event_type_id_bytes[i]; | ||
| } | ||
|
|
||
| let mut fields = [0; N + 1]; | ||
| fields[0] = Event::get_event_type_id().to_field(); | ||
|
Comment on lines
+73
to
+74
Contributor
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. Nice |
||
| for i in 0..serialized_event.len() { | ||
| let bytes: [u8; 32] = serialized_event[i].to_be_bytes(); | ||
| for j in 0..32 { | ||
| plaintext_bytes[32 + i * 32 + j] = bytes[j]; | ||
| } | ||
| fields[i + 1] = serialized_event[i]; | ||
| } | ||
|
|
||
| plaintext_bytes | ||
| fields_to_bytes(fields) | ||
| } | ||
|
|
||
| // Note: This function is basically a copy of ./note/encryption.nr::encrypt_log. TODO: Merge the functions once | ||
| // the final note and event log layout is clear. Seems to me that the functions should be the same as encrypt_log | ||
| // is quite general and takes in an arbitrary plaintext. The note specific thing seems to be that in that function | ||
| // we perform some note-specific log length assertions. | ||
| fn compute_log<Event, let N: u32>( | ||
| context: PrivateContext, | ||
| event: Event, | ||
|
|
@@ -210,6 +208,9 @@ where | |
| // Convert the encrypted bytes to fields, because logs are field-based | ||
| // ***************************************************************************** | ||
|
|
||
| // TODO(#12749): As Mike pointed out, we need to make logs produced by different encryption schemes | ||
| // indistinguishable from each other and for this reason the output here and in the last for-loop of this function | ||
| // should cover a full field. | ||
| let log_bytes_as_fields = bytes_to_fields(log_bytes); | ||
|
|
||
| // ***************************************************************************** | ||
|
|
@@ -236,9 +237,14 @@ where | |
| offset += log_bytes_as_fields.len(); | ||
|
|
||
| for i in offset..PRIVATE_LOG_SIZE_IN_FIELDS { | ||
| // We need to get a random value that fits in 31 bytes to not leak information about the size of the log | ||
| // (all the "real" log fields contain at most 31 bytes because of the way we convert the bytes to fields). | ||
| // TODO(#12749): Long term, this is not a good solution. | ||
|
|
||
| // Safety: we assume that the sender wants for the log to be private - a malicious one could simply reveal its | ||
| // contents publicly. It is therefore fine to trust the sender to provide random padding. | ||
| final_log[i] = unsafe { random() }; | ||
| let field_bytes = unsafe { get_random_bytes::<31>() }; | ||
| final_log[i] = Field::from_be_bytes::<31>(field_bytes); | ||
| } | ||
|
|
||
| final_log | ||
|
|
||
1 change: 1 addition & 0 deletions
1
...-projects/aztec-nr/aztec/src/encrypted_logs/log_assembly_strategies/default_aes128/mod.nr
This file contains hidden or 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,2 +1,3 @@ | ||
| pub mod arithmetic_generics_utils; | ||
| pub mod event; | ||
| pub mod note; |
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.
Uh oh!
There was an error while loading. Please reload this page.