Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enforce formatting of noir code #6271

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ jobs:
# working-directory: ./yarn-project/scripts
# run: earthly-ci -P --secret AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} --secret AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} --secret AZTEC_BOT_COMMENTER_GITHUB_TOKEN=${{ secrets.AZTEC_BOT_GITHUB_TOKEN }} +bench-comment

noir-format:
needs: setup
runs-on: ${{ inputs.username || github.actor }}-x86
steps:
- {
uses: actions/checkout@v4,
with: { ref: "${{ github.event.pull_request.head.sha }}" },
}
- uses: ./.github/ci-setup-action
with:
dockerhub_password: "${{ secrets.DOCKERHUB_PASSWORD }}"
concurrency_key: noir-format-${{ inputs.username || github.actor }}-x86
- name: "Format Noir"
working-directory: ./noir/
timeout-minutes: 25
run: earthly-ci --no-output ./+format
- name: "Format noir-projects"
working-directory: ./noir-projects/
timeout-minutes: 25
run: earthly-ci --no-output ./+format

# barretenberg (prover) native and AVM (public VM) tests
# only ran on x86 for resource reasons (memory intensive)
bb-native-tests:
Expand Down
15 changes: 14 additions & 1 deletion noir-projects/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ test:
RUN cd aztec-nr && nargo test --silence-warnings
RUN cd noir-contracts && nargo test --silence-warnings

format:
FROM +build

# TODO: https://github.com/noir-lang/noir/issues/4980
# WORKDIR /usr/src/noir-projects/noir-protocol-circuits
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
# RUN nargo fmt --check

WORKDIR /usr/src/noir-projects/noir-contracts
RUN nargo fmt --check

WORKDIR /usr/src/noir-projects/aztec-nr
RUN nargo fmt --check

gates-report:
FROM +build
WORKDIR /usr/src/noir-projects/noir-protocol-circuits
Expand All @@ -38,4 +51,4 @@ gates-report:

RUN NARGO_BACKEND_PATH=/usr/src/barretenberg/cpp/build/bin/bb nargo info --json > gates_report.json

SAVE ARTIFACT gates_report.json gates_report.json
SAVE ARTIFACT gates_report.json gates_report.json
48 changes: 23 additions & 25 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@ pub fn compute_secret_hash(secret: Field) -> Field {
pedersen_hash([secret], GENERATOR_INDEX__SECRET_HASH)
}

