Skip to content

Commit

Permalink
Merge imports in the standard library and test programs
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 23, 2024
1 parent f30b438 commit d05702c
Show file tree
Hide file tree
Showing 53 changed files with 130 additions and 173 deletions.
2 changes: 1 addition & 1 deletion noir_stdlib/src/array/check_shuffle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ where
}

mod test {
use super::check_shuffle;
use crate::cmp::Eq;
use super::check_shuffle;

struct CompoundStruct {
a: bool,
Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/array/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cmp::{Eq, Ord};
use crate::convert::From;
use crate::runtime::is_unconstrained;
use crate::{cmp::{Eq, Ord}, convert::From, runtime::is_unconstrained};

mod check_shuffle;
mod quicksort;
Expand Down
3 changes: 1 addition & 2 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};
use crate::cmp::Eq;
use crate::{cmp::Eq, ops::{Add, Div, Mul, Sub}};

global bn254_fq = &[
0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ where
}

mod cmp_tests {
use crate::cmp::{min, max};
use crate::cmp::{max, min};

#[test]
fn sanity_check_min() {
Expand Down
12 changes: 7 additions & 5 deletions noir_stdlib/src/collections/map.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::default::Default;
use crate::hash::{Hash, Hasher, BuildHasher};
use crate::collections::bounded_vec::BoundedVec;
use crate::{
cmp::Eq,
collections::bounded_vec::BoundedVec,
default::Default,
hash::{BuildHasher, Hash, Hasher},
option::Option,
};

// We use load factor alpha_max = 0.75.
// Upon exceeding it, assert will fail in order to inform the user
Expand Down
5 changes: 1 addition & 4 deletions noir_stdlib/src/collections/umap.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::default::Default;
use crate::hash::{Hash, Hasher, BuildHasher};
use crate::{cmp::Eq, default::Default, hash::{BuildHasher, Hash, Hasher}, option::Option};

// An unconstrained hash table with open addressing and quadratic probing.
// Note that "unconstrained" here means that almost all operations on this
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/ec/consts/te.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::ec::tecurve::affine::Curve as TECurve;
use crate::ec::tecurve::affine::{Curve as TECurve, Point as TEPoint};

pub struct BabyJubjub {
pub curve: TECurve,
Expand Down
36 changes: 20 additions & 16 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ pub mod affine {
// Points are represented by two-dimensional Cartesian coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
// See e.g. <https://eprint.iacr.org/2017/212.pdf> for details on the correspondences.
use crate::ec::montcurve::curvegroup;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;
use crate::ec::tecurve::affine::Curve as TECurve;
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::ec::is_square;
use crate::ec::safe_inverse;
use crate::ec::sqrt;
use crate::ec::ZETA;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
is_square,
montcurve::curvegroup,
safe_inverse,
sqrt,
swcurve::affine::{Curve as SWCurve, Point as SWPoint},
tecurve::affine::{Curve as TECurve, Point as TEPoint},
ZETA,
},
};

// Curve specification
pub struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x)
Expand Down Expand Up @@ -222,12 +224,14 @@ pub mod curvegroup {
// Points are represented by three-dimensional projective (homogeneous) coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
// See e.g. <https://eprint.iacr.org/2017/212.pdf> for details on the correspondences.
use crate::ec::montcurve::affine;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::ec::tecurve::curvegroup::Curve as TECurve;
use crate::ec::tecurve::curvegroup::Point as TEPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::affine,
swcurve::curvegroup::{Curve as SWCurve, Point as SWPoint},
tecurve::curvegroup::{Curve as TECurve, Point as TEPoint},
},
};

pub struct Curve { // Montgomery Curve configuration (ky^2 z = x*(x^2 + j*x*z + z*z))
pub j: Field,
Expand Down
9 changes: 2 additions & 7 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ pub mod affine {
// Points are represented by two-dimensional Cartesian coordinates.
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
// for reasons of efficiency, cf. <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates>.
use crate::ec::swcurve::curvegroup;
use crate::ec::safe_inverse;
use crate::ec::is_square;
use crate::ec::sqrt;
use crate::cmp::Eq;
use crate::{cmp::Eq, ec::{is_square, safe_inverse, sqrt, swcurve::curvegroup}};

// Curve specification
pub struct Curve { // Short Weierstrass curve
Expand Down Expand Up @@ -190,8 +186,7 @@ pub mod curvegroup {
// CurveGroup representation of Weierstrass curves
// Points are represented by three-dimensional Jacobian coordinates.
// See <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates> for details.
use crate::ec::swcurve::affine;
use crate::cmp::Eq;
use crate::{cmp::Eq, ec::swcurve::affine};

// Curve specification
pub struct Curve { // Short Weierstrass curve
Expand Down
28 changes: 16 additions & 12 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pub mod affine {
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
// for reasons of efficiency.
// See <https://eprint.iacr.org/2008/522.pdf> for details.
use crate::ec::tecurve::curvegroup;
use crate::ec::montcurve::affine::Curve as MCurve;
use crate::ec::montcurve::affine::Point as MPoint;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::affine::{Curve as MCurve, Point as MPoint},
swcurve::affine::{Curve as SWCurve, Point as SWPoint},
tecurve::curvegroup,
},
};

// Curve specification
pub struct Curve { // Twisted Edwards curve
Expand Down Expand Up @@ -197,12 +199,14 @@ pub mod curvegroup {
// CurveGroup coordinate representation of Twisted Edwards curves
// Points are represented by four-dimensional projective coordinates, viz. extended Twisted Edwards coordinates.
// See section 3 of <https://eprint.iacr.org/2008/522.pdf> for details.
use crate::ec::tecurve::affine;
use crate::ec::montcurve::curvegroup::Curve as MCurve;
use crate::ec::montcurve::curvegroup::Point as MPoint;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::curvegroup::{Curve as MCurve, Point as MPoint},
swcurve::curvegroup::{Curve as SWCurve, Point as SWPoint},
tecurve::affine,
},
};

// Curve specification
pub struct Curve { // Twisted Edwards curve
Expand Down
10 changes: 5 additions & 5 deletions noir_stdlib/src/eddsa.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ec::consts::te::baby_jubjub;
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::hash::Hasher;
use crate::hash::poseidon::PoseidonHasher;
use crate::default::Default;
use crate::{
default::Default,
ec::{consts::te::baby_jubjub, tecurve::affine::Point as TEPoint},
hash::{Hasher, poseidon::PoseidonHasher},
};

// Returns true if signature is valid
pub fn eddsa_poseidon_verify(
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/embedded_curve_ops.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ops::arith::{Add, Sub, Neg};
use crate::cmp::Eq;
use crate::{cmp::Eq, ops::arith::{Add, Neg, Sub}};

/// A point on the embedded elliptic curve
/// By definition, the base field of the embedded curve is the scalar field of the proof system curve, i.e the Noir Field.
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn lt(a: Field, b: Field) -> bool {
mod tests {
// TODO: Allow imports from "super"
use crate::field::bn254::{
decompose, compute_lt, assert_gt, gt, TWO_POW_128, compute_lte, PLO, PHI,
assert_gt, compute_lt, compute_lte, decompose, gt, PHI, PLO, TWO_POW_128,
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod bn254;
use bn254::lt as bn254_lt;
use crate::runtime::is_unconstrained;
use bn254::lt as bn254_lt;

impl Field {
/// Asserts that `self` can be represented in `bit_size` bits.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/keccak.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::collections::vec::Vec;
use crate::runtime::is_unconstrained;
use crate::{collections::vec::Vec, runtime::is_unconstrained};

global BLOCK_SIZE_IN_BYTES: u32 = 136; //(1600 - BITS * 2) / WORD_SIZE;
global WORD_SIZE: u32 = 8; // Limbs are made up of u64s so 8 bytes each.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/mimc.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

// mimc-p/p implementation
// constants are (publicly generated) random numbers, for instance using keccak as a ROM.
Expand Down
12 changes: 7 additions & 5 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod keccak;
pub mod sha256;
pub mod sha512;

use crate::default::Default;
use crate::uint128::U128;
use crate::embedded_curve_ops::{
EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,
use crate::{
default::Default,
embedded_curve_ops::{
EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,
},
meta::derive_via,
uint128::U128,
};
use crate::meta::derive_via;

// Kept for backwards compatibility
pub use sha256::{digest, sha256, sha256_compression, sha256_var};
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/bn254/consts.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Used like so: sage generate_parameters_grain.sage 1 0 254 2 8 56 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// Constants for various Poseidon instances in the case of the prime field of the same order as BN254.
// Consistent with https://github.com/iden3/circomlib/blob/master/circuits/poseidon.circom and https://github.com/iden3/circomlib/blob/master/circuits/poseidon_constants.circom
use crate::hash::poseidon::PoseidonConfig;
use crate::hash::poseidon::config;
use crate::hash::poseidon::{config, PoseidonConfig};
// S-box power
fn alpha() -> Field {
5
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/bn254/perm.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Instantiations of Poseidon permutation for the prime field of the same order as BN254
use crate::hash::poseidon::bn254::consts;
use crate::hash::poseidon::permute;
use crate::hash::poseidon::{bn254::consts, permute};

#[field(bn254)]
pub fn x5_2(mut state: [Field; 2]) -> [Field; 2] {
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod bn254; // Instantiations of Poseidon for prime field of the same order as BN254
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

// A config struct defining the parameters of the Poseidon instance to use.
//
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon2.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

comptime global RATE: u32 = 3;

Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/meta/expr.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Contains methods on the built-in `Expr` type for quoted, syntactically valid expressions.

use crate::option::Option;
use crate::meta::op::UnaryOp;
use crate::meta::op::BinaryOp;
use crate::{meta::op::{BinaryOp, UnaryOp}, option::Option};

impl Expr {
/// If this expression is an array literal `[elem1, ..., elemN]`, this returns a slice of each element in the array.
Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/meta/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub comptime fn type_of<T>(x: T) -> Type {}

// docs:start:derive_example
// These are needed for the unconstrained hashmap we're using to store derive functions
use crate::collections::umap::UHashMap;
use crate::hash::BuildHasherDefault;
use crate::hash::poseidon2::Poseidon2Hasher;
use crate::{collections::umap::UHashMap, hash::{BuildHasherDefault, poseidon2::Poseidon2Hasher}};

// A derive function is one that given a struct definition can
// create us a quoted trait impl from it.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/quoted.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::{cmp::Eq, option::Option};

impl Quoted {
#[builtin(quoted_as_expr)]
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/trait_constraint.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::Eq;
use crate::{cmp::Eq, hash::{Hash, Hasher}};

impl Eq for TraitConstraint {
comptime fn eq(self, other: Self) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/trait_def.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::Eq;
use crate::{cmp::Eq, hash::{Hash, Hasher}};

impl TraitDefinition {
#[builtin(trait_def_as_trait_constraint)]
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/typ.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Contains methods on the built-in `Type` type used for representing a type in the source program.

use crate::cmp::Eq;
use crate::option::Option;
use crate::{cmp::Eq, option::Option};

/// Creates and returns an unbound type variable. This is a special kind of type internal
/// to type checking which will type check with any other type. When it is type checked
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/ops/mod.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod arith;
pub(crate) mod bit;

pub use arith::{Add, Sub, Mul, Div, Rem, Neg};
pub use bit::{Not, BitOr, BitAnd, BitXor, Shl, Shr};
pub use arith::{Add, Div, Mul, Neg, Rem, Sub};
pub use bit::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
4 changes: 1 addition & 3 deletions noir_stdlib/src/option.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::{Ordering, Ord, Eq};
use crate::default::Default;
use crate::{cmp::{Eq, Ord, Ordering}, default::Default, hash::{Hash, Hasher}};

pub struct Option<T> {
_is_some: bool,
Expand Down
23 changes: 13 additions & 10 deletions noir_stdlib/src/prelude.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub use crate::collections::vec::Vec;
pub use crate::collections::bounded_vec::BoundedVec;
pub use crate::option::Option;
pub use crate::{print, println, assert_constant};
pub use crate::uint128::U128;
pub use crate::cmp::{Eq, Ord};
pub use crate::default::Default;
pub use crate::convert::{From, Into};
pub use crate::meta::{derive, derive_via};
pub use crate::panic::panic;
pub use crate::{
assert_constant,
cmp::{Eq, Ord},
collections::{bounded_vec::BoundedVec, vec::Vec},
convert::{From, Into},
default::Default,
meta::{derive, derive_via},
option::Option,
panic::panic,
print,
println,
uint128::U128,
};
3 changes: 1 addition & 2 deletions noir_stdlib/src/string.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::collections::vec::Vec;
use crate::convert::From;
use crate::{collections::vec::Vec, convert::From};

impl<let N: u32> str<N> {
/// Converts the given string into a byte array
Expand Down
Loading

0 comments on commit d05702c

Please sign in to comment.