Skip to content

Commit

Permalink
chore(ci): enforce formatting of noir code in CI (#4422)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

We currently format everything in the repository except our noir source
code. If we enforce this internally then we'll uncover issues in the
formatter earlier and provide a good example of what Noir source should
look like.

We then now run `nargo fmt --check` on the stdlib and
`test_programs/execution_success`

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Feb 26, 2024
1 parent cd796de commit 15c5618
Show file tree
Hide file tree
Showing 46 changed files with 455 additions and 397 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,68 @@ jobs:

- name: Run `yarn lint`
run: yarn lint

build-nargo:
runs-on: ubuntu-22.04
timeout-minutes: 30

steps:
- name: Checkout Noir repo
uses: actions/checkout@v4

- name: Setup toolchain
uses: dtolnay/rust-toolchain@1.73.0

- uses: Swatinem/rust-cache@v2
with:
key: x86_64-unknown-linux-gnu
cache-on-failure: true
save-if: ${{ github.event_name != 'merge_group' }}

- name: Build Nargo
run: cargo build --package nargo_cli --release

- name: Package artifacts
run: |
mkdir dist
cp ./target/release/nargo ./dist/nargo
7z a -ttar -so -an ./dist/* | 7z a -si ./nargo-x86_64-unknown-linux-gnu.tar.gz
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: nargo
path: ./dist/*
retention-days: 3

nargo_fmt:
needs: [build-nargo]
name: Nargo fmt
runs-on: ubuntu-latest
timeout-minutes: 30

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download nargo binary
uses: actions/download-artifact@v4
with:
name: nargo
path: ./nargo

- name: Set nargo on PATH
run: |
nargo_binary="${{ github.workspace }}/nargo/nargo"
chmod +x $nargo_binary
echo "$(dirname $nargo_binary)" >> $GITHUB_PATH
export PATH="$PATH:$(dirname $nargo_binary)"
nargo -V
- name: Format stdlib
working-directory: ./noir_stdlib
run: nargo fmt --check

- name: Format test suite
working-directory: ./test_programs
run: ./format.sh check
23 changes: 11 additions & 12 deletions noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,41 @@ impl<T, N> [T; N] {
for i in 0..N {
let pos = find_index(sorted_index, i);
assert(sorted_index[pos] == i);
}
}
// Sort the array using the indexes
for i in 0..N {
for i in 0..N {
result[i] = self[sorted_index[i]];
}
}
// Ensure the array is sorted
for i in 0..N-1 {
assert(ordering(result[i], result[i+1]));
for i in 0..N - 1 {
assert(ordering(result[i], result[i + 1]));
}

result
}

/// Returns the index of the elements in the array that would sort it, using the provided custom sorting function.
unconstrained fn get_sorting_index<Env>(self, ordering: fn[Env](T, T) -> bool) -> [u64; N] {
let mut result = [0;N];
let mut result = [0; N];
let mut a = self;
for i in 0..N {
result[i] = i;
}
for i in 1 .. N {
for i in 1..N {
for j in 0..i {
if ordering(a[i], a[j]) {
let old_a_j = a[j];
a[j] = a[i];
a[i] = old_a_j;
let old_j = result[j];
result[j] = result[i];
result[i] = old_j;
result[i] = old_j;
}
}
}
result
}


// Converts an array into a slice.
pub fn as_slice(self) -> [T] {
let mut slice = [];
Expand All @@ -68,7 +67,7 @@ impl<T, N> [T; N] {
let first_elem = f(self[0]);
let mut ret = [first_elem; N];

for i in 1 .. self.len() {
for i in 1..self.len() {
ret[i] = f(self[i]);
}

Expand All @@ -90,7 +89,7 @@ impl<T, N> [T; N] {
// element of the given array as its starting accumulator value.
pub fn reduce<Env>(self, f: fn[Env](T, T) -> T) -> T {
let mut accumulator = self[0];
for i in 1 .. self.len() {
for i in 1..self.len() {
accumulator = f(accumulator, self[i]);
}
accumulator
Expand Down Expand Up @@ -122,7 +121,7 @@ unconstrained fn find_index<N>(a: [u64; N], find: u64) -> u64 {
for i in 0..a.len() {
if a[i] == find {
result = i;
}
}
}
result
}
16 changes: 5 additions & 11 deletions noir_stdlib/src/bigint.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ops::{Add, Sub, Mul, Div, Rem,};

use crate::ops::{Add, Sub, Mul, Div, Rem};

global bn254_fq = [0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30];
Expand All @@ -13,7 +12,6 @@ global secpr1_fq = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
global secpr1_fr = [0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,0xFF, 0xFF, 0xFF, 0xFF];


struct BigInt {
pointer: u32,
Expand All @@ -22,17 +20,13 @@ struct BigInt {

impl BigInt {
#[builtin(bigint_add)]
fn bigint_add(self, other: BigInt) -> BigInt {
}
fn bigint_add(self, other: BigInt) -> BigInt {}
#[builtin(bigint_sub)]
fn bigint_sub(self, other: BigInt) -> BigInt {
}
fn bigint_sub(self, other: BigInt) -> BigInt {}
#[builtin(bigint_mul)]
fn bigint_mul(self, other: BigInt) -> BigInt {
}
fn bigint_mul(self, other: BigInt) -> BigInt {}
#[builtin(bigint_div)]
fn bigint_div(self, other: BigInt) -> BigInt {
}
fn bigint_div(self, other: BigInt) -> BigInt {}
#[builtin(bigint_from_le_bytes)]
fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {}
#[builtin(bigint_to_le_bytes)]
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len
}

pub fn max_len(_self: BoundedVec<T, MaxLen>) -> u64{
pub fn max_len(_self: BoundedVec<T, MaxLen>) -> u64 {
MaxLen
}

Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/collections/vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ impl<T> Vec<T> {
/// points beyond the end of the vector.
pub fn get(self, index: u64) -> T {
self.slice[index]
}
}

/// Push a new element to the end of the vector, returning a
/// new vector with a length one greater than the
/// original unmodified vector.
pub fn push(&mut self, elem: T) {
pub fn push(&mut self, elem: T) {
self.slice = self.slice.push_back(elem);
}

/// Pop an element from the end of the given vector, returning
/// a new vector with a length of one less than the given vector,
/// as well as the popped element.
/// Panics if the given vector's length is zero.
pub fn pop(&mut self) -> T {
pub fn pop(&mut self) -> T {
let (popped_slice, last_elem) = self.slice.pop_back();
self.slice = popped_slice;
last_elem
Expand All @@ -42,7 +42,7 @@ impl<T> Vec<T> {
/// after it to the right
pub fn insert(&mut self, index: u64, elem: T) {
self.slice = self.slice.insert(index, elem);
}
}

/// Remove an element at a specified index, shifting all elements
/// after it to the left, returning the removed element
Expand Down
Loading

0 comments on commit 15c5618

Please sign in to comment.