Skip to content

Commit

Permalink
fix: false decryption fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jun 24, 2024
1 parent fd92d46 commit 04a601b
Show file tree
Hide file tree
Showing 47 changed files with 376 additions and 153 deletions.
2 changes: 1 addition & 1 deletion barretenberg/ts/src/types/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Fr {
const valueBigInt = typeof value === 'bigint' ? value : toBigIntBE(value);

if (valueBigInt > Fr.MAX_VALUE) {
throw new Error(`Fr out of range: ${valueBigInt}`);
throw new Error(`Value 0x${valueBigInt.toString(16)} is greater or equal to field modulus.`);
}

this.value = typeof value === 'bigint' ? toBufferBE(value) : value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod test {
use dep::protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, traits::Serialize,
abis::event_selector::EventSelector
abis::{event_selector::EventSelector, note_selector::NoteSelector}
};

use crate::{
Expand All @@ -60,7 +60,9 @@ mod test {
impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
fn compute_note_content_hash(self) -> Field {1}

fn get_note_type_id() -> Field {1}
fn get_note_type_id() -> NoteSelector {
NoteSelector::from_field(1)
}

fn get_header(self) -> NoteHeader { self.header}

Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/note_interface.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::context::PrivateContext;
use crate::note::note_header::NoteHeader;
use dep::protocol_types::grumpkin_point::GrumpkinPoint;
use dep::protocol_types::abis::note_selector::NoteSelector;

// docs:start:note_interface
trait NoteInterface<N, M> {
Expand All @@ -24,7 +24,7 @@ trait NoteInterface<N, M> {
fn set_header(&mut self, header: NoteHeader) -> ();

// Autogenerated by the #[aztec(note)] macro unless it is overridden by a custom implementation
fn get_note_type_id() -> Field;
fn get_note_type_id() -> NoteSelector;

// Autogenerated by the #[aztec(note)] macro unless it is overridden by a custom implementation
fn to_be_bytes(self, storage_slot: Field) -> [u8; M];
Expand Down
33 changes: 26 additions & 7 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint};
use dep::protocol_types::{abis::note_selector::NoteSelector, address::AztecAddress, grumpkin_point::GrumpkinPoint};

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

unconstrained pub fn emit_encrypted_note_log<M>(
note_hash_counter: u32,
Expand All @@ -13,7 +17,12 @@ unconstrained pub fn emit_encrypted_note_log<M>(
}

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

unconstrained pub fn emit_encrypted_event_log<M>(
contract_address: AztecAddress,
Expand All @@ -29,7 +38,7 @@ unconstrained pub fn emit_encrypted_event_log<M>(
unconstrained fn compute_encrypted_note_log_oracle<N, M>(
_contract_address: AztecAddress,
_storage_slot: Field,
_note_type_id: Field,
_note_type_id: NoteSelector,
_ovsk_app: Field,
_ovpk_m: GrumpkinPoint,
_ivpk_m: GrumpkinPoint,
Expand All @@ -39,7 +48,7 @@ unconstrained fn compute_encrypted_note_log_oracle<N, M>(
unconstrained pub fn compute_encrypted_note_log<N, M>(
contract_address: AztecAddress,
storage_slot: Field,
note_type_id: Field,
note_type_id: NoteSelector,
ovsk_app: Field,
ovpk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
Expand Down Expand Up @@ -89,7 +98,12 @@ unconstrained pub fn compute_encrypted_event_log<N, M>(
}

#[oracle(emitUnencryptedLog)]
unconstrained fn emit_unencrypted_log_oracle_private<T>(_contract_address: AztecAddress, _event_selector: Field, _message: T, _counter: u32) -> Field {}
unconstrained 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,
Expand All @@ -101,7 +115,12 @@ unconstrained pub fn emit_unencrypted_log_private_internal<T>(
}

#[oracle(emitContractClassUnencryptedLog)]
unconstrained fn emit_contract_class_unencrypted_log_private<N>(contract_address: AztecAddress, event_selector: Field, message: [Field; N], counter: u32) -> Field {}
unconstrained 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,
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::note::{note_header::NoteHeader, note_interface::NoteInterface};

use dep::protocol_types::{address::AztecAddress, utils::arr_copy_slice};
use dep::protocol_types::{abis::note_selector::NoteSelector, address::AztecAddress, utils::arr_copy_slice};

#[oracle(notifyCreatedNote)]
unconstrained fn notify_created_note_oracle<N>(
_storage_slot: Field,
_note_type_id: Field,
_note_type_id: NoteSelector,
_serialized_note: [Field; N],
_inner_note_hash: Field,
_counter: u32
) -> Field {}

unconstrained pub fn notify_created_note<N>(
storage_slot: Field,
note_type_id: Field,
note_type_id: NoteSelector,
serialized_note: [Field; N],
inner_note_hash: Field,
counter: u32
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interface::NoteInterface}};

use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint, traits::Eq};
use dep::protocol_types::{address::AztecAddress, abis::note_selector::NoteSelector, traits::Eq};

global MOCK_NOTE_LENGTH = 1;
// MOCK_NOTE_LENGTH * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)
Expand Down Expand Up @@ -35,8 +35,8 @@ impl NoteInterface<MOCK_NOTE_LENGTH, MOCK_NOTE_BYTES_LENGTH> for MockNote {
self.header = header;
}

fn get_note_type_id() -> Field {
0
fn get_note_type_id() -> NoteSelector {
NoteSelector::from_field(0)
}

fn compute_note_hash_and_nullifier(self, _context: &mut PrivateContext) -> (Field, Field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod side_effect;
mod read_request;
mod log_hash;
mod note_hash;
mod note_selector;
mod nullifier;
mod public_data_read;
mod public_data_update_request;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::utils::field::field_from_bytes;
use dep::std::cmp::Eq;
use crate::traits::{Serialize, Deserialize, FromField, ToField, Empty};

global SELECTOR_SIZE = 4;

struct NoteSelector {
// 1st 4-bytes (big-endian leftmost) of abi-encoding of an note.
inner: Field,
}

impl Eq for NoteSelector {
fn eq(self, other: NoteSelector) -> bool {
other.inner == self.inner
}
}

impl Serialize<1> for NoteSelector {
fn serialize(self: Self) -> [Field; 1] {
[self.inner]
}
}

impl Deserialize<1> for NoteSelector {
fn deserialize(fields: [Field; 1]) -> Self {
Self {
inner: fields[0]
}
}
}

impl FromField for NoteSelector {
fn from_field(field: Field) -> Self {
Self { inner: field }
}
}

impl ToField for NoteSelector {
fn to_field(self) -> Field {
self.inner
}
}

impl Empty for NoteSelector {
fn empty() -> Self {
Self { inner: 0 }
}
}

impl NoteSelector {
pub fn from_signature<N>(signature: str<N>) -> Self {
let bytes = signature.as_bytes();
let hash = dep::std::hash::keccak256(bytes, bytes.len() as u32);

let mut selector_be_bytes = [0; SELECTOR_SIZE];
for i in 0..SELECTOR_SIZE {
selector_be_bytes[i] = hash[i];
}

NoteSelector::from_field(field_from_bytes(selector_be_bytes, true))
}

pub fn zero() -> Self {
Self { inner: 0 }
}

fn to_be_bytes(self, byte_size: u32) -> [u8] {
self.inner.to_be_bytes(byte_size)
}
}
1 change: 0 additions & 1 deletion noir/noir-repo/aztec_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ noirc_errors.workspace = true
iter-extended.workspace = true
convert_case = "0.6.0"
regex = "1.10"

Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn generate_compute_note_hash_and_optionally_a_nullifier_source(
// get_note_type_id of each of the note types.

let if_statements: Vec<String> = note_types.iter().map(|note_type| format!(
"if (note_type_id == {0}::get_note_type_id()) {{
"if (note_type_id.eq({0}::get_note_type_id())) {{
dep::aztec::note::utils::compute_note_hash_and_optionally_a_nullifier({0}::deserialize_content, note_header, compute_nullifier, serialized_note)
}}"
, note_type)).collect();
Expand All @@ -211,7 +211,7 @@ fn generate_compute_note_hash_and_optionally_a_nullifier_source(
contract_address: dep::aztec::protocol_types::address::AztecAddress,
nonce: Field,
storage_slot: Field,
note_type_id: Field,
note_type_id: dep::aztec::protocol_types::abis::note_selector::NoteSelector,
compute_nullifier: bool,
serialized_note: [Field; {}],
) -> pub [Field; 4] {{
Expand Down
Loading

0 comments on commit 04a601b

Please sign in to comment.