Skip to content

Commit

Permalink
chore: replicate noir-lang/noir#4946
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jun 26, 2024
1 parent 646d45a commit c5742b6
Show file tree
Hide file tree
Showing 112 changed files with 133 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,32 @@ fn resolve_path_to_ns(
// Resolve from the root of the crate
resolve_path_from_crate_root(crate_id, importing_crate, import_path, def_maps)
}
crate::ast::PathKind::Dep => {
resolve_external_dep(def_map, import_directive, def_maps, importing_crate)
}
crate::ast::PathKind::Plain => {
// Plain paths are only used to import children modules. It's possible to allow import of external deps, but maybe this distinction is better?
// In Rust they can also point to external Dependencies, if no children can be found with the specified name
// There is a possibility that the import path is empty
// In that case, early return
if import_path.is_empty() {
return resolve_name_in_module(
crate_id,
importing_crate,
import_path,
import_directive.module_id,
def_maps,
);
}

let current_mod_id = ModuleId { krate: crate_id, local_id: import_directive.module_id };
let current_mod = &def_map.modules[current_mod_id.local_id.0];
let first_segment = import_path.first().expect("ice: could not fetch first segment");
if current_mod.find_name(first_segment).is_none() {
// Resolve externally when first segment is unresolved
return resolve_external_dep(
def_map,
import_directive,
def_maps,
importing_crate,
);
}

resolve_name_in_module(
crate_id,
importing_crate,
Expand All @@ -154,8 +174,15 @@ fn resolve_path_to_ns(
def_maps,
)
}

crate::ast::PathKind::Dep => resolve_external_dep(
def_map,
import_directive,
def_maps,
importing_crate,
),
}
}
}

