Skip to content

Commit

Permalink
Merge branch 'master' into feature/debugger-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mverzilli authored Dec 11, 2023
2 parents 01efcd0 + 010fdb6 commit f304e63
Show file tree
Hide file tree
Showing 30 changed files with 596 additions and 113 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/test-cargo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test cargo

on:
push:
branches:
- 'master'

jobs:
build:
name: Test cargo
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get current date
id: date
run: echo "date=$(date +'%Y.%m.%d.%H.%M')" >> $GITHUB_STATE
- name: prepare docker images tags
id: prep
run: |
REGISTRY="ghcr.io"
IMG="${REGISTRY}/${{ github.repository }}"
IMAGE=$(echo "$IMG" | tr '[:upper:]' '[:lower:]')
TAGS="${IMAGE}:${{ github.sha }}"
TAGS="${TAGS},${IMAGE}:latest,${IMAGE}:v${{ steps.date.outputs.date }}"
echo ::set-output name=tags::${TAGS}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Test cargo
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.ci
tags: ${{ steps.prep.outputs.tags }}
target: test-cargo
cache-from: type=gha
cache-to: type=gha,mode=max
16 changes: 13 additions & 3 deletions .github/workflows/test-js-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,13 @@ jobs:
run: |
yarn test:integration
# This is a noop job which depends on all test jobs
# This is a job which depends on all test jobs and reports the overall status.
# This allows us to add/remove test jobs without having to update the required workflows.
tests-end:
name: End
runs-on: ubuntu-latest
# We want this job to always run (even if the dependant jobs fail) as we want this job to fail rather than skipping.
if: ${{ always() }}
needs:
- test-acvm_js-node
- test-acvm_js-browser
Expand All @@ -435,5 +437,13 @@ jobs:
- test-integration

steps:
- name: Noop
run: echo "noop"
- name: Report overall success
run: |
if [[ $FAIL == true ]]; then
exit 1
else
exit 0
fi
env:
# We treat any skipped or failing jobs as a failure for the workflow as a whole.
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'skipped') }}
44 changes: 44 additions & 0 deletions .github/workflows/test-js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test JS packages

on:
push:
branches:
- 'master'

jobs:
build:
name: Test JS packages
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get current date
id: date
run: echo "date=$(date +'%Y.%m.%d.%H.%M')" >> $GITHUB_STATE
- name: prepare docker images tags
id: prep
run: |
REGISTRY="ghcr.io"
IMG="${REGISTRY}/${{ github.repository }}"
IMAGE=$(echo "$IMG" | tr '[:upper:]' '[:lower:]')
TAGS="${IMAGE}:${{ github.sha }}"
TAGS="${TAGS},${IMAGE}:latest,${IMAGE}:v${{ steps.date.outputs.date }}"
echo ::set-output name=tags::${TAGS}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Test JS packages
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.ci
tags: ${{ steps.prep.outputs.tags }}
target: test-js
cache-from: type=gha
cache-to: type=gha,mode=max
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Dockerfile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
FROM rust:1-slim-bookworm as test-base
RUN apt-get update && apt-get upgrade -y && apt-get install build-essential git -y
WORKDIR /usr/src/noir
COPY . .
RUN ./scripts/bootstrap_native.sh
ENV PATH="${PATH}:/usr/src/noir/target/release/"

FROM test-base as test-cargo
RUN apt-get install -y curl libc++-dev
RUN ./scripts/test_native.sh

FROM test-base as test-js
RUN apt-get install pkg-config libssl-dev -y
RUN ./scripts/install_wasm-bindgen.sh
RUN apt-get install -y ca-certificates curl gnupg
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update && apt-get install nodejs -y
RUN corepack enable
RUN yarn --immutable
RUN apt-get install -y jq
RUN yarn build
RUN yarn workspace @noir-lang/acvm_js test
RUN npx playwright install && npx playwright install-deps
RUN yarn workspace @noir-lang/acvm_js test:browser
RUN yarn workspace @noir-lang/noirc_abi test
RUN yarn workspace @noir-lang/noirc_abi test:browser
RUN yarn workspace @noir-lang/backend_barretenberg test
RUN ./scripts/nargo_compile_noir_js_assert_lt.sh
RUN rm -rf /usr/src/noir/tooling/noir_js/test/noir_compiled_examples/assert_lt/target/debug_assert_lt.json
RUN yarn workspace @noir-lang/noir_js test
RUN yarn workspace @noir-lang/source-resolver test
RUN ./scripts/nargo_compile_wasm_fixtures.sh
RUN yarn workspace @noir-lang/noir_wasm test:node
RUN yarn workspace @noir-lang/noir_wasm test:browser
RUN ./scripts/nargo_compile_noir_codegen_assert_lt.sh
RUN rm -rf /usr/src/noir/tooling/noir_codegen/test/assert_lt/target/debug_assert_lt.json
RUN yarn workspace @noir-lang/noir_codegen test
RUN apt-get install -y libc++-dev
RUN yarn test:integration
4 changes: 3 additions & 1 deletion Dockerfile.packages
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ RUN apk update \
npm \
yarn \
bash \
jq
jq \
git