pub fn compute_encrypted_log_hash<N, M, L>(
encrypted_log: [Field; M]
) -> Field where [Field; N]: LensForEncryptedLog<N, M, L> {
pub fn compute_encrypted_log_hash<N, M, L>(encrypted_log: [Field; M]) -> Field where [Field; N]: LensForEncryptedLog<N, M, L> {
let mut bytes = [0; L];
// Note that bytes.append(encrypted_log[i].to_be_bytes(31)) results in bound error
for i in 0..M-1 {
for i in 0..M - 1 {
let to_add = encrypted_log[i].to_be_bytes(31);
for j in 0..31 {
bytes[i*31 + j] = to_add[j];
}
}
// can't assign as L - not in scope error for: L-31*(M-1)
let num_bytes = bytes.len() as u32 - 31*(M-1);
let to_add_final = encrypted_log[M-1].to_be_bytes(num_bytes);
let num_bytes = bytes.len() as u32 - 31 * (M - 1);
let to_add_final = encrypted_log[M - 1].to_be_bytes(num_bytes);
for j in 0..num_bytes {
bytes[(M-1)*31 + j] = to_add_final[j];
bytes[(M-1)*31 + j] = to_add_final[j];
}
sha256_to_field(bytes)
}

pub fn compute_unencrypted_log_hash<T, N, M>(
contract_address: AztecAddress,
event_selector: Field,
log: T,
log: T
) -> Field where T: ToBytesForUnencryptedLog<N, M> {
let message_bytes: [u8; N] = log.to_be_bytes_arr();
// can't use N - not in scope error
Expand Down Expand Up @@ -182,42 +180,42 @@ fn compute_enc_log_hash_304() {
0x00b938289e563b0fe01982cd9b8d9e33e3069046768ad01c0fb05e429e7b7909,
0x00fbcc257a3211f705b471eee763b0f43876a2b2178fab6d2b09bd2b7e086584,
0x000000000000008c3289b5793b7448f4d45ecde039d004b6f037cad10b5c2336
];
];
let hash = compute_encrypted_log_hash(input);
assert(hash == 0x001e3c013994947fe28957a876bf1b2c3a69ac69cc92909efd4f2ae9b972f893);
}

#[test]
fn compute_enc_log_hash_368() {
let input = [
0x0000000000000000000000000000000000000000000000000000000000000000,
0x002190697d2a50e229a7a077e0951073f7d51e46679f10466153c308b63b1ea9,
0x00543e346facc6799b94514c9d461bcc836c04b083b13c2e4544a39130473c1e,
0x000df76d59526f8f953bcc7d9f77cdaefd36435931f0d7348f794bc275b42ded,
0x00a6d390ee1723af7f7ac1ae4fc81a266b2370fe07040a36d06dbe242e02413e,
0x00acbce15b6af1fbe94bd0f7b70f11768265dff77bfe63398f2a053efdfdf26d,
0x00b8b131b9f42c689beb095ba4f4a836d4d15c9068d0422e9add6ca82b786329,
0x00661a6a654b38f0f97d404ef5553e0efea9ed670561ae86685b31bbb2824fac,
0x00113a6b58edfaec0065b365f66ba8d8aa68254b8690035e8d671a17a843f0a1,
0x0023f2d2eae8c4449bac8f268a3e62a3faace1fe1401f0efdc8b0ccfbc8fb271,
0x00cf6603f8c61993dd2f662c719671c61727a2f4e925fb988b23d31feccd77d9,
0x0000000000a402a84b7294671799c38dd805f6a827a3a12633fdf91a57debe1f
];
0x0000000000000000000000000000000000000000000000000000000000000000,
0x002190697d2a50e229a7a077e0951073f7d51e46679f10466153c308b63b1ea9,
0x00543e346facc6799b94514c9d461bcc836c04b083b13c2e4544a39130473c1e,
0x000df76d59526f8f953bcc7d9f77cdaefd36435931f0d7348f794bc275b42ded,
0x00a6d390ee1723af7f7ac1ae4fc81a266b2370fe07040a36d06dbe242e02413e,
0x00acbce15b6af1fbe94bd0f7b70f11768265dff77bfe63398f2a053efdfdf26d,
0x00b8b131b9f42c689beb095ba4f4a836d4d15c9068d0422e9add6ca82b786329,
0x00661a6a654b38f0f97d404ef5553e0efea9ed670561ae86685b31bbb2824fac,
0x00113a6b58edfaec0065b365f66ba8d8aa68254b8690035e8d671a17a843f0a1,
0x0023f2d2eae8c4449bac8f268a3e62a3faace1fe1401f0efdc8b0ccfbc8fb271,
0x00cf6603f8c61993dd2f662c719671c61727a2f4e925fb988b23d31feccd77d9,
0x0000000000a402a84b7294671799c38dd805f6a827a3a12633fdf91a57debe1f
];
let hash = compute_encrypted_log_hash(input);
assert(hash == 0x00a0d651ac0cbc01b72430fa6a05d91738595af6e0229347b4c9968223387aeb);
assert(hash == 0x00a0d651ac0cbc01b72430fa6a05d91738595af6e0229347b4c9968223387aeb);
}

#[test]
fn compute_unenc_log_hash_array() {
let contract_address = AztecAddress::from_field(0x233a3e0df23b2b15b324194cb4a151f26c0b7333250781d34cc269d85dc334c6);
let event_selector = 5;
let log = [
let log = [
0x20660de09f35f876e3e69d227b2a35166ad05f09d82d06366ec9b6f65a51fec2,
0x1b52bfe3b8689761916f76dc3d38aa8810860db325cd39ca611eed980091f01c,
0x2e559c4045c378a56ad13b9edb1e8de4e7ad3b3aa35cc7ba9ec77f7a68fa43a4,
0x25d0f689c4a4178a29d59306f2675824d19be6d25e44fa03b03f49c263053dd2,
0x2d513a722d6f352dc0961f156afdc5e31495b9f0e35cb069261a8e55e2df67fd
];
];
let hash = compute_unencrypted_log_hash(contract_address, event_selector, log);
assert(hash == 0x00846d6969c8c2f61d39cd2762efcb0abb14f88d59c2675910251ef2bcffe9a7);
}
Expand Down
5 changes: 2 additions & 3 deletions noir-projects/aztec-nr/aztec/src/keys.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
mod getters;
mod point_to_symmetric_key;

