Skip to content
Merged
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
3 changes: 2 additions & 1 deletion noir-projects/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod globals;
mod inputs;

mod packed_returns;
mod private_context;
mod public_context;
mod interface;
Expand All @@ -14,6 +15,6 @@ use call_interfaces::{
PublicStaticVoidCallInterface
};
use private_context::PrivateContext;
use private_context::PackedReturns;
use packed_returns::PackedReturns;
use public_context::PublicContext;
use public_context::FunctionReturns;
31 changes: 31 additions & 0 deletions noir-projects/aztec-nr/aztec/src/context/packed_returns.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{hash::hash_args_array, oracle::returns::unpack_returns};
use dep::protocol_types::traits::Deserialize;

struct PackedReturns {
packed_returns: Field,
}

impl PackedReturns {
pub fn new(packed_returns: Field) -> Self {
PackedReturns { packed_returns }
}

pub fn assert_empty(self) {
assert_eq(self.packed_returns, 0);
}

pub fn raw(self) -> Field {
self.packed_returns
}

pub fn unpack<N>(self) -> [Field; N] {
let unpacked: [Field; N] = unpack_returns(self.packed_returns);
assert_eq(self.packed_returns, hash_args_array(unpacked));
unpacked
}

pub fn unpack_into<T, N>(self) -> T where T: Deserialize<N> {
let unpacked: [Field; N] = self.unpack();
Deserialize::deserialize(unpacked)
}
}
81 changes: 9 additions & 72 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::{
context::{inputs::PrivateContextInputs, interface::ContextInterface},
context::{inputs::PrivateContextInputs, interface::ContextInterface, packed_returns::PackedReturns},
messaging::process_l1_to_l2_message,
hash::{hash_args_array, ArgsHasher, compute_unencrypted_log_hash},
keys::constants::{NULLIFIER_INDEX, OUTGOING_INDEX, NUM_KEY_TYPES, sk_generators},
note::{note_interface::NoteInterface, utils::compute_note_hash_for_insertion},
oracle::{
key_validation_request::get_key_validation_request, arguments, returns,
key_validation_request::get_key_validation_request, arguments, returns::pack_returns,
call_private_function::call_private_function_internal, header::get_header_at,
logs::{emit_encrypted_log, emit_encrypted_note_log, compute_encrypted_log},
logs::{
emit_encrypted_log, emit_encrypted_note_log, compute_encrypted_log,
emit_contract_class_unencrypted_log_private_internal, emit_unencrypted_log_private_internal
},
logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog},
enqueue_public_function_call::{
enqueue_public_function_call_internal, set_public_teardown_function_call_internal,
Expand Down Expand Up @@ -35,8 +38,8 @@ use dep::protocol_types::{
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader,
traits::{is_empty, Deserialize, Empty}, utils::arrays::find_index
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{is_empty, Empty},
utils::arrays::find_index
};

// When finished, one can call .finish() to convert back to the abi
Expand Down Expand Up @@ -153,7 +156,7 @@ impl PrivateContext {
}

pub fn set_return_hash(&mut self, returns_hasher: ArgsHasher) {
returns::pack_returns(returns_hasher.fields);
pack_returns(returns_hasher.fields);
self.return_hash = returns_hasher.hash();
}

Expand Down Expand Up @@ -717,69 +720,3 @@ impl Empty for PrivateContext {
}
}
}

// TODO(#6640)): This should be in its own file
struct PackedReturns {
packed_returns: Field,
}

impl PackedReturns {
pub fn new(packed_returns: Field) -> Self {
PackedReturns { packed_returns }
}

pub fn assert_empty(self) {
assert_eq(self.packed_returns, 0);
}

pub fn raw(self) -> Field {
self.packed_returns
}

pub fn unpack<N>(self) -> [Field; N] {
let unpacked: [Field; N] = returns::unpack_returns(self.packed_returns);
assert_eq(self.packed_returns, hash_args_array(unpacked));
unpacked
}

pub fn unpack_into<T, N>(self) -> T where T: Deserialize<N> {
let unpacked: [Field; N] = self.unpack();
Deserialize::deserialize(unpacked)
}
}