WORKDIR /usr/src/noir
COPY . .
RUN ./scripts/bootstrap_packages.sh
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This crate's minimum supported rustc version is 1.71.1.

## Working on this project

This project uses [Nix](https://nixos.org/) and [direnv](https://direnv.net/) to streamline the development experience. Please follow [our guidelines](https://noir-lang.org/getting_started/nargo_installation/#option-4-compile-from-source) to setup your environment for working on the project.
This project uses [Nix](https://nixos.org/) and [direnv](https://direnv.net/) to streamline the development experience. Please follow [our guidelines](https://noir-lang.org/getting_started/nargo_installation/#option-3-compile-from-source) to setup your environment for working on the project.

### Building against a different local/remote version of Barretenberg

Expand Down
24 changes: 23 additions & 1 deletion acvm-repo/acir_field/src/generic_ark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ impl<F: PrimeField> FieldElement<F> {
}
pub fn from_hex(hex_str: &str) -> Option<FieldElement<F>> {
let value = hex_str.strip_prefix("0x").unwrap_or(hex_str);
let hex_as_bytes = hex::decode(value).ok()?;
// Values of odd length require an additional "0" prefix
let sanitized_value =
if value.len() % 2 == 0 { value.to_string() } else { format!("0{}", value) };
let hex_as_bytes = hex::decode(sanitized_value).ok()?;
Some(FieldElement::from_be_bytes_reduce(&hex_as_bytes))
}

Expand Down Expand Up @@ -446,6 +449,25 @@ mod tests {
assert_eq!(minus_i_field_element.to_hex(), string);
}
}

#[test]
fn deserialize_even_and_odd_length_hex() {
// Test cases of (odd, even) length hex strings
let hex_strings =
vec![("0x0", "0x00"), ("0x1", "0x01"), ("0x002", "0x0002"), ("0x00003", "0x000003")];
for (i, case) in hex_strings.into_iter().enumerate() {
let i_field_element =
crate::generic_ark::FieldElement::<ark_bn254::Fr>::from(i as i128);
let odd_field_element =
crate::generic_ark::FieldElement::<ark_bn254::Fr>::from_hex(case.0).unwrap();
let even_field_element =
crate::generic_ark::FieldElement::<ark_bn254::Fr>::from_hex(case.1).unwrap();

assert_eq!(i_field_element, odd_field_element);
assert_eq!(odd_field_element, even_field_element);
}
}

#[test]
fn max_num_bits_smoke() {
let max_num_bits_bn254 = crate::generic_ark::FieldElement::<ark_bn254::Fr>::max_num_bits();
Expand Down
6 changes: 5 additions & 1 deletion compiler/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ mod file_map;
mod file_reader;

pub use file_map::{File, FileId, FileMap, PathString};

// Re-export for the lsp
pub use codespan_reporting::files as codespan_files;

use file_reader::is_stdlib_asset;
pub use file_reader::FileReader;

Expand All @@ -19,7 +23,7 @@ pub const FILE_EXTENSION: &str = "nr";

pub struct FileManager {
root: PathBuf,
file_map: file_map::FileMap,
file_map: FileMap,
id_to_path: HashMap<FileId, PathBuf>,
path_to_id: HashMap<PathBuf, FileId>,
file_reader: Box<FileReader>,
Expand Down
11 changes: 3 additions & 8 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,11 @@ impl FunctionBuilder {
(FieldElement::max_num_bits(), self.insert_binary(predicate, BinaryOp::Mul, pow))
};

let instruction = Instruction::Binary(Binary { lhs, rhs: pow, operator: BinaryOp::Mul });
if max_bit <= bit_size {
self.insert_instruction(instruction, None).first()
self.insert_binary(lhs, BinaryOp::Mul, pow)
} else {
let result = self.insert_instruction(instruction, None).first();
self.insert_instruction(
Instruction::Truncate { value: result, bit_size, max_bit_size: max_bit },
None,
)
.first()
let result = self.insert_binary(lhs, BinaryOp::Mul, pow);
self.insert_truncate(result, bit_size, max_bit)
}
}

Expand Down
14 changes: 13 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,23 @@ impl Instruction {
}
None
}
Instruction::Truncate { value, bit_size, .. } => {
Instruction::Truncate { value, bit_size, max_bit_size } => {
if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) {
let integer_modulus = 2_u128.pow(*bit_size);
let truncated = numeric_constant.to_u128() % integer_modulus;
SimplifiedTo(dfg.make_constant(truncated.into(), typ))
} else if let Value::Instruction { instruction, .. } = &dfg[dfg.resolve(*value)] {
if let Instruction::Truncate { bit_size: src_bit_size, .. } = &dfg[*instruction]
{
// If we're truncating the value to fit into the same or larger bit size then this is a noop.
if src_bit_size <= bit_size && src_bit_size <= max_bit_size {
SimplifiedTo(*value)
} else {
None
}
} else {
None
}
} else {
None
}
Expand Down
49 changes: 12 additions & 37 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,11 @@ impl<'a> FunctionContext<'a> {
let bit_width =
self.builder.numeric_constant(FieldElement::from(2_i128.pow(bit_size)), Type::field());
let sign_not = self.builder.insert_binary(one, BinaryOp::Sub, sign);
let as_field =
self.builder.insert_instruction(Instruction::Cast(input, Type::field()), None).first();
let sign_field =
self.builder.insert_instruction(Instruction::Cast(sign, Type::field()), None).first();
let as_field = self.builder.insert_cast(input, Type::field());
let sign_field = self.builder.insert_cast(sign, Type::field());
let positive_predicate = self.builder.insert_binary(sign_field, BinaryOp::Mul, as_field);
let two_complement = self.builder.insert_binary(bit_width, BinaryOp::Sub, as_field);
let sign_not_field = self
.builder
.insert_instruction(Instruction::Cast(sign_not, Type::field()), None)
.first();
let sign_not_field = self.builder.insert_cast(sign_not, Type::field());
let negative_predicate =
self.builder.insert_binary(sign_not_field, BinaryOp::Mul, two_complement);
self.builder.insert_binary(positive_predicate, BinaryOp::Add, negative_predicate)
Expand Down Expand Up @@ -315,17 +310,8 @@ impl<'a> FunctionContext<'a> {
match operator {
BinaryOpKind::Add | BinaryOpKind::Subtract => {
// Result is computed modulo the bit size
let mut result = self
.builder
.insert_instruction(
Instruction::Truncate {
value: result,
bit_size,
max_bit_size: bit_size + 1,
},
None,
)
.first();
let mut result =
self.builder.insert_truncate(result, bit_size, bit_size + 1);
result = self.builder.insert_cast(result, Type::unsigned(bit_size));

self.check_signed_overflow(result, lhs, rhs, operator, bit_size, location);
Expand All @@ -335,17 +321,7 @@ impl<'a> FunctionContext<'a> {
// Result is computed modulo the bit size
let mut result =
self.builder.insert_cast(result, Type::unsigned(2 * bit_size));
result = self
.builder
.insert_instruction(
Instruction::Truncate {
value: result,
bit_size,
max_bit_size: 2 * bit_size,
},
None,
)
.first();
result = self.builder.insert_truncate(result, bit_size, 2 * bit_size);

self.check_signed_overflow(result, lhs, rhs, operator, bit_size, location);
self.builder.insert_cast(result, result_type)
Expand Down Expand Up @@ -476,17 +452,16 @@ impl<'a> FunctionContext<'a> {

// Then we check the signed product fits in a signed integer of bit_size-bits
let not_same = self.builder.insert_binary(one, BinaryOp::Sub, same_sign);
let not_same_sign_field = self
.builder
.insert_instruction(Instruction::Cast(not_same, Type::unsigned(bit_size)), None)
.first();
let not_same_sign_field =
self.builder.insert_cast(not_same, Type::unsigned(bit_size));
let positive_maximum_with_offset =
self.builder.insert_binary(half_width, BinaryOp::Add, not_same_sign_field);
let product_overflow_check =
self.builder.insert_binary(product, BinaryOp::Lt, positive_maximum_with_offset);
self.builder.set_location(location).insert_instruction(
Instruction::Constrain(product_overflow_check, one, Some(message)),
None,
self.builder.set_location(location).insert_constrain(
product_overflow_check,
one,
Some(message),
);
}
BinaryOpKind::ShiftLeft => unreachable!("shift is not supported for signed integer"),
Expand Down
5 changes: 1 addition & 4 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,10 +1662,7 @@ impl<'a> Resolver<'a> {
fn eval_global_as_array_length(&mut self, global: StmtId) -> u64 {
let stmt = match self.interner.statement(&global) {
HirStatement::Let(let_expr) => let_expr,
other => {
dbg!(other);
return 0;
}
_ => return 0,
};

let length = stmt.expression;
Expand Down
Loading

0 comments on commit f304e63

Please sign in to comment.