Skip to content

Commit

Permalink
refactor: camelCase in noir-projects -> snake_case (#5381)
Browse files Browse the repository at this point in the history
Can we add a ci check for style on noir code so this doesn't creep back
in again?

Resolves #1928.
  • Loading branch information
sklppy88 authored Mar 22, 2024
1 parent 5d669b9 commit eea711f
Show file tree
Hide file tree
Showing 32 changed files with 292 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ In this contract, we will store three vars:

1. admin, as an Aztec address held in public state
2. tally, as a map with key as the persona and value as the number (in Field) held in public state
3. voteEnded, as a boolean held in public state
3. vote_ended, as a boolean held in public state

## Constructor

The next step is to initialize the contract with a constructor. The constructor will take an address as a parameter and set the admin.

#include_code constructor noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr rust

This function takes the admin argument and writes it to the storage. We are also using this function to set the `voteEnded` boolean as false in the same way.
This function takes the admin argument and writes it to the storage. We are also using this function to set the `vote_ended` boolean as false in the same way.

## Casting a vote privately

Expand Down Expand Up @@ -125,7 +125,7 @@ Create this new public function like this:

The first thing we do here is assert that the vote has not ended.

`assert()` takes two arguments: the assertion, in this case that `storage.voteEnded` is not false, and the error thrown if the assertion fails.
`assert()` takes two arguments: the assertion, in this case that `storage.vote_ended` is not false, and the error thrown if the assertion fails.

The code after the assertion will only run if the assertion is true. In this snippet, we read the current vote tally at the voteId, add 1 to it, and write this new number to the voteId. The `Field` element allows us to use `+` to add to an integer.

Expand Down
16 changes: 8 additions & 8 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ impl PublicContextInterface for AVMContext {
temporary_function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420];
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];

let results = call(
gas,
contract_address,
args,
temporary_function_selector.to_field()
);
let returnData: [Field; RETURN_VALUES_LENGTH] = results.0;
let data_to_return: [Field; RETURN_VALUES_LENGTH] = results.0;
let success: u8 = results.1;
assert(success == 1, "Nested call failed!");

returnData
data_to_return
}

fn static_call_public_function<ARGS_COUNT>(
Expand All @@ -142,17 +142,17 @@ impl PublicContextInterface for AVMContext {
temporary_function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420];
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];

let (returnData, success): ([Field; RETURN_VALUES_LENGTH], u8) = call_static(
let (data_to_return, success): ([Field; RETURN_VALUES_LENGTH], u8) = call_static(
gas,
contract_address,
args,
temporary_function_selector.to_field()
);

assert(success == 1, "Nested static call failed!");
returnData
data_to_return
}

