-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chip handler for memory, identical to that for registers. Once the logic is verified, the next step will be to deduplicate.
- Loading branch information
Showing
8 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub const WORD_SIZE: usize = 4; | ||
|
||
pub const INFO_OUT_ADDR: u32 = 0xC000_0000; | ||
pub const INFO_OUT_ADDR: u32 = 0xC000_0000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::{ | ||
chip_handler::{MemoryChipOperations, MemoryExpr}, | ||
circuit_builder::CircuitBuilder, | ||
error::ZKVMError, | ||
expression::{Expression, ToExpr, WitIn}, | ||
instructions::riscv::config::ExprLtConfig, | ||
structs::RAMType, | ||
}; | ||
use ff_ext::ExtensionField; | ||
|
||
impl<'a, E: ExtensionField, NR: Into<String>, N: FnOnce() -> NR> MemoryChipOperations<E, NR, N> | ||
for CircuitBuilder<'a, E> | ||
{ | ||
#[allow(dead_code)] | ||
fn memory_read( | ||
&mut self, | ||
name_fn: N, | ||
memory_addr: &WitIn, | ||
prev_ts: Expression<E>, | ||
ts: Expression<E>, | ||
value: MemoryExpr<E>, | ||
) -> Result<(Expression<E>, ExprLtConfig), ZKVMError> { | ||
self.namespace(name_fn, |cb| { | ||
// READ (a, v, t) | ||
let read_record = cb.rlc_chip_record( | ||
[ | ||
vec![Expression::<E>::Constant(E::BaseField::from( | ||
RAMType::Memory as u64, | ||
))], | ||
vec![memory_addr.expr()], | ||
value.to_vec(), | ||
vec![prev_ts.clone()], | ||
] | ||
.concat(), | ||
); | ||
// Write (a, v, t) | ||
let write_record = cb.rlc_chip_record( | ||
[ | ||
vec![Expression::<E>::Constant(E::BaseField::from( | ||
RAMType::Memory as u64, | ||
))], | ||
vec![memory_addr.expr()], | ||
value.to_vec(), | ||
vec![ts.clone()], | ||
] | ||
.concat(), | ||
); | ||
cb.read_record(|| "read_record", read_record)?; | ||
cb.write_record(|| "write_record", write_record)?; | ||
|
||
// assert prev_ts < current_ts | ||
let lt_cfg = cb.less_than(|| "prev_ts < ts", prev_ts, ts.clone(), Some(true))?; | ||
|
||
let next_ts = ts + 1.into(); | ||
|
||
Ok((next_ts, lt_cfg)) | ||
}) | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn memory_write( | ||
&mut self, | ||
name_fn: N, | ||
memory_addr: &WitIn, | ||
prev_ts: Expression<E>, | ||
ts: Expression<E>, | ||
prev_values: MemoryExpr<E>, | ||
value: MemoryExpr<E>, | ||
) -> Result<(Expression<E>, ExprLtConfig), ZKVMError> { | ||
self.namespace(name_fn, |cb| { | ||
// READ (a, v, t) | ||
let read_record = cb.rlc_chip_record( | ||
[ | ||
vec![Expression::<E>::Constant(E::BaseField::from( | ||
RAMType::Memory as u64, | ||
))], | ||
vec![memory_addr.expr()], | ||
prev_values.to_vec(), | ||
vec![prev_ts.clone()], | ||
] | ||
.concat(), | ||
); | ||
// Write (a, v, t) | ||
let write_record = cb.rlc_chip_record( | ||
[ | ||
vec![Expression::<E>::Constant(E::BaseField::from( | ||
RAMType::Memory as u64, | ||
))], | ||
vec![memory_addr.expr()], | ||
value.to_vec(), | ||
vec![ts.clone()], | ||
] | ||
.concat(), | ||
); | ||
cb.read_record(|| "read_record", read_record)?; | ||
cb.write_record(|| "write_record", write_record)?; | ||
|
||
let lt_cfg = cb.less_than(|| "prev_ts < ts", prev_ts, ts.clone(), Some(true))?; | ||
|
||
let next_ts = ts + 1.into(); | ||
|
||
Ok((next_ts, lt_cfg)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters