Skip to content

Commit

Permalink
refactor: Always use serialize function to get hash preimage in noir …
Browse files Browse the repository at this point in the history
…circuits or when comparing structs etc #3595 (#5439)

Fully resolves #3595.
  • Loading branch information
sklppy88 authored Mar 26, 2024
1 parent 5a6bcef commit 22e0f0d
Showing 1 changed file with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,17 @@ struct CallContext {

impl CallContext {
fn assert_is_zero(self) {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
assert(self.msg_sender.to_field() == 0);
assert(self.storage_contract_address.to_field() == 0);
assert(self.portal_contract_address.to_field() == 0);
assert(self.function_selector.to_field() == 0);
assert(self.is_delegate_call == false);
assert(self.is_static_call == false);
assert(self.side_effect_counter == 0);
let serialized: [Field; CALL_CONTEXT_LENGTH] = self.serialize();

for i in 0..CALL_CONTEXT_LENGTH {
assert(serialized[i] == 0);
}
}
}

impl Eq for CallContext {
fn eq(self, call_context: CallContext) -> bool {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
call_context.msg_sender.eq(self.msg_sender)
& call_context.storage_contract_address.eq(self.storage_contract_address)
& call_context.portal_contract_address.eq(self.portal_contract_address)
& call_context.function_selector.eq(self.function_selector)
& (call_context.is_delegate_call == self.is_delegate_call)
& (call_context.is_static_call == self.is_static_call)
& (call_context.side_effect_counter == self.side_effect_counter)
fn eq(self, other: CallContext) -> bool {
self.serialize() == other.serialize()
}
}

Expand Down Expand Up @@ -80,13 +70,58 @@ impl Deserialize<CALL_CONTEXT_LENGTH> for CallContext {
}

#[test]
fn serialization_of_empty() {
fn serialize_deserialize_of_empty() {
let context: CallContext = dep::std::unsafe::zeroed();
let serialized = context.serialize();
let deserialized = CallContext::deserialize(serialized);
assert(context.eq(deserialized));
}

#[test]
fn assert_is_zero() {
let context: CallContext = dep::std::unsafe::zeroed();
context.assert_is_zero();
}

#[test(should_fail)]
fn not_zero_assert_is_zero() {
let mut context: CallContext = dep::std::unsafe::zeroed();
context.is_delegate_call = true;
context.assert_is_zero();
}

#[test]
fn test_eq() {
let mut context1: CallContext = dep::std::unsafe::zeroed();
let mut context2: CallContext = dep::std::unsafe::zeroed();

context1.is_delegate_call = true;
context2.is_delegate_call = true;

let address: AztecAddress = AztecAddress::from_field(69420);
context1.msg_sender = address;
context2.msg_sender = address;

assert(context1.eq(context2));
}

#[test(should_fail)]
fn not_eq_test_eq() {
let mut context1: CallContext = dep::std::unsafe::zeroed();
let mut context2: CallContext = dep::std::unsafe::zeroed();

context1.is_delegate_call = true;
context2.is_delegate_call = false;

let address1: AztecAddress = AztecAddress::from_field(69420);
let address2: AztecAddress = AztecAddress::from_field(42069);

context1.msg_sender = address1;
context2.msg_sender = address2;

assert(context1.eq(context2));
}

#[test]
fn hash_smoke() {
let context: CallContext = dep::std::unsafe::zeroed();
Expand Down

0 comments on commit 22e0f0d

Please sign in to comment.