forked from DelphinusLab/zkWasm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_call.rs
128 lines (114 loc) · 4.12 KB
/
op_call.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::circuits::cell::*;
use crate::circuits::etable::allocator::*;
use crate::circuits::etable::ConstraintBuilder;
use crate::circuits::etable::EventTableCommonConfig;
use crate::circuits::etable::EventTableOpcodeConfig;
use crate::circuits::etable::EventTableOpcodeConfigBuilder;
use crate::circuits::jtable::expression::JtableLookupEntryEncode;
use crate::circuits::jtable::JumpTableConfig;
use crate::circuits::utils::bn_to_field;
use crate::circuits::utils::step_status::StepStatus;
use crate::circuits::utils::table_entry::EventTableEntryWithMemoryInfo;
use crate::circuits::utils::Context;
use crate::constant_from;
use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::plonk::Error;
use halo2_proofs::plonk::Expression;
use halo2_proofs::plonk::VirtualCells;
use specs::encode::frame_table::encode_frame_table_entry;
use specs::encode::opcode::encode_call;
use specs::step::StepInfo;
pub struct CallConfig<F: FieldExt> {
index_cell: AllocatedCommonRangeCell<F>,
frame_table_lookup: AllocatedJumpTableLookupCell<F>,
}
pub struct CallConfigBuilder {}
impl<F: FieldExt> EventTableOpcodeConfigBuilder<F> for CallConfigBuilder {
fn configure(
common_config: &EventTableCommonConfig<F>,
allocator: &mut EventTableCellAllocator<F>,
constraint_builder: &mut ConstraintBuilder<F>,
) -> Box<dyn EventTableOpcodeConfig<F>> {
let index_cell = allocator.alloc_common_range_cell();
let frame_table_lookup = common_config.jtable_lookup_cell;
let fid_cell = common_config.fid_cell;
let iid_cell = common_config.iid_cell;
let frame_id_cell = common_config.frame_id_cell;
let eid = common_config.eid_cell;
constraint_builder.constraints.push((
"return frame table lookups",
Box::new(move |meta| {
vec![
frame_table_lookup.0.expr(meta)
- JumpTableConfig::encode_lookup(
eid.expr(meta),
frame_id_cell.expr(meta),
index_cell.expr(meta),
fid_cell.expr(meta),
iid_cell.expr(meta) + constant_from!(1),
),
]
}),
));
Box::new(CallConfig {
index_cell,
frame_table_lookup,
})
}
}
impl<F: FieldExt> EventTableOpcodeConfig<F> for CallConfig<F> {
fn opcode(&self, meta: &mut VirtualCells<'_, F>) -> Expression<F> {
encode_call(self.index_cell.expr(meta))
}
fn assign(
&self,
ctx: &mut Context<'_, F>,
step: &StepStatus,
entry: &EventTableEntryWithMemoryInfo,
) -> Result<(), Error> {
match &entry.eentry.step_info {
StepInfo::Call { index } => {
self.index_cell.assign(ctx, F::from(*index as u64))?;
self.frame_table_lookup.0.assign(
ctx,
bn_to_field(&encode_frame_table_entry(
step.current.eid.into(),
step.current.last_jump_eid.into(),
(*index).into(),
step.current.fid.into(),
(step.current.iid + 1).into(),
)),
)?;
Ok(())
}
_ => unreachable!(),
}
}
fn jops_expr(&self, _meta: &mut VirtualCells<'_, F>) -> Option<Expression<F>> {
Some(constant_from!(1))
}
fn jops(&self) -> u32 {
1
}
fn next_frame_id(
&self,
meta: &mut VirtualCells<'_, F>,
common_config: &EventTableCommonConfig<F>,
) -> Option<Expression<F>> {
Some(common_config.eid_cell.curr_expr(meta))
}
fn next_fid(
&self,
meta: &mut VirtualCells<'_, F>,
_common_config: &EventTableCommonConfig<F>,
) -> Option<Expression<F>> {
Some(self.index_cell.expr(meta))
}
fn next_iid(
&self,
_meta: &mut VirtualCells<'_, F>,
_common_config: &EventTableCommonConfig<F>,
) -> Option<Expression<F>> {
Some(constant_from!(0))
}
}