Skip to content

Commit

Permalink
chore: add unsafe blocks to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 27, 2024
1 parent 5c9b841 commit d809292
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ unconstrained fn checks_in_brillig() {
}

fn main() {
checks();
checks_in_brillig();
unsafe {
checks();
checks_in_brillig();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
fn main(x: u32) {
assert(entry_point(x) == 2);
swap_entry_point(x, x + 1);
assert(deep_entry_point(x) == 4);
unsafe {
assert(entry_point(x) == 2);
swap_entry_point(x, x + 1);
assert(deep_entry_point(x) == 4);
}
}

fn inner(x: u32) -> u32 {
Expand Down
6 changes: 4 additions & 2 deletions test_programs/execution_success/brillig_arrays/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//
// The features being tested are array reads and writes
fn main(x: [Field; 3]) {
read_array(x);
read_write_array(x);
unsafe {
read_array(x);
read_write_array(x);
}
}

unconstrained fn read_array(x: [Field; 3]) {
Expand Down
4 changes: 3 additions & 1 deletion test_programs/execution_success/brillig_blake2s/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use dep::std;
//
// The features being tested is blake2s in brillig
fn main(x: [u8; 5], result: [u8; 32]) {
assert(blake2s(x) == result);
unsafe {
assert(blake2s(x) == result);
}
}

unconstrained fn blake2s(x: [u8; 5]) -> [u8; 32] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//
// The features being tested is brillig calls passing arrays around
fn main(x: [u32; 3]) {
assert(entry_point(x) == 9);
another_entry_point(x);
unsafe {
assert(entry_point(x) == 9);
another_entry_point(x);
}
}

unconstrained fn inner(x: [u32; 3]) -> [u32; 3] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
//
// The features being tested is brillig calls with conditionals
fn main(x: [u32; 3]) {
assert(entry_point(x[0]) == 7);
assert(entry_point(x[1]) == 8);
assert(entry_point(x[2]) == 9);
assert(entry_point(42) == 0);
unsafe {
assert(entry_point(x[0]) == 7);
assert(entry_point(x[1]) == 8);
assert(entry_point(x[2]) == 9);
assert(entry_point(42) == 0);
}
}

unconstrained fn inner_1() -> u32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//
// The features being tested is basic conditonal on brillig
fn main(x: Field) {
assert(4 == conditional(x == 1));
unsafe {
assert(4 == conditional(x == 1));
}
}

unconstrained fn conditional(x: bool) -> Field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use dep::std;
//
// The features being tested is ecdsa in brillig
fn main(hashed_message: [u8; 32], pub_key_x: [u8; 32], pub_key_y: [u8; 32], signature: [u8; 64]) {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
unsafe {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
}
}

unconstrained fn ecdsa(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use dep::std;
//
// The features being tested is ecdsa in brillig
fn main(hashed_message: [u8; 32], pub_key_x: [u8; 32], pub_key_y: [u8; 32], signature: [u8; 64]) {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
unsafe {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
}
}

unconstrained fn ecdsa(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ struct myStruct {
//
// The features being tested is the identity function in Brillig
fn main(x: Field) {
assert(x == identity(x));
// TODO: add support for array comparison
let arr = identity_array([x, x]);
assert(x == arr[0]);
assert(x == arr[1]);
unsafe {
assert(x == identity(x));
// TODO: add support for array comparison
let arr = identity_array([x, x]);
assert(x == arr[0]);
assert(x == arr[1]);

let s = myStruct { foo: x, foo_arr: [x, x] };
let identity_struct = identity_struct(s);
assert(x == identity_struct.foo);
assert(x == identity_struct.foo_arr[0]);
assert(x == identity_struct.foo_arr[1]);
let s = myStruct { foo: x, foo_arr: [x, x] };
let identity_struct = identity_struct(s);
assert(x == identity_struct.foo);
assert(x == identity_struct.foo_arr[0]);
assert(x == identity_struct.foo_arr[1]);
}
}

unconstrained fn identity(x: Field) -> Field {
Expand Down
26 changes: 14 additions & 12 deletions test_programs/execution_success/brillig_keccak/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ use dep::std;
//
// The features being tested is keccak256 in brillig
fn main(x: Field, result: [u8; 32]) {
// We use the `as` keyword here to denote the fact that we want to take just the first byte from the x Field
// The padding is taken care of by the program
let digest = keccak256([x as u8], 1);
assert(digest == result);
//#1399: variable meesage size
let message_size = 4;
let hash_a = keccak256([1, 2, 3, 4], message_size);
let hash_b = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size);
unsafe {
// We use the `as` keyword here to denote the fact that we want to take just the first byte from the x Field
// The padding is taken care of by the program
let digest = keccak256([x as u8], 1);
assert(digest == result);
//#1399: variable meesage size
let message_size = 4;
let hash_a = keccak256([1, 2, 3, 4], message_size);
let hash_b = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size);

assert(hash_a == hash_b);
assert(hash_a == hash_b);

let message_size_big = 8;
let hash_c = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size_big);
let message_size_big = 8;
let hash_c = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size_big);

assert(hash_a != hash_c);
assert(hash_a != hash_c);
}
}

unconstrained fn keccak256<N>(data: [u8; N], msg_len: u32) -> [u8; 32] {
Expand Down

0 comments on commit d809292

Please sign in to comment.