// TODO(#6640)): This should not be here but in oracle folder
#[oracle(emitUnencryptedLog)]
fn emit_unencrypted_log_oracle_private<T>(
_contract_address: AztecAddress,
_event_selector: Field,
_message: T,
_counter: u32
) -> Field {}

unconstrained pub fn emit_unencrypted_log_private_internal<T>(
contract_address: AztecAddress,
event_selector: Field,
message: T,
counter: u32
) -> Field {
emit_unencrypted_log_oracle_private(contract_address, event_selector, message, counter)
}

#[oracle(emitContractClassUnencryptedLog)]
fn emit_contract_class_unencrypted_log_private<N>(
contract_address: AztecAddress,
event_selector: Field,
message: [Field; N],
counter: u32
) -> Field {}

unconstrained pub fn emit_contract_class_unencrypted_log_private_internal<N>(
contract_address: AztecAddress,
event_selector: Field,
message: [Field; N],
counter: u32
) -> Field {
emit_contract_class_unencrypted_log_private(contract_address, event_selector, message, counter)
}

10 changes: 5 additions & 5 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dep::protocol_types::{
use crate::{
context::PrivateContext,
oracle::{keys::get_public_keys_and_partial_address, key_validation_request::get_key_validation_request},
keys::{public_keys::PublicKeys, constants::{NULLIFIER_INDEX, INCOMING_INDEX, OUTGOING_INDEX}},
keys::{public_keys::PublicKeys, constants::{NULLIFIER_INDEX, INCOMING_INDEX, OUTGOING_INDEX, TAGGING_INDEX}},
state_vars::{shared_mutable::shared_mutable_private_getter::SharedMutablePrivateGetter}
};

Expand All @@ -17,7 +17,7 @@ trait KeyGetters {
fn get_npk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint;
fn get_ivpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint;
fn get_ovpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint;
// fn get_tpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint;
fn get_tpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint;
fn get_npk_m_hash(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Field;
}

Expand All @@ -34,9 +34,9 @@ impl KeyGetters for Header {
get_master_key(context, address, OUTGOING_INDEX, self)
}

// fn get_tpk_m(self, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint {
// get_master_key(context, address, TAGGING_INDEX, self)
// }
fn get_tpk_m(self, context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint {
get_master_key(context, address, TAGGING_INDEX, self)
}

fn get_npk_m_hash(self, context: &mut PrivateContext, address: AztecAddress) -> Field {
get_master_key(context, address, NULLIFIER_INDEX, self).hash()
Expand Down
34 changes: 34 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,37 @@ unconstrained pub fn compute_encrypted_log<N, M>(
preimage
)
}

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

unconstrained pub fn emit_unencrypted_log_private_internal<T>(
contract_address: AztecAddress,
event_selector: Field,
message: T,
counter: u32
) -> Field {
emit_unencrypted_log_oracle_private(contract_address, event_selector, message, counter)
}

#[oracle(emitContractClassUnencryptedLog)]
fn emit_contract_class_unencrypted_log_private<N>(
contract_address: AztecAddress,
event_selector: Field,
message: [Field; N],
counter: u32
) -> Field {}

unconstrained pub fn emit_contract_class_unencrypted_log_private_internal<N>(
contract_address: AztecAddress,
event_selector: Field,
message: [Field; N],
counter: u32
) -> Field {
emit_contract_class_unencrypted_log_private(contract_address, event_selector, message, counter)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecAddress, KeyValidationRequest, computeOvskApp, derivePublicKeyFromSecretKey } from '@aztec/circuits.js';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { GrumpkinScalar } from '@aztec/foundation/fields';

import { L1NotePayload } from './l1_note_payload.js';

Expand Down Expand Up @@ -48,10 +48,7 @@ describe('L1 Note Payload', () => {

const getKeyValidationRequest = (ovskM: GrumpkinScalar, app: AztecAddress) => {
const ovskApp = computeOvskApp(ovskM, app);
// TODO(#6640)): get rid of this ugly conversion
const ovskAppFr = Fr.fromBuffer(ovskApp.toBuffer());

const ovpkM = derivePublicKeyFromSecretKey(ovskM);
return new KeyValidationRequest(ovpkM, ovskAppFr);
return new KeyValidationRequest(ovpkM, ovskApp);
};
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AztecAddress,
GrumpkinPrivateKey,
type GrumpkinPrivateKey,
type KeyValidationRequest,
type PublicKey,
computeIvpkApp,
Expand Down Expand Up @@ -106,11 +106,8 @@ export class L1NotePayload {
this.note,
).computeCiphertext(ephSk, ivpkApp);

// TODO(#6640)): do we want the conversion to be here? Unify the type everywhere?
const ovskApp = GrumpkinPrivateKey.fromBuffer(ovKeys.skApp.toBuffer());

const outgoingBodyCiphertext = new EncryptedLogOutgoingBody(ephSk, recipient, ivpkApp).computeCiphertext(
ovskApp,
ovKeys.skAppAsGrumpkinPrivateKey,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks funky without the () 🤷

ephPk,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecAddress, KeyValidationRequest, computeOvskApp, derivePublicKeyFromSecretKey } from '@aztec/circuits.js';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { GrumpkinScalar } from '@aztec/foundation/fields';

import { L1NotePayload } from './l1_note_payload.js';
import { TaggedNote } from './tagged_note.js';
Expand Down Expand Up @@ -52,10 +52,8 @@ describe('L1 Note Payload', () => {

const getKeyValidationRequest = (ovskM: GrumpkinScalar, app: AztecAddress) => {
const ovskApp = computeOvskApp(ovskM, app);
// TODO(#6640)): get rid of this ugly conversion
const ovskAppFr = Fr.fromBuffer(ovskApp.toBuffer());

const ovpkM = derivePublicKeyFromSecretKey(ovskM);
return new KeyValidationRequest(ovpkM, ovskAppFr);

return new KeyValidationRequest(ovpkM, ovskApp);
};
});
19 changes: 16 additions & 3 deletions yarn-project/circuits.js/src/structs/key_validation_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ import { Fr, Point } from '@aztec/foundation/fields';
import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { KEY_VALIDATION_REQUEST_LENGTH } from '../constants.gen.js';
import { GrumpkinPrivateKey } from '../types/grumpkin_private_key.js';

/**
* Request for validating keys used in the app.
*/
export class KeyValidationRequest {
/** App-siloed secret key corresponding to the same underlying secret as master public key above. */
public readonly skApp: Fr;

constructor(
/** Master public key corresponding to the same underlying secret as app secret key below. */
public readonly pkM: Point,
/** App-siloed secret key corresponding to the same underlying secret as master public key above. */
public readonly skApp: Fr,
) {}
skApp: Fr | GrumpkinPrivateKey,
) {
// I am doing this conversion here because in some places skApp is represented as GrumpkinPrivateKey (Fq).
// I can do this conversion even though Fq.MODULUS is larger than Fr.MODULUS because when we pass in
// the skApp as GrumpkinPrivateKey it was converted to that form from Fr. So, it is safe to convert it back
// to Fr. If this would change in the future the code below will throw an error so it should be easy to debug.
this.skApp = skApp instanceof Fr ? skApp : new Fr(skApp.toBigInt());
}

toBuffer() {
return serializeToBuffer(this.pkM, this.skApp);
}

get skAppAsGrumpkinPrivateKey() {
return new GrumpkinPrivateKey(this.skApp.toBigInt());
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new KeyValidationRequest(Point.fromBuffer(reader), Fr.fromBuffer(reader));
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/pxe/src/note_processor/note_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ describe('Note Processor', () => {

ownerIvskM = allOwnerKeys.masterIncomingViewingSecretKey;
ownerIvpkM = allOwnerKeys.publicKeys.masterIncomingViewingPublicKey;
// TODO(#6640)): get rid of this ugly conversion
ownerOvKeys = new KeyValidationRequest(
allOwnerKeys.publicKeys.masterOutgoingViewingPublicKey,
Fr.fromBuffer(computeOvskApp(allOwnerKeys.masterOutgoingViewingSecretKey, app).toBuffer()),
computeOvskApp(allOwnerKeys.masterOutgoingViewingSecretKey, app),
);
});

Expand Down
Loading