Skip to content
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

refactor: Always use serialize function to get hash preimage in noir circuits or when comparing structs etc #3595 #5439

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
Loading