Skip to content

Commit

Permalink
refactor: Cleanup duplicated methods for structs after traits (AztecP…
Browse files Browse the repository at this point in the history
…rotocol#3912)

After adding the Empty and Eq trait, we can remove duplicated methods
for fields and structs.
  • Loading branch information
sirasistant authored Jan 10, 2024
1 parent 5c6bc1c commit 8711b25
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ use dep::types::{
arrays::{
array_length,
array_to_bounded_vec,
is_empty_array,
is_empty_struct_array,
struct_array_to_bounded_vec,
validate_array,
validate_struct_array,
},
bounded_vec::BoundedVec,
},
traits::is_empty_array,
};

pub fn validate_arrays(app_public_inputs: PrivateCircuitPublicInputs) {
Expand All @@ -53,15 +50,9 @@ pub fn validate_arrays(app_public_inputs: PrivateCircuitPublicInputs) {
// to push_array_to_array() routines which rely on the passed arrays to be well-formed.

validate_array(app_public_inputs.return_values);
validate_struct_array(app_public_inputs.read_requests, |r: SideEffect| r.is_empty());
validate_struct_array(
app_public_inputs.new_commitments,
|c: SideEffect| c.is_empty()
);
validate_struct_array(
app_public_inputs.new_nullifiers,
|n: SideEffectLinkedToNoteHash| n.is_empty()
);
validate_array(app_public_inputs.read_requests);
validate_array(app_public_inputs.new_commitments);
validate_array(app_public_inputs.new_nullifiers);
validate_array(app_public_inputs.private_call_stack_hashes);
validate_array(app_public_inputs.public_call_stack_hashes);
validate_array(app_public_inputs.new_l2_to_l1_msgs);
Expand Down Expand Up @@ -114,13 +105,13 @@ pub fn initialize_end_values(
// functions within this circuit:
let start = previous_kernel.public_inputs.end;

public_inputs.end.read_requests = struct_array_to_bounded_vec(start.read_requests, |c: SideEffect| c.is_empty(), SideEffect::empty());
public_inputs.end.read_requests = array_to_bounded_vec(start.read_requests);

public_inputs.end.new_commitments = struct_array_to_bounded_vec(start.new_commitments, |c: SideEffect| c.is_empty(), SideEffect::empty());
public_inputs.end.new_nullifiers = struct_array_to_bounded_vec(start.new_nullifiers, |c: SideEffectLinkedToNoteHash| c.is_empty(), SideEffectLinkedToNoteHash::empty());
public_inputs.end.new_commitments = array_to_bounded_vec(start.new_commitments);
public_inputs.end.new_nullifiers = array_to_bounded_vec(start.new_nullifiers);

public_inputs.end.private_call_stack = struct_array_to_bounded_vec(start.private_call_stack, |c: CallRequest| c.is_empty(), CallRequest::empty());
public_inputs.end.public_call_stack = struct_array_to_bounded_vec(start.public_call_stack, |c: CallRequest| c.is_empty(), CallRequest::empty());
public_inputs.end.private_call_stack = array_to_bounded_vec(start.private_call_stack);
public_inputs.end.public_call_stack = array_to_bounded_vec(start.public_call_stack);
public_inputs.end.new_l2_to_l1_msgs = array_to_bounded_vec(start.new_l2_to_l1_msgs);

public_inputs.end.encrypted_logs_hash = start.encrypted_logs_hash;
Expand All @@ -130,7 +121,7 @@ pub fn initialize_end_values(
public_inputs.end.unencrypted_log_preimages_length = start.unencrypted_log_preimages_length;

public_inputs.end.optionally_revealed_data = start.optionally_revealed_data;
public_inputs.end.new_contracts = struct_array_to_bounded_vec(start.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::empty());
public_inputs.end.new_contracts = array_to_bounded_vec(start.new_contracts);
}

fn perform_static_call_checks(private_call: PrivateCallData) {
Expand All @@ -139,13 +130,10 @@ fn perform_static_call_checks(private_call: PrivateCallData) {
if is_static_call {
// No state changes are allowed for static calls:
assert(
is_empty_struct_array(public_inputs.new_commitments, |c: SideEffect| c.is_empty()), "new_commitments must be empty for static calls"
is_empty_array(public_inputs.new_commitments), "new_commitments must be empty for static calls"
);
assert(
is_empty_struct_array(
public_inputs.new_nullifiers,
|n: SideEffectLinkedToNoteHash| n.is_empty()
), "new_nullifiers must be empty for static calls"
is_empty_array(public_inputs.new_nullifiers), "new_nullifiers must be empty for static calls"
);
}
}
Expand Down Expand Up @@ -241,23 +229,15 @@ pub fn update_end_values(private_call: PrivateCallData, public_inputs: &mut Kern

// Call stacks
// Private call stack.
let private_call_stack = struct_array_to_bounded_vec(
private_call.private_call_stack,
|c: CallRequest| c.is_empty(),
CallRequest::empty()
);
let private_call_stack = array_to_bounded_vec(private_call.private_call_stack);
validate_call_requests(
private_call_stack,
private_call_public_inputs.private_call_stack_hashes,
private_call
);
public_inputs.end.private_call_stack.push_vec(private_call_stack);
// Public call stack.
let public_call_stack = struct_array_to_bounded_vec(
private_call.public_call_stack,
|c: CallRequest| c.is_empty(),
CallRequest::empty()
);
let public_call_stack = array_to_bounded_vec(private_call.public_call_stack);
validate_call_requests(
public_call_stack,
private_call_public_inputs.public_call_stack_hashes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dep::types::{
},
mocked::{Proof, verify_previous_kernel_state},
transaction::request::TxRequest,
utils::arrays::{is_empty_array, is_empty_struct_array},
traits::is_empty_array,
};

// Initialization struct for private inputs to the private kernel
Expand Down Expand Up @@ -135,7 +135,7 @@ mod tests {
},
tests::private_call_data_builder::PrivateCallDataBuilder,
transaction::request::TxRequest,
utils::arrays::{array_length, struct_array_length},
utils::arrays::array_length,
};

struct PrivateKernelInitInputsBuilder {
Expand Down Expand Up @@ -520,7 +520,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -536,7 +536,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -552,7 +552,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -568,7 +568,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -584,7 +584,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 1);
assert_eq(array_length(public_inputs.end.read_requests), 1);
}

#[test]
Expand All @@ -601,7 +601,7 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 1);
assert_eq(array_length(public_inputs.end.read_requests), 1);
}

#[test]
Expand All @@ -617,8 +617,6 @@ mod tests {
assert_eq(public_inputs.end.new_nullifiers[0].value, builder.tx_request.hash());

// non-transient read requests are NOT forwarded
assert_eq(
struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), MAX_READ_REQUESTS_PER_CALL
);
assert_eq(array_length(public_inputs.end.read_requests), MAX_READ_REQUESTS_PER_CALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use dep::types::{
side_effect::{SideEffect, SideEffectLinkedToNoteHash},
},
mocked::verify_previous_kernel_state,
utils::arrays::{array_length, struct_array_length},
};

struct PrivateKernelInputsInner {
Expand Down Expand Up @@ -97,7 +96,7 @@ mod tests {
address::AztecAddress,
hash::compute_logs_hash,
utils::{
arrays::{array_length, struct_array_length},
arrays::array_length,
bounded_vec::BoundedVec,
},
};
Expand Down Expand Up @@ -626,7 +625,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -638,7 +637,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -650,7 +649,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -662,7 +661,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 0);
assert_eq(array_length(public_inputs.end.read_requests), 0);
}

#[test]
Expand All @@ -674,7 +673,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 1);
assert_eq(array_length(public_inputs.end.read_requests), 1);
}

#[test]
Expand All @@ -688,7 +687,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), 1);
assert_eq(array_length(public_inputs.end.read_requests), 1);
}

#[test]
Expand All @@ -700,9 +699,7 @@ mod tests {
let public_inputs = builder.execute();

// non-transient read requests are NOT forwarded
assert_eq(
struct_array_length(public_inputs.end.read_requests, |r: SideEffect| r.is_empty()), MAX_READ_REQUESTS_PER_CALL
);
assert_eq(array_length(public_inputs.end.read_requests), MAX_READ_REQUESTS_PER_CALL);
}

#[test]
Expand Down
Loading

0 comments on commit 8711b25

Please sign in to comment.