Skip to content

Commit f1de609

Browse files
10to4hero78119matthiasgoergensnoel2004mcalancea
authored
Feat/structural witin add (#740)
Work for #654 --------- Co-authored-by: sm.wu <[email protected]> Co-authored-by: Matthias Görgens <[email protected]> Co-authored-by: Ho <[email protected]> Co-authored-by: mcalancea <[email protected]> Co-authored-by: noelwei <[email protected]>
1 parent 4d3948f commit f1de609

20 files changed

+499
-279
lines changed

ceno_zkvm/src/chip_handler/general.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ff_ext::ExtensionField;
33
use crate::{
44
circuit_builder::{CircuitBuilder, ConstraintSystem, SetTableSpec},
55
error::ZKVMError,
6-
expression::{Expression, Fixed, Instance, ToExpr, WitIn},
6+
expression::{Expression, Fixed, Instance, StructuralWitIn, ToExpr, WitIn},
77
instructions::riscv::constants::{
88
END_CYCLE_IDX, END_PC_IDX, EXIT_CODE_IDX, INIT_CYCLE_IDX, INIT_PC_IDX, PUBLIC_IO_IDX,
99
UINT_LIMBS,
@@ -28,6 +28,21 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
2828
self.cs.create_witin(name_fn)
2929
}
3030

31+
pub fn create_structural_witin<NR, N>(
32+
&mut self,
33+
name_fn: N,
34+
max_len: usize,
35+
offset: u32,
36+
multi_factor: usize,
37+
) -> StructuralWitIn
38+
where
39+
NR: Into<String>,
40+
N: FnOnce() -> NR,
41+
{
42+
self.cs
43+
.create_structural_witin(name_fn, max_len, offset, multi_factor)
44+
}
45+
3146
pub fn create_fixed<NR, N>(&mut self, name_fn: N) -> Result<Fixed, ZKVMError>
3247
where
3348
NR: Into<String>,

ceno_zkvm/src/circuit_builder.rs

+32-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
ROMType,
1010
chip_handler::utils::rlc_chip_record,
1111
error::ZKVMError,
12-
expression::{Expression, Fixed, Instance, WitIn},
12+
expression::{Expression, Fixed, Instance, StructuralWitIn, WitIn},
1313
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
1414
witness::RowMajorMatrix,
1515
};
@@ -59,14 +59,6 @@ pub struct LogupTableExpression<E: ExtensionField> {
5959
pub table_len: usize,
6060
}
6161

62-
// TODO encapsulate few information of table spec to SetTableAddrType value
63-
// once confirm syntax is friendly and parsed by recursive verifier
64-
#[derive(Clone, Debug)]
65-
pub enum SetTableAddrType {
66-
FixedAddr,
67-
DynamicAddr(DynamicAddr),
68-
}
69-
7062
#[derive(Clone, Debug)]
7163
pub struct DynamicAddr {
7264
pub addr_witin_id: usize,
@@ -75,12 +67,13 @@ pub struct DynamicAddr {
7567

7668
#[derive(Clone, Debug)]
7769
pub struct SetTableSpec {
78-
pub addr_type: SetTableAddrType,
79-
pub len: usize,
70+
pub len: Option<usize>,
71+
pub structural_witins: Vec<StructuralWitIn>,
8072
}
8173

8274
#[derive(Clone, Debug)]
8375
pub struct SetTableExpression<E: ExtensionField> {
76+
/// table expression
8477
pub expr: Expression<E>,
8578

8679
// TODO make decision to have enum/struct
@@ -92,10 +85,12 @@ pub struct SetTableExpression<E: ExtensionField> {
9285
pub struct ConstraintSystem<E: ExtensionField> {
9386
pub(crate) ns: NameSpace,
9487

95-
// pub platform: Platform,
9688
pub num_witin: WitnessId,
9789
pub witin_namespace_map: Vec<String>,
9890

91+
pub num_structural_witin: WitnessId,
92+
pub structural_witin_namespace_map: Vec<String>,
93+
9994
pub num_fixed: usize,
10095
pub fixed_namespace_map: Vec<String>,
10196

@@ -152,6 +147,8 @@ impl<E: ExtensionField> ConstraintSystem<E> {
152147
num_witin: 0,
153148
// platform,
154149
witin_namespace_map: vec![],
150+
num_structural_witin: 0,
151+
structural_witin_namespace_map: vec![],
155152
num_fixed: 0,
156153
fixed_namespace_map: vec![],
157154
ns: NameSpace::new(root_name_fn),
@@ -209,20 +206,36 @@ impl<E: ExtensionField> ConstraintSystem<E> {
209206
}
210207

211208
pub fn create_witin<NR: Into<String>, N: FnOnce() -> NR>(&mut self, n: N) -> WitIn {
212-
let wit_in = WitIn {
213-
id: {
214-
let id = self.num_witin;
215-
self.num_witin = self.num_witin.strict_add(1);
216-
id
217-
},
218-
};
209+
let wit_in = WitIn { id: self.num_witin };
210+
self.num_witin = self.num_witin.strict_add(1);
219211

220212
let path = self.ns.compute_path(n().into());
221213
self.witin_namespace_map.push(path);
222214

223215
wit_in
224216
}
225217

218+
pub fn create_structural_witin<NR: Into<String>, N: FnOnce() -> NR>(
219+
&mut self,
220+
n: N,
221+
max_len: usize,
222+
offset: u32,
223+
multi_factor: usize,
224+
) -> StructuralWitIn {
225+
let wit_in = StructuralWitIn {
226+
id: self.num_structural_witin,
227+
max_len,
228+
offset,
229+
multi_factor,
230+
};
231+
self.num_structural_witin = self.num_structural_witin.strict_add(1);
232+
233+
let path = self.ns.compute_path(n().into());
234+
self.structural_witin_namespace_map.push(path);
235+
236+
wit_in
237+
}
238+
226239
pub fn create_fixed<NR: Into<String>, N: FnOnce() -> NR>(
227240
&mut self,
228241
n: N,

0 commit comments

Comments
 (0)