Skip to content

Commit 4795f9a

Browse files
committed
fix: reduce memory_bridge enabled degree from 3 to 2
1 parent df92fec commit 4795f9a

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

extensions/memcpy/circuit/src/iteration.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +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,
6465
// -1 for the first iteration, 1 for the last iteration, 0 for the middle iterations
6566
pub is_boundary: T,
6667
pub data_1: [T; MEMCPY_LOOP_NUM_LIMBS],
@@ -105,8 +106,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
105106
};
106107

107108
let shift = local.shift[0] * AB::Expr::TWO + local.shift[1];
108-
let is_shift_non_zero = or::<AB::Expr>(local.shift[0], local.shift[1]);
109-
let is_shift_zero = not::<AB::Expr>(is_shift_non_zero.clone());
109+
let is_shift_zero = not::<AB::Expr>(local.is_shift_non_zero.clone());
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]);
@@ -122,6 +122,8 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
122122

123123
let len = local.len[0]
124124
+ local.len[1] * AB::Expr::from_canonical_u32(1 << (2 * MEMCPY_LOOP_LIMB_BITS));
125+
let prev_len = prev.len[0]
126+
+ prev.len[1] * AB::Expr::from_canonical_u32(1 << (2 * MEMCPY_LOOP_LIMB_BITS));
125127

126128
// write_data =
127129
// (local.data_1[shift..4], prev.data_4[0..shift]),
@@ -165,6 +167,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
165167
builder.assert_bool(local.is_valid);
166168
local.shift.iter().for_each(|x| builder.assert_bool(*x));
167169
builder.assert_bool(local.is_valid_not_start);
170+
builder.assert_bool(local.is_shift_non_zero);
168171
// is_boundary is either -1, 0 or 1
169172
builder.assert_tern(local.is_boundary + AB::Expr::ONE);
170173

@@ -174,6 +177,9 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
174177
and::<AB::Expr>(local.is_valid, is_not_start),
175178
);
176179

180+
// is_shift_non_zero is correct
181+
builder.assert_eq(local.is_shift_non_zero, or::<AB::Expr>(local.shift[0], local.shift[1]));
182+
177183
// if !is_valid, then is_boundary = 0, shift = 0 (we will use this assumption later)
178184
let mut is_not_valid_when = builder.when(not::<AB::Expr>(local.is_valid));
179185
is_not_valid_when.assert_zero(local.is_boundary);
@@ -183,7 +189,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
183189
// and dest = prev_dest + 16, shift = prev_shift
184190
let mut is_valid_not_start_when = builder.when(local.is_valid_not_start);
185191
is_valid_not_start_when
186-
.assert_eq(local.len[0], prev.len[0] - AB::Expr::from_canonical_u32(16));
192+
.assert_eq(len.clone(), prev_len - AB::Expr::from_canonical_u32(16));
187193
is_valid_not_start_when
188194
.assert_eq(local.source, prev.source + AB::Expr::from_canonical_u32(16));
189195
is_valid_not_start_when.assert_eq(local.dest, prev.dest + AB::Expr::from_canonical_u32(16));
@@ -199,7 +205,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
199205
// since is_shift_non_zero degree is 2, we need to keep the degree of the condition to 1
200206
builder
201207
.when(not::<AB::Expr>(prev.is_valid_not_start) - not::<AB::Expr>(prev.is_valid))
202-
.assert_eq(local.timestamp, prev.timestamp + is_shift_non_zero.clone());
208+
.assert_eq(local.timestamp, prev.timestamp + local.is_shift_non_zero.clone());
203209

