Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 24, 2024
1 parent 217ae60 commit 0b50db6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
23 changes: 10 additions & 13 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ use crate::{
encrypted_logs::header::EncryptedLogHeader,
keys::point_to_symmetric_key::point_to_symmetric_key,
};
use protocol_types::public_keys::AddressPoint;

pub fn compute_encrypted_log<let P: u32, let M: u32>(
contract_address: AztecAddress,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
plaintext: [u8; P],
) -> [u8; M] {
let (eph_sk, eph_pk) = generate_ephemeral_key_pair();

let header = EncryptedLogHeader::new(contract_address);

let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, recipient);
let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, recipient.to_address_point());
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
let incoming_body_ciphertext =
compute_incoming_body_ciphertext(plaintext, eph_sk, IvpkM { inner: recipient.to_point() });
compute_incoming_body_ciphertext(plaintext, eph_sk, recipient.to_address_point());
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(
recipient,
IvpkM { inner: recipient.to_point() },
fr_to_fq(ovsk_app),
eph_sk,
eph_pk,
Expand Down Expand Up @@ -96,9 +95,9 @@ fn generate_ephemeral_key_pair() -> (Scalar, Point) {
pub fn compute_incoming_body_ciphertext<let P: u32>(
plaintext: [u8; P],
eph_sk: Scalar,
ivpk: IvpkM,
address_point: AddressPoint,
) -> [u8] {
let full_key = point_to_symmetric_key(eph_sk, ivpk.to_point());
let full_key = point_to_symmetric_key(eph_sk, address_point.to_point());
let mut sym_key = [0; 16];
let mut iv = [0; 16];

Expand All @@ -113,7 +112,6 @@ pub fn compute_incoming_body_ciphertext<let P: u32>(
/// be able to derive the key with which the incoming log can be decrypted.
pub fn compute_outgoing_body_ciphertext(
recipient: AztecAddress,
recipient_ivpk: IvpkM,
ovsk_app: Scalar,
eph_sk: Scalar,
eph_pk: Point,
Expand All @@ -126,7 +124,7 @@ pub fn compute_outgoing_body_ciphertext(
let serialized_eph_sk_low: [u8; 32] = eph_sk.lo.to_be_bytes();

let address_bytes: [u8; 32] = recipient.to_field().to_be_bytes();
let serialized_recipient_ivpk = point_to_bytes(recipient_ivpk.to_point());
let serialized_recipient_ivpk = point_to_bytes(recipient.to_address_point().to_point());

for i in 0..32 {
buffer[i] = serialized_eph_sk_high[i];
Expand Down Expand Up @@ -163,6 +161,7 @@ mod test {
address::AztecAddress, public_keys::{OvpkM, IvpkM}, point::Point, scalar::Scalar,
};
use std::test::OracleMock;
use protocol_types::public_keys::AddressPoint;

#[test]
unconstrained fn test_encrypted_log_matches_typescript() {
Expand Down Expand Up @@ -207,7 +206,6 @@ mod test {
contract_address,
ovsk_app,
ovpk_m,
ivpk_m,
recipient,
plaintext,
);
Expand Down Expand Up @@ -249,7 +247,7 @@ mod test {
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
hi: 0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
};
let ivpk = IvpkM {
let address_point = AddressPoint {
inner: Point {
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
Expand All @@ -267,7 +265,7 @@ mod test {

// `compute_incoming_body_ciphertext(...)` function then derives symmetric key from `eph_sk` and `ivpk` and encrypts
// the note plaintext using AES-128.
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, ivpk);
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, address_point);

// The following value was generated by `encrypted_note_log_incoming_body.test.ts`.
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
Expand Down Expand Up @@ -307,13 +305,12 @@ mod test {
};

let eph_pk = derive_public_key(eph_sk);
let recipient_ivpk = IvpkM { inner: derive_public_key(recipient_ivsk) };
let recipient_address_point = AddressPoint { inner: derive_public_key(recipient_ivsk) };

let recipient = AztecAddress::from_field(0xdeadbeef);

let ciphertext = compute_outgoing_body_ciphertext(
recipient,
recipient_ivpk,
sender_ovsk_app,
eph_sk,
eph_pk,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::{
ec::{sqrt, pow},
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar},
};
use crate::public_keys::AddressPoint;

// Aztec address
pub struct AztecAddress {
Expand Down Expand Up @@ -66,8 +67,12 @@ impl Deserialize<AZTEC_ADDRESS_LENGTH> for AztecAddress {
}
}

impl ToPoint for AztecAddress {
fn to_point(self) -> Point {
impl AztecAddress {
pub fn zero() -> Self {
Self { inner: 0 }
}

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

Expand All @@ -84,13 +89,7 @@ impl ToPoint for AztecAddress {
y = (BN254_FR_MODULUS_DIV_2 + BN254_FR_MODULUS_DIV_2 + 1) - y;
}

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

impl AztecAddress {
pub fn zero() -> Self {
Self { inner: 0 }
AddressPoint { inner: Point { x: self.inner, y, is_infinite: false } }
}

pub fn compute_preaddress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ impl Deserialize<PUBLIC_KEYS_LENGTH> for PublicKeys {
}
}

pub struct AddressPoint {
inner: Point,
}

impl ToPoint for AddressPoint {
fn to_point(self) -> Point {
self.inner
}
}

#[test]
unconstrained fn compute_public_keys_hash() {
let keys = PublicKeys {
Expand Down

0 comments on commit 0b50db6

Please sign in to comment.