Skip to content

Commit

Permalink
Merge 0445ec5 into 4dfd7f0
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Apr 20, 2024
2 parents 4dfd7f0 + 0445ec5 commit 7dfeb7b
Show file tree
Hide file tree
Showing 285 changed files with 1,328 additions and 1,317 deletions.
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ impl fmt::Display for Keyword {
Keyword::Dep => write!(f, "dep"),
Keyword::Distinct => write!(f, "distinct"),
Keyword::Else => write!(f, "else"),
Keyword::Field => write!(f, "Field"),
Keyword::Field => write!(f, "field"),
Keyword::Fn => write!(f, "fn"),
Keyword::For => write!(f, "for"),
Keyword::FormatString => write!(f, "fmtstr"),
Expand Down Expand Up @@ -915,7 +915,11 @@ impl Keyword {
"dep" => Keyword::Dep,
"distinct" => Keyword::Distinct,
"else" => Keyword::Else,
// Currently we allow both uppercase and lowercase
// Fields. This will be used as a transition solution
// where we eventually deprecate the uppercase variant.
"Field" => Keyword::Field,
"field" => Keyword::Field,
"fn" => Keyword::Fn,
"for" => Keyword::For,
"fmtstr" => Keyword::FormatString,
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<T, N> [T; N] {
}
}

// helper function used to look up the position of a value in an array of Field
// helper function used to look up the position of a value in an array of field
// Note that function returns 0 if the value is not found
unconstrained fn find_index<N>(a: [u64; N], find: u64) -> u64 {
let mut result = 0;
Expand Down
6 changes: 3 additions & 3 deletions noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Eq {
}
// docs:end:eq-trait

impl Eq for Field { fn eq(self, other: Field) -> bool { self == other } }
impl Eq for field { fn eq(self, other: field) -> bool { self == other } }

impl Eq for u64 { fn eq(self, other: u64) -> bool { self == other } }
impl Eq for u32 { fn eq(self, other: u32) -> bool { self == other } }
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Eq for Ordering {
// Noir doesn't have enums yet so we emulate (Lt | Eq | Gt) with a struct
// that has 3 public functions for constructing the struct.
struct Ordering {
result: Field,
result: field,
}

impl Ordering {
Expand All @@ -105,7 +105,7 @@ trait Ord {
}
// docs:end:ord-trait

// Note: Field deliberately does not implement Ord
// Note: field deliberately does not implement Ord

impl Ord for u64 {
fn cmp(self, other: u64) -> Ordering {
Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ mod bounded_vec_tests {

#[test]
fn empty_equality() {
let mut bounded_vec1: BoundedVec<Field, 3> = BoundedVec::new();
let mut bounded_vec2: BoundedVec<Field, 3> = BoundedVec::new();
let mut bounded_vec1: BoundedVec<field, 3> = BoundedVec::new();
let mut bounded_vec2: BoundedVec<field, 3> = BoundedVec::new();

assert_eq(bounded_vec1, bounded_vec2);
}

#[test]
fn inequality() {
let mut bounded_vec1: BoundedVec<Field, 3> = BoundedVec::new();
let mut bounded_vec2: BoundedVec<Field, 3> = BoundedVec::new();
let mut bounded_vec1: BoundedVec<field, 3> = BoundedVec::new();
let mut bounded_vec2: BoundedVec<field, 3> = BoundedVec::new();
bounded_vec1.push(1);
bounded_vec2.push(2);

Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ impl From<u8> for u32 { fn from(value: u8) -> u32 { value as u32 } }
impl From<u8> for u64 { fn from(value: u8) -> u64 { value as u64 } }
impl From<u32> for u64 { fn from(value: u32) -> u64 { value as u64 } }

impl From<u8> for Field { fn from(value: u8) -> Field { value as Field } }
impl From<u32> for Field { fn from(value: u32) -> Field { value as Field } }
impl From<u64> for Field { fn from(value: u64) -> Field { value as Field } }
impl From<u8> for field { fn from(value: u8) -> field { value as field } }
impl From<u32> for field { fn from(value: u32) -> field { value as field } }
impl From<u64> for field { fn from(value: u64) -> field { value as field } }

// Signed integers

Expand All @@ -48,5 +48,5 @@ impl From<bool> for u64 { fn from(value: bool) -> u64 { value as u64 } }
impl From<bool> for i8 { fn from(value: bool) -> i8 { value as i8 } }
impl From<bool> for i32 { fn from(value: bool) -> i32 { value as i32 } }
impl From<bool> for i64 { fn from(value: bool) -> i64 { value as i64 } }
impl From<bool> for Field { fn from(value: bool) -> Field { value as Field } }
impl From<bool> for field { fn from(value: bool) -> field { value as field } }
// docs:end:from-impls
2 changes: 1 addition & 1 deletion noir_stdlib/src/default.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Default {
}
// docs:end:default-trait

impl Default for Field { fn default() -> Field { 0 } }
impl Default for field { fn default() -> field { 0 } }

impl Default for u8 { fn default() -> u8 { 0 } }
impl Default for u32 { fn default() -> u32 { 0 } }
Expand Down
48 changes: 24 additions & 24 deletions noir_stdlib/src/ec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod consts; // Commonly used curve presets
//
// Curves
// ======
// A curve configuration (Curve) is completely determined by the Field coefficients of its defining
// A curve configuration (Curve) is completely determined by the field coefficients of its defining
// equation (a and b in the case of swcurve, a and d in the case of tecurve, and j and k in
// the case of montcurve) together with a generator (`gen`) in the corresponding coordinate system.
// For example, the Baby Jubjub curve configuration as defined in ERC-2494 may be instantiated as a Twisted
Expand All @@ -74,13 +74,13 @@ mod consts; // Commonly used curve presets
//
// `constrain tecurve::Point::zero().eq(bjj_affine.subtract(bjj_affine.gen, bjj_affine.gen));`
//
// scalar multiplication as the `mul` method, where the scalar is assumed to be a Field* element, e.g.
// scalar multiplication as the `mul` method, where the scalar is assumed to be a field* element, e.g.
//
// `constrain tecurve::Point::zero().eq(bjj_affine.mul(2, tecurve::Point::zero());`
//
// There is a scalar multiplication method (`bit_mul`) provided where the scalar input is expected to be
// an array of bits (little-endian convention), as well as a multi-scalar multiplication method** (`msm`)
// which takes an array of Field elements and an array of elliptic curve points as arguments, both assumed
// which takes an array of field elements and an array of elliptic curve points as arguments, both assumed
// to be of the same length.
//
// Curve configurations may be converted between different coordinate representations by calling the `into_group`
Expand All @@ -96,30 +96,30 @@ mod consts; // Commonly used curve presets
//
// Curve maps
// ==========
// There are a few different ways of mapping Field elements to elliptic curves. Here we provide the simplified
// There are a few different ways of mapping field elements to elliptic curves. Here we provide the simplified
// Shallue-van de Woestijne-Ulas and Elligator 2 methods, the former being applicable to all curve types
// provided above subject to the constraint that the coefficients of the corresponding Short Weierstraß curve satisfies
// a*b != 0 and the latter being applicable to Montgomery and Twisted Edwards curves subject to the constraint that
// the coefficients of the corresponding Montgomery curve satisfy j*k != 0 and (j^2 - 4)/k^2 is non-square.
//
// The simplified Shallue-van de Woestijne-Ulas method is exposed as the method `swu_map` on the Curve configuration and
// depends on two parameters, a Field element z != -1 for which g(x) - z is irreducible over Field and g(b/(z*a)) is
// depends on two parameters, a field element z != -1 for which g(x) - z is irreducible over field and g(b/(z*a)) is
// square, where g(x) = x^3 + a*x + b is the right-hand side of the defining equation of the corresponding Short
// Weierstraß curve, and a Field element u to be mapped onto the curve. For example, in the case of bjj_affine above,
// Weierstraß curve, and a field element u to be mapped onto the curve. For example, in the case of bjj_affine above,
// it may be determined using the scripts provided at <https://github.com/cfrg/draft-irtf-cfrg-hash-to-curve> that z = 5.
//
// The Elligator 2 method is exposed as the method `elligator2_map` on the Curve configurations of Montgomery and
// Twisted Edwards curves. Like the simplified SWU method above, it depends on a certain non-square element of Field,
// but this element need not satisfy any further conditions, so it is included as the (Field-dependent) constant
//`ZETA` below. Thus, the `elligator2_map` method depends only on one parameter, the Field element to be mapped onto
// Twisted Edwards curves. Like the simplified SWU method above, it depends on a certain non-square element of field,
// but this element need not satisfy any further conditions, so it is included as the (field-dependent) constant
//`ZETA` below. Thus, the `elligator2_map` method depends only on one parameter, the field element to be mapped onto
// the curve.
//
// For details on all of the above in the context of hashing to elliptic curves, see <https://datatracker.ietf.org/doc/id/draft-irtf-cfrg-hash-to-curve-06.html>.
//
//
// *TODO: Replace Field with Bigint.
// *TODO: Replace field with Bigint.
// **TODO: Support arrays of structs to make this work.
// Field-dependent constant ZETA = a non-square element of Field
// Field-dependent constant ZETA = a non-square element of field
// Required for Elligator 2 map
// TODO: Replace with built-in constant.
global ZETA = 5;
Expand All @@ -143,37 +143,37 @@ global C5 = 19103219067921713944291392827692070036145651957329286315305642004821
// out
//}
// TODO: Make this built-in.
pub fn safe_inverse(x: Field) -> Field {
pub fn safe_inverse(x: field) -> field {
if x == 0 { 0 } else { 1 / x }
}
// Boolean indicating whether Field element is a square, i.e. whether there exists a y in Field s.t. x = y*y.
pub fn is_square(x: Field) -> bool {
// Boolean indicating whether field element is a square, i.e. whether there exists a y in field s.t. x = y*y.
pub fn is_square(x: field) -> bool {
let v = pow(x, 0 - 1 / 2);

v * (v - 1) == 0
}
// Power function of two Field arguments of arbitrary size.
// Adapted from std::field::pow_32.
pub fn pow(x: Field, y: Field) -> Field {
// Power function of two field arguments of arbitrary size.
// Adapted from std::field_element::pow_32.
pub fn pow(x: field, y: field) -> field {
// As in tests with minor modifications
let N_BITS = crate::field::modulus_num_bits();
let N_BITS = crate::field_element::modulus_num_bits();

let mut r = 1 as Field;
let mut r: field = 1;
let b = y.to_le_bits(N_BITS as u32);

for i in 0..N_BITS {
r *= r;
r *= (b[N_BITS - 1 - i] as Field)*x + (1-b[N_BITS - 1 - i] as Field);
r *= (b[N_BITS - 1 - i] as field)*x + (1-b[N_BITS - 1 - i] as field);
}

r
}
// Tonelli-Shanks algorithm for computing the square root of a Field element.
// Requires C1 = max{c: 2^c divides (p-1)}, where p is the order of Field
// Tonelli-Shanks algorithm for computing the square root of a field element.
// Requires C1 = max{c: 2^c divides (p-1)}, where p is the order of field
// as well as C3 = (C2 - 1)/2, where C2 = (p-1)/(2^c1),
// and C5 = ZETA^C2, where ZETA is a non-square element of Field.
// and C5 = ZETA^C2, where ZETA is a non-square element of field.
// These are pre-computed above as globals.
pub fn sqrt(x: Field) -> Field {
pub fn sqrt(x: field) -> field {
let mut z = pow(x, C3);
let mut t = z * z * x;
z *= x;
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/ec/consts/te.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ec::tecurve::affine::Curve as TECurve;
struct BabyJubjub {
curve: TECurve,
base8: TEPoint,
suborder: Field,
suborder: field,
}

#[field(bn254)]
Expand Down
44 changes: 22 additions & 22 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ mod affine {

// Curve specification
struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x)
j: Field,
k: Field,
j: field,
k: field,
// Generator as point in Cartesian coordinates
gen: Point
}
// Point in Cartesian coordinates
struct Point {
x: Field,
y: Field,
x: field,
y: field,
infty: bool // Indicator for point at infinity
}

impl Point {
// Point constructor
pub fn new(x: Field, y: Field) -> Self {
pub fn new(x: field, y: field) -> Self {
Self { x, y, infty: false }
}

Expand Down Expand Up @@ -81,7 +81,7 @@ mod affine {

impl Curve {
// Curve constructor
pub fn new(j: Field, k: Field, gen: Point) -> Self {
pub fn new(j: field, k: field, gen: Point) -> Self {
// Check curve coefficients
assert(k != 0);
assert(j * j != 4);
Expand Down Expand Up @@ -119,12 +119,12 @@ mod affine {
}

// Scalar multiplication (p + ... + p n times)
fn mul(self, n: Field, p: Point) -> Point {
fn mul(self, n: field, p: Point) -> Point {
self.into_tecurve().mul(n, p.into_tecurve()).into_montcurve()
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<N>(self, n: [field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down Expand Up @@ -174,10 +174,10 @@ mod affine {
}

// Elligator 2 map-to-curve method; see <https://datatracker.ietf.org/doc/id/draft-irtf-cfrg-hash-to-curve-06.html#name-elligator-2-method>.
fn elligator2_map(self, u: Field) -> Point {
fn elligator2_map(self, u: field) -> Point {
let j = self.j;
let k = self.k;
let z = ZETA; // Non-square Field element required for map
let z = ZETA; // Non-square field element required for map

// Check whether curve is admissible
assert(j != 0);
Expand Down Expand Up @@ -205,7 +205,7 @@ mod affine {
}

// SWU map-to-curve method (via rational map)
fn swu_map(self, z: Field, u: Field) -> Point {
fn swu_map(self, z: field, u: field) -> Point {
self.map_from_swcurve(self.into_swcurve().swu_map(z, u))
}
}
Expand All @@ -223,21 +223,21 @@ mod curvegroup {
use crate::cmp::Eq;

struct Curve { // Montgomery Curve configuration (ky^2 z = x*(x^2 + j*x*z + z*z))
j: Field,
k: Field,
j: field,
k: field,
// Generator as point in projective coordinates
gen: Point
}
// Point in projective coordinates
struct Point {
x: Field,
y: Field,
z: Field
x: field,
y: field,
z: field
}

impl Point {
// Point constructor
pub fn new(x: Field, y: Field, z: Field) -> Self {
pub fn new(x: field, y: field, z: field) -> Self {
Self { x, y, z }
}

Expand Down Expand Up @@ -282,7 +282,7 @@ mod curvegroup {

impl Curve {
// Curve constructor
pub fn new(j: Field, k: Field, gen: Point) -> Self {
pub fn new(j: field, k: field, gen: Point) -> Self {
// Check curve coefficients
assert(k != 0);
assert(j * j != 4);
Expand Down Expand Up @@ -320,12 +320,12 @@ mod curvegroup {
}

// Scalar multiplication (p + ... + p n times)
pub fn mul(self, n: Field, p: Point) -> Point {
pub fn mul(self, n: field, p: Point) -> Point {
self.into_tecurve().mul(n, p.into_tecurve()).into_montcurve()
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<N>(self, n: [field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down Expand Up @@ -367,12 +367,12 @@ mod curvegroup {
}

// Elligator 2 map-to-curve method
fn elligator2_map(self, u: Field) -> Point {
fn elligator2_map(self, u: field) -> Point {
self.into_affine().elligator2_map(u).into_group()
}

// SWU map-to-curve method (via rational map)
fn swu_map(self, z: Field, u: Field) -> Point {
fn swu_map(self, z: field, u: field) -> Point {
self.into_affine().swu_map(z, u).into_group()
}
}
Expand Down
Loading

0 comments on commit 7dfeb7b

Please sign in to comment.