Skip to content

Commit

Permalink
chore: format integration tests (#3399)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored Nov 16, 2023
1 parent 5c006ce commit fac19a3
Show file tree
Hide file tree
Showing 205 changed files with 660 additions and 1,042 deletions.
2 changes: 1 addition & 1 deletion compiler/integration-tests/circuits/main/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main(x : Field, y : pub Field) {
fn main(x: Field, y: pub Field) {
assert(x != y);
}
10 changes: 5 additions & 5 deletions compiler/integration-tests/circuits/recursion/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use dep::std;

fn main(
verification_key : [Field; 114],
proof : [Field; 94],
public_inputs : [Field; 1],
key_hash : Field,
input_aggregation_object : [Field; 16],
verification_key: [Field; 114],
proof: [Field; 94],
public_inputs: [Field; 1],
key_hash: Field,
input_aggregation_object: [Field; 16]
) -> pub [Field; 16] {
let vk : [Field] = verification_key;
let p : [Field] = proof;
Expand Down
1 change: 0 additions & 1 deletion compiler/wasm/fixtures/deps/lib-a/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use dep::lib_b::assert_non_zero;

pub fn divide(a: u64, b: u64) -> u64 {
Expand Down
3 changes: 1 addition & 2 deletions compiler/wasm/fixtures/deps/lib-b/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pub fn assert_non_zero(x: u64) {
assert(x != 0);
assert(x != 0);
}
4 changes: 2 additions & 2 deletions compiler/wasm/fixtures/deps/noir-script/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::lib_a::divide;
fn main(x : u64, y : pub u64) {
divide(x, y);
fn main(x: u64, y: pub u64) {
divide(x, y);
}
2 changes: 1 addition & 1 deletion compiler/wasm/fixtures/simple/noir-script/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main(x : u64, y : pub u64) {
fn main(x: u64, y: pub u64) {
assert(x < y);
}
1 change: 0 additions & 1 deletion noir_stdlib/src/collections/vec.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct Vec<T> {
slice: [T]
}

// A mutable vector type implemented as a wrapper around immutable slices.
// A separate type is technically not needed but helps differentiate which operations are mutable.
impl<T> Vec<T> {
Expand Down
46 changes: 16 additions & 30 deletions noir_stdlib/src/ec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,15 @@ mod consts; // Commonly used curve presets
//
// *TODO: Replace Field with Bigint.
// **TODO: Support arrays of structs to make this work.


// Field-dependent constant ZETA = a non-square element of Field
// Required for Elligator 2 map
// TODO: Replace with built-in constant.
global ZETA = 5;

// Field-dependent constants for Tonelli-Shanks algorithm (see sqrt function below)
// TODO: Possibly make this built-in.
global C1 = 28;
global C3 = 40770029410420498293352137776570907027550720424234931066070132305055;
global C5 = 19103219067921713944291392827692070036145651957329286315305642004821462161904;

// Higher-order version of scalar multiplication
// TODO: Make this work so that the submodules' bit_mul may be defined in terms of it.
//fn bit_mul<T,N>(add: fn(T,T) -> T, e: T, bits: [u1; N], p: T) -> T {
Expand All @@ -146,67 +142,57 @@ global C5 = 19103219067921713944291392827692070036145651957329286315305642004821
//
// out
//}

// TODO: Make this built-in.
pub fn safe_inverse(x: Field) -> Field {
if x == 0 {
0
} else {
1/x
}
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 {
let v = pow(x, 0 - 1/2);
let v = pow(x, 0 - 1 / 2);

v*(v-1) == 0
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 { // As in tests with minor modifications
pub fn pow(x: Field, y: Field) -> Field {
// As in tests with minor modifications
let N_BITS = crate::field::modulus_num_bits();

let mut r = 1 as Field;
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
}

// 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.
// These are pre-computed above as globals.
pub fn sqrt(x: Field) -> Field {
let mut z = pow(x, C3);
let mut t = z*z*x;
let mut t = z * z * x;
z *= x;
let mut b = t;
let mut c = C5;

for i in 0..(C1-1) {

for _j in 1..(C1-i-1) {


for i in 0..(C1 - 1) {
for _j in 1..(C1 - i - 1) {
b *= b;

}

z *= if b == 1 { 1 } else { c };

c *= c;

t *= if b == 1 { 1 } else { c };

b = t;
}

z
}
18 changes: 6 additions & 12 deletions noir_stdlib/src/ec/consts/te.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ struct BabyJubjub {
pub fn baby_jubjub() -> BabyJubjub {
BabyJubjub {
// Baby Jubjub (ERC-2494) parameters in affine representation
curve: TECurve::new(
168700,
curve: TECurve::new(168700,
168696,
// G
TEPoint::new(
995203441582195749578291179787384436505546430278305826713579947235728471134,
5472060717959818805561601436314318772137091100104008585924551046643952123905,
),
),
TEPoint::new(995203441582195749578291179787384436505546430278305826713579947235728471134,
5472060717959818805561601436314318772137091100104008585924551046643952123905)),
// [8]G precalculated
base8: TEPoint::new(
5299619240641551281634865583518297030282874472190772894086521144482721001553,
16950150798460657717958625567821834550301663161624707787222815936182638968203,
),
base8: TEPoint::new(5299619240641551281634865583518297030282874472190772894086521144482721001553,
16950150798460657717958625567821834550301663161624707787222815936182638968203),
// The size of the group formed from multiplying the base field by 8.
suborder: 2736030358979909402780800718157159386076813972158567259200215660948447373041,
suborder: 2736030358979909402780800718157159386076813972158567259200215660948447373041
}
}
3 changes: 0 additions & 3 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ mod affine {
use crate::ec::safe_inverse;
use crate::ec::sqrt;
use crate::ec::ZETA;

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

// Point in Cartesian coordinates
struct Point {
x: Field,
Expand Down Expand Up @@ -228,7 +226,6 @@ mod curvegroup {
// Generator as point in projective coordinates
gen: Point
}

// Point in projective coordinates
struct Point {
x: Field,
Expand Down
4 changes: 0 additions & 4 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod affine {
use crate::ec::safe_inverse;
use crate::ec::is_square;
use crate::ec::sqrt;

// Curve specification
struct Curve { // Short Weierstraß curve
// Coefficients in defining equation y^2 = x^3 + ax + b
Expand All @@ -16,7 +15,6 @@ mod affine {
// Generator as point in Cartesian coordinates
gen: Point
}

// Point in Cartesian coordinates
struct Point {
x: Field,
Expand Down Expand Up @@ -184,7 +182,6 @@ mod curvegroup {
// 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;

// Curve specification
struct Curve { // Short Weierstraß curve
// Coefficients in defining equation y^2 = x^3 + axz^4 + bz^6
Expand All @@ -193,7 +190,6 @@ mod curvegroup {
// Generator as point in Cartesian coordinates
gen: Point
}

// Point in three-dimensional Jacobian coordinates
struct Point {
x: Field,
Expand Down
5 changes: 0 additions & 5 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod affine {
use crate::ec::montcurve::affine::Point as MPoint;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;

// Curve specification
struct Curve { // Twisted Edwards curve
// Coefficients in defining equation ax^2 + y^2 = 1 + dx^2y^2
Expand All @@ -18,7 +17,6 @@ mod affine {
// Generator as point in Cartesian coordinates
gen: Point
}

// Point in Cartesian coordinates
struct Point {
x: Field,
Expand Down Expand Up @@ -76,7 +74,6 @@ mod affine {
}
}


impl Curve {
// Curve constructor
pub fn new(a: Field, d: Field, gen: Point) -> Curve {
Expand Down Expand Up @@ -201,7 +198,6 @@ mod curvegroup {
use crate::ec::montcurve::curvegroup::Point as MPoint;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;

// Curve specification
struct Curve { // Twisted Edwards curve
// Coefficients in defining equation a(x^2 + y^2)z^2 = z^4 + dx^2y^2
Expand All @@ -210,7 +206,6 @@ mod curvegroup {
// Generator as point in projective coordinates
gen: Point
}

// Point in extended twisted Edwards coordinates
struct Point {
x: Field,
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/ecdsa_secp256k1.nr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[foreign(ecdsa_secp256k1)]
pub fn verify_signature<N>(_public_key_x : [u8; 32], _public_key_y : [u8; 32], _signature: [u8; 64], _message_hash: [u8; N]) -> bool {}
pub fn verify_signature<N>(_public_key_x: [u8; 32], _public_key_y: [u8; 32], _signature: [u8; 64], _message_hash: [u8; N]) -> bool {}
2 changes: 1 addition & 1 deletion noir_stdlib/src/ecdsa_secp256r1.nr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[foreign(ecdsa_secp256r1)]
pub fn verify_signature<N>(_public_key_x : [u8; 32], _public_key_y : [u8; 32], _signature: [u8; 64], _message_hash: [u8; N]) -> bool {}
pub fn verify_signature<N>(_public_key_x: [u8; 32], _public_key_y: [u8; 32], _signature: [u8; 64], _message_hash: [u8; N]) -> bool {}
11 changes: 1 addition & 10 deletions noir_stdlib/src/eddsa.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::hash::poseidon;
use crate::ec::consts::te::baby_jubjub;
use crate::ec::tecurve::affine::Point as TEPoint;

// Returns true if x is less than y
fn lt_bytes32(x: Field, y: Field) -> bool {
let x_bytes = x.to_le_bytes(32);
Expand All @@ -21,15 +20,14 @@ fn lt_bytes32(x: Field, y: Field) -> bool {
}
x_is_lt
}

// 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,
message: Field
) -> bool {
// Verifies by testing:
// S * B8 = R8 + H(R8, A, m) * A8
Expand All @@ -40,26 +38,19 @@ pub fn eddsa_poseidon_verify(

let signature_r8 = TEPoint::new(signature_r8_x, signature_r8_y);
assert(bjj.curve.contains(signature_r8));

// Ensure S < Subgroup Order
assert(lt_bytes32(signature_s, bjj.suborder));

// Calculate the h = H(R, A, msg)
let hash: Field = poseidon::bn254::hash_5([signature_r8_x, signature_r8_y, pub_key_x, pub_key_y, message]);

// Calculate second part of the right side: right2 = h*8*A

// Multiply by 8 by doubling 3 times. This also ensures that the result is in the subgroup.
let pub_key_mul_2 = bjj.curve.add(pub_key, pub_key);
let pub_key_mul_4 = bjj.curve.add(pub_key_mul_2, pub_key_mul_2);
let pub_key_mul_8 = bjj.curve.add(pub_key_mul_4, pub_key_mul_4);

// We check that A8 is not zero.
assert(!pub_key_mul_8.is_zero());

// Compute the right side: R8 + h * A8
let right = bjj.curve.add(signature_r8, bjj.curve.mul(hash, pub_key_mul_8));

// Calculate left side of equation left = S * B8
let left = bjj.curve.mul(signature_s, bjj.base8);

Expand Down
7 changes: 2 additions & 5 deletions noir_stdlib/src/field.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

impl Field {
pub fn to_le_bits(self: Self, bit_size: u32) -> [u1] {
crate::assert_constant(bit_size);
Expand Down Expand Up @@ -82,9 +81,8 @@ pub fn modulus_be_bytes() -> [u8] {}

#[builtin(modulus_le_bytes)]
pub fn modulus_le_bytes() -> [u8] {}

// Convert a 32 byte array to a field element
pub fn bytes32_to_field(bytes32 : [u8; 32]) -> Field {
pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {
// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
Expand All @@ -95,7 +93,6 @@ pub fn bytes32_to_field(bytes32 : [u8; 32]) -> Field {
low = low + (bytes32[16 + 15 - i] as Field) * v;
v = v * 256;
}

// Abuse that a % p + b % p = (a + b) % p and that low < p
low + high * v
}
}
Loading

0 comments on commit fac19a3

Please sign in to comment.