Skip to content

Commit df689b2

Browse files
committed
use customized field conversion
1 parent 5719a1c commit df689b2

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

vlayer/ethereum/circuits/lib/src/slot.nr

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use dep::bignum::U256;
2-
use dep::ethereum::{misc::types::Bytes32, uint256::{from, from_field, into}};
2+
use dep::ethereum::{misc::types::Bytes32, uint256::{from, into}};
33
use dep::keccak256::keccak256;
44

55
global STORAGE_KEY_HASH_INPUT_LENGTH: u32 = 64;
@@ -16,16 +16,21 @@ pub(crate) fn dynamic_array(slot: U256, size: Field, index: Field) -> U256 {
1616
dynamic_array_with_precalculated_slot(start, size, index)
1717
}
1818

19+
fn field_to_u256(f: Field) -> U256 {
20+
let bytes = f.to_be_bytes();
21+
from(bytes)
22+
}
23+
1924
pub(crate) fn dynamic_array_with_precalculated_slot(slot: U256, size: Field, index: Field) -> U256 {
20-
let product = from_field(size * index);
25+
// TODO: use from_field() instead of field_to_u256()
26+
let product = field_to_u256(size * index);
2127
let sum = slot + product;
22-
// TODO: Uncomment this when we have a way to check for overflows in the circuit without getting brillig errors
23-
// assert(sum >= slot, "Attempt to add with overflow");
28+
assert(sum >= slot, "Attempt to add with overflow");
2429
sum
2530
}
2631

2732
pub(crate) fn struct_slot(slot: U256, offset: Field) -> U256 {
28-
let sum = slot + from_field(offset);
29-
// assert(sum >= slot, "Attempt to add with overflow");
33+
let sum = slot + field_to_u256(offset);
34+
assert(sum >= slot, "Attempt to add with overflow");
3035
sum
3136
}

vlayer/ethereum/circuits/lib/src/slot_test.nr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ mod dynamic_array {
8787
assert_eq(expected_slot, dynamic_array(slot, 2, 7));
8888
}
8989

90-
// #[test(should_fail_with = "Attempt to add with overflow")]
91-
// fn fail_overflow() {
92-
// let slot = from_field(6);
93-
// let _ = dynamic_array(slot, 2, MAX_FIELD_VALUE);
94-
// }
90+
#[test(should_fail_with = "Attempt to add with overflow")]
91+
fn fail_overflow() {
92+
let slot = from_field(6);
93+
let _ = dynamic_array(slot, 2, MAX_FIELD_VALUE);
94+
}
9595
}
9696

9797
mod dynamic_array_with_precalculated_slot {
@@ -121,11 +121,11 @@ mod dynamic_array_with_precalculated_slot {
121121
assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 10, 1_000_000_000));
122122
}
123123

124-
// #[test(should_fail_with = "Attempt to add with overflow")]
125-
// fn fail_overflow() {
126-
// let slot = from([0xfe; 32]);
127-
// let _ = dynamic_array_with_precalculated_slot(slot, 2, MAX_FIELD_VALUE);
128-
// }
124+
#[test(should_fail_with = "Attempt to add with overflow")]
125+
fn fail_overflow() {
126+
let slot = from([0xfe; 32]);
127+
let _ = dynamic_array_with_precalculated_slot(slot, 2, MAX_FIELD_VALUE);
128+
}
129129
}
130130

131131
mod struct_slot {
@@ -142,9 +142,9 @@ mod struct_slot {
142142
assert_eq(expected_slot, struct_slot(slot, 256));
143143
}
144144

145-
// #[test(should_fail_with = "Attempt to add with overflow")]
146-
// fn overflow() {
147-
// let slot = from([0xff; 32]);
148-
// let _ = struct_slot(slot, 256);
149-
// }
145+
#[test(should_fail_with = "Attempt to add with overflow")]
146+
fn overflow() {
147+
let slot = from([0xff; 32]);
148+
let _ = struct_slot(slot, 256);
149+
}
150150
}

0 commit comments

Comments
 (0)