Skip to content

Commit 7e259eb

Browse files
committed
feat: add memcpy tests
1 parent 7a7969f commit 7e259eb

File tree

8 files changed

+186
-910
lines changed

8 files changed

+186
-910
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ members = [
6363
"extensions/pairing/guest",
6464
"extensions/memcpy/circuit",
6565
"extensions/memcpy/transpiler",
66+
"extensions/memcpy/tests",
6667
"guest-libs/ff_derive/",
6768
"guest-libs/k256/",
6869
"guest-libs/p256/",

crates/vm/src/arch/testing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub const BITWISE_OP_LOOKUP_BUS: BusIndex = 9;
2929
pub const BYTE_XOR_BUS: BusIndex = 10;
3030
pub const RANGE_TUPLE_CHECKER_BUS: BusIndex = 11;
3131
pub const MEMORY_MERKLE_BUS: BusIndex = 12;
32+
pub const MEMCPY_BUS: BusIndex = 13;
3233

3334
pub const RANGE_CHECKER_BUS: BusIndex = 4;
3435

extensions/memcpy/circuit/src/iteration.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub struct MemcpyIterCols<T> {
6161
pub shift: [T; 2],
6262
pub is_valid: T,
6363
pub is_valid_not_start: T,
64-
pub is_shift_non_zero: T,
64+
pub is_shift_zero: T,
6565
// -1 for the first iteration, 1 for the last iteration, 0 for the middle iterations
6666
pub is_boundary: T,
6767
pub data_1: [T; MEMCPY_LOOP_NUM_LIMBS],
@@ -106,7 +106,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
106106
};
107107

108108
let shift = local.shift[0] * AB::Expr::TWO + local.shift[1];
109-
let is_shift_zero = not::<AB::Expr>(local.is_shift_non_zero.clone());
109+
let is_shift_non_zero = not::<AB::Expr>(local.is_shift_zero);
110110
let is_shift_one = and::<AB::Expr>(local.shift[0], not::<AB::Expr>(local.shift[1]));
111111
let is_shift_two = and::<AB::Expr>(not::<AB::Expr>(local.shift[0]), local.shift[1]);
112112
let is_shift_three = and::<AB::Expr>(local.shift[0], local.shift[1]);
@@ -141,7 +141,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
141141
.iter()
142142
.map(|(prev_data, next_data)| {
143143
array::from_fn::<_, MEMCPY_LOOP_NUM_LIMBS, _>(|i| {
144-
is_shift_zero.clone() * (next_data[i])
144+
local.is_shift_zero.clone() * (next_data[i])
145145
+ is_shift_one.clone()
146146
* (if i < 3 {
147147
next_data[i + 1]
@@ -167,7 +167,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
167167
builder.assert_bool(local.is_valid);
168168
local.shift.iter().for_each(|x| builder.assert_bool(*x));
169169
builder.assert_bool(local.is_valid_not_start);
170-
builder.assert_bool(local.is_shift_non_zero);
170+
builder.assert_bool(local.is_shift_zero);
171171
// is_boundary is either -1, 0 or 1
172172
builder.assert_tern(local.is_boundary + AB::Expr::ONE);
173173

@@ -178,7 +178,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
178178
);
179179

180180
// is_shift_non_zero is correct
181-
builder.assert_eq(local.is_shift_non_zero, or::<AB::Expr>(local.shift[0], local.shift[1]));
181+
builder.assert_eq(local.is_shift_zero, not::<AB::Expr>(or::<AB::Expr>(local.shift[0], local.shift[1])));
182182

183183
// if !is_valid, then is_boundary = 0, shift = 0 (we will use this assumption later)
184184
let mut is_not_valid_when = builder.when(not::<AB::Expr>(local.is_valid));
@@ -205,7 +205,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
205205
// since is_shift_non_zero degree is 2, we need to keep the degree of the condition to 1
206206
builder
207207
.when(not::<AB::Expr>(prev.is_valid_not_start) - not::<AB::Expr>(prev.is_valid))
208-
.assert_eq(local.timestamp, prev.timestamp + local.is_shift_non_zero.clone());
208+
.assert_eq(local.timestamp, prev.timestamp + is_shift_non_zero.clone());
209209

210210
// if prev.is_valid_not_start and local.is_valid_not_start, then timestamp=prev_timestamp+8
211211
// prev.is_valid_not_start is the opposite of previous condition
@@ -247,7 +247,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
247247
.enumerate()
248248
.for_each(|(idx, (data, read_aux))| {
249249
let is_valid_read = if idx == 3 {
250-
or::<AB::Expr>(local.is_shift_non_zero.clone(), local.is_valid_not_start)
250+
or::<AB::Expr>(is_shift_non_zero.clone(), local.is_valid_not_start)
251251
} else {
252252
local.is_valid_not_start.into()
253253
};
@@ -694,7 +694,7 @@ impl<F: PrimeField32> TraceFiller<F> for MemcpyIterFiller {
694694
} else {
695695
F::ZERO
696696
};
697-
cols.is_shift_non_zero = F::from_canonical_u8((record.inner.shift != 0) as u8);
697+
cols.is_shift_zero = F::from_canonical_u8((record.inner.shift == 0) as u8);
698698
cols.is_valid_not_start = F::from_canonical_u8(1 - is_start as u8);
699699
cols.is_valid = F::ONE;
700700
cols.shift = [record.inner.shift & 1, record.inner.shift >> 1]

extensions/memcpy/circuit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod extension;
33
mod iteration;
44
mod loops;
55

6+
pub use bus::*;
67
pub use extension::*;
78
pub use iteration::*;
89
pub use loops::*;

0 commit comments

Comments
 (0)