Skip to content

Commit

Permalink
refactor: secret derivation funcs naming cleanup (#10637)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Dec 12, 2024
1 parent 9836036 commit 5c50711
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dep::protocol_types::{
scalar::Scalar,
};

use crate::keys::point_to_symmetric_key::point_to_symmetric_key;
use crate::keys::secret_derivation::derive_aes_secret;

use std::aes128::aes128_encrypt;

Expand All @@ -22,7 +22,7 @@ impl EncryptedLogHeader {
where
T: ToPoint,
{
let full_key = point_to_symmetric_key(secret, pk.to_point());
let full_key = derive_aes_secret(secret, pk.to_point());
let mut sym_key = [0; 16];
let mut iv = [0; 16];

Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{

use crate::{
encrypted_logs::header::EncryptedLogHeader,
keys::point_to_symmetric_key::point_to_symmetric_key,
keys::secret_derivation::derive_aes_secret,
oracle::{
notes::{get_app_tag_as_sender, increment_app_tagging_secret_index_as_sender},
random::random,
Expand Down Expand Up @@ -208,7 +208,7 @@ pub fn compute_incoming_body_ciphertext<let P: u32>(
eph_sk: Scalar,
address_point: AddressPoint,
) -> [u8] {
let full_key = point_to_symmetric_key(eph_sk, address_point.to_point());
let full_key = derive_aes_secret(eph_sk, address_point.to_point());
let mut sym_key = [0; 16];
let mut iv = [0; 16];

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/keys/mod.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod constants;
pub mod getters;
pub mod point_to_symmetric_key;
pub mod secret_derivation;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::utils::point::point_to_bytes;
use dep::protocol_types::{constants::GENERATOR_INDEX__SYMMETRIC_KEY, point::Point, scalar::Scalar};
use std::{embedded_curve_ops::multi_scalar_mul, hash::sha256};

// 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: Scalar, point: Point) -> [u8; 32] {
pub fn derive_aes_secret(secret: Scalar, point: Point) -> [u8; 32] {
let shared_secret = point_to_bytes(multi_scalar_mul([point], [secret]));

let mut shared_secret_bytes_with_separator: [u8; 33] = std::mem::zeroed();
Expand All @@ -18,7 +16,7 @@ pub fn point_to_symmetric_key(secret: Scalar, point: Point) -> [u8; 32] {
}

#[test]
unconstrained fn test_point_to_symmetric_key_matches_noir() {
unconstrained fn test_derive_aes_secret_matches_noir() {
// Value taken from "derive shared secret" test in encrypt_buffer.test.ts
let secret = Scalar {
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
Expand All @@ -30,7 +28,7 @@ unconstrained fn test_point_to_symmetric_key_matches_noir() {
is_infinite: false,
};

let key = point_to_symmetric_key(secret, point);
let key = derive_aes_secret(secret, point);

// The following value was generated by `encrypt_buffer.test.ts`.
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type GrumpkinScalar, type PublicKey } from '@aztec/circuits.js';
import { Aes128 } from '@aztec/circuits.js/barretenberg';

import { deriveDiffieHellmanAESSecret } from './shared_secret_derivation.js';
import { deriveAESSecret } from './shared_secret_derivation.js';

/**
* Encrypts the plaintext using the secret key and public key
Expand All @@ -16,7 +16,7 @@ export function encrypt(
plaintext: Buffer,
secret: GrumpkinScalar,
publicKey: PublicKey,
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveDiffieHellmanAESSecret,
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveAESSecret,
): Buffer {
const aesSecret = deriveSecret(secret, publicKey);
const key = aesSecret.subarray(0, 16);
Expand All @@ -38,7 +38,7 @@ export function decrypt(
ciphertext: Buffer,
secret: GrumpkinScalar,
publicKey: PublicKey,
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveDiffieHellmanAESSecret,
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveAESSecret,
): Buffer {
const aesSecret = deriveSecret(secret, publicKey);
const key = aesSecret.subarray(0, 16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { numToUInt8 } from '@aztec/foundation/serialize';
* @param publicKey - The public key used to derive shared secret.
* @returns A derived AES secret key.
* @throws If the public key is zero.
* TODO(#5726): This function is called point_to_symmetric_key in Noir. I don't like that name much since point is not
* the only input of the function. Unify naming once we have a better name.
*/
export function deriveDiffieHellmanAESSecret(secretKey: GrumpkinScalar, publicKey: PublicKey): Buffer {
export function deriveAESSecret(secretKey: GrumpkinScalar, publicKey: PublicKey): Buffer {
if (publicKey.isZero()) {
throw new Error(
`Attempting to derive AES secret with a zero public key. You have probably passed a zero public key in your Noir code somewhere thinking that the note won't broadcasted... but it was.`,
Expand Down

0 comments on commit 5c50711

Please sign in to comment.