use crate::keys::getters::{get_npk_m, get_ivpk_m,
// Commented out as it's currently not enabled in key registry
// Add once enabled in key registry:
// get_ovpk_m,
// get_tpk_m
};
use crate::keys::getters::{get_npk_m, get_ivpk_m};
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use dep::protocol_types::{constants::GENERATOR_INDEX__SYMMETRIC_KEY, grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, utils::arr_copy_slice};
use dep::protocol_types::{
constants::GENERATOR_INDEX__SYMMETRIC_KEY, grumpkin_private_key::GrumpkinPrivateKey,
grumpkin_point::GrumpkinPoint, utils::arr_copy_slice
};
use dep::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
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/note/constants.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ global MAX_NOTE_FIELDS_LENGTH: u64 = 20;
// + 2 for EXTRA_DATA: [number_of_return_notes, contract_address]
global GET_NOTE_ORACLE_RETURN_LENGTH: u64 = MAX_NOTE_FIELDS_LENGTH + 1 + 2;
global MAX_NOTES_PER_PAGE: u64 = 10;
global VIEW_NOTE_ORACLE_RETURN_LENGTH: u64 = MAX_NOTES_PER_PAGE * (MAX_NOTE_FIELDS_LENGTH + 1) + 2;
global VIEW_NOTE_ORACLE_RETURN_LENGTH: u64 = MAX_NOTES_PER_PAGE * (MAX_NOTE_FIELDS_LENGTH + 1) + 2;
11 changes: 9 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::note::{
};
use crate::oracle;

fn extract_property_value_from_selector<N>(serialized_note: [Field; N], selector: PropertySelector) -> Field {
fn extract_property_value_from_selector<N>(
serialized_note: [Field; N],
selector: PropertySelector
) -> Field {
// Selectors use PropertySelectors in order to locate note properties inside the serialized note.
// This allows easier packing and custom (de)serialization schemas. A note property is located
// inside the serialized note using the index inside the array, a byte offset and a length.
Expand All @@ -26,7 +29,11 @@ fn extract_property_value_from_selector<N>(serialized_note: [Field; N], selector
value_field
}

fn check_note_header<Note, N>(context: PrivateContext, storage_slot: Field, note: Note) where Note: NoteInterface<N> {
fn check_note_header<Note, N>(
context: PrivateContext,
storage_slot: Field,
note: Note
) where Note: NoteInterface<N> {
let header = note.get_header();
let contract_address = context.this_address();
assert(header.contract_address.eq(contract_address));
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/encryption.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[oracle(aes128Encrypt)]
pub fn aes128_encrypt_oracle<N, M>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8; M] {}

Expand Down
26 changes: 10 additions & 16 deletions noir-projects/aztec-nr/aztec/src/oracle/keys.nr
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use dep::protocol_types::{
address::{
AztecAddress,
PartialAddress,
},
grumpkin_point::GrumpkinPoint,
};
use dep::protocol_types::{address::{AztecAddress, PartialAddress}, grumpkin_point::GrumpkinPoint};

use crate::hash::poseidon2_hash;

#[oracle(getPublicKeysAndPartialAddress)]
fn get_public_keys_and_partial_address_oracle(_address: AztecAddress) -> [Field; 9] {}

unconstrained fn get_public_keys_and_partial_address_oracle_wrapper(address: AztecAddress) -> [Field; 9] {
get_public_keys_and_partial_address_oracle(address)
get_public_keys_and_partial_address_oracle(address)
}

fn get_public_keys_and_partial_address(address: AztecAddress) -> ([GrumpkinPoint; 4], PartialAddress) {
let result = get_public_keys_and_partial_address_oracle_wrapper(address);
let result = get_public_keys_and_partial_address_oracle_wrapper(address);

let nullifier_pub_key = GrumpkinPoint::new(result[0], result[1]);
let incoming_pub_key = GrumpkinPoint::new(result[2], result[3]);
let outgoing_pub_key = GrumpkinPoint::new(result[4], result[5]);
let tagging_pub_key = GrumpkinPoint::new(result[6], result[7]);
let partial_address = PartialAddress::from_field(result[8]);
let nullifier_pub_key = GrumpkinPoint::new(result[0], result[1]);
let incoming_pub_key = GrumpkinPoint::new(result[2], result[3]);
let outgoing_pub_key = GrumpkinPoint::new(result[4], result[5]);
let tagging_pub_key = GrumpkinPoint::new(result[6], result[7]);
let partial_address = PartialAddress::from_field(result[8]);

([nullifier_pub_key, incoming_pub_key, outgoing_pub_key, tagging_pub_key], partial_address)
}
([nullifier_pub_key, incoming_pub_key, outgoing_pub_key, tagging_pub_key], partial_address)
}
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn emit_encrypted_log_oracle<N, M>(
_note_type_id: Field,
_encryption_pub_key: GrumpkinPoint,
_preimage: [Field; N],
_counter: u32,
_counter: u32
) -> [Field; M] {}

unconstrained pub fn emit_encrypted_log<N, M>(
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/nullifier_key.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ unconstrained fn get_nullifier_keys_internal(account: AztecAddress) -> Nullifier
NullifierKeys {
account,
master_nullifier_public_key: GrumpkinPoint { x: result[0], y: result[1] },
app_nullifier_secret_key: result[2],
app_nullifier_secret_key: result[2]
}
}

Expand Down
5 changes: 1 addition & 4 deletions noir-projects/aztec-nr/tests/src/mock/test_note.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::aztec::context::PrivateContext;
use dep::aztec::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
};
use dep::aztec::note::{note_header::NoteHeader, note_interface::NoteInterface};

global TEST_NOTE_LENGTH = 1;

Expand Down
5 changes: 1 addition & 4 deletions noir-projects/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::aztec::{
protocol_types::{
address::AztecAddress, traits::{Deserialize, Serialize},
constants::GENERATOR_INDEX__NOTE_NULLIFIER
},
protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}, constants::GENERATOR_INDEX__NOTE_NULLIFIER},
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption},
oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key},
hash::poseidon2_hash, context::PrivateContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteHeader, NoteInterface};
use dep::aztec::{
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption,
hash::poseidon2_hash,
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash,
oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use dep::aztec::prelude::{AztecAddress, FunctionSelector, PrivateContext, NoteHeader, NoteGetterOptions, NoteViewerOptions};

use dep::aztec::{
protocol_types::{
traits::{ToField, Serialize, FromField},
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
},
protocol_types::{traits::{ToField, Serialize, FromField}, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL},
context::{PublicContext, Context}, note::note_getter::view_notes, state_vars::PrivateSet,
note::constants::MAX_NOTES_PER_PAGE,
note::constants::MAX_NOTES_PER_PAGE
};
use dep::std;
use dep::std::{option::Option};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteInterface, NoteHeader, PrivateContex
use dep::aztec::{
note::{utils::compute_note_hash_for_consumption},
oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key},
hash::poseidon2_hash, protocol_types::{traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER},
hash::poseidon2_hash, protocol_types::{traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER}
};

