Skip to content

Commit

Permalink
chore: format stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 26, 2024
1 parent a9f014b commit 8190761
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 165 deletions.
10 changes: 5 additions & 5 deletions noir_stdlib/src/ec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@ 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 {
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_element::pow_32.
pub fn pow(x: Field, y: Field) -> Field {
pub fn pow(x: field, y: field) -> field {
// As in tests with minor modifications
let N_BITS = crate::field_element::modulus_num_bits();

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

for i in 0..N_BITS {
Expand All @@ -173,7 +173,7 @@ pub fn pow(x: Field, y: Field) -> 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.
// 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
24 changes: 12 additions & 12 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod affine {

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,7 +174,7 @@ 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
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 Down Expand Up @@ -237,7 +237,7 @@ mod curvegroup {

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
20 changes: 10 additions & 10 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod affine {

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 @@ -70,7 +70,7 @@ mod affine {

impl Curve {
// Curve constructor
pub fn new(a: Field, b: Field, gen: Point) -> Curve {
pub fn new(a: field, b: field, gen: Point) -> Curve {
// Check curve coefficients
assert(4 * a * a * a + 27 * b * b != 0);

Expand Down Expand Up @@ -139,12 +139,12 @@ mod affine {
}

// 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_group().mul(n, p.into_group()).into_affine()
}

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

for i in 0..N {
Expand All @@ -162,7 +162,7 @@ mod affine {
// Simplified Shallue-van de Woestijne-Ulas map-to-curve method; see <https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-16.html#name-simplified-shallue-van-de-w>.
// First determine non-square z != -1 in Field s.t. g(x) - z irreducible over Field and g(b/(z*a)) is square,
// where g(x) = x^3 + a*x + b. swu_map(c,z,.) then maps a Field element to a point on curve c.
fn swu_map(self, z: Field, u: Field) -> Point {
fn swu_map(self, z: field, u: field) -> Point {
// Check whether curve is admissible
assert(self.a * self.b != 0);

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

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 @@ -254,7 +254,7 @@ mod curvegroup {

impl Curve {
// Curve constructor
pub fn new(a: Field, b: Field, gen: Point) -> Curve {
pub fn new(a: field, b: field, gen: Point) -> Curve {
// Check curve coefficients
assert(4 * a * a * a + 27 * b * b != 0);

Expand Down Expand Up @@ -349,7 +349,7 @@ 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 {
let N_BITS = crate::field_element::modulus_num_bits();

// TODO: temporary workaround until issue 1354 is solved
Expand All @@ -363,7 +363,7 @@ mod curvegroup {
}

// 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 All @@ -379,7 +379,7 @@ mod curvegroup {
}

// Simplified SWU map-to-curve method
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
24 changes: 12 additions & 12 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod affine {

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

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

impl Curve {
// Curve constructor
pub fn new(a: Field, d: Field, gen: Point) -> Curve {
pub fn new(a: field, d: field, gen: Point) -> Curve {
// Check curve coefficients
assert(a * d * (a - d) != 0);

Expand Down Expand Up @@ -137,12 +137,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_group().mul(n, p.into_group()).into_affine()
}

// 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 @@ -182,12 +182,12 @@ mod affine {
}

// Elligator 2 map-to-curve method (via rational map)
fn elligator2_map(self, u: Field) -> Point {
fn elligator2_map(self, u: field) -> Point {
self.into_montcurve().elligator2_map(u).into_tecurve()
}

// Simplified 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_montcurve().swu_map(z, u).into_tecurve()
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ mod curvegroup {

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

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

impl Curve {
// Curve constructor
pub fn new(a: Field, d: Field, gen: Point) -> Curve {
pub fn new(a: field, d: field, gen: Point) -> Curve {
// Check curve coefficients
assert(a * d * (a - d) != 0);

Expand Down Expand Up @@ -353,7 +353,7 @@ 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 {
let N_BITS = crate::field_element::modulus_num_bits();

// TODO: temporary workaround until issue 1354 is solved
Expand All @@ -367,7 +367,7 @@ mod curvegroup {
}

// 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 @@ -403,12 +403,12 @@ mod curvegroup {
}

// Elligator 2 map-to-curve method (via rational maps)
fn elligator2_map(self, u: Field) -> Point {
fn elligator2_map(self, u: field) -> Point {
self.into_montcurve().elligator2_map(u).into_tecurve()
}

// Simplified 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_montcurve().swu_map(z, u).into_tecurve()
}
}
Expand Down
12 changes: 6 additions & 6 deletions noir_stdlib/src/eddsa.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::ec::tecurve::affine::Point as TEPoint;

// Returns true if signature is valid
pub fn eddsa_poseidon_verify(
pub_key_x: Field,
pub_key_y: Field,
signature_s: Field,
signature_r8_x: Field,
signature_r8_y: Field,
message: Field
pub_key_x: field,
pub_key_y: field,
signature_s: field,
signature_r8_x: field,
signature_r8_y: field,
message: field
) -> bool {
// Verifies by testing:
// S * B8 = R8 + H(R8, A, m) * A8
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/field_element.nr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl field {
self as u1
}

pub fn lt(self, another: Field) -> bool {
pub fn lt(self, another: field) -> bool {
if crate::compat::is_bn254() {
bn254_lt(self, another)
} else {
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn bytes32_to_field(bytes32: [u8; 32]) -> field {
low + high * v
}

fn lt_fallback(x: Field, y: Field) -> bool {
fn lt_fallback(x: field, y: field) -> bool {
let num_bytes = (modulus_num_bits() as u32 + 7) / 8;
let x_bytes = x.to_le_bytes(num_bytes);
let y_bytes = y.to_le_bytes(num_bytes);
Expand Down
Loading

0 comments on commit 8190761

Please sign in to comment.