Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::macros::aztec;
#[aztec]
contract BoxReact {
use dep::aztec::{
keys::public_keys::{IvpkM, OvpkM},
protocol_types::address::public_keys::{IvpkM, OvpkM},
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
macros::{storage::storage, functions::{private, public, initializer}}
Expand Down
2 changes: 1 addition & 1 deletion boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::macros::aztec;
#[aztec]
contract Vanilla {
use dep::aztec::{
keys::public_keys::{IvpkM, OvpkM},
protocol_types::address::public_keys::{IvpkM, OvpkM},
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
macros::{storage::storage, functions::{private, public, initializer}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_log,
keys::{getters::get_ovsk_app, public_keys::{OvpkM, IvpkM}}, oracle::random::random
encrypted_logs::payload::compute_encrypted_log, keys::getters::get_ovsk_app, oracle::random::random
};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field};
use dep::protocol_types::{address::{AztecAddress, public_keys::{OvpkM, IvpkM}}, hash::sha256_to_field};

fn compute_raw_event_log<Event, let N: u32>(
context: PrivateContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{
context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface},
keys::{getters::get_ovsk_app, public_keys::{PublicKeys, OvpkM, IvpkM}},
encrypted_logs::payload::compute_encrypted_log
keys::getters::get_ovsk_app, encrypted_logs::payload::compute_encrypted_log
};
use dep::protocol_types::{
address::{public_keys::{PublicKeys, OvpkM, IvpkM}, AztecAddress}, hash::sha256_to_field,
abis::note_hash::NoteHash
};
use dep::protocol_types::{hash::sha256_to_field, address::AztecAddress, abis::note_hash::NoteHash};

fn compute_raw_note_log<Note, let N: u32>(
context: PrivateContext,
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/header.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::{address::AztecAddress, scalar::Scalar, point::Point};
use dep::protocol_types::{address::{AztecAddress, public_keys::{PublicKeys, IvpkM, ToPoint}}, scalar::Scalar, point::Point};

use crate::keys::{point_to_symmetric_key::point_to_symmetric_key, public_keys::ToPoint};
use crate::keys::point_to_symmetric_key::point_to_symmetric_key;

use std::aes128::aes128_encrypt;

Expand Down Expand Up @@ -36,7 +36,7 @@ unconstrained fn test_encrypted_log_header_matches_noir() {
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
hi: 0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06
};
let point = crate::keys::public_keys::IvpkM {
let point = IvpkM {
inner: Point {
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
Expand Down
22 changes: 11 additions & 11 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::{
address::AztecAddress, scalar::Scalar, point::Point, constants::GENERATOR_INDEX__SYMMETRIC_KEY,
hash::poseidon2_hash_with_separator
address::{AztecAddress, public_keys::{OvpkM, IvpkM}}, scalar::Scalar, point::Point,
constants::GENERATOR_INDEX__SYMMETRIC_KEY, hash::poseidon2_hash_with_separator
};
use std::{
aes128::aes128_encrypt, embedded_curve_ops::fixed_base_scalar_mul as derive_public_key,
Expand All @@ -9,7 +9,7 @@ use std::{

use crate::{
oracle::random::random, utils::point::point_to_bytes, encrypted_logs::{header::EncryptedLogHeader},
keys::{point_to_symmetric_key::point_to_symmetric_key, public_keys::{OvpkM, IvpkM}}
keys::{point_to_symmetric_key::point_to_symmetric_key}
};

pub fn compute_encrypted_log<let P: u32, let M: u32>(
Expand All @@ -24,10 +24,13 @@ pub fn compute_encrypted_log<let P: u32, let M: u32>(

let header = EncryptedLogHeader::new(contract_address);

let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ivpk);
let address_point = recipient.to_point();
let address_ivpk = IvpkM { inner: address_point };

let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, address_ivpk);
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
let incoming_body_ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, ivpk);
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(recipient, ivpk, fr_to_fq(ovsk_app), eph_sk, eph_pk);
let incoming_body_ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, address_ivpk);
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(recipient, address_ivpk, fr_to_fq(ovsk_app), eph_sk, eph_pk);

let mut encrypted_bytes: [u8; M] = [0; M];
// @todo We ignore the tags for now
Expand Down Expand Up @@ -147,12 +150,9 @@ pub fn compute_outgoing_body_ciphertext(
}

mod test {
use crate::{
encrypted_logs::payload::{compute_encrypted_log, compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext},
keys::public_keys::{OvpkM, IvpkM}
};
use crate::{encrypted_logs::payload::{compute_encrypted_log, compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext}};
use std::embedded_curve_ops::fixed_base_scalar_mul as derive_public_key;
use dep::protocol_types::{address::AztecAddress, point::Point, scalar::Scalar};
use dep::protocol_types::{address::{AztecAddress, public_keys::{OvpkM, IvpkM}}, point::Point, scalar::Scalar};
use std::test::OracleMock;

#[test]
Expand Down
7 changes: 4 additions & 3 deletions noir-projects/aztec-nr/aztec/src/keys/getters/mod.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::address::{AztecAddress, public_keys::PublicKeys};
use crate::{
oracle::{keys::get_public_keys_and_partial_address, key_validation_request::get_key_validation_request},
keys::{public_keys::PublicKeys, constants::{NULLIFIER_INDEX, OUTGOING_INDEX}}
keys::{constants::{NULLIFIER_INDEX, OUTGOING_INDEX}}
};

mod test;
Expand All @@ -24,8 +24,9 @@ pub fn get_public_keys(account: AztecAddress) -> PublicKeys {
let (hinted_canonical_public_keys, partial_address) = unsafe {
get_public_keys_and_partial_address(account)
};

assert_eq(
account, AztecAddress::compute(hinted_canonical_public_keys.hash(), partial_address), "Invalid public keys hint for address"
account, AztecAddress::compute_from_public_keys(hinted_canonical_public_keys, partial_address), "Invalid public keys hint for address"
);

hinted_canonical_public_keys
Expand Down
3 changes: 0 additions & 3 deletions noir-projects/aztec-nr/aztec/src/keys/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod constants;
mod getters;
mod point_to_symmetric_key;
mod public_keys;

pub use crate::keys::public_keys::{PublicKeys, PUBLIC_KEYS_LENGTH};
6 changes: 4 additions & 2 deletions noir-projects/aztec-nr/aztec/src/oracle/keys.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::keys::{PublicKeys, public_keys::{NpkM, IvpkM, OvpkM, TpkM}};
use dep::protocol_types::{address::{AztecAddress, PartialAddress}, point::Point};
use dep::protocol_types::{
address::{public_keys::{PublicKeys, NpkM, IvpkM, OvpkM, TpkM}, AztecAddress, PartialAddress},
point::Point
};

#[oracle(getPublicKeysAndPartialAddress)]
unconstrained fn get_public_keys_and_partial_address_oracle(_address: AztecAddress) -> [Field; 13] {}
Expand Down
3 changes: 1 addition & 2 deletions noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dep::protocol_types::{
abis::function_selector::FunctionSelector, address::AztecAddress,
abis::function_selector::FunctionSelector, address::{AztecAddress, PublicKeys},
constants::CONTRACT_INSTANCE_LENGTH, contract_instance::ContractInstance
};
use crate::context::inputs::PrivateContextInputs;
use crate::test::helpers::utils::TestAccount;
use crate::keys::public_keys::PublicKeys;

unconstrained pub fn reset() {
oracle_reset();
Expand Down
3 changes: 1 addition & 2 deletions noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use dep::protocol_types::{
traits::{Deserialize, Serialize}, address::AztecAddress,
traits::{Deserialize, Serialize}, address::{AztecAddress, PublicKeys, PUBLIC_KEYS_LENGTH},
abis::{function_selector::FunctionSelector, private_circuit_public_inputs::PrivateCircuitPublicInputs},
contract_instance::ContractInstance
};

use crate::context::inputs::PrivateContextInputs;
use crate::context::call_interfaces::CallInterface;
use crate::test::helpers::cheatcodes;
use crate::keys::public_keys::{PUBLIC_KEYS_LENGTH, PublicKeys};

use crate::oracle::{execution::{get_block_number, get_contract_address}};
use protocol_types::constants::PUBLIC_DISPATCH_SELECTOR;
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/utils/point.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::point::Point;

// I am storing the modulus divided by 2 plus 1 here because full modulus would throw "String literal too large" error
// I am storing the modulus minus 1 divided by 2 here because full modulus would throw "String literal too large" error
// Full modulus is 21888242871839275222246405745257275088548364400416034343698204186575808495617
global BN254_FR_MODULUS_DIV_2: Field = 10944121435919637611123202872628637544274182200208017171849102093287904247808;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::authwit::auth_witness;
use dep::aztec::{
protocol_types::{address::PartialAddress, utils::arr_copy_slice},
keys::{PublicKeys, PUBLIC_KEYS_LENGTH}
};
use dep::aztec::protocol_types::{address::{PartialAddress, PublicKeys, PUBLIC_KEYS_LENGTH}, utils::arr_copy_slice};

pub struct AuthWitness {
keys: PublicKeys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use dep::aztec::prelude::{NoteGetterOptions, NoteViewerOptions, NoteInterface, NullifiableNote, PrivateSet};
use dep::aztec::{
context::{PrivateContext, UnconstrainedContext},
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
note::note_emission::OuterNoteEmission, keys::public_keys::NpkM
protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, address::public_keys::NpkM},
note::note_emission::OuterNoteEmission
};
use crate::types::token_note::OwnedNote;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ contract Test {
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note;
use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event_with_randomness_unconstrained;

use dep::aztec::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point};
use dep::aztec::protocol_types::{
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point,
address::public_keys::IvpkM
};

use dep::aztec::note::constants::MAX_NOTES_PER_PAGE;
use dep::aztec::keys::getters::get_public_keys;

use dep::aztec::{
hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, keys::public_keys::IvpkM,
hash::{pedersen_hash, compute_secret_hash, ArgsHasher},
note::{
lifecycle::{create_note, destroy_note_unsafe}, note_getter::{get_notes, view_notes},
note_getter_options::NoteStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use dep::aztec::prelude::{NoteGetterOptions, NoteViewerOptions, NoteInterface, PrivateSet};
use dep::aztec::{
context::{PrivateContext, UnconstrainedContext},
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, address::public_keys::NpkM},
note::{note_interface::NullifiableNote, note_getter::view_notes, note_emission::OuterNoteEmission},
keys::{getters::get_public_keys, public_keys::NpkM}
keys::getters::get_public_keys
};
use crate::types::token_note::OwnedNote;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl PrivateCallDataValidator {
);
// println(f"computed_partial_address={computed_partial_address}");

let computed_address = AztecAddress::compute(self.data.public_keys_hash, computed_partial_address);
let computed_address = AztecAddress::compute_from_public_keys(self.data.public_keys, computed_partial_address);
// println(f"computed_address={computed_address}");

assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn validate_contract_address_incorrect_partial_address_preimage_fails() {
fn validate_contract_address_incorrect_address_preimage_fails() {
let mut builder = PrivateCallDataValidatorBuilder::new();

builder.private_call.public_keys_hash.inner = builder.private_call.public_keys_hash.inner + 1;
builder.private_call.public_keys.ivpk_m.inner.x = builder.private_call.public_keys.ivpk_m.inner.x + 1;

builder.validate();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::{private_call_stack_item::PrivateCallStackItem},
address::{SaltedInitializationHash, PublicKeysHash}, constants::FUNCTION_TREE_HEIGHT,
address::{SaltedInitializationHash, PublicKeysHash, PublicKeys}, constants::FUNCTION_TREE_HEIGHT,
merkle_tree::membership::MembershipWitness, recursion::{verification_key::VerificationKey}
};

Expand All @@ -10,7 +10,7 @@ pub struct PrivateCallData {
vk: VerificationKey,

salted_initialization_hash: SaltedInitializationHash,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
contract_class_artifact_hash: Field,
contract_class_public_bytecode_commitment: Field,
function_leaf_membership_witness: MembershipWitness<FUNCTION_TREE_HEIGHT>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use crate::{
address::{partial_address::PartialAddress, public_keys_hash::PublicKeysHash},
address::{partial_address::PartialAddress, public_keys::PublicKeys, public_keys_hash::PublicKeysHash},
constants::{AZTEC_ADDRESS_LENGTH, GENERATOR_INDEX__CONTRACT_ADDRESS_V1},
hash::poseidon2_hash_with_separator, traits::{Empty, FromField, ToField, Serialize, Deserialize},
utils
};

global BN254_FR_MODULUS_DIV_2: Field = 10944121435919637611123202872628637544274182200208017171849102093287904247808;

// We do below because `use crate::point::Point;` does not work
use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

use std::{
ec::{sqrt, pow},
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar}
};
use crate::constants::GENERATOR_INDEX__PUBLIC_KEYS_HASH;

// Aztec address
pub struct AztecAddress {
inner : Field
Expand Down Expand Up @@ -60,6 +71,32 @@ impl AztecAddress {
)
}

pub fn compute_from_public_keys(public_keys: PublicKeys, partial_address: PartialAddress) -> AztecAddress {
let public_keys_hash = public_keys.hash();

let pre_address = poseidon2_hash_with_separator(
[public_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
);

let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(public_keys.ivpk_m.to_point());
AztecAddress::from_field(address_point.x)
}

pub fn compute_new(
public_keys_hash: PublicKeysHash,
partial_address: PartialAddress,
ivpk_m: Point
) -> AztecAddress {
let pre_address = poseidon2_hash_with_separator(
[public_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
);

let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(ivpk_m);
AztecAddress::from_field(address_point.x)
}

pub fn is_zero(self) -> bool {
self.inner == 0
}
Expand All @@ -72,6 +109,28 @@ impl AztecAddress {
let result = utils::conditional_assign(predicate, rhs.to_field(), lhs.to_field());
Self { inner: result }
}

pub fn to_point(self) -> Point {
// Calculate y^2 = x^3 - 17
let y_squared = pow(self.inner, 3) - 17;

// We can see if y is square first, or we can soft fail with just sqrt(y_squared);
// If y is not square, the x-coordinate is not on the curve
// Do we throw here or soft continue ?
// let y_is_square = is_square(y_squared);
// assert(y_is_square);

let mut y = sqrt(y_squared);

// We can NOT do a check like the below. We do not have access to the sign, and this derivation produces "both" points
// assert(y.lt(BN254_FR_MODULUS_DIV_2) | y.eq(BN254_FR_MODULUS_DIV_2));

if (!(y.lt(BN254_FR_MODULUS_DIV_2) | y.eq(BN254_FR_MODULUS_DIV_2))) {
y = (BN254_FR_MODULUS_DIV_2 + BN254_FR_MODULUS_DIV_2 + 1) - y;
}

Point { x: self.inner, y, is_infinite: false }
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod aztec_address;
mod eth_address;
mod partial_address;
mod public_keys;
mod public_keys_hash;
mod salted_initialization_hash;

pub use aztec_address::AztecAddress;
pub use eth_address::EthAddress;
pub use partial_address::PartialAddress;
pub use public_keys::{PublicKeys, PUBLIC_KEYS_LENGTH};
pub use public_keys_hash::PublicKeysHash;
pub use salted_initialization_hash::SaltedInitializationHash;
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use dep::protocol_types::{
address::PublicKeysHash, constants::GENERATOR_INDEX__PUBLIC_KEYS_HASH,
hash::poseidon2_hash_with_separator, point::{Point, POINT_LENGTH},
use crate::{
address::public_keys_hash::PublicKeysHash, constants::GENERATOR_INDEX__PUBLIC_KEYS_HASH,
hash::poseidon2_hash_with_separator, point::POINT_LENGTH,
traits::{Deserialize, Serialize, Empty, is_empty, Hash}
};

global PUBLIC_KEYS_LENGTH: u32 = 12;
use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

pub global PUBLIC_KEYS_LENGTH: u32 = 12;

pub struct PublicKeys {
npk_m: NpkM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ global GAS_LENGTH: u32 = 2;
global GAS_SETTINGS_LENGTH: u32 = GAS_LENGTH * 2 + GAS_FEES_LENGTH + /* inclusion_fee */ 1;
global CALL_CONTEXT_LENGTH: u32 = 5;
global CONTENT_COMMITMENT_LENGTH: u32 = 4;
global CONTRACT_INSTANCE_LENGTH: u32 = 5;
global CONTRACT_INSTANCE_LENGTH: u32 = 8;
global CONTRACT_STORAGE_READ_LENGTH: u32 = 3;
global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: u32 = 3;
global ETH_ADDRESS_LENGTH: u32 = 1;
Expand Down
Loading