Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jul 9, 2024
1 parent bd26f55 commit 0b73463
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 93 deletions.
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,4 +1,4 @@
use dep::protocol_types::{address::AztecAddress, embedded_curve_scalar::EmbeddedCurveScalar, point::Point};
use dep::protocol_types::{address::AztecAddress, scalar::Scalar, point::Point};

use crate::keys::point_to_symmetric_key::point_to_symmetric_key;

Expand All @@ -13,7 +13,7 @@ impl EncryptedLogHeader {
EncryptedLogHeader { address }
}

fn compute_ciphertext(self, secret: EmbeddedCurveScalar, point: Point) -> [u8; 48] {
fn compute_ciphertext(self, secret: Scalar, point: Point) -> [u8; 48] {
let full_key = point_to_symmetric_key(secret, point);
let mut sym_key = [0; 16];
let mut iv = [0; 16];
Expand All @@ -32,7 +32,7 @@ impl EncryptedLogHeader {
fn test_encrypted_log_header() {
let address = AztecAddress::from_field(0xdeadbeef);
let header = EncryptedLogHeader::new(address);
let secret = EmbeddedCurveScalar::new(
let secret = Scalar::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd
);
Expand Down
11 changes: 5 additions & 6 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::note::note_interface::NoteInterface;
use crate::event::event_interface::EventInterface;
use dep::protocol_types::{embedded_curve_scalar::EmbeddedCurveScalar, point::Point};
use dep::protocol_types::{scalar::Scalar, point::Point};

use std::aes128::aes128_encrypt;
use crate::keys::point_to_symmetric_key::point_to_symmetric_key;
Expand All @@ -20,7 +20,7 @@ impl<M> EncryptedLogIncomingBody<M> {
EncryptedLogIncomingBody { plaintext }
}

pub fn compute_ciphertext(self, eph_sk: EmbeddedCurveScalar, ivpk_app: Point) -> [u8] {
pub fn compute_ciphertext(self, eph_sk: Scalar, ivpk_app: Point) -> [u8] {
let full_key = point_to_symmetric_key(eph_sk, ivpk_app);
let mut sym_key = [0; 16];
let mut iv = [0; 16];
Expand All @@ -37,8 +37,7 @@ mod test {
use crate::encrypted_logs::incoming_body::EncryptedLogIncomingBody;
use dep::protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
embedded_curve_scalar::EmbeddedCurveScalar, point::Point, traits::Serialize,
abis::event_selector::EventSelector
scalar::Scalar, point::Point, traits::Serialize, abis::event_selector::EventSelector
};

use crate::{
Expand Down Expand Up @@ -119,7 +118,7 @@ mod test {

let storage_slot = 2;

let eph_sk = EmbeddedCurveScalar::new(
let eph_sk = Scalar::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd
);
Expand Down Expand Up @@ -218,7 +217,7 @@ mod test {
fn test_encrypted_log_event_incoming_body() {
let test_event = TestEvent { value0: 1, value1: 2, value2: 3 };

let eph_sk = EmbeddedCurveScalar::new(
let eph_sk = Scalar::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd
);
Expand Down
25 changes: 12 additions & 13 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/outgoing_body.nr
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
use dep::protocol_types::{
address::AztecAddress, embedded_curve_scalar::EmbeddedCurveScalar, point::Point,
constants::GENERATOR_INDEX__SYMMETRIC_KEY, hash::poseidon2_hash
address::AztecAddress, scalar::Scalar, point::Point, constants::GENERATOR_INDEX__SYMMETRIC_KEY,
hash::poseidon2_hash
};

use std::aes128::aes128_encrypt;

use crate::keys::point_to_symmetric_key::point_to_symmetric_key;

struct EncryptedLogOutgoingBody {
eph_sk: EmbeddedCurveScalar,
eph_sk: Scalar,
recipient: AztecAddress,
recipient_ivpk_app: Point,
}

impl EncryptedLogOutgoingBody {
pub fn new(eph_sk: EmbeddedCurveScalar, recipient: AztecAddress, recipient_ivpk_app: Point) -> Self {
pub fn new(eph_sk: Scalar, recipient: AztecAddress, recipient_ivpk_app: Point) -> Self {
Self { eph_sk, recipient, recipient_ivpk_app }
}

pub fn compute_ciphertext(self, ovsk_app: EmbeddedCurveScalar, eph_pk: Point) -> [u8; 176] {
pub fn compute_ciphertext(self, ovsk_app: Scalar, eph_pk: Point) -> [u8; 176] {
// Again, we could compute `eph_pk` here, but we keep the interface more similar
// and also make it easier to optimise it later as we just pass it along

let mut buffer: [u8; 160] = [0; 160];

let serialized_eph_sk: [Field; 2] = self.eph_sk.serialize();
let serialized_eph_sk_high = serialized_eph_sk[0].to_be_bytes(32);
let serialized_eph_sk_low = serialized_eph_sk[1].to_be_bytes(32);
let serialized_eph_sk_high = self.eph_sk.hi.to_be_bytes(32);
let serialized_eph_sk_low = self.eph_sk.lo.to_be_bytes(32);

let address_bytes = self.recipient.to_field().to_be_bytes(32);
let serialized_recipient_ivpk_app = self.recipient_ivpk_app.serialize();
Expand All @@ -44,7 +43,7 @@ impl EncryptedLogOutgoingBody {
// We compute the symmetric key using poseidon.
let full_key: [u8; 32] = poseidon2_hash(
[
ovsk_app.high, ovsk_app.low, eph_pk.x, eph_pk.y,
ovsk_app.hi, ovsk_app.lo, eph_pk.x, eph_pk.y,
GENERATOR_INDEX__SYMMETRIC_KEY as Field
]
).to_be_bytes(32).as_array();
Expand All @@ -64,22 +63,22 @@ mod test {
use crate::encrypted_logs::outgoing_body::EncryptedLogOutgoingBody;
use dep::protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
embedded_curve_scalar::EmbeddedCurveScalar, point::Point, hash::poseidon2_hash
scalar::Scalar, point::Point, hash::poseidon2_hash
};

use crate::context::PrivateContext;

#[test]
fn test_encrypted_log_outgoing_body() {
let eph_sk = EmbeddedCurveScalar::new(
let eph_sk = Scalar::new(
0x000000000000000000000000000000000f096b423017226a18461115fa8d34bb,
0x00000000000000000000000000000000d0d302ee245dfaf2807e604eec4715fe
);
let recipient_ivsk_app = EmbeddedCurveScalar::new(
let recipient_ivsk_app = Scalar::new(
0x000000000000000000000000000000000f4d97c25d578f9348251a71ca17ae31,
0x000000000000000000000000000000004828f8f95676ebb481df163f87fd4022
);
let sender_ovsk_app = EmbeddedCurveScalar::new(
let sender_ovsk_app = Scalar::new(
0x00000000000000000000000000000000089c6887cb1446d86c64e81afc78048b,
0x0000000000000000000000000000000074d2e28c6bc5176ac02cf7c7d36a444e
);
Expand Down
10 changes: 5 additions & 5 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::protocol_types::{
address::AztecAddress, embedded_curve_scalar::EmbeddedCurveScalar, point::{Point, pub_key_to_bytes},
address::AztecAddress, scalar::Scalar, point::{Point, pub_key_to_bytes},
constants::{GENERATOR_INDEX__IVSK_M, GENERATOR_INDEX__OVSK_M}, hash::poseidon2_hash
};

Expand All @@ -24,7 +24,7 @@ pub fn compute_encrypted_event_log<Event, NB, MB, OB>(
event: Event
) -> [u8; OB] where Event: EventInterface<NB, MB> {
// @todo Need to draw randomness from the full domain of Fq not only Fr
let eph_sk: EmbeddedCurveScalar = fr_to_private_key(unsafe_rand());
let eph_sk: Scalar = fr_to_private_key(unsafe_rand());
let eph_pk = eph_sk.derive_public_key();

// TODO: (#7177) This value needs to be populated!
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn compute_encrypted_note_log<Note, N, NB, M>(
note: Note
) -> [u8; M] where Note: NoteInterface<N, NB> {
// @todo Need to draw randomness from the full domain of Fq not only Fr
let eph_sk: EmbeddedCurveScalar = fr_to_private_key(unsafe_rand());
let eph_sk: Scalar = fr_to_private_key(unsafe_rand());
let eph_pk = eph_sk.derive_public_key();

// TODO: (#7177) This value needs to be populated!
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn compute_encrypted_note_log<Note, N, NB, M>(
encrypted_bytes
}

fn fr_to_private_key(r: Field) -> EmbeddedCurveScalar {
fn fr_to_private_key(r: Field) -> Scalar {
let r_bytes = r.to_be_bytes(32);

let mut high_bytes = [0; 32];
Expand All @@ -143,7 +143,7 @@ fn fr_to_private_key(r: Field) -> EmbeddedCurveScalar {
let low = bytes32_to_field(low_bytes);
let high = bytes32_to_field(high_bytes);

EmbeddedCurveScalar::new(high, low)
Scalar::new(high, low)
}

fn compute_ivpk_app(ivpk: Point, contract_address: AztecAddress) -> Point {
Expand Down
15 changes: 6 additions & 9 deletions noir-projects/aztec-nr/aztec/src/keys/point_to_symmetric_key.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use dep::protocol_types::{
constants::GENERATOR_INDEX__SYMMETRIC_KEY, embedded_curve_scalar::EmbeddedCurveScalar,
point::{Point, pub_key_to_bytes}, utils::arr_copy_slice
constants::GENERATOR_INDEX__SYMMETRIC_KEY, scalar::Scalar, point::{Point, pub_key_to_bytes},
utils::arr_copy_slice
};
use std::{hash::sha256, embedded_curve_ops::{EmbeddedCurveScalar, multi_scalar_mul}};
use std::{hash::sha256, embedded_curve_ops::multi_scalar_mul};

// TODO(#5726): This function is called deriveAESSecret in TS. I don't like point_to_symmetric_key name much since
// point is not the only input of the function. Unify naming with TS once we have a better name.
pub fn point_to_symmetric_key(secret: EmbeddedCurveScalar, point: Point) -> [u8; 32] {
let shared_secret_fields = multi_scalar_mul(
[Point { x: point.x, y: point.y, is_infinite: false }],
[EmbeddedCurveScalar { lo: secret.low, hi: secret.high }]
);
pub fn point_to_symmetric_key(secret: Scalar, point: Point) -> [u8; 32] {
let shared_secret_fields = multi_scalar_mul([point], [secret]);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/6061): make the func return Point struct directly
let shared_secret = pub_key_to_bytes(Point::new(shared_secret_fields[0], shared_secret_fields[1], false));
let mut shared_secret_bytes_with_separator = [0 as u8; 65];
Expand All @@ -22,7 +19,7 @@ pub fn point_to_symmetric_key(secret: EmbeddedCurveScalar, point: Point) -> [u8;
#[test]
fn check_point_to_symmetric_key() {
// Value taken from "derive shared secret" test in encrypt_buffer.test.ts
let secret = EmbeddedCurveScalar::new(
let secret = Scalar::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ contract AvmTest {
global big_field_136_bits: Field = 0x991234567890abcdef1234567890abcdef;

// Libs
use std::embedded_curve_ops::{EmbeddedCurveScalar, multi_scalar_mul};
use std::embedded_curve_ops::multi_scalar_mul;
use dep::aztec::protocol_types::constants::CONTRACT_INSTANCE_LENGTH;
use dep::aztec::prelude::{Map, Deserialize};
use dep::aztec::state_vars::PublicMutable;
use dep::aztec::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH, point::Point};
use dep::aztec::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH, point::Point, scalar::Scalar};
use dep::aztec::oracle::get_contract_instance::{get_contract_instance_avm, get_contract_instance_internal_avm};
use dep::aztec::protocol_types::abis::function_selector::FunctionSelector;
use dep::aztec::context::gas::GasOpts;
Expand Down Expand Up @@ -147,8 +147,8 @@ contract AvmTest {
#[aztec(public)]
fn variable_base_msm() -> [Field; 3] {
let g = Point { x: 1, y: 17631683881184975370165255887551781615748388533673675138860, is_infinite: false };
let scalar = EmbeddedCurveScalar { lo: 3, hi: 0 };
let scalar2 = EmbeddedCurveScalar { lo: 20, hi: 0 };
let scalar = Scalar { lo: 3, hi: 0 };
let scalar2 = Scalar { lo: 20, hi: 0 };
let triple_g = multi_scalar_mul([g, g], [scalar, scalar2]);
triple_g
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use dep::aztec::{
prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, point::Point, hash::poseidon2_hash},
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, point::Point, scalar::Scalar, hash::poseidon2_hash},
note::utils::compute_note_hash_for_consumption, oracle::unsafe_rand::unsafe_rand,
keys::getters::get_nsk_app, note::note_getter_options::PropertySelector
};
use dep::std::field::bn254::decompose;
use dep::std::embedded_curve_ops::{EmbeddedCurveScalar, multi_scalar_mul, fixed_base_scalar_mul};
use dep::std::embedded_curve_ops::multi_scalar_mul;

trait OwnedNote {
fn new(amount: U128, owner_npk_m_hash: Field) -> Self;
Expand Down Expand Up @@ -80,15 +80,15 @@ impl NoteInterface<TOKEN_NOTE_LEN, TOKEN_NOTE_BYTES_LEN> for TokenNote {
// by leveraging homomorphism.
multi_scalar_mul(
[G1, G1, G1],
[EmbeddedCurveScalar {
[Scalar {
lo: self.amount.to_integer(),
hi: 0
},
EmbeddedCurveScalar {
Scalar {
lo: npk_lo,
hi: npk_hi
},
EmbeddedCurveScalar {
Scalar {
lo: random_lo,
hi: random_hi,
}]
Expand Down Expand Up @@ -204,11 +204,11 @@ impl PrivatelyRefundable for TokenNote {
// 2. Now that we have correct representationsn of fee payer and randomness we can compute `G ^ (fee_payer_npk + randomness)`
let incomplete_fee_payer_point = multi_scalar_mul(
[G1, G1],
[EmbeddedCurveScalar {
[Scalar {
lo: fee_payer_npk_m_hash_lo,
hi: fee_payer_npk_m_hash_hi
},
EmbeddedCurveScalar {
Scalar {
lo: fee_payer_randomness_lo,
hi: fee_payer_randomness_hi
}]
Expand All @@ -223,15 +223,15 @@ impl PrivatelyRefundable for TokenNote {
// 4. We compute `G ^ (user_npk_m_hash + funded_amount + randomness)`
let incomplete_user_point = multi_scalar_mul(
[G1, G1, G1],
[EmbeddedCurveScalar {
[Scalar {
lo: user_lo,
hi: user_hi
},
EmbeddedCurveScalar {
Scalar {
lo: funded_amount_lo,
hi: funded_amount_hi
},
EmbeddedCurveScalar {
Scalar {
lo: user_randomness_lo,
hi: user_randomness_hi
}]
Expand All @@ -256,7 +256,7 @@ impl PrivatelyRefundable for TokenNote {
// 2. We compute the fee point as `G ^ transaction_fee`
let fee_point_raw = multi_scalar_mul(
[G1],
[EmbeddedCurveScalar {
[Scalar {
lo: transaction_fee_lo,
hi: transaction_fee_hi,
}]
Expand Down
10 changes: 5 additions & 5 deletions noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract Test {
use dep::aztec::protocol_types::{
abis::private_circuit_public_inputs::PrivateCircuitPublicInputs,
constants::{MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, CANONICAL_KEY_REGISTRY_ADDRESS},
traits::{Serialize, ToField, FromField}, point::Point, embedded_curve_scalar::EmbeddedCurveScalar,
traits::{Serialize, ToField, FromField}, point::Point, scalar::Scalar,
storage::map::derive_storage_slot_in_map
};

Expand Down Expand Up @@ -404,14 +404,14 @@ contract Test {
}

#[aztec(private)]
fn compute_note_header_ciphertext(secret: EmbeddedCurveScalar, point: Point) -> [u8; 48] {
fn compute_note_header_ciphertext(secret: Scalar, point: Point) -> [u8; 48] {
EncryptedLogHeader::new(context.this_address()).compute_ciphertext(secret, point)
}

// 64 bytes + 32 * #fields + 16 = 112 bytes
#[aztec(private)]
fn compute_incoming_log_body_ciphertext(
secret: EmbeddedCurveScalar,
secret: Scalar,
point: Point,
storage_slot: Field,
value: Field
Expand All @@ -422,10 +422,10 @@ contract Test {

#[aztec(private)]
fn compute_outgoing_log_body_ciphertext(
eph_sk: EmbeddedCurveScalar,
eph_sk: Scalar,
recipient: AztecAddress,
recipient_ivpk_app: Point,
ovsk_app: EmbeddedCurveScalar
ovsk_app: Scalar
) -> [u8; 176] {
let eph_pk = eph_sk.derive_public_key();
EncryptedLogOutgoingBody::new(eph_sk, recipient, recipient_ivpk_app).compute_ciphertext(ovsk_app, eph_pk)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract TestLog {
use dep::aztec::prelude::PrivateSet;
use dep::aztec::protocol_types::{traits::Serialize, point::Point, embedded_curve_scalar::EmbeddedCurveScalar, address::AztecAddress};
use dep::aztec::protocol_types::{traits::Serialize, point::Point, scalar::Scalar, address::AztecAddress};
use dep::value_note::value_note::ValueNote;
use dep::aztec::encrypted_logs::incoming_body::EncryptedLogIncomingBody;
use dep::aztec::event::event_interface::EventInterface;
Expand Down Expand Up @@ -29,7 +29,7 @@ contract TestLog {

#[aztec(private)]
fn compute_incoming_log_body_ciphertext(
secret: EmbeddedCurveScalar,
secret: Scalar,
point: Point,
randomness: Field,
event_type_id: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ mod tests {
max_block_number::MaxBlockNumber, note_hash::{NoteHash, ScopedNoteHash},
nullifier::{Nullifier, ScopedNullifier}, log_hash::NoteLogHash, read_request::ScopedReadRequest
},
address::AztecAddress, embedded_curve_scalar::EmbeddedCurveScalar,
tests::{fixture_builder::FixtureBuilder}, utils::{arrays::{array_eq, array_length}},
traits::{Empty, is_empty, is_empty_array}, point::Point
address::AztecAddress, scalar::Scalar, tests::{fixture_builder::FixtureBuilder},
utils::{arrays::{array_eq, array_length}}, traits::{Empty, is_empty, is_empty_array},
point::Point
};

struct PrivateKernelResetInputsBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod tests {
kernel_circuit_public_inputs::KernelCircuitPublicInputs, max_block_number::MaxBlockNumber,
note_hash::{NoteHash, ScopedNoteHash}, nullifier::{Nullifier, ScopedNullifier}, gas::Gas
},
address::{AztecAddress, EthAddress}, embedded_curve_scalar::EmbeddedCurveScalar,
address::{AztecAddress, EthAddress}, scalar::Scalar,
hash::{
sha256_to_field, silo_note_hash, silo_nullifier, compute_siloed_encrypted_log_hash,
compute_siloed_unencrypted_log_hash
Expand Down
Loading

0 comments on commit 0b73463

Please sign in to comment.