Skip to content

Commit

Permalink
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Browse files Browse the repository at this point in the history
…pcode
  • Loading branch information
Maddiaa0 authored May 10, 2024
2 parents 3cf29f3 + c3c602f commit 19fab2e
Show file tree
Hide file tree
Showing 105 changed files with 4,887 additions and 724 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95d4d133d1eb5e0eb44cd928d8183d890e970a13
b541e793e20fa3c991e0328ec2ff7926bdcdfd45
28 changes: 28 additions & 0 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,34 @@ fn handle_black_box_function(avm_instrs: &mut Vec<AvmInstruction>, operation: &B
..Default::default()
});
}
BlackBoxOp::ToRadix {
input,
radix,
output,
} => {
let num_limbs = output.size;
let input_offset = input.0;
let output_offset = output.pointer.0;
assert!(radix <= &256u32, "Radix must be less than or equal to 256");

avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::TORADIXLE,
indirect: Some(FIRST_OPERAND_INDIRECT),
tag: None,
operands: vec![
AvmOperand::U32 {
value: input_offset as u32,
},
AvmOperand::U32 {
value: output_offset as u32,
},
AvmOperand::U32 { value: *radix },
AvmOperand::U32 {
value: num_limbs as u32,
},
],
})
}
_ => panic!("Transpiler doesn't know how to process {:?}", operation),
}
}
Expand Down
74 changes: 69 additions & 5 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ struct BlackBoxOp {
Program::HeapVector inputs;
Program::HeapArray iv;
Program::HeapArray key;
Program::MemoryAddress length;
Program::HeapVector outputs;

friend bool operator==(const AES128Encrypt&, const AES128Encrypt&);
Expand Down Expand Up @@ -896,6 +895,16 @@ struct BlackBoxOp {
static Sha256Compression bincodeDeserialize(std::vector<uint8_t>);
};

struct ToRadix {
Program::MemoryAddress input;
uint32_t radix;
Program::HeapArray output;

friend bool operator==(const ToRadix&, const ToRadix&);
std::vector<uint8_t> bincodeSerialize() const;
static ToRadix bincodeDeserialize(std::vector<uint8_t>);
};

std::variant<AES128Encrypt,
Sha256,
Blake2s,
Expand All @@ -916,7 +925,8 @@ struct BlackBoxOp {
BigIntFromLeBytes,
BigIntToLeBytes,
Poseidon2Permutation,
Sha256Compression>
Sha256Compression,
ToRadix>
value;

friend bool operator==(const BlackBoxOp&, const BlackBoxOp&);
Expand Down Expand Up @@ -3939,9 +3949,6 @@ inline bool operator==(const BlackBoxOp::AES128Encrypt& lhs, const BlackBoxOp::A
if (!(lhs.key == rhs.key)) {
return false;
}
if (!(lhs.length == rhs.length)) {
return false;
}
if (!(lhs.outputs == rhs.outputs)) {
return false;
}
Expand Down Expand Up @@ -5141,6 +5148,63 @@ Program::BlackBoxOp::Sha256Compression serde::Deserializable<Program::BlackBoxOp

namespace Program {

inline bool operator==(const BlackBoxOp::ToRadix& lhs, const BlackBoxOp::ToRadix& rhs)
{
if (!(lhs.input == rhs.input)) {
return false;
}
if (!(lhs.radix == rhs.radix)) {
return false;
}
if (!(lhs.output == rhs.output)) {
return false;
}
return true;
}

inline std::vector<uint8_t> BlackBoxOp::ToRadix::bincodeSerialize() const
{
auto serializer = serde::BincodeSerializer();
serde::Serializable<BlackBoxOp::ToRadix>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline BlackBoxOp::ToRadix BlackBoxOp::ToRadix::bincodeDeserialize(std::vector<uint8_t> input)
{
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<BlackBoxOp::ToRadix>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw_or_abort("Some input bytes were not read");
}
return value;
}

} // end of namespace Program

template <>
template <typename Serializer>
void serde::Serializable<Program::BlackBoxOp::ToRadix>::serialize(const Program::BlackBoxOp::ToRadix& obj,
Serializer& serializer)
{
serde::Serializable<decltype(obj.input)>::serialize(obj.input, serializer);
serde::Serializable<decltype(obj.radix)>::serialize(obj.radix, serializer);
serde::Serializable<decltype(obj.output)>::serialize(obj.output, serializer);
}

template <>
template <typename Deserializer>
Program::BlackBoxOp::ToRadix serde::Deserializable<Program::BlackBoxOp::ToRadix>::deserialize(
Deserializer& deserializer)
{
Program::BlackBoxOp::ToRadix obj;
obj.input = serde::Deserializable<decltype(obj.input)>::deserialize(deserializer);
obj.radix = serde::Deserializable<decltype(obj.radix)>::deserialize(deserializer);
obj.output = serde::Deserializable<decltype(obj.output)>::deserialize(deserializer);
return obj;
}

namespace Program {

inline bool operator==(const BlockId& lhs, const BlockId& rhs)
{
if (!(lhs.value == rhs.value)) {
Expand Down
24 changes: 11 additions & 13 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use dep::protocol_types::{address::AztecAddress, constants::CANONICAL_KEY_REGISTRY_ADDRESS, grumpkin_point::GrumpkinPoint};
use dep::protocol_types::{
address::{AztecAddress, PublicKeysHash}, constants::CANONICAL_KEY_REGISTRY_ADDRESS,
grumpkin_point::GrumpkinPoint
};
use crate::{
context::PrivateContext, oracle::keys::get_public_keys_and_partial_address,
state_vars::{
Expand Down Expand Up @@ -80,20 +83,15 @@ fn fetch_key_from_registry(
fn fetch_and_constrain_keys(address: AztecAddress) -> [GrumpkinPoint; 4] {
let (public_keys, partial_address) = get_public_keys_and_partial_address(address);

let nullifier_pub_key = public_keys[0];
let incoming_pub_key = public_keys[1];
let outgoing_pub_key = public_keys[2];
let tagging_pub_key = public_keys[3];
let npk_m = public_keys[0];
let ivpk_m = public_keys[1];
let ovpk_m = public_keys[2];
let tpk_m = public_keys[3];

let computed_address = AztecAddress::compute_from_public_keys_and_partial_address(
nullifier_pub_key,
incoming_pub_key,
outgoing_pub_key,
tagging_pub_key,
partial_address
);
let public_keys_hash = PublicKeysHash::compute(npk_m, ivpk_m, ovpk_m, tpk_m);
let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

assert(computed_address.eq(address));

[nullifier_pub_key, incoming_pub_key, outgoing_pub_key, tagging_pub_key]
[npk_m, ivpk_m, ovpk_m, tpk_m]
}
22 changes: 5 additions & 17 deletions noir-projects/aztec-nr/aztec/src/oracle/get_public_key.nr
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
use dep::protocol_types::{address::{AztecAddress, PartialAddress, PublicKeysHash}, grumpkin_point::GrumpkinPoint};

#[oracle(getPublicKeyAndPartialAddress)]
fn get_public_key_and_partial_address_oracle(_address: AztecAddress) -> [Field; 3] {}

unconstrained fn get_public_key_and_partial_address_internal(address: AztecAddress) -> [Field; 3] {
get_public_key_and_partial_address_oracle(address)
}
use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint};
use crate::oracle::keys::get_public_keys_and_partial_address;

// To be nuked in my next PR: https://github.com/AztecProtocol/aztec-packages/pull/6219
pub fn get_public_key(address: AztecAddress) -> GrumpkinPoint {
let result = get_public_key_and_partial_address_internal(address);
let pub_key = GrumpkinPoint::new(result[0], result[1]);
let partial_address = PartialAddress::from_field(result[2]);

// TODO(#5830): disabling the following constraint until we update the oracle according to the new key scheme
// let calculated_address = AztecAddress::compute(PublicKeysHash::compute(pub_key), partial_address);
// assert(calculated_address.eq(address));

pub_key
let result = get_public_keys_and_partial_address(address);
result.0[1]
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,10 @@ contract AvmTest {
fn send_l2_to_l1_msg(recipient: EthAddress, content: Field) {
context.message_portal(recipient, content)
}

#[aztec(public-vm)]
fn to_radix_le(input: Field) -> [u8; 10] {
let result: [u8] = input.to_le_radix(/*base=*/ 2, /*limbs=*/ 10);
result.as_array()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract KeyRegistry {

use dep::aztec::{
state_vars::{SharedMutable, Map},
protocol_types::{grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress}}
protocol_types::{grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress, PublicKeysHash}}
};

global KEY_ROTATION_DELAY = 5;
Expand All @@ -27,11 +27,7 @@ contract KeyRegistry {
}

#[aztec(public)]
fn rotate_nullifier_public_key(
address: AztecAddress,
new_nullifier_public_key: GrumpkinPoint,
nonce: Field
) {
fn rotate_npk_m(address: AztecAddress, new_npk_m: GrumpkinPoint, nonce: Field) {
// TODO: (#6137)
if (!address.eq(context.msg_sender())) {
assert_current_call_valid_authwit_public(&mut context, address);
Expand All @@ -41,26 +37,21 @@ contract KeyRegistry {

let npk_m_x_registry = storage.npk_m_x_registry.at(address);
let npk_m_y_registry = storage.npk_m_y_registry.at(address);
npk_m_x_registry.schedule_value_change(new_nullifier_public_key.x);
npk_m_y_registry.schedule_value_change(new_nullifier_public_key.y);
npk_m_x_registry.schedule_value_change(new_npk_m.x);
npk_m_y_registry.schedule_value_change(new_npk_m.y);
}

#[aztec(public)]
fn register(
address: AztecAddress,
partial_address: PartialAddress,
nullifier_public_key: GrumpkinPoint,
incoming_public_key: GrumpkinPoint,
outgoing_public_key: GrumpkinPoint,
tagging_public_key: GrumpkinPoint
npk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
ovpk_m: GrumpkinPoint,
tpk_m: GrumpkinPoint
) {
let computed_address = AztecAddress::compute_from_public_keys_and_partial_address(
nullifier_public_key,
incoming_public_key,
outgoing_public_key,
tagging_public_key,
partial_address
);
let public_keys_hash = PublicKeysHash::compute(npk_m, ivpk_m, ovpk_m, tpk_m);
let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

assert(computed_address.eq(address), "Computed address does not match supplied address");

Expand All @@ -73,14 +64,14 @@ contract KeyRegistry {
// let tpk_m_x_registry = storage.tpk_m_x_registry.at(address);
// let tpk_m_y_registry = storage.tpk_m_y_registry.at(address);

npk_m_x_registry.schedule_value_change(nullifier_public_key.x);
npk_m_y_registry.schedule_value_change(nullifier_public_key.y);
ivpk_m_x_registry.schedule_value_change(incoming_public_key.x);
ivpk_m_y_registry.schedule_value_change(incoming_public_key.y);
npk_m_x_registry.schedule_value_change(npk_m.x);
npk_m_y_registry.schedule_value_change(npk_m.y);
ivpk_m_x_registry.schedule_value_change(ivpk_m.x);
ivpk_m_y_registry.schedule_value_change(ivpk_m.y);
// Commented out as we hit the max enqueued public calls limit when not done so
// ovpk_m_x_registry.schedule_value_change(outgoing_public_key.x);
// ovpk_m_y_registry.schedule_value_change(outgoing_public_key.y);
// tpk_m_x_registry.schedule_value_change(tagging_public_key.x);
// tpk_m_y_registry.schedule_value_change(tagging_public_key.y);
// ovpk_m_x_registry.schedule_value_change(ovpk_m.x);
// ovpk_m_y_registry.schedule_value_change(ovpk_m.y);
// tpk_m_x_registry.schedule_value_change(tpk_m.x);
// tpk_m_y_registry.schedule_value_change(tpk_m.y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ contract SchnorrSingleKeyAccount {

use dep::authwit::{entrypoint::{app::AppPayload, fee::FeePayload}, account::AccountActions};

use crate::{util::recover_address, auth_oracle::get_auth_witness};
// use crate::{util::recover_address, auth_oracle::get_auth_witness};
use crate::auth_oracle::get_auth_witness;

global ACCOUNT_ACTIONS_STORAGE_SLOT = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ use dep::aztec::protocol_types::address::PublicKeysHash;
use dep::std::{schnorr::verify_signature_slice};
use crate::auth_oracle::AuthWitness;

pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddress {
let message_bytes = message_hash.to_be_bytes(32);
let verification = verify_signature_slice(
witness.owner.x,
witness.owner.y,
witness.signature,
message_bytes
);
assert(verification == true);
// TODO(#5830): the following is currently broken because we are no longer able to compute public keys hash
// pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddress {
// let message_bytes = message_hash.to_be_bytes(32);
// let verification = verify_signature_slice(
// witness.owner.x,
// witness.owner.y,
// witness.signature,
// message_bytes
// );
// assert(verification == true);

AztecAddress::compute(
PublicKeysHash::compute(witness.owner),
witness.partial_address
)
}
// AztecAddress::compute(
// PublicKeysHash::compute(witness.owner),
// witness.partial_address
// )
// }
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,6 @@ impl AztecAddress {
)
}

pub fn compute_from_public_keys_and_partial_address(
nullifier_public_key: GrumpkinPoint,
incoming_public_key: GrumpkinPoint,
outgoing_public_key: GrumpkinPoint,
tagging_public_key: GrumpkinPoint,
partial_address: PartialAddress
) -> AztecAddress {
let public_keys_hash = PublicKeysHash::compute_new(
nullifier_public_key,
incoming_public_key,
outgoing_public_key,
tagging_public_key
);

let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

computed_address
}

pub fn is_zero(self) -> bool {
self.inner == 0
}
Expand All @@ -93,7 +74,7 @@ impl AztecAddress {
}

#[test]
fn compute_address_from_partial_and_pubkey() {
fn compute_address_from_partial_and_pub_keys_hash() {
let pub_keys_hash = PublicKeysHash::from_field(1);
let partial_address = PartialAddress::from_field(2);

Expand Down
Loading

0 comments on commit 19fab2e

Please sign in to comment.