Skip to content

Commit

Permalink
feat!: typing partial address, deduplicating Point, Point -> `Gru…
Browse files Browse the repository at this point in the history
…mpkinPoint` (#3814)

Fixes #3682
  • Loading branch information
benesjan authored Jan 3, 2024
1 parent f091f49 commit 44458be
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 179 deletions.
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use dep::protocol_types::{
storage_update_request::StorageUpdateRequest,
},
hash::hash_args,
point::Point,
grumpkin_point::GrumpkinPoint,
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
Expand Down Expand Up @@ -313,7 +313,7 @@ impl PrivateContext {
global_variables_hash: fields[151],
},
contract_deployment_data: ContractDeploymentData {
deployer_public_key: Point {
deployer_public_key: GrumpkinPoint {
x: fields[152],
y: fields[153],
},
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/history/contract_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::protocol_types::{
new_contract_data::NewContractData as ContractLeafPreimage,
},
address::{AztecAddress, EthAddress},
point::Point,
grumpkin_point::GrumpkinPoint,
};
use dep::std::merkle::compute_merkle_root;

Expand All @@ -22,7 +22,7 @@ use crate::{
// it is what it expects. The constructor param check is the reason of why we pass in the preimage of contract's
// aztec address instead of just the address.
pub fn prove_contract_inclusion(
deployer_public_key: Point,
deployer_public_key: GrumpkinPoint,
contract_address_salt: Field,
function_tree_root: Field,
constructor_hash: Field,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/log.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::context::{PrivateContext, PublicContext};
use crate::oracle;
use crate::types::point::Point;
use dep::protocol_types::{
address::AztecAddress,
grumpkin_point::GrumpkinPoint,
};

pub fn emit_encrypted_log<N>(
context: &mut PrivateContext,
contract_address: AztecAddress,
storage_slot: Field,
encryption_pub_key: Point,
encryption_pub_key: GrumpkinPoint,
log: [Field; N]
) {
let _ = oracle::logs::emit_encrypted_log(contract_address, storage_slot, encryption_pub_key, log);
Expand Down
20 changes: 12 additions & 8 deletions yarn-project/aztec-nr/aztec/src/oracle/get_public_key.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::types::point::Point;
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::{
address::{
AztecAddress,
PartialAddress,
},
grumpkin_point::GrumpkinPoint,
};

#[oracle(getPublicKeyAndPartialAddress)]
fn get_public_key_and_partial_address_oracle(_address: AztecAddress) -> [Field; 3] {}
Expand All @@ -8,14 +13,13 @@ unconstrained fn get_public_key_and_partial_address_internal(address: AztecAddre
get_public_key_and_partial_address_oracle(address)
}

pub fn get_public_key(address: AztecAddress) -> Point {
pub fn get_public_key(address: AztecAddress) -> GrumpkinPoint {
let result = get_public_key_and_partial_address_internal(address);
let pub_key_x = result[0];
let pub_key_y = result[1];
let partial_address = result[2];
let pub_key = GrumpkinPoint::new(result[0], result[1]);
let partial_address = PartialAddress::from_field(result[2]);

let calculated_address = AztecAddress::compute(pub_key_x, pub_key_y, partial_address);
let calculated_address = AztecAddress::compute(pub_key, partial_address);
assert(calculated_address.eq(address));

Point::new(pub_key_x, pub_key_y)
pub_key
}
10 changes: 6 additions & 4 deletions yarn-project/aztec-nr/aztec/src/oracle/get_secret_key.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::oracle::get_public_key::get_public_key;
use crate::types::point::Point;
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::{
address::AztecAddress,
grumpkin_point::GrumpkinPoint,
};

#[oracle(getSecretKey)]
fn get_secret_key_oracle(_owner: Point) -> [Field; dep::std::grumpkin_scalar::GRUMPKIN_SCALAR_SERIALIZED_LEN] {}
fn get_secret_key_oracle(_owner: GrumpkinPoint) -> [Field; dep::std::grumpkin_scalar::GRUMPKIN_SCALAR_SERIALIZED_LEN] {}

unconstrained fn get_secret_key_internal(owner_public_key: Point) -> dep::std::grumpkin_scalar::GrumpkinScalar {
unconstrained fn get_secret_key_internal(owner_public_key: GrumpkinPoint) -> dep::std::grumpkin_scalar::GrumpkinScalar {
dep::std::grumpkin_scalar::deserialize_grumpkin_scalar(get_secret_key_oracle(owner_public_key))
}

Expand Down
22 changes: 18 additions & 4 deletions yarn-project/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
use crate::types::point::Point;
use dep::protocol_types::{
address::AztecAddress,
constants::NUM_FIELDS_PER_SHA256,
grumpkin_point::GrumpkinPoint,
};

// TODO: Should take encrypted data.
#[oracle(emitEncryptedLog)]
fn emit_encrypted_log_oracle<N>(_contract_address: AztecAddress, _storage_slot: Field, _encryption_pub_key: Point, _preimage: [Field; N]) -> Field {}
fn emit_encrypted_log_oracle<N>(
_contract_address: AztecAddress,
_storage_slot: Field,
_encryption_pub_key: GrumpkinPoint,
_preimage: [Field; N]
) -> Field {}

unconstrained pub fn emit_encrypted_log<N>(contract_address: AztecAddress, storage_slot: Field, encryption_pub_key: Point, preimage: [Field; N]) -> [Field; NUM_FIELDS_PER_SHA256] {
unconstrained pub fn emit_encrypted_log<N>(
contract_address: AztecAddress,
storage_slot: Field,
encryption_pub_key: GrumpkinPoint,
preimage: [Field; N]
) -> [Field; NUM_FIELDS_PER_SHA256] {
[emit_encrypted_log_oracle(contract_address, storage_slot, encryption_pub_key, preimage), 0]
}

#[oracle(emitUnencryptedLog)]
fn emit_unencrypted_log_oracle<T>(_contract_address: AztecAddress, _event_selector: Field, _message: T) -> Field {}

unconstrained pub fn emit_unencrypted_log<T>(contract_address: AztecAddress, event_selector: Field, message: T) -> [Field; NUM_FIELDS_PER_SHA256] {
unconstrained pub fn emit_unencrypted_log<T>(
contract_address: AztecAddress,
event_selector: Field,
message: T
) -> [Field; NUM_FIELDS_PER_SHA256] {
// https://github.com/AztecProtocol/aztec-packages/issues/885
[emit_unencrypted_log_oracle(contract_address, event_selector, message), 0]
}
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/types.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod point;
mod vec; // This can/should be moved out into an official noir library
mod type_serialization;
27 changes: 0 additions & 27 deletions yarn-project/aztec-nr/aztec/src/types/point.nr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use dep::protocol_types::constants::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL};
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::{
address::AztecAddress,
constants::{
MAX_NOTES_PER_PAGE,
MAX_READ_REQUESTS_PER_CALL,
},
};
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
note::{
Expand All @@ -9,7 +14,6 @@ use dep::aztec::{
},
oracle::get_secret_key::get_secret_key,
state_vars::set::Set,
types::point::Point,
};
use dep::std;
use dep::std::{
Expand Down Expand Up @@ -88,7 +92,10 @@ struct Deck {
set: Set<ValueNote, VALUE_NOTE_LEN>,
}

pub fn filter_cards<N>(notes: [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL], desired_cards: [Card; N]) -> [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL] {
pub fn filter_cards<N>(
notes: [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL],
desired_cards: [Card; N]
) -> [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL] {
let mut selected = [Option::none(); MAX_READ_REQUESTS_PER_CALL];

let mut found = [false; N];
Expand Down Expand Up @@ -210,8 +217,10 @@ pub fn get_pack_cards(seed: Field, owner: AztecAddress) -> [Card; PACK_CARDS] {
}

pub fn compute_deck_strength<N>(cards: [Card; N]) -> Field {
cards.fold(0,
cards.fold(
0,
|acc, card: Card| {
acc + card.strength as Field
})
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contract InclusionProofs {
AztecAddress,
EthAddress,
},
point::Point,
grumpkin_point::GrumpkinPoint,
};
use dep::aztec::{
state_vars::{
Expand Down Expand Up @@ -211,7 +211,7 @@ contract InclusionProofs {
// contract's aztec address instead of just the address.
#[aztec(private)]
fn test_contract_inclusion_proof(
deployer_public_key: Point,
deployer_public_key: GrumpkinPoint,
contract_address_salt: Field,
function_tree_root: Field,
constructor_hash: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ contract SchnorrHardcodedAccount {
use dep::std;
use dep::aztec::{
abi::{ PrivateCircuitPublicInputs, PrivateContextInputs, Hasher },
types::{ vec::BoundedVec, point::Point },
context::PrivateContext,
types::vec::BoundedVec,
};

use dep::authwit:: {
Expand Down Expand Up @@ -58,10 +58,12 @@ contract SchnorrHardcodedAccount {
}

// Verify signature using hardcoded public key
let verification = std::schnorr::verify_signature(public_key_x,
let verification = std::schnorr::verify_signature(
public_key_x,
public_key_y,
signature,
message_hash.to_be_bytes(32));
message_hash.to_be_bytes(32)
);
assert(verification == true);
true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use dep::aztec::types::point::Point;
use dep::authwit::auth_witness;
use dep::protocol_types::{
address::PartialAddress,
grumpkin_point::GrumpkinPoint,
};

struct AuthWitness {
owner: Point,
owner: GrumpkinPoint,
signature: [u8; 64],
partial_address: Field,
partial_address: PartialAddress,
}

impl AuthWitness {
Expand All @@ -14,9 +17,9 @@ impl AuthWitness {
signature[i] = values[i + 2] as u8;
}
Self {
owner: Point::new(values[0], values[1]),
owner: GrumpkinPoint::new(values[0], values[1]),
signature,
partial_address: values[66],
partial_address: PartialAddress::from_field(values[66]),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddres
);
assert(verification == true);

AztecAddress::compute(witness.owner.x, witness.owner.y, witness.partial_address)
AztecAddress::compute(witness.owner, witness.partial_address)
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
use crate::point::Point;
use crate::address::AztecAddress;
use crate::hash::{compute_partial_address,compute_contract_address_from_partial};
use crate::{
address::{
AztecAddress,
PartialAddress,
},
grumpkin_point::GrumpkinPoint,
};

struct CompleteAddress{
struct CompleteAddress {
address : AztecAddress,
public_key : Point,
// TODO(David): Can we type this as AztecAddress instead of Field?
partial_address: Field,
public_key : GrumpkinPoint,
partial_address: PartialAddress,
}

impl CompleteAddress{
fn assert_is_zero(self) {
self.address.assert_is_zero();
self.public_key.assert_is_zero();
assert(self.partial_address == 0);
self.partial_address.assert_is_zero();
}

pub fn compute(point : Point, contract_address_salt : Field, function_tree_root : Field, constructor_hash : Field) -> CompleteAddress {
let partial_address = compute_partial_address(contract_address_salt, function_tree_root, constructor_hash);
pub fn compute(public_key : GrumpkinPoint, contract_address_salt : Field, function_tree_root : Field, constructor_hash : Field) -> CompleteAddress {
let partial_address = PartialAddress::compute(contract_address_salt, function_tree_root, constructor_hash);

CompleteAddress{
address : compute_contract_address_from_partial(point, partial_address),
public_key : point,
address : AztecAddress::compute(public_key, partial_address),
public_key,
partial_address,
}
}
Expand Down
Loading

0 comments on commit 44458be

Please sign in to comment.