-
Notifications
You must be signed in to change notification settings - Fork 236
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: hash logs inside circuit #5934
Merged
Merged
Changes from 18 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
5193d02
feat: WIP first commit - logs hashed inside contexts
MirandaWood 47a036d
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood b945e63
chore: oh boy deployer addr here we go
MirandaWood f7346e0
feat: acc logs len in circuit, add docs and tests
MirandaWood 00f5857
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 7b594ac
fix: fix deploy.nr after portal removal
MirandaWood a163af6
fix: strs as logs, generic size enc log returns
MirandaWood 56c1505
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood ae9eae4
fix: make enc log len generic, replace slices with arrs
MirandaWood 75ec074
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 0e903b8
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood ab41572
feat: impl strs for unenc logs, generate more trait impls
MirandaWood b5227f9
feat: track nested public logs with arr, link counter issue
MirandaWood 533c4cc
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood beddeb9
fix: merge fixes
MirandaWood 247de1e
feat: add logscache, tests, remove old hacks for logs sorting
MirandaWood 4c80dab
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 19c7801
fix: merge issues
MirandaWood 9dadba6
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood fd92b48
chore: increase test timeout + clarify comments
MirandaWood 462ef24
Merge branch 'master' into mw/logs-hash
MirandaWood 57f8b11
fix: merge fix
MirandaWood a922640
feat: address comments + fmt
MirandaWood 5d465fd
feat: flat hash logs in tail/L1
MirandaWood d7a68bd
chore: fmt
MirandaWood 547fd5b
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood e7cc4c4
feat: revert arr, track logs with counters, sort in exec. res.
MirandaWood bfc54a6
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 9af800e
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 4050be3
chore: remove redundant check + comments
MirandaWood 2b25410
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 91c51e7
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 1726fb3
chore: remove comments, remove redundant .reverse
MirandaWood 18b9057
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood e591c87
chore: moved nested logs tests now e2e_nested has been refactored
MirandaWood 5cf5b5a
chore: fmt + update docs for 0.37 release
MirandaWood 529241d
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood afa345f
Merge remote-tracking branch 'origin' into mw/logs-hash
MirandaWood 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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
use crate::{ | ||
context::{inputs::PrivateContextInputs, interface::ContextInterface}, | ||
messaging::process_l1_to_l2_message, hash::{hash_args_array, ArgsHasher}, | ||
messaging::process_l1_to_l2_message, | ||
hash::{hash_args_array, ArgsHasher, compute_encrypted_log_hash, compute_unencrypted_log_hash}, | ||
oracle::{ | ||
arguments, returns, call_private_function::call_private_function_internal, | ||
enqueue_public_function_call::enqueue_public_function_call_internal, header::get_header_at, | ||
logs::emit_encrypted_log, logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog}, | ||
nullifier_key::{get_nullifier_keys, NullifierKeys} | ||
} | ||
}; | ||
|
@@ -28,14 +30,11 @@ use dep::protocol_types::{ | |
MAX_ENCRYPTED_LOGS_PER_CALL, MAX_UNENCRYPTED_LOGS_PER_CALL | ||
}, | ||
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, | ||
grumpkin_private_key::GrumpkinPrivateKey, header::Header, | ||
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, header::Header, | ||
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, | ||
traits::{is_empty, Deserialize, Empty} | ||
}; | ||
|
||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
// use dep::std::collections::vec::Vec; | ||
|
||
// When finished, one can call .finish() to convert back to the abi | ||
struct PrivateContext { | ||
// docs:start:private-context | ||
|
@@ -44,8 +43,8 @@ struct PrivateContext { | |
|
||
min_revertible_side_effect_counter: u32, | ||
|
||
args_hash : Field, | ||
return_hash : Field, | ||
args_hash: Field, | ||
return_hash: Field, | ||
|
||
max_block_number: MaxBlockNumber, | ||
|
||
|
@@ -64,9 +63,10 @@ struct PrivateContext { | |
// Header of a block whose state is used during private execution (not the block the transaction is included in). | ||
historical_header: Header, | ||
|
||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
encrypted_logs_hashes: BoundedVec<SideEffect, MAX_ENCRYPTED_LOGS_PER_CALL>, | ||
unencrypted_logs_hashes: BoundedVec<SideEffect, MAX_UNENCRYPTED_LOGS_PER_CALL>, | ||
encrypted_log_preimages_length: Field, | ||
unencrypted_log_preimages_length: Field, | ||
// encrypted_logs_preimages: Vec<Field>, | ||
// unencrypted_logs_preimages: Vec<Field>, | ||
|
||
|
@@ -132,9 +132,10 @@ impl PrivateContext { | |
private_call_stack_hashes: BoundedVec::new(), | ||
public_call_stack_hashes: BoundedVec::new(), | ||
new_l2_to_l1_msgs: BoundedVec::new(), | ||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
encrypted_logs_hashes: BoundedVec::new(), | ||
unencrypted_logs_hashes: BoundedVec::new(), | ||
encrypted_log_preimages_length: 0, | ||
unencrypted_log_preimages_length: 0, | ||
// encrypted_logs_preimages: Vec::new(), | ||
// unencrypted_logs_preimages: Vec::new(), | ||
nullifier_key: Option::none() | ||
|
@@ -159,11 +160,7 @@ impl PrivateContext { | |
} | ||
|
||
pub fn finish(self) -> PrivateCircuitPublicInputs { | ||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
let encrypted_log_preimages_length = 0; | ||
let unencrypted_log_preimages_length = 0; | ||
|
||
let priv_circuit_pub_inputs = PrivateCircuitPublicInputs { | ||
PrivateCircuitPublicInputs { | ||
call_context: self.inputs.call_context, | ||
args_hash: self.args_hash, | ||
returns_hash: self.return_hash, | ||
|
@@ -181,13 +178,11 @@ impl PrivateContext { | |
end_side_effect_counter: self.side_effect_counter, | ||
encrypted_logs_hashes: self.encrypted_logs_hashes.storage, | ||
unencrypted_logs_hashes: self.unencrypted_logs_hashes.storage, | ||
encrypted_log_preimages_length, | ||
unencrypted_log_preimages_length, | ||
encrypted_log_preimages_length: self.encrypted_log_preimages_length + 4, | ||
unencrypted_log_preimages_length: self.unencrypted_log_preimages_length + 4, | ||
historical_header: self.historical_header, | ||
tx_context: self.inputs.tx_context | ||
}; | ||
|
||
priv_circuit_pub_inputs | ||
} | ||
} | ||
|
||
pub fn end_setup(&mut self) { | ||
|
@@ -258,25 +253,74 @@ impl PrivateContext { | |
} | ||
// docs:end:consume_l1_to_l2_message | ||
|
||
pub fn push_encrypted_log(&mut self, log_hash: Field) { | ||
let side_effect = SideEffect { value: log_hash, counter: self.side_effect_counter }; | ||
self.encrypted_logs_hashes.push(side_effect); | ||
self.side_effect_counter = self.side_effect_counter + 1; | ||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
} | ||
|
||
// TODO: We might want to remove this since emitting unencrypted logs from private functions is violating privacy. | ||
// --> might be a better approach to force devs to make a public function call that emits the log if needed then | ||
// it would be less easy to accidentally leak information. | ||
// If we decide to keep this function around would make sense to wait for traits and then merge it with emit_unencrypted_log. | ||
pub fn emit_unencrypted_log<T>(&mut self, log: T) { | ||
pub fn emit_unencrypted_log<T, N, M>(&mut self, log: T) where T: ToBytesForUnencryptedLog<N, M> { | ||
let event_selector = 5; // TODO: compute actual event selector. | ||
let log_hash = emit_unencrypted_log_private_internal(self.this_address(), event_selector, log); | ||
|
||
let contract_address = self.this_address(); | ||
let log_slice = log.to_be_bytes_arr(); | ||
let log_hash = compute_unencrypted_log_hash( | ||
contract_address, | ||
event_selector, | ||
log, | ||
); | ||
let side_effect = SideEffect { value: log_hash, counter: self.side_effect_counter }; | ||
self.unencrypted_logs_hashes.push(side_effect); | ||
self.side_effect_counter = self.side_effect_counter + 1; | ||
// 44 = addr (32) + selector (4) + raw log len (4) + processed log len (4) | ||
self.unencrypted_log_preimages_length = self.unencrypted_log_preimages_length + 44 + log_slice.len().to_field(); | ||
MirandaWood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// call oracle | ||
let _void = emit_unencrypted_log_private_internal(contract_address, event_selector, log); | ||
} | ||
|
||
// This fn exists separately from emit_unencrypted_log because sha hashing the preimage | ||
// is too large to compile (16,200 fields, 518,400 bytes) => the oracle hashes it | ||
// It is ONLY used with contract_class_registerer_contract since we already assert correctness: | ||
// - Contract class -> we will commit to the packed bytecode (currently a TODO) | ||
// - Private function -> we provide a membership proof | ||
// - Unconstrained function -> we provide a membership proof | ||
// Ordinary logs are not protected by the above so this fn shouldn't be called by anything else | ||
pub fn emit_contract_class_unencrypted_log<N>(&mut self, log: [Field; N]) { | ||
let event_selector = 5; // TODO: compute actual event selector. | ||
let contract_address = self.this_address(); | ||
let log_hash = emit_contract_class_unencrypted_log_private_internal( | ||
contract_address, | ||
event_selector, | ||
log | ||
); | ||
let side_effect = SideEffect { value: log_hash, counter: self.side_effect_counter }; | ||
self.unencrypted_logs_hashes.push(side_effect); | ||
self.side_effect_counter = self.side_effect_counter + 1; | ||
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) | ||
// 44 = addr (32) + selector (4) + raw log len (4) + processed log len (4) | ||
self.unencrypted_log_preimages_length = self.unencrypted_log_preimages_length + 44 + N*32; | ||
} | ||
|
||
pub fn emit_encrypted_log<N, M, L>( | ||
&mut self, | ||
contract_address: AztecAddress, | ||
storage_slot: Field, | ||
note_type_id: Field, | ||
encryption_pub_key: GrumpkinPoint, | ||
preimage: [Field; N] | ||
) where [Field; N]: LensForEncryptedLog<N, M, L> { | ||
// TODO(1139): perform encryption in the circuit | ||
// The oracle call should come last, but we require the encrypted value for now | ||
let encrypted_log: [Field; M] = emit_encrypted_log( | ||
contract_address, | ||
storage_slot, | ||
note_type_id, | ||
encryption_pub_key, | ||
preimage | ||
); | ||
let log_hash = compute_encrypted_log_hash(encrypted_log); | ||
let side_effect = SideEffect { value: log_hash, counter: self.side_effect_counter }; | ||
self.encrypted_logs_hashes.push(side_effect); | ||
self.side_effect_counter = self.side_effect_counter + 1; | ||
let encrypted_log_byte_len = 112 + 32*(N + 3); | ||
// + processed log len (4) | ||
self.encrypted_log_preimages_length = self.encrypted_log_preimages_length + encrypted_log_byte_len + 4; | ||
} | ||
|
||
pub fn call_private_function<ARGS_COUNT>( | ||
|
@@ -546,8 +590,8 @@ impl Empty for PrivateContext { | |
inputs: PrivateContextInputs::empty(), | ||
side_effect_counter: 0 as u32, | ||
min_revertible_side_effect_counter: 0 as u32, | ||
args_hash : 0, | ||
return_hash : 0, | ||
args_hash: 0, | ||
return_hash: 0, | ||
max_block_number: MaxBlockNumber::empty(), | ||
note_hash_read_requests: BoundedVec::new(), | ||
nullifier_read_requests: BoundedVec::new(), | ||
|
@@ -560,6 +604,8 @@ impl Empty for PrivateContext { | |
historical_header: Header::empty(), | ||
encrypted_logs_hashes: BoundedVec::new(), | ||
unencrypted_logs_hashes: BoundedVec::new(), | ||
encrypted_log_preimages_length: 0, | ||
unencrypted_log_preimages_length: 0, | ||
nullifier_key: Option::none(), | ||
} | ||
} | ||
|
@@ -609,3 +655,20 @@ unconstrained pub fn emit_unencrypted_log_private_internal<T>( | |
// https://github.com/AztecProtocol/aztec-packages/issues/885 | ||
emit_unencrypted_log_oracle_private(contract_address, event_selector, message) | ||
} | ||
|
||
#[oracle(emitContractClassUnencryptedLog)] | ||
LeilaWang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn emit_contract_class_unencrypted_log_private<N>( | ||
contract_address: AztecAddress, | ||
event_selector: Field, | ||
message: [Field; N] | ||
) -> Field {} | ||
|
||
|
||
unconstrained pub fn emit_contract_class_unencrypted_log_private_internal<N>( | ||
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. Doesn't need to be public. |
||
contract_address: AztecAddress, | ||
event_selector: Field, | ||
message: [Field; N] | ||
) -> Field { | ||
emit_contract_class_unencrypted_log_private(contract_address, event_selector, message) | ||
} | ||
|
Oops, something went wrong.
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.
The AVM context and interface was only changed to allow the public and private contexts to use traits in
logs_traits
(these define fixed lengths for many types, so we can sha hash inside the circuit). These don't need to be carried over!