Skip to content

Commit

Permalink
refactor: replacing unsafe::zeroed() (#5685)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Apr 11, 2024
1 parent 19dbe46 commit ea3884e
Show file tree
Hide file tree
Showing 65 changed files with 761 additions and 154 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::traits::Serialize;
use dep::protocol_types::traits::{Serialize, Empty};

global PRIVATE_GLOBAL_VARIABLES_LENGTH: u64 = 2;

Expand All @@ -15,3 +15,12 @@ impl Serialize<PRIVATE_GLOBAL_VARIABLES_LENGTH> for PrivateGlobalVariables {
[self.chain_id, self.version]
}
}

impl Empty for PrivateGlobalVariables {
fn empty() -> Self {
PrivateGlobalVariables {
chain_id: 0,
version: 0,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dep::protocol_types::{abis::call_context::CallContext, header::Header};
use dep::protocol_types::{abis::call_context::CallContext, header::Header, traits::Empty};
use crate::context::globals::private_global_variables::PrivateGlobalVariables;


// PrivateContextInputs are expected to be provided to each private function
// docs:start:private-context-inputs
struct PrivateContextInputs {
Expand All @@ -10,3 +11,14 @@ struct PrivateContextInputs {
start_side_effect_counter: u32,
}
// docs:end:private-context-inputs

impl Empty for PrivateContextInputs {
fn empty() -> Self {
PrivateContextInputs {
call_context : CallContext::empty(),
historical_header: Header::empty(),
private_global_variables: PrivateGlobalVariables::empty(),
start_side_effect_counter: 0 as u32,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::context::globals::public_global_variables::PublicGlobalVariables;

use dep::protocol_types::{abis::call_context::CallContext, header::Header};
use dep::protocol_types::{abis::call_context::CallContext, header::Header, traits::Empty};

// PublicContextInputs are expected to be provided to each public function
// docs:start:public-context-inputs
Expand All @@ -13,3 +13,14 @@ struct PublicContextInputs {
start_side_effect_counter: u32,
}
// docs:end:public-context-inputs

impl Empty for PublicContextInputs {
fn empty() -> Self {
PublicContextInputs {
call_context: CallContext::empty(),
historical_header: Header::empty(),
public_global_variables: PublicGlobalVariables::empty(),
start_side_effect_counter: 0 as u32,
}
}
}
27 changes: 25 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use dep::protocol_types::{
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{is_empty, Deserialize}
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{is_empty, Deserialize, Empty}
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
Expand Down Expand Up @@ -130,7 +130,7 @@ impl PrivateContext {
min_revertible_side_effect_counter,
args_hash,
return_hash: 0,
max_block_number: MaxBlockNumber::default(),
max_block_number: MaxBlockNumber::empty(),
note_hash_read_requests: BoundedVec::new(),
nullifier_read_requests: BoundedVec::new(),
nullifier_key_validation_requests: BoundedVec::new(),
Expand Down Expand Up @@ -531,6 +531,29 @@ impl PrivateContext {
}
}

impl Empty for PrivateContext {
fn empty() -> Self {
PrivateContext {
inputs: PrivateContextInputs::empty(),
side_effect_counter: 0 as u32,
min_revertible_side_effect_counter: 0 as u32,
args_hash : 0,
return_hash : 0,
max_block_number: MaxBlockNumber::empty(),
note_hash_read_requests: BoundedVec::new(),
nullifier_read_requests: BoundedVec::new(),
nullifier_key_validation_requests: BoundedVec::new(),
new_note_hashes: BoundedVec::new(),
new_nullifiers: BoundedVec::new(),
private_call_stack_hashes : BoundedVec::new(),
public_call_stack_hashes : BoundedVec::new(),
new_l2_to_l1_msgs : BoundedVec::new(),
historical_header: Header::empty(),
nullifier_key: Option::none(),
}
}
}

struct PackedReturns {
packed_returns: Field,
}
Expand Down
25 changes: 24 additions & 1 deletion noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use dep::protocol_types::{
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::Deserialize
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{Deserialize, Empty}
};

struct PublicContext {
Expand Down Expand Up @@ -316,6 +316,29 @@ impl PublicContextInterface for PublicContext {
}
}

impl Empty for PublicContext {
fn empty() -> Self {
PublicContext {
inputs: PublicContextInputs::empty(),
side_effect_counter: 0 as u32,
args_hash : 0,
return_hash : 0,
nullifier_read_requests: BoundedVec::new(),
nullifier_non_existent_read_requests: BoundedVec::new(),
contract_storage_update_requests: BoundedVec::new(),
contract_storage_reads: BoundedVec::new(),
public_call_stack_hashes: BoundedVec::new(),
new_note_hashes: BoundedVec::new(),
new_nullifiers: BoundedVec::new(),
new_l2_to_l1_msgs: BoundedVec::new(),
unencrypted_logs_hash: 0,
unencrypted_logs_preimages_length: 0,
historical_header: Header::empty(),
prover_address: AztecAddress::zero(),
}
}
}

#[oracle(checkNullifierExists)]
fn nullifier_exists_oracle(nullifier: Field) -> Field {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<T, DELAY> SharedMutable<T, DELAY> {
}

mod test {
use dep::std::{unsafe, merkle::compute_merkle_root, test::OracleMock};
use dep::std::{merkle::compute_merkle_root, test::OracleMock};

use crate::{
context::{PublicContext, PrivateContext, Context},
Expand All @@ -116,11 +116,11 @@ mod test {

fn create_context(block_number: Field, private: bool) -> Context {
if private {
let mut private_context: PrivateContext = unsafe::zeroed();
let mut private_context = PrivateContext::empty();
private_context.historical_header.global_variables.block_number = block_number;
Context::private(&mut private_context)
} else {
let mut public_context: PublicContext = unsafe::zeroed();
let mut public_context = PublicContext::empty();
public_context.inputs.public_global_variables.block_number = block_number;
Context::public(&mut public_context)
}
Expand Down
14 changes: 7 additions & 7 deletions noir-projects/aztec-nr/tests/src/note_getter_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::mock::test_note::TestNote;

#[test]
fn sets_note_manually_and_fetches_it() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();
context.inputs.call_context.storage_contract_address = AztecAddress::from_field(69);

let mut test_note = TestNote::new(1337);
Expand All @@ -28,7 +28,7 @@ fn sets_note_manually_and_fetches_it() {

#[test(should_fail)]
fn cannot_return_zero_notes() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();
let storage_slot: Field = 0;
let mut opt_notes: [Option<TestNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];

Expand All @@ -38,7 +38,7 @@ fn cannot_return_zero_notes() {

#[test(should_fail)]
fn mismatched_address() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();
context.inputs.call_context.storage_contract_address = AztecAddress::from_field(1);

let storage_slot: Field = 0;
Expand All @@ -51,7 +51,7 @@ fn mismatched_address() {

#[test(should_fail)]
fn mismatched_storage_slot() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();
context.inputs.call_context.storage_contract_address = AztecAddress::from_field(1);

let mut test_note = TestNote::new(1);
Expand All @@ -68,7 +68,7 @@ fn mismatched_storage_slot() {

#[test(should_fail)]
fn invalid_selector() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();
context.inputs.call_context.storage_contract_address = AztecAddress::from_field(1);

let mut test_note = TestNote::new(1);
Expand All @@ -90,7 +90,7 @@ fn invalid_selector() {

#[test(should_fail)]
fn invalid_note_order() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();

let mut opt_notes: [Option<TestNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];
opt_notes[0] = Option::some(TestNote::new(1));
Expand All @@ -108,7 +108,7 @@ fn invalid_note_order() {

#[test]
fn sparse_notes_array() {
let mut context: PrivateContext = dep::std::unsafe::zeroed();
let mut context = PrivateContext::empty();

let mut opt_notes: [Option<TestNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];
opt_notes[1] = Option::some(TestNote::new(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use dep::types::{mocked::AggregationObject};
use dep::types::{mocked::AggregationObject, traits::Empty};

struct ParityPublicInputs {
aggregation_object: AggregationObject,
sha_root: Field,
converted_root: Field,
}

impl Empty for ParityPublicInputs {
fn empty() -> Self {
ParityPublicInputs {
aggregation_object: AggregationObject::empty(),
sha_root: 0,
converted_root: 0,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use dep::types::mocked::Proof;
use dep::types::{
mocked::Proof,
traits::Empty
};
use crate::parity_public_inputs::ParityPublicInputs;

struct RootParityInput {
proof: Proof,
public_inputs: ParityPublicInputs,
}

impl Empty for RootParityInput {
fn empty() -> Self {
RootParityInput {
proof: Proof::empty(),
public_inputs: ParityPublicInputs::empty(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::common;
use dep::std::{cmp::Eq, option::Option, unsafe};
use dep::std::{cmp::Eq, option::Option};
use dep::reset_kernel_lib::{NullifierReadRequestHints, PrivateValidationRequestProcessor};
use dep::types::{
abis::{
Expand Down Expand Up @@ -40,7 +40,7 @@ impl KernelCircuitPublicInputsComposer {
sorted_nullifiers_indexes: [u64; MAX_NEW_NULLIFIERS_PER_TX],
transient_note_hash_index_hints: [u64; MAX_NEW_NULLIFIERS_PER_TX]
) -> Self {
let public_inputs: PrivateKernelCircuitPublicInputsBuilder = unsafe::zeroed();
let public_inputs = PrivateKernelCircuitPublicInputsBuilder::empty();

KernelCircuitPublicInputsComposer {
public_inputs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::common;
use dep::std::unsafe;
use dep::types::{
abis::{
combined_constant_data::CombinedConstantData, private_kernel::private_call_data::PrivateCallData,
Expand Down Expand Up @@ -77,7 +76,7 @@ impl PrivateKernelInitCircuitPrivateInputs {
}

pub fn native_private_kernel_circuit_initial(self) -> PrivateKernelCircuitPublicInputs {
let mut public_inputs: PrivateKernelCircuitPublicInputsBuilder = unsafe::zeroed();
let mut public_inputs = PrivateKernelCircuitPublicInputsBuilder::empty();

self.initialize_end_values(&mut public_inputs);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::common;
use dep::std::unsafe;
use dep::types::{
abis::{
kernel_data::PrivateKernelData, private_kernel::private_call_data::PrivateCallData,
Expand Down Expand Up @@ -27,7 +26,7 @@ impl PrivateKernelInnerCircuitPrivateInputs {
}

pub fn native_private_kernel_circuit_inner(self) -> PrivateKernelCircuitPublicInputs {
let mut public_inputs : PrivateKernelCircuitPublicInputsBuilder = unsafe::zeroed();
let mut public_inputs = PrivateKernelCircuitPublicInputsBuilder::empty();

common::validate_previous_kernel_values(self.previous_kernel.public_inputs.end);

Expand Down Expand Up @@ -73,7 +72,6 @@ mod tests {
messaging::l2_to_l1_message::L2ToL1Message, utils::{arrays::array_length},
tests::{private_call_data_builder::PrivateCallDataBuilder, fixture_builder::FixtureBuilder}
};
use dep::std::unsafe;

struct PrivateKernelInnerInputsBuilder {
previous_kernel: FixtureBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ impl PrivateKernelTailCircuitPrivateInputs {
}

mod tests {
use dep::std::unsafe;
use crate::private_kernel_tail::PrivateKernelTailCircuitPrivateInputs;
use dep::reset_kernel_lib::{
tests::nullifier_read_request_hints_builder::NullifierReadRequestHintsBuilder,
read_request_reset::{PendingReadHint, ReadRequestState, ReadRequestStatus}
};
use dep::types::constants::{
MAX_NOTE_HASH_READ_REQUESTS_PER_TX, MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX,
MAX_NULLIFIER_READ_REQUESTS_PER_TX
MAX_NULLIFIER_READ_REQUESTS_PER_TX, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX
};
use dep::types::{
abis::{
kernel_circuit_public_inputs::KernelCircuitPublicInputs, max_block_number::MaxBlockNumber,
side_effect::{SideEffect, SideEffectLinkedToNoteHash, Ordered}
},
grumpkin_private_key::GrumpkinPrivateKey,
hash::{compute_note_hash_nonce, compute_unique_siloed_note_hash},
tests::{fixture_builder::FixtureBuilder, sort::sort_get_sorted_hints},
utils::{arrays::{array_eq, array_length}}, traits::{Empty, is_empty, is_empty_array}
Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
sorted_new_nullifiers_indexes,
nullifier_read_request_hints: self.nullifier_read_request_hints_builder.to_hints(),
nullifier_commitment_hints: sorted_nullifier_commitment_hints,
master_nullifier_secret_keys: unsafe::zeroed()
master_nullifier_secret_keys: [GrumpkinPrivateKey::empty(); MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX]
};
kernel.native_private_kernel_circuit_tail()
}
Expand Down
Loading

0 comments on commit ea3884e

Please sign in to comment.