Skip to content

Commit

Permalink
chore(noir): constrain expr; -> assert(expr); (#1276)
Browse files Browse the repository at this point in the history
* chore(noir): constrain expr; -> assert(expr);

* chore(noir): replace remaining `constrain` with `assert(expr)`

---------

Co-authored-by: Tom French <[email protected]>
  • Loading branch information
joss-aztec and TomAFrench authored May 2, 2023
1 parent 752d2f9 commit 562c185
Show file tree
Hide file tree
Showing 67 changed files with 362 additions and 347 deletions.
2 changes: 1 addition & 1 deletion crates/nargo_cli/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ compiler_version = "{CARGO_PKG_VERSION}"
);

const EXAMPLE: &str = r#"fn main(x : Field, y : pub Field) {
constrain x != y;
assert(x != y);
}
#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/compile_tests_data/pass/basic.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

fn main(x : Field, y : Field) {
constrain x != y;
assert(x != y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn main(x : Field, y : Field) {
let _k = std::hash::pedersen([x]);
let _l = hello(x);

constrain x != import::hello(y);
assert(x != import::hello(y));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

fn main(x : Field, y : Field) {
constrain x != y;
assert(x != y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ fn main(x : Field, y : Field) {
let _k = dep::std::hash::pedersen([x]);
let _l = hello(x);

constrain x != import::hello(y);
assert(x != import::hello(y));
}
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/1_mul/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ fn main(mut x: u32, y: u32, z: u32) {
x *= x; //144
x *= x; //20736
x *= x; //429 981 696
constrain x == z;
assert(x == z);
}
4 changes: 2 additions & 2 deletions crates/nargo_cli/tests/test_data/2_div/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Testing integer division: 7/3 = 2
fn main(mut x: u32, y: u32, z: u32) {
let a = x % y;
constrain x / y == z;
constrain a == x - z*y;
assert(x / y == z);
assert(a == x - z*y);
}
4 changes: 2 additions & 2 deletions crates/nargo_cli/tests/test_data/3_add/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Test integer addition: 3 + 4 = 7
fn main(mut x: u32, y: u32, z: u32) {
x += y;
constrain x == z;
assert(x == z);

x *= 8;
constrain x>9;
assert(x>9);
}
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/4_sub/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Test unsafe integer subtraction with underflow: 12 - 2418266113 = 1876701195 modulo 2^32
fn main(mut x: u32, y: u32, z: u32) {
x -= y;
constrain x == z;
assert(x == z);
}
4 changes: 2 additions & 2 deletions crates/nargo_cli/tests/test_data/5_over/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Test odd bits integer
fn main(mut x: u32, y: u32) {
x = x * x;
constrain y == x;
assert(y == x);

let c:u3 = 2;
constrain c > x as u3;
assert(c > x as u3);
}
4 changes: 2 additions & 2 deletions crates/nargo_cli/tests/test_data/6/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fn main(x: [u8; 5], result: pub [u8; 32]) {
let mut digest = std::hash::sha256(x);
digest[0] = 5 as u8;
digest = std::hash::sha256(x);
constrain digest == result;
assert(digest == result);

let y = [12,45,78,41];
let h = std::hash::mimc_bn254(y);
constrain h == 18226366069841799622585958305961373004333097209608110160936134895615261821931;
assert(h == 18226366069841799622585958305961373004333097209608110160936134895615261821931);
}
24 changes: 12 additions & 12 deletions crates/nargo_cli/tests/test_data/6_array/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
c = z*z*y[i];
z -= c;
}
constrain (z==0); //y[4]=0, so c and z are always 0
assert(z==0); //y[4]=0, so c and z are always 0

//Test 2:
c = 2301 as u32;
Expand All @@ -17,7 +17,7 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
c = z*z*x[i];
z += x[i]*y[i] - c;
}
constrain (z==3814912846);
assert(z==3814912846);

//Test 3:
c = 2300001 as u32;
Expand All @@ -29,7 +29,7 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
z *= c;
}
}
constrain (z==41472);
assert(z==41472);

//Test 4:
z = y[4];
Expand All @@ -39,16 +39,16 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
z += x[i+j] - y[i+j];
}
}
constrain (z ==11539);
assert(z ==11539);

//Test 5:
let cc = if z < 1 { x } else { y };
constrain cc[0] == y[0];
assert(cc[0] == y[0]);

// Test 6: for-each loops
for y_elem in y {
for x_elem in x {
constrain x_elem != y_elem;
assert(x_elem != y_elem);
}
}

Expand All @@ -57,15 +57,15 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
}

// fn dyn_array(mut x: [u32; 5], y: Field, z: Field) {
// constrain x[y] == 111;
// constrain x[z] == 101;
// assert(x[y] == 111);
// assert(x[z] == 101);
// x[z] = 0;
// constrain x[y] == 111;
// constrain x[1] == 0;
// assert(x[y] == 111);
// assert(x[1] == 0);
// if y as u32 < 10 {
// x[y] = x[y] - 2;
// } else {
// x[y] = 0;
// }
// constrain x[4] == 109;
// }
// assert(x[4] == 109);
// }
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/7/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ use dep::std;

