Skip to content

Commit

Permalink
refactor: nuking encryption oracles (#8817)
Browse files Browse the repository at this point in the history
We've had encryption oracles which were completely unused. I originally created them when we didn't have a functional Noir ones. No point in keeping them around as at this point it was just an unnecessary maintenance cost.
  • Loading branch information
benesjan authored and Rumata888 committed Sep 27, 2024
1 parent d86f9c7 commit e2d877c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 412 deletions.
12 changes: 0 additions & 12 deletions noir-projects/aztec-nr/aztec/src/oracle/encryption.nr

This file was deleted.

103 changes: 10 additions & 93 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
@@ -1,116 +1,33 @@
use dep::protocol_types::{address::AztecAddress, point::Point};
use dep::protocol_types::address::AztecAddress;

// = 480 + 32 * N bytes
#[oracle(emitEncryptedNoteLog)]
unconstrained fn emit_encrypted_note_log_oracle<let M: u32>(_note_hash_counter: u32, _encrypted_note: [u8; M], _counter: u32) {}

unconstrained pub fn emit_encrypted_note_log<let M: u32>(
note_hash_counter: u32,
encrypted_note: [u8; M],
counter: u32
) {
unconstrained pub fn emit_encrypted_note_log<let M: u32>(note_hash_counter: u32, encrypted_note: [u8; M], counter: u32) {
emit_encrypted_note_log_oracle(note_hash_counter, encrypted_note, counter)
}

#[oracle(emitEncryptedEventLog)]
unconstrained fn emit_encrypted_event_log_oracle<let M: u32>(_contract_address: AztecAddress, _randomness: Field, _encrypted_event: [u8; M], _counter: u32) {}

unconstrained pub fn emit_encrypted_event_log<let M: u32>(
contract_address: AztecAddress,
randomness: Field,
encrypted_event: [u8; M],
counter: u32
) {
emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter)
}

// = 480 + 32 * N bytes
#[oracle(computeEncryptedNoteLog)]
unconstrained fn compute_encrypted_note_log_oracle<let N: u32, let M: u32>(
_contract_address: AztecAddress,
_storage_slot: Field,
_note_type_id: Field,
_ovsk_app: Field,
_ovpk_m: Point,
_ivpk_m: Point,
_recipient: AztecAddress,
_preimage: [Field; N]
) -> [u8; M] {}

unconstrained pub fn compute_encrypted_note_log<let N: u32, let M: u32>(
contract_address: AztecAddress,
storage_slot: Field,
note_type_id: Field,
ovsk_app: Field,
ovpk_m: Point,
ivpk_m: Point,
recipient: AztecAddress,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_note_log_oracle(
contract_address,
storage_slot,
note_type_id,
ovsk_app,
ovpk_m,
ivpk_m,
recipient,
preimage
)
}

// = 480 + 32 * N bytes
#[oracle(computeEncryptedEventLog)]
unconstrained fn compute_encrypted_event_log_oracle<let N: u32, let M: u32>(
unconstrained fn emit_encrypted_event_log_oracle<let M: u32>(
_contract_address: AztecAddress,
_randomness: Field,
_event_type_id: Field,
_ovsk_app: Field,
_ovpk_m: Point,
_ivpk_m: Point,
_recipient: AztecAddress,
_preimage: [Field; N]
) -> [u8; M] {}
_encrypted_event: [u8; M],
_counter: u32
) {}

unconstrained pub fn compute_encrypted_event_log<let N: u32, let M: u32>(
contract_address: AztecAddress,
randomness: Field,
event_type_id: Field,
ovsk_app: Field,
ovpk_m: Point,
ivpk_m: Point,
recipient: AztecAddress,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_event_log_oracle(
contract_address,
randomness,
event_type_id,
ovsk_app,
ovpk_m,
ivpk_m,
recipient,
preimage
)
unconstrained pub fn emit_encrypted_event_log<let M: u32>(contract_address: AztecAddress, randomness: Field, encrypted_event: [u8; M], counter: u32) {
emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter)
}

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

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

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

unconstrained pub fn emit_contract_class_unencrypted_log_private_internal<let N: u32>(contract_address: AztecAddress, message: [Field; N], counter: u32) -> Field {
emit_contract_class_unencrypted_log_private(contract_address, message, counter)
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

mod arguments;
mod call_private_function;
mod encryption;
mod execution;
mod get_contract_instance;
mod get_l1_to_l2_membership_witness;
Expand Down
20 changes: 8 additions & 12 deletions noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ use dep::aztec::macros::aztec;
contract Test {

use dep::aztec::prelude::{
AztecAddress, EthAddress, FunctionSelector, NoteHeader, NoteGetterOptions, NoteViewerOptions,
PrivateContext, PrivateImmutable, PrivateSet, SharedImmutable
AztecAddress, EthAddress, FunctionSelector, NoteGetterOptions, NoteViewerOptions,
PrivateImmutable, PrivateSet
};
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note;
use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event_with_keys_with_randomness;

use dep::aztec::protocol_types::{
abis::private_circuit_public_inputs::PrivateCircuitPublicInputs,
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::{Serialize, ToField, FromField},
point::Point, scalar::Scalar, storage::map::derive_storage_slot_in_map
};
use dep::aztec::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point, scalar::Scalar};

use dep::aztec::encrypted_logs::header::EncryptedLogHeader;
use dep::aztec::encrypted_logs::payload::{compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext};
Expand All @@ -26,21 +22,21 @@ contract Test {
use dep::aztec::keys::getters::get_public_keys;

use dep::aztec::{
context::inputs::private_context_inputs::PrivateContextInputs,
hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, keys::public_keys::IvpkM,
note::{
lifecycle::{create_note, destroy_note_unsafe}, note_getter::{get_notes, view_notes},
note_getter_options::NoteStatus
},
deploy::deploy_contract as aztec_deploy_contract,
oracle::{encryption::aes128_encrypt, unsafe_rand::unsafe_rand}, utils::comparison::Comparator,
deploy::deploy_contract as aztec_deploy_contract, oracle::unsafe_rand::unsafe_rand,
utils::comparison::Comparator,
macros::{storage::storage, events::event, functions::{private, public, internal}}
};
use dep::token_portal_content_hash_lib::{get_mint_private_content_hash, get_mint_public_content_hash};
use dep::value_note::value_note::ValueNote;
// TODO investigate why the macros require EmbeddedCurvePoint and EmbeddedCurveScalar
use std::embedded_curve_ops::{EmbeddedCurveScalar, EmbeddedCurvePoint, fixed_base_scalar_mul as derive_public_key};
use std::meta::derive;
use std::aes128::aes128_encrypt;

use crate::test_note::TestNote;

Expand Down Expand Up @@ -406,12 +402,12 @@ contract Test {

#[private]
fn encrypt(input: [u8; 64], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] {
aes128_encrypt(input, iv, key)
aes128_encrypt(input, iv, key).as_array()
}

#[private]
fn encrypt_with_padding(input: [u8; 65], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] {
aes128_encrypt(input, iv, key)
aes128_encrypt(input, iv, key).as_array()
}

#[private]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,20 @@ unconstrained fn decode_hex<let N: u32, let M: u32>(s: str<N>) -> [u8; M] {
}

unconstrained fn cipher(plaintext: [u8; 12], iv: [u8; 16], key: [u8; 16]) -> [u8; 16] {
let slice_res = std::aes128::aes128_encrypt(plaintext, iv, key);
let mut result = [0; 16];
for i in 0..16 {
result[i] = slice_res[i];
}
result
let result = std::aes128::aes128_encrypt(plaintext, iv, key);
result.as_array()
}

fn main(inputs: str<12>, iv: str<16>, key: str<16>, output: str<32>) {
let result = std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes());
let result: [u8; 16] = std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()).as_array();

let output_bytes: [u8; 16] = unsafe {
let output_bytes: [u8; 16] = decode_hex(output);
for i in 0..16 {
assert(result[i] == output_bytes[i]);
}
output_bytes
decode_hex(output)
};
assert(result == output_bytes);

unsafe {
let unconstrained_result = cipher(inputs.as_bytes(), iv.as_bytes(), key.as_bytes());
for i in 0..16 {
assert(unconstrained_result[i] == output_bytes[i]);
}
}
let unconstrained_result = unsafe {
cipher(inputs.as_bytes(), iv.as_bytes(), key.as_bytes())
};
assert(unconstrained_result == output_bytes);
}
82 changes: 1 addition & 81 deletions yarn-project/simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { MerkleTreeId, UnencryptedL2Log } from '@aztec/circuit-types';
import { KeyValidationRequest } from '@aztec/circuits.js';
import { FunctionSelector, NoteSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';

import { type ACVMField } from '../acvm_types.js';
import { frToBoolean, frToNumber, fromACVMField } from '../deserialize.js';
Expand Down Expand Up @@ -329,72 +328,6 @@ export class Oracle {
this.typedOracle.emitEncryptedNoteLog(+noteHashCounter, processedInput, +counter);
}

computeEncryptedEventLog(
[contractAddress]: ACVMField[],
[randomness]: ACVMField[],
[eventTypeId]: ACVMField[],
[ovskApp]: ACVMField[],
[ovpkMX]: ACVMField[],
[ovpkMY]: ACVMField[],
[ovpkMIsInfinite]: ACVMField[],
[ivpkMX]: ACVMField[],
[ivpkMY]: ACVMField[],
[ivpkMIsInfinite]: ACVMField[],
[recipient]: ACVMField[],
preimage: ACVMField[],
): ACVMField[] {
const ovpkM = new Point(fromACVMField(ovpkMX), fromACVMField(ovpkMY), !fromACVMField(ovpkMIsInfinite).isZero());
const ovKeys = new KeyValidationRequest(ovpkM, Fr.fromString(ovskApp));
const ivpkM = new Point(fromACVMField(ivpkMX), fromACVMField(ivpkMY), !fromACVMField(ivpkMIsInfinite).isZero());
const encLog = this.typedOracle.computeEncryptedEventLog(
AztecAddress.fromString(contractAddress),
Fr.fromString(randomness),
Fr.fromString(eventTypeId),
ovKeys,
ivpkM,
AztecAddress.fromString(recipient),
preimage.map(fromACVMField),
);
const bytes: ACVMField[] = [];
encLog.forEach(v => {
bytes.push(toACVMField(v));
});
return bytes;
}

computeEncryptedNoteLog(
[contractAddress]: ACVMField[],
[storageSlot]: ACVMField[],
[noteTypeId]: ACVMField[],
[ovskApp]: ACVMField[],
[ovpkMX]: ACVMField[],
[ovpkMY]: ACVMField[],
[ovpkMIsInfinite]: ACVMField[],
[ivpkMX]: ACVMField[],
[ivpkMY]: ACVMField[],
[ivpkMIsInfinite]: ACVMField[],
[recipient]: ACVMField[],
preimage: ACVMField[],
): ACVMField[] {
const ovpkM = new Point(fromACVMField(ovpkMX), fromACVMField(ovpkMY), !fromACVMField(ovpkMIsInfinite).isZero());
const ovKeys = new KeyValidationRequest(ovpkM, Fr.fromString(ovskApp));
const ivpkM = new Point(fromACVMField(ivpkMX), fromACVMField(ivpkMY), !fromACVMField(ivpkMIsInfinite).isZero());
const encLog = this.typedOracle.computeEncryptedNoteLog(
AztecAddress.fromString(contractAddress),
Fr.fromString(storageSlot),
NoteSelector.fromField(Fr.fromString(noteTypeId)),
ovKeys,
ivpkM,
AztecAddress.fromString(recipient),
preimage.map(fromACVMField),
);
const bytes: ACVMField[] = [];
encLog.forEach(v => {
bytes.push(toACVMField(v));
});
return bytes;
}

emitUnencryptedLog([contractAddress]: ACVMField[], message: ACVMField[], [counter]: ACVMField[]): ACVMField {
const logPayload = Buffer.concat(message.map(fromACVMField).map(f => f.toBuffer()));
const log = new UnencryptedL2Log(AztecAddress.fromString(contractAddress), logPayload);
Expand Down Expand Up @@ -479,17 +412,4 @@ export class Oracle {
notifySetMinRevertibleSideEffectCounter([minRevertibleSideEffectCounter]: ACVMField[]) {
this.typedOracle.notifySetMinRevertibleSideEffectCounter(frToNumber(fromACVMField(minRevertibleSideEffectCounter)));
}

aes128Encrypt(input: ACVMField[], initializationVector: ACVMField[], key: ACVMField[]): ACVMField[] {
// Convert each field to a number and then to a buffer (1 byte is stored in 1 field)
const processedInput = Buffer.from(input.map(fromACVMField).map(f => f.toNumber()));
const processedIV = Buffer.from(initializationVector.map(fromACVMField).map(f => f.toNumber()));
const processedKey = Buffer.from(key.map(fromACVMField).map(f => f.toNumber()));

// Encrypt the input
const ciphertext = this.typedOracle.aes128Encrypt(processedInput, processedIV, processedKey);

// Convert each byte of ciphertext to a field and return it
return Array.from(ciphertext).map(byte => toACVMField(byte));
}
}
29 changes: 0 additions & 29 deletions yarn-project/simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type NoteStatus,
type NullifierMembershipWitness,
type PublicDataWitness,
type PublicKey,
type SiblingPath,
type UnencryptedL2Log,
} from '@aztec/circuit-types';
Expand Down Expand Up @@ -204,30 +203,6 @@ export abstract class TypedOracle {
throw new OracleMethodNotAvailableError('emitEncryptedNoteLog');
}

computeEncryptedEventLog(
_contractAddress: AztecAddress,
_randomness: Fr,
_eventTypeId: Fr,
_ovKeys: KeyValidationRequest,
_ivpkM: PublicKey,
_recipient: AztecAddress,
_preimage: Fr[],
): Buffer {
throw new OracleMethodNotAvailableError('computeEncryptedEventLog');
}

computeEncryptedNoteLog(
_contractAddress: AztecAddress,
_storageSlot: Fr,
_noteTypeId: NoteSelector,
_ovKeys: KeyValidationRequest,
_ivpkM: PublicKey,
_recipient: AztecAddress,
_preimage: Fr[],
): Buffer {
throw new OracleMethodNotAvailableError('computeEncryptedNoteLog');
}

emitUnencryptedLog(_log: UnencryptedL2Log, _counter: number): void {
throw new OracleMethodNotAvailableError('emitUnencryptedLog');
}
Expand Down Expand Up @@ -273,10 +248,6 @@ export abstract class TypedOracle {
throw new OracleMethodNotAvailableError('notifySetMinRevertibleSideEffectCounter');
}

aes128Encrypt(_input: Buffer, _initializationVector: Buffer, _key: Buffer): Buffer {
throw new OracleMethodNotAvailableError('encrypt');
}

debugLog(_message: string, _fields: Fr[]): void {
throw new OracleMethodNotAvailableError('debugLog');
}
Expand Down
Loading

0 comments on commit e2d877c

Please sign in to comment.