fn resolve_path_from_crate_root(
crate_id: CrateId,
Expand Down Expand Up @@ -270,8 +297,9 @@ fn resolve_external_dep(
.get(&crate_name.0.contents)
.ok_or_else(|| PathResolutionError::Unresolved(crate_name.to_owned()))?;

// Create an import directive for the dependency crate
let path_without_crate_name = &path[1..]; // XXX: This will panic if the path is of the form `use dep::std` Ideal algorithm will not distinguish between crate and module
// XXX: This will panic if the path is of the form `use std`. Ideal algorithm will not distinguish between crate and module
// See `singleton_import.nr` test case for a check that such cases are handled elsewhere.
let path_without_crate_name = &path[1..];

let path = Path {
segments: path_without_crate_name.to_vec(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(crate) Path: Path = {
},

<lo:@L> "dep" "::" <segments:PathSegments> <hi:@R> => {
let kind = PathKind::Dep;
let kind = PathKind::Plain;
let span = Span::from(lo as u32..hi as u32);
Path { segments, kind, span }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ mod test {
"use foo::{bar, hello}",
"use foo::{bar as bar2, hello}",
"use foo::{bar as bar2, hello::{foo}, nested::{foo, bar}}",
"use dep::{std::println, bar::baz}",
"use std::{println, bar::baz}",
];

let invalid_use_statements = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::eddsa::{eddsa_poseidon_verify};
use std::eddsa::{eddsa_poseidon_verify};

fn main(
msg: pub Field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::hash::poseidon;
use std::hash::poseidon;

fn main(input: [Field; 2]) -> pub Field {
poseidon::bn254::hash_2(input)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::hash;
use std::hash;

global SIZE = 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::hash;
use std::hash;

global SIZE = 30;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main(input: [u8; 2]) -> pub [u8; 32] {
std::hash::sha256(input)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

global SIZE = 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

global SIZE = 30;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let x = dep::std::unsafe::zeroed();
let x = std::unsafe::zeroed();
foo(x);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::assert_constant;
use std::assert_constant;

fn main(x: Field) {
foo(5, x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main() {
let var = -1 as u8;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use dep::std::assert_constant;

fn main() -> pub u63 {
5
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Bar<T> {

impl<T> Bar<T> {
fn zeroed<A>(_self: Self) -> A {
dep::std::unsafe::zeroed()
std::unsafe::zeroed()
}
}

Expand All @@ -18,5 +18,5 @@ fn foo<T>(bar: Bar<T>) {
fn main(x: Field, y: pub Field) {
let bar1: Bar<Field> = Bar { one: x, two: y, other: 0 };

assert(bar1.zeroed::<u32, Field>() == 0);
assert(bar1.zeroed::<Field>() == 0);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main() {
//Regression for to_le_bits() constant evaluation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Tests may be checked against https://github.com/cfrg/draft-irtf-cfrg-hash-to-curve/tree/main/poc
use dep::std::ec::tecurve::affine::Curve as AffineCurve;
use dep::std::ec::tecurve::affine::Point as Gaffine;
use dep::std::ec::tecurve::curvegroup::Curve;
use dep::std::ec::tecurve::curvegroup::Point as G;
use std::ec::tecurve::affine::Curve as AffineCurve;
use std::ec::tecurve::affine::Point as Gaffine;
use std::ec::tecurve::curvegroup::Curve;
use std::ec::tecurve::curvegroup::Point as G;

use dep::std::ec::swcurve::affine::Point as SWGaffine;
use dep::std::ec::swcurve::curvegroup::Point as SWG;
use std::ec::swcurve::affine::Point as SWGaffine;
use std::ec::swcurve::curvegroup::Point as SWG;

use dep::std::ec::montcurve::affine::Point as MGaffine;
use dep::std::ec::montcurve::curvegroup::Point as MG;
use std::ec::montcurve::affine::Point as MGaffine;
use std::ec::montcurve::curvegroup::Point as MG;

fn main() {
// This test only makes sense if Field is the right prime field.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
// This test checks that we perform dead-instruction-elimination on intrinsic functions.
fn main(x: Field) {
let hash = std::hash::pedersen_commitment([x]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main() {
// s: Struct<?, ()>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use dep::std::ec::tecurve::affine::Curve as AffineCurve;
use dep::std::ec::tecurve::affine::Point as Gaffine;
use dep::std::ec::tecurve::curvegroup::Curve;
use dep::std::ec::tecurve::curvegroup::Point as G;
use std::ec::tecurve::affine::Curve as AffineCurve;
use std::ec::tecurve::affine::Point as Gaffine;
use std::ec::tecurve::curvegroup::Curve;
use std::ec::tecurve::curvegroup::Point as G;

use dep::std::ec::swcurve::affine::Point as SWGaffine;
use dep::std::ec::swcurve::curvegroup::Point as SWG;
use std::ec::swcurve::affine::Point as SWGaffine;
use std::ec::swcurve::curvegroup::Point as SWG;

use dep::std::ec::montcurve::affine::Point as MGaffine;
use dep::std::ec::montcurve::curvegroup::Point as MG;
use std::ec::montcurve::affine::Point as MGaffine;
use std::ec::montcurve::curvegroup::Point as MG;

fn main() {
// Define Baby Jubjub (ERC-2494) parameters in affine representation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main() {
let x: u8 = 0x61;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
fn main() {
let a = "hello";
let b = a.as_bytes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

trait MyDefault {
fn my_default(x: Field, y: Field) -> Self;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

trait MyDefault {
fn my_default(x: Field, y: Field) -> Self;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

trait MyDefault {
fn my_default(x: Field, y: Field) -> Self;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::std::collections::vec::Vec;
use std::collections::vec::Vec;

fn main(x: Field, y: pub Field) {
let mut vector = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main() {
let a: Field = 3 / 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main(x: Field) {
let a: Field = x / 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
// It is expected that `y` must be equal to 0.
fn main(x: Field, y: pub Field) {
let a: Field = x / y;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::std::collections::map::HashMap;
use dep::std::hash::BuildHasherDefault;
use dep::std::hash::poseidon2::Poseidon2Hasher;
use std::collections::map::HashMap;
use std::hash::BuildHasherDefault;
use std::hash::poseidon2::Poseidon2Hasher;

struct Entry{
key: Field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
// Test unsafe integer subtraction with underflow: 12 - 2418266113 = 1876701195 modulo 2^32
fn main(mut x: u32, y: u32, z: u32) {
x = std::wrapping_sub(x,y);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
// Test unsafe integer arithmetic
// Test odd bits integer
fn main(mut x: u32, y: u32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// If you do not cast, it will take all the bytes from the field element!
// Mimc input is an array of field elements
// The function is called mimc_bn254 to emphasize its parameters are chosen for bn254 curve, it should be used only with a proving system using the same curve (e.g Plonk from Aztec)
use dep::std;

fn main(x: [u8; 5], result: pub [u8; 32]) {
let mut digest = std::hash::sha256(x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
//Basic tests for arrays
fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
let mut c = 2301;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Pre-alpha dependencies must now be prefixed with the word "dep".
// The line below indicates that we would like to pull in the standard library dependency.
use dep::std;

fn main(x: [u8; 5], result: [u8; 32]) {
let digest = std::hash::blake2s(x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

unconstrained fn decode_ascii(ascii: u8) -> u8 {
if ascii < 58 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn compute_root(leaf: [u8; 32], path: [u8; 64], _index: u32, root: [u8; 32]) {
hash_input[j + b] = path[offset + j];
}

current = dep::std::hash::sha256(hash_input);
current = std::hash::sha256(hash_input);
index = index >> 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn main(mut x: [Foo; 3], y: pub Field, hash_result: pub [u8; 32]) {
// Make sure that we are passing a dynamic array to the black box function call
// by setting the array using a dynamic index here
hash_input[y - 1] = 0;
let hash = dep::std::hash::sha256(hash_input);
let hash = std::hash::sha256(hash_input);
assert_eq(hash, hash_result);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::std::bigint;
use dep::std::{bigint::Secpk1Fq, println};
use std::bigint;
use std::{bigint::Secpk1Fq, println};

fn main(mut x: [u8; 5], y: [u8; 5]) {
let a = bigint::Secpk1Fq::from_le_bytes(&[x[0], x[1], x[2], x[3], x[4]]);
Expand All @@ -10,16 +10,16 @@ fn main(mut x: [u8; 5], y: [u8; 5]) {
a_be_bytes[31-i] = x[i];
b_be_bytes[31-i] = y[i];
}
let a_field = dep::std::field::bytes32_to_field(a_be_bytes);
let b_field = dep::std::field::bytes32_to_field(b_be_bytes);
let a_field = std::field::bytes32_to_field(a_be_bytes);
let b_field = std::field::bytes32_to_field(b_be_bytes);

// Regression for issue #4682
let c = if x[0] != 0 {
test_unconstrained1(a, b)
} else {
test_unconstrained2(a, b)
};
assert(c.array[0] == dep::std::wrapping_mul(x[0], y[0]));
assert(c.array[0] == std::wrapping_mul(x[0], y[0]));

let a_bytes = a.to_le_bytes();
let b_bytes = b.to_le_bytes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

fn main(x: [u8; 5], result: [u8; 32]) {
let digest = std::hash::blake3(x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;
// Tests a very simple program.
//
// The features being tested is blake2s in brillig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dep::std;

unconstrained fn main(x: [u8; 5], result: [u8; 32]) {
let digest = std::hash::blake3(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ struct Outer {
// If we don't take into account block parameter liveness, this function will need 5*500=2500 stack items
unconstrained fn main(conditions: [bool; 5]) -> pub Outer {
let out0 = if conditions[0] {
let mut outer: Outer = dep::std::unsafe::zeroed();
let mut outer: Outer = std::unsafe::zeroed();
outer.middle_a.inner_a.a = 1;
outer
} else {
let mut outer: Outer= dep::std::unsafe::zeroed();
let mut outer: Outer= std::unsafe::zeroed();
outer.middle_f.inner_c.d = 2;
outer
};
Expand Down
Loading

0 comments on commit c5742b6

Please sign in to comment.