Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not simplify mod or div #2741

Closed
wants to merge 13 commits into from
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ impl Instruction {
use Instruction::*;

match self {
Binary(_)
| Cast(_, _)
Binary(binary) => matches!(binary.operator, BinaryOp::Div | BinaryOp::Mod),
Cast(_, _)
| Not(_)
| Truncate { .. }
| Allocate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "die_div_by_zero"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use dep::std;

fn main() {
let x: u32 = 1 / 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "die_mod_by_zero"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use dep::std;

fn main() {
let x: u32 = 1 % 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "1327_concrete_in_generic"
type = "bin"
authors = [""]
compiler_version = "0.6.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
input = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ---

fn new_concrete_c_over_d() -> C<D> {
let d_method_interface = get_d_method_interface();
C::new(d_method_interface)
}

// ---

// Map<V>
struct B<T_C> {
new_concrete_t_c_constructor: fn()->T_C,
}

impl<T_C> B<T_C> {
fn new(new_concrete_t_c_constructor: fn () -> T_C) -> B<T_C> {
B { new_concrete_t_c_constructor }
}

fn get_t_c(self) -> T_C {
let new_concrete_t_c_constructor = self.new_concrete_t_c_constructor;
new_concrete_t_c_constructor()
}
}

// ---

// Set<Note>
struct C<T_D> {
t_d_interface: MethodInterface<T_D>,
}

impl<T_D> C<T_D> {
fn new (t_d_interface: MethodInterface<T_D>) -> Self {
C { t_d_interface }
}

fn call_method_of_t_d(self, t_d: T_D) -> Field {
let some_method_on_t_d = self.t_d_interface.some_method_on_t_d;
some_method_on_t_d(t_d)
}
}

// ---

struct MethodInterface<T_D> {
some_method_on_t_d: fn(T_D)->Field,
}

// ---

// Note
struct D {
d: Field,
}

fn d_method(input: D) -> Field {
input.d * input.d
}

fn get_d_method_interface() -> MethodInterface<D> {
MethodInterface {
some_method_on_t_d: d_method,
}
}

// ---

fn main(input: Field) -> pub Field {
let b: B<C<D>> = B::new(new_concrete_c_over_d);
let c: C<D> = b.get_t_c(); // Singleton<Note>
let d: D = D { d: input }; // Note
let output = c.call_method_of_t_d(d);

output
}

// ---
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "1_mul"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = "3"
y = "4"
z = "429981696"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Test unsafe integer multiplication with overflow: 12^8 = 429 981 696
// The circuit should handle properly the growth of the bit size
fn main(mut x: u32, y: u32, z: u32) {
x *= y;
x *= x; //144
x *= x; //20736
x *= x; //429 981 696
assert(x == z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "2_div"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Testing integer division: 7/3 = 2
fn main(mut x: u32, y: u32, z: u32) {
let a = x % y;
assert(x / y == z);
assert(a == x - z*y);
assert((50 as u64) % (9 as u64) == 5);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "3_add"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = "3"
y = "4"
z = "7"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Test integer addition: 3 + 4 = 7
fn main(mut x: u32, y: u32, z: u32) {
x += y;
assert(x == z);

x *= 8;
assert(x>9);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "4_sub"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = "12"
y = "2418266113"
z = "1876701195"
10 changes: 10 additions & 0 deletions tooling/nargo_cli/tests/execution_success_backup/4_sub/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use dep::std;
// Test unsafe integer subtraction with underflow: 12 - 2418266113 = 1876701195 modulo 2^32
fn main(mut x: u32, y: u32, z: u32) {
x = std::wrapping_sub(x,y);
assert(x == z);

// Test constant underflow (regression for #2045)
let x = -1 as u4;
assert(x == 15);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "5_over"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "43046721"
y = "3793632897"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use dep::std;

// Test unsafe integer arithmetic
// Test odd bits integer
fn main(mut x: u32, y: u32) {
x = std::wrapping_mul(x,x);
assert(y == x);

let c:u3 = 2;
assert(c > x as u3);
}
7 changes: 7 additions & 0 deletions tooling/nargo_cli/tests/execution_success_backup/6/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "6"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
39 changes: 39 additions & 0 deletions tooling/nargo_cli/tests/execution_success_backup/6/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

# hello as bytes
# used : https://emn178.github.io/online-tools/sha256.html
x = [104, 101, 108, 108, 111]

result = [
0x2c,
0xf2,
0x4d,
0xba,
0x5f,
0xb0,
0xa3,
0x0e,
0x26,
0xe8,
0x3b,
0x2a,
0xc5,
0xb9,
0xe2,
0x9e,
0x1b,
0x16,
0x1e,
0x5c,
0x1f,
0xa7,
0x42,
0x5e,
0x73,
0x04,
0x33,
0x62,
0x93,
0x8b,
0x98,
0x24,
]
20 changes: 20 additions & 0 deletions tooling/nargo_cli/tests/execution_success_backup/6/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Sha256 circuit where the input is 5 bytes
// not five field elements since sha256 operates over
// bytes.
//
// If you do not cast, it will take all the bytes from the field element!

// Mimc input is an array of field elements
// The function is called mimc_bn254 to emphasize its parameters are chosen for bn254 curve, it should be used only with a proving system using the same curve (e.g Plonk from Aztec)
use dep::std;

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);
assert(digest == result);

let y = [12,45,78,41];
let h = std::hash::mimc_bn254(y);
assert(h == 18226366069841799622585958305961373004333097209608110160936134895615261821931);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "6_array"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
x = [104, 101, 108, 108, 111]
y = [10, 81, 18, 48, 0]
z = "59"
t = "10"

#7128
#15309
#16349
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use dep::std;
//Basic tests for arrays
fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
let mut c = 2301;
z = y[4];
//Test 1:
for i in 0..5 {
c = z*z*y[i];
z -= c;
}
assert(z == 0); //y[4]=0, so c and z are always 0

//Test 2:
c = 2301 as u32;
for i in 0..5 {
c = t+2 as u32;
c = std::wrapping_mul(std::wrapping_mul(z,z),x[i]);
z =std::wrapping_add(z, std::wrapping_sub(x[i]*y[i] , c));
}
assert(z == 3814912846);

//Test 3:
c = 2300001 as u32;
z = y[4];
for i in 0..5 {
z = z + x[i]*y[i];
for _i in 0..3 {
c = i as u32 - 2 as u32;
z = std::wrapping_mul(z,c);
}
}
assert(z == 41472);

//Test 4:
z = y[4];
for i in 0..3 {
z += x[i] * y[i];
for j in 0..2 {
z += x[i+j] - y[i+j];
}
}
assert(z == 11539);

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

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

// Test 7: Arrays of tuples/structs
let mut tuple_array = [(1, 2), (3, 4), (5, 6)];
tuple_array[1] = (7, 8);
assert(tuple_array[1].1 == 8);
}
7 changes: 7 additions & 0 deletions tooling/nargo_cli/tests/execution_success_backup/7/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "7"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Loading
Loading