// Shows how to create a custom note
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use dep::aztec::prelude::{
AztecAddress, FunctionSelector, NoteHeader, NoteInterface, NoteGetterOptions, PrivateContext
};
use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteInterface, NoteGetterOptions, PrivateContext};

use dep::aztec::{
note::utils::compute_note_hash_for_consumption,
oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key},
hash::poseidon2_hash, protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash, protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER
};

global ECDSA_PUBLIC_KEY_NOTE_LEN: Field = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ contract KeyRegistry {

use dep::aztec::{
state_vars::{SharedMutable, Map},
protocol_types::{
grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress},
}
protocol_types::{grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress}}
};

global KEY_ROTATION_DELAY = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// be read/nullified before their creation etc.
contract PendingNoteHashes {
// Libs
use dep::aztec::prelude::{
AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, Map, PrivateSet
};
use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, Map, PrivateSet};
use dep::value_note::{balance_utils, filter::filter_notes_min_sum, value_note::{VALUE_NOTE_LEN, ValueNote}};
use dep::aztec::context::{PublicContext, Context};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContex
use dep::aztec::{
note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash,
oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key},
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER
};

global PUBLIC_KEY_NOTE_LEN: Field = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use dep::aztec::{
note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption},
hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext},
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER
};

global TRANSPARENT_NOTE_LEN: Field = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,12 @@ contract Uniswap {
token.eq(TokenBridge::at(token_bridge).get_token().static_call(&mut context)), "input_asset address is not the same as seen in the bridge contract"
);
}

// /// Unconstrained ///

// this method exists solely for e2e tests to test that nonce gets incremented each time.
unconstrained fn nonce_for_burn_approval() -> pub Field {
storage.nonce_for_burn_approval.read()
}
// docs:end:assert_token_is_same

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::parity_lib::{RootParityInputs, ParityPublicInputs};

#[recursive]
fn main(inputs: RootParityInputs) -> pub ParityPublicInputs {
fn main(inputs: RootParityInputs) -> pub ParityPublicInputs {
inputs.root_parity_circuit()
}
Loading