204210
// if prev.is_valid_not_start and local.is_valid_not_start, then timestamp=prev_timestamp+8
205211
// prev.is_valid_not_start is the opposite of previous condition
@@ -222,7 +228,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
222228
+ (local.is_boundary + AB::Expr::ONE) * AB::Expr::from_canonical_usize(4),
223229
local.dest,
224230
local.source,
225-
len,
231+
len.clone(),
226232
(AB::Expr::ONE - local.is_boundary) * shift.clone() * (AB::F::TWO).inverse()
227233
+ (local.is_boundary + AB::Expr::ONE) * AB::Expr::TWO,
228234
)
@@ -241,7 +247,7 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
241247
.enumerate()
242248
.for_each(|(idx, (data, read_aux))| {
243249
let is_valid_read = if idx == 3 {
244-
or::<AB::Expr>(is_shift_non_zero.clone(), local.is_valid_not_start)
250+
or::<AB::Expr>(local.is_shift_non_zero.clone(), local.is_valid_not_start)
245251
} else {
246252
local.is_valid_not_start.into()
247253
};
@@ -288,10 +294,10 @@ impl<AB: InteractionBuilder> Air<AB> for MemcpyIterAir {
288294
),
289295
];
290296
self.range_bus
291-
.push(local.len[0], len_bits_limit[0].clone(), true)
297+
.push(local.len[0].clone(), len_bits_limit[0].clone(), true)
292298
.eval(builder, local.is_valid);
293299
self.range_bus
294-
.push(local.len[1], len_bits_limit[1].clone(), true)
300+
.push(local.len[1].clone(), len_bits_limit[1].clone(), true)
295301
.eval(builder, local.is_valid);
296302
}
297303
}
@@ -688,6 +694,7 @@ impl<F: PrimeField32> TraceFiller<F> for MemcpyIterFiller {
688694
} else {
689695
F::ZERO
690696
};
697+
cols.is_shift_non_zero = F::from_canonical_u8((record.inner.shift != 0) as u8);
691698
cols.is_valid_not_start = F::from_canonical_u8(1 - is_start as u8);
692699
cols.is_valid = F::ONE;
693700
cols.shift = [record.inner.shift & 1, record.inner.shift >> 1]

extensions/memcpy/circuit/src/loops.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,6 @@ impl ChipUsageGetter for MemcpyLoopChip {
453453
fn air_name(&self) -> String {
454454
get_air_name(&self.air)
455455
}
456-
fn constant_trace_height(&self) -> Option<usize> {
457-
Some(self.records.lock().unwrap().len())
458-
}
459456
fn current_trace_height(&self) -> usize {
460457
self.records.lock().unwrap().len()
461458
}

extensions/sha256/circuit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ openvm-circuit = { workspace = true }
1616
openvm-instructions = { workspace = true }
1717
openvm-sha256-transpiler = { workspace = true }
1818
openvm-rv32im-circuit = { workspace = true }
19+
openvm-memcpy-circuit = { workspace = true }
1920
openvm-sha256-air = { workspace = true }
2021

2122
derive-new.workspace = true

extensions/sha256/circuit/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use openvm_circuit::{
1111
system::{SystemChipInventory, SystemCpuBuilder, SystemExecutor},
1212
};
1313
use openvm_circuit_derive::VmConfig;
14+
use openvm_memcpy_circuit::{Memcpy, MemcpyCpuProverExt, MemcpyExecutor};
1415
use openvm_rv32im_circuit::{
1516
Rv32I, Rv32IExecutor, Rv32ImCpuProverExt, Rv32Io, Rv32IoExecutor, Rv32M, Rv32MExecutor,
1617
};
@@ -54,6 +55,8 @@ pub struct Sha256Rv32Config {
5455
pub io: Rv32Io,
5556
#[extension]
5657
pub sha256: Sha256,
58+
#[extension]
59+
pub memcpy: Memcpy,
5760
}
5861

5962
impl Default for Sha256Rv32Config {
@@ -64,6 +67,7 @@ impl Default for Sha256Rv32Config {
6467
rv32m: Rv32M::default(),
6568
io: Rv32Io,
6669
sha256: Sha256,
70+
memcpy: Memcpy,
6771
}
6872
}
6973
}
@@ -99,6 +103,7 @@ where
99103
VmProverExtension::<E, _, _>::extend_prover(&Rv32ImCpuProverExt, &config.rv32m, inventory)?;
100104
VmProverExtension::<E, _, _>::extend_prover(&Rv32ImCpuProverExt, &config.io, inventory)?;
101105
VmProverExtension::<E, _, _>::extend_prover(&Sha2CpuProverExt, &config.sha256, inventory)?;
106+
VmProverExtension::<E, _, _>::extend_prover(&MemcpyCpuProverExt, &config.memcpy, inventory)?;
102107
Ok(chip_complex)
103108
}
104109
}

0 commit comments

Comments
 (0)