fn main(x: [u8; 5], result: [u8; 32]) {
let digest = std::hash::blake2s(x);
constrain digest == result;
assert(digest == result);
}
32 changes: 16 additions & 16 deletions crates/nargo_cli/tests/test_data/7_function/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ fn f2(mut x: Field) -> Field{
// Simple example
fn test0(mut a: Field) {
a = f2(a);
constrain a == 3;
assert(a == 3);
}

// Nested call
fn test1(mut a: Field) {
a = f1(a);
constrain a == 4;
assert(a == 4);
}

fn test2(z: Field, t: u32 ) {
let a = z + t as Field;
constrain a == 64;
assert(a == 64);
let e = pow(z, t as Field);
constrain e == 714924299;
assert(e == 714924299);
}

fn pow(base: Field, exponent: Field) -> Field {
Expand All @@ -46,7 +46,7 @@ fn test3(x: [u8; 3]) -> [u8; 3] {
for i in 0..3 {
buffer[i] = x[i];
}
constrain buffer == x;
assert(buffer == x);
buffer
}

Expand All @@ -59,7 +59,7 @@ fn test_multiple2() -> my_struct {
}

fn test_multiple3(x: u32, y: u32) {
constrain x == y;
assert(x == y);
}

struct my_struct {
Expand All @@ -73,18 +73,18 @@ struct my2 {
}

fn test_multiple4(s: my_struct) {
constrain s.a == s.b+2;
assert(s.a == s.b+2);
}

fn test_multiple5(a: (u32, u32)) {
constrain a.0 == a.1+2;
assert(a.0 == a.1+2);
}


fn test_multiple6(a: my2, b: my_struct, c: (my2, my_struct)) {
test_multiple4(a.aa);
test_multiple5((b.a, b.b));
constrain c.0.aa.a == c.1.a;
assert(c.0.aa.a == c.1.a);
}


Expand All @@ -110,28 +110,28 @@ fn main(x: u32 , y: u32 , a: Field, arr1: [u32; 9], arr2: [u32; 9]) {
ab = ab + a;
(x,ab)
};
constrain my_block.1 == 4;
assert(my_block.1 == 4);

test0(a);
test1(a);
test2(x as Field, y);
constrain bar()[0] == 0;
assert(bar()[0] == 0);

let mut b = [0 as u8, 5 as u8, 2 as u8];
let c = test3(b);
constrain b == c;
assert(b == c);
b[0] = 1 as u8;
let cc = test3(b);
constrain c != cc;
assert(c != cc);
let e = test_multiple(x, y);
constrain e.1 == e.0 + 54 as u32;
assert(e.1 == e.0 + 54 as u32);
let d = test_multiple2();
constrain d.b == d.a + 2 as u32;
assert(d.b == d.a + 2 as u32);
test_multiple3(y, y);

//Regression test for issue #628:
let result = first(arr_to_field(arr1), arr_to_field(arr2));
constrain result[0] == arr1[0] as Field;
assert(result[0] == arr1[0] as Field);
}


Expand Down
24 changes: 12 additions & 12 deletions crates/nargo_cli/tests/test_data/8_integration/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn iterate1(mut a0: u32) -> u32{
}

fn array_noteq(a: [u32; 4], b: [u32; 4]) {
constrain a != b;
assert(a != b);
}

fn test3(mut b: [Field; 4]) -> [Field; 4] {
Expand Down Expand Up @@ -105,7 +105,7 @@ fn iterate3( mut hash: [u32; 8]) -> [u32; 8] {
g = f;
a = t1+t2;
}
constrain a == 2470696267;
assert(a == 2470696267);
hash[0] = hash[0] + a;
hash[1] = hash[1] + b;
hash[2] = hash[2] + c;
Expand All @@ -126,7 +126,7 @@ fn test5() {

sha_hash = iterate2(sha_hash);

constrain sha_hash[0] == 9;
assert(sha_hash[0] == 9);
}


Expand Down Expand Up @@ -244,31 +244,31 @@ fn sig1(x: u32) -> u32 {

fn main(a: [u32; 100], b: [u32; 100], c: [u32; 4], mut d: [u32; 4], m: [u8; 32]) {
let e = matrix_mul_10(a,b);
constrain e[6] == 1866842232;
assert(e[6] == 1866842232);
let f = matrix_mul_2(c,d);
constrain f[3] == 2082554100;
assert(f[3] == 2082554100);

let mut a = [1 as u32, 2, 3, 4];
a = test4(a);
constrain a[3] == 20;
assert(a[3] == 20);
a = test4(c);
constrain a[3] == c[1] * 10;
assert(a[3] == c[1] * 10);

d[0] += c[0];
d[0] += c[1];
constrain d[0] == 2739986880;
assert(d[0] == 2739986880);

let h = iterate1(1);
constrain h == 4;
assert(h == 4);

let x = d;
array_noteq(x, [d[0], d[1], d[2], 0]);

let mut h5 = [d[0] as Field, d[1] as Field, d[2] as Field, d[3] as Field];
let t5 = test3(h5);
constrain t5[3] == 3;
assert(t5[3] == 3);
h5 = test3(h5);
constrain h5[3] == 3;
assert(h5[3] == 3);

test5();

Expand All @@ -279,5 +279,5 @@ fn main(a: [u32; 100], b: [u32; 100], c: [u32; 4], mut d: [u32; 4], m: [u8; 32])
sha_hash = iterate3(sha_hash);

let h6 = test6(m);
constrain h6[0]== 523008072; //31.. 3800709683;
assert(h6[0]== 523008072); //31.. 3800709683
}
Loading

0 comments on commit 562c185

Please sign in to comment.