fn delegate_call_public_function<ARGS_COUNT>(
Expand Down Expand Up @@ -263,7 +263,7 @@ fn send_l2_to_l1_msg(recipient: EthAddress, content: Field) {}

#[oracle(avmOpcodeCall)]
fn call<ARGS_COUNT, RET_SIZE>(
gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas]
gas: [Field; 3], // gas allocation: [l1_gas, l2_gas, da_gas]
address: AztecAddress,
args: [Field; ARGS_COUNT],
// TODO(5110): consider passing in calldata directly
Expand All @@ -273,7 +273,7 @@ fn call<ARGS_COUNT, RET_SIZE>(

#[oracle(avmOpcodeStaticCall)]
fn call_static<ARGS_COUNT, RET_SIZE>(
gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas]
gas: [Field; 3], // gas allocation: [l1_gas, l2_gas, da_gas]
address: AztecAddress,
args: [Field; ARGS_COUNT],
// TODO(5110): consider passing in calldata directly
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn create_note<Note, N>(
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
// TODO: change this to note.set_header(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
Note::set_header(note, header);
// As `is_transient` is true, this will compute the inner note hsah
let inner_note_hash = compute_note_hash_for_insertion(*note);
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn create_note_hash_from_public<Note, N>(
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
// TODO: change this to note.set_header(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
Note::set_header(note, header);
let inner_note_hash = compute_note_hash_for_insertion(*note);

Expand Down
16 changes: 8 additions & 8 deletions noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ fn check_note_fields<N>(serialized_note: [Field; N], selects: BoundedVec<Option<
let value_field = extract_property_value_from_selector(serialized_note, select.property_selector);

// Values are computed ahead of time because circuits evaluate all branches
let isEqual = value_field == select.value.to_field();
let isLt = value_field.lt(select.value.to_field());
let is_equal = value_field == select.value.to_field();
let is_lt = value_field.lt(select.value.to_field());

if (select.comparator == Comparator.EQ) {
assert(isEqual, "Mismatch return note field.");
assert(is_equal, "Mismatch return note field.");
} else if (select.comparator == Comparator.NEQ) {
assert(!isEqual, "Mismatch return note field.");
assert(!is_equal, "Mismatch return note field.");
} else if (select.comparator == Comparator.LT) {
assert(isLt, "Mismatch return note field.");
assert(is_lt, "Mismatch return note field.");
} else if (select.comparator == Comparator.LTE) {
assert(isLt | isEqual, "Mismatch return note field.");
assert(is_lt | is_equal, "Mismatch return note field.");
} else if (select.comparator == Comparator.GT) {
assert(!isLt & !isEqual, "Mismatch return note field.");
assert(!is_lt & !is_equal, "Mismatch return note field.");
} else if (select.comparator == Comparator.GTE) {
assert(!isLt, "Mismatch return note field.");
assert(!is_lt, "Mismatch return note field.");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn compute_note_hash_and_nullifier<T, N, S>(
serialized_note: [Field; S] // docs:end:compute_note_hash_and_nullifier_args
) -> [Field; 4] where T: NoteInterface<N> {
let mut note = deserialize_content(arr_copy_slice(serialized_note, [0; N], 0));
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
// TODO: change this to note.set_header(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
T::set_header((&mut note), note_header);

let inner_note_hash = compute_inner_note_hash(note);
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ unconstrained pub fn get_notes<Note, N, M, S, NS>(
let header = NoteHeader { contract_address, nonce, storage_slot, is_transient };
let serialized_note = arr_copy_slice(fields, [0; N], read_offset + 2);
let mut note = Note::deserialize_content(serialized_note);
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
// TODO: change this to note.set_header(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
Note::set_header(&mut note, header);
placeholder_opt_notes[i] = Option::some(note);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,68 +291,68 @@ contract AvmTest {

// Directly call the external call opcode to initiate a nested call to the add function
#[aztec(public-vm)]
fn raw_nested_call_to_add(argA: Field, argB: Field) -> pub Field {
fn raw_nested_call_to_add(arg_a: Field, arg_b: Field) -> pub Field {
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)").to_field();
let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420];
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];

// Nested call
let results = context.call_public_function_raw(gas, context.this_address(), selector, [argA, argB]);
let returnData: [Field; 1] = results.0;
// this explicit size ^ is necessary to ensure that retSize is compile-time
// (ensure the returnData is in a HeapArray not a HeapVector)
let results = context.call_public_function_raw(gas, context.this_address(), selector, [arg_a, arg_b]);
let data_to_return: [Field; 1] = results.0;
// this explicit size ^ is necessary to ensure that ret_size is compile-time
// (ensure the data_to_return is in a HeapArray not a HeapVector)
let success: u8 = results.1;

assert(success == 1, "Call failed");

let addResult = returnData[0];
addResult
let add_result = data_to_return[0];
add_result
}

// Use the `call_public_function` wrapper to initiate a nested call to the add function
#[aztec(public-vm)]
fn nested_call_to_add(argA: Field, argB: Field) -> pub Field {
fn nested_call_to_add(arg_a: Field, arg_b: Field) -> pub Field {
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)");

// Nested call using standard context interface function
let returnData: [Field; RETURN_VALUES_LENGTH] = context.call_public_function(context.this_address(), selector, [argA, argB]);
// this explicit size ^ is necessary to ensure that retSize is compile-time
// (ensure the returnData is in a HeapArray not a HeapVector)
let data_to_return: [Field; RETURN_VALUES_LENGTH] = context.call_public_function(context.this_address(), selector, [arg_a, arg_b]);
// this explicit size ^ is necessary to ensure that ret_size is compile-time
// (ensure the data_to_return is in a HeapArray not a HeapVector)

let addResult = returnData[0];
addResult
let add_result = data_to_return[0];
add_result
}

// Directly call_static the external call opcode to initiate a nested call to the add function
#[aztec(public-vm)]
fn raw_nested_static_call_to_add(argA: Field, argB: Field) -> pub (Field, u8) {
fn raw_nested_static_call_to_add(arg_a: Field, arg_b: Field) -> pub (Field, u8) {
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)").to_field();
let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420];
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];

let (resultData, success): ([Field; 1], u8) = context.static_call_public_function_raw(gas, context.this_address(), selector, [argA, argB]);
let (result_data, success): ([Field; 1], u8) = context.static_call_public_function_raw(gas, context.this_address(), selector, [arg_a, arg_b]);

(resultData[0], success)
(result_data[0], success)
}

// Directly call_static `set_storage_single`. Should fail since it's accessing storage.
#[aztec(public-vm)]
fn raw_nested_static_call_to_set_storage() -> pub u8 {
let selector = FunctionSelector::from_signature("set_storage_single(Field)").to_field();
let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420];
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];
let calldata: [Field; 1] = [20];

let (_returnData, success): ([Field; 0], u8) = context.static_call_public_function_raw(gas, context.this_address(), selector, calldata);
let (_data_to_return, success): ([Field; 0], u8) = context.static_call_public_function_raw(gas, context.this_address(), selector, calldata);

success
}

// Indirectly call_static the external call opcode to initiate a nested call to the add function
#[aztec(public-vm)]
fn nested_static_call_to_add(argA: Field, argB: Field) -> pub Field {
fn nested_static_call_to_add(arg_a: Field, arg_b: Field) -> pub Field {
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)");

let resultData: [Field; RETURN_VALUES_LENGTH] = context.static_call_public_function(context.this_address(), selector, [argA, argB]);
let result_data: [Field; RETURN_VALUES_LENGTH] = context.static_call_public_function(context.this_address(), selector, [arg_a, arg_b]);

resultData[0]
result_data[0]
}

// Indirectly call_static `set_storage_single`. Should revert since it's accessing storage.
Expand Down
30 changes: 15 additions & 15 deletions noir-projects/noir-contracts/contracts/child_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,40 @@ contract Child {
// Can only be called from this contract.
#[aztec(private)]
#[aztec(internal)]
fn valueInternal(input: Field) -> Field {
fn value_internal(input: Field) -> Field {
input + context.chain_id() + context.version()
}

// Returns base_value + chain_id + version + block_number + timestamp
#[aztec(public)]
fn pubGetValue(base_value: Field) -> Field {
let returnValue = base_value
fn pub_get_value(base_value: Field) -> Field {
let return_value = base_value
+ context.chain_id()
+ context.version()
+ context.block_number()
+ context.timestamp();

returnValue
return_value
}

// Sets `current_value` to `new_value`
#[aztec(public)]
fn pubSetValue(new_value: Field) -> Field {
fn pub_set_value(new_value: Field) -> Field {
storage.current_value.write(new_value);
emit_unencrypted_log(&mut context, new_value);

new_value
}

#[aztec(private)]
fn privateSetValue(new_value: Field, owner: AztecAddress) -> Field {
fn private_set_value(new_value: Field, owner: AztecAddress) -> Field {
let mut note = ValueNote::new(new_value, owner);
storage.a_private_value.insert(&mut note, true);
new_value
}

#[aztec(private)]
fn privateGetValue(amount: Field, owner: AztecAddress) -> Field {
fn private_get_value(amount: Field, owner: AztecAddress) -> Field {
let options = NoteGetterOptions::new().select(ValueNote::properties().value, amount, Option::none()).select(
ValueNote::properties().owner,
owner.to_field(),
Expand All @@ -67,7 +67,7 @@ contract Child {

// Increments `current_value` by `new_value`
#[aztec(public)]
fn pubIncValue(new_value: Field) -> Field {
fn pub_inc_value(new_value: Field) -> Field {
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
emit_unencrypted_log(&mut context, new_value);
Expand All @@ -78,7 +78,7 @@ contract Child {
// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
#[aztec(internal)]
fn pubIncValueInternal(new_value: Field) -> Field {
fn pub_inc_value_internal(new_value: Field) -> Field {
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
emit_unencrypted_log(&mut context, new_value);
Expand All @@ -87,20 +87,20 @@ contract Child {
}

#[aztec(public)]
fn setValueTwiceWithNestedFirst() {
let pubSetValueSelector = FunctionSelector::from_signature("pubSetValue(Field)");
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);
fn set_value_twice_with_nested_first() {
let pub_set_value_selector = FunctionSelector::from_signature("pub_set_value(Field)");
let _ret = context.call_public_function(context.this_address(), pub_set_value_selector, [10]);

storage.current_value.write(20);
emit_unencrypted_log(&mut context, 20);
}

#[aztec(public)]
fn setValueTwiceWithNestedLast() {
fn set_value_twice_with_nested_last() {
storage.current_value.write(20);
emit_unencrypted_log(&mut context, 20);

let pubSetValueSelector = FunctionSelector::from_signature("pubSetValue(Field)");
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);
let pub_set_value_selector = FunctionSelector::from_signature("pub_set_value(Field)");
let _ret = context.call_public_function(context.this_address(), pub_set_value_selector, [10]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ contract Delegator {

#[aztec(private)]
fn private_delegate_set_value(
targetContract: AztecAddress,
targetSelector: FunctionSelector,
target_contract: AztecAddress,
target_selector: FunctionSelector,
args: [Field; 2]
) {
// Call the target private function
let return_values = context.delegate_call_private_function(targetContract, targetSelector, args);
let return_values = context.delegate_call_private_function(target_contract, target_selector, args);
// Copy the return value from the call to this function's return values
return_values[0]
}

#[aztec(private)]
fn enqueued_delegate_set_value(
targetContract: AztecAddress,
targetSelector: FunctionSelector,
target_contract: AztecAddress,
target_selector: FunctionSelector,
args: [Field; 1]
) {
context.delegate_call_public_function(targetContract, targetSelector, args);
context.delegate_call_public_function(target_contract, target_selector, args);
}

#[aztec(public)]
fn public_delegate_set_value(
targetContract: AztecAddress,
targetSelector: FunctionSelector,
target_contract: AztecAddress,
target_selector: FunctionSelector,
args: [Field; 1]
) {
let _ = context.delegate_call_public_function(targetContract, targetSelector, args);
let _ = context.delegate_call_public_function(target_contract, target_selector, args);
}

unconstrained fn view_private_value(amount: Field, owner: AztecAddress) -> pub Field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract EasyPrivateToken {
}

// Helper function to get the balance of a user ("unconstrained" is a Noir alternative of Solidity's "view" function).
unconstrained fn getBalance(owner: AztecAddress) -> pub Field {
unconstrained fn get_balance(owner: AztecAddress) -> pub Field {
let balances = storage.balances;

// Return the sum of all notes in the set.
Expand Down
Loading

0 comments on commit eea711f

Please sign in to comment.