Skip to content

Commit 8d3d06e

Browse files
committed
feat: bigint metered handler
1 parent 714c97a commit 8d3d06e

File tree

8 files changed

+204
-167
lines changed

8 files changed

+204
-167
lines changed

extensions/bigint/circuit/src/base_alu.rs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ struct BaseAluPreCompute {
3434
c: u8,
3535
}
3636

37+
macro_rules! dispatch {
38+
($execute_impl:ident, $local_opcode:ident) => {
39+
Ok(match $local_opcode {
40+
BaseAluOpcode::ADD => $execute_impl::<_, _, AddOp>,
41+
BaseAluOpcode::SUB => $execute_impl::<_, _, SubOp>,
42+
BaseAluOpcode::XOR => $execute_impl::<_, _, XorOp>,
43+
BaseAluOpcode::OR => $execute_impl::<_, _, OrOp>,
44+
BaseAluOpcode::AND => $execute_impl::<_, _, AndOp>,
45+
})
46+
};
47+
}
48+
3749
impl<F: PrimeField32> Executor<F> for Rv32BaseAlu256Executor {
3850
fn pre_compute_size(&self) -> usize {
3951
size_of::<BaseAluPreCompute>()
@@ -50,14 +62,8 @@ impl<F: PrimeField32> Executor<F> for Rv32BaseAlu256Executor {
5062
{
5163
let data: &mut BaseAluPreCompute = data.borrow_mut();
5264
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
53-
let fn_ptr = match local_opcode {
54-
BaseAluOpcode::ADD => execute_e1_impl::<_, _, AddOp>,
55-
BaseAluOpcode::SUB => execute_e1_impl::<_, _, SubOp>,
56-
BaseAluOpcode::XOR => execute_e1_impl::<_, _, XorOp>,
57-
BaseAluOpcode::OR => execute_e1_impl::<_, _, OrOp>,
58-
BaseAluOpcode::AND => execute_e1_impl::<_, _, AndOp>,
59-
};
60-
Ok(fn_ptr)
65+
66+
dispatch!(execute_e1_impl, local_opcode)
6167
}
6268

6369
#[cfg(feature = "tco")]
@@ -72,14 +78,8 @@ impl<F: PrimeField32> Executor<F> for Rv32BaseAlu256Executor {
7278
{
7379
let data: &mut BaseAluPreCompute = data.borrow_mut();
7480
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
75-
let fn_ptr = match local_opcode {
76-
BaseAluOpcode::ADD => execute_e1_tco_handler::<_, _, AddOp>,
77-
BaseAluOpcode::SUB => execute_e1_tco_handler::<_, _, SubOp>,
78-
BaseAluOpcode::XOR => execute_e1_tco_handler::<_, _, XorOp>,
79-
BaseAluOpcode::OR => execute_e1_tco_handler::<_, _, OrOp>,
80-
BaseAluOpcode::AND => execute_e1_tco_handler::<_, _, AndOp>,
81-
};
82-
Ok(fn_ptr)
81+
82+
dispatch!(execute_e1_tco_handler, local_opcode)
8383
}
8484
}
8585

@@ -101,14 +101,26 @@ impl<F: PrimeField32> MeteredExecutor<F> for Rv32BaseAlu256Executor {
101101
let data: &mut E2PreCompute<BaseAluPreCompute> = data.borrow_mut();
102102
data.chip_idx = chip_idx as u32;
103103
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
104-
let fn_ptr = match local_opcode {
105-
BaseAluOpcode::ADD => execute_e2_impl::<_, _, AddOp>,
106-
BaseAluOpcode::SUB => execute_e2_impl::<_, _, SubOp>,
107-
BaseAluOpcode::XOR => execute_e2_impl::<_, _, XorOp>,
108-
BaseAluOpcode::OR => execute_e2_impl::<_, _, OrOp>,
109-
BaseAluOpcode::AND => execute_e2_impl::<_, _, AndOp>,
110-
};
111-
Ok(fn_ptr)
104+
105+
dispatch!(execute_e2_impl, local_opcode)
106+
}
107+
108+
#[cfg(feature = "tco")]
109+
fn metered_handler<Ctx>(
110+
&self,
111+
chip_idx: usize,
112+
pc: u32,
113+
inst: &Instruction<F>,
114+
data: &mut [u8],
115+
) -> Result<Handler<F, Ctx>, StaticProgramError>
116+
where
117+
Ctx: MeteredExecutionCtxTrait,
118+
{
119+
let data: &mut E2PreCompute<BaseAluPreCompute> = data.borrow_mut();
120+
data.chip_idx = chip_idx as u32;
121+
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
122+
123+
dispatch!(execute_e2_tco_handler, local_opcode)
112124
}
113125
}
114126

@@ -137,6 +149,7 @@ unsafe fn execute_e1_impl<F: PrimeField32, CTX: ExecutionCtxTrait, OP: AluOp>(
137149
execute_e12_impl::<F, CTX, OP>(pre_compute, vm_state);
138150
}
139151

152+
#[create_tco_handler]
140153
unsafe fn execute_e2_impl<F: PrimeField32, CTX: MeteredExecutionCtxTrait, OP: AluOp>(
141154
pre_compute: &[u8],
142155
vm_state: &mut VmExecState<F, GuestMemory, CTX>,

extensions/bigint/circuit/src/branch_eq.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ struct BranchEqPreCompute {
3232
b: u8,
3333
}
3434

35+
macro_rules! dispatch {
36+
($execute_impl:ident, $local_opcode:ident) => {
37+
match $local_opcode {
38+
BranchEqualOpcode::BEQ => Ok($execute_impl::<_, _, false>),
39+
BranchEqualOpcode::BNE => Ok($execute_impl::<_, _, true>),
40+
}
41+
};
42+
}
43+
3544
impl<F: PrimeField32> Executor<F> for Rv32BranchEqual256Executor {
3645
fn pre_compute_size(&self) -> usize {
3746
size_of::<BranchEqPreCompute>()
@@ -48,11 +57,7 @@ impl<F: PrimeField32> Executor<F> for Rv32BranchEqual256Executor {
4857
{
4958
let data: &mut BranchEqPreCompute = data.borrow_mut();
5059
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
51-
let fn_ptr = match local_opcode {
52-
BranchEqualOpcode::BEQ => execute_e1_impl::<_, _, false>,
53-
BranchEqualOpcode::BNE => execute_e1_impl::<_, _, true>,
54-
};
55-
Ok(fn_ptr)
60+
dispatch!(execute_e1_impl, local_opcode)
5661
}
5762

5863
#[cfg(feature = "tco")]
@@ -67,11 +72,7 @@ impl<F: PrimeField32> Executor<F> for Rv32BranchEqual256Executor {
6772
{
6873
let data: &mut BranchEqPreCompute = data.borrow_mut();
6974
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
70-
let fn_ptr = match local_opcode {
71-
BranchEqualOpcode::BEQ => execute_e1_tco_handler::<_, _, false>,
72-
BranchEqualOpcode::BNE => execute_e1_tco_handler::<_, _, true>,
73-
};
74-
Ok(fn_ptr)
75+
dispatch!(execute_e1_tco_handler, local_opcode)
7576
}
7677
}
7778

@@ -93,11 +94,24 @@ impl<F: PrimeField32> MeteredExecutor<F> for Rv32BranchEqual256Executor {
9394
let data: &mut E2PreCompute<BranchEqPreCompute> = data.borrow_mut();
9495
data.chip_idx = chip_idx as u32;
9596
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
96-
let fn_ptr = match local_opcode {
97-
BranchEqualOpcode::BEQ => execute_e2_impl::<_, _, false>,
98-
BranchEqualOpcode::BNE => execute_e2_impl::<_, _, true>,
99-
};
100-
Ok(fn_ptr)
97+
dispatch!(execute_e2_impl, local_opcode)
98+
}
99+
100+
#[cfg(feature = "tco")]
101+
fn metered_handler<Ctx>(
102+
&self,
103+
chip_idx: usize,
104+
pc: u32,
105+
inst: &Instruction<F>,
106+
data: &mut [u8],
107+
) -> Result<Handler<F, Ctx>, StaticProgramError>
108+
where
109+
Ctx: MeteredExecutionCtxTrait,
110+
{
111+
let data: &mut E2PreCompute<BranchEqPreCompute> = data.borrow_mut();
112+
data.chip_idx = chip_idx as u32;
113+
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
114+
dispatch!(execute_e2_tco_handler, local_opcode)
101115
}
102116
}
103117

@@ -129,6 +143,7 @@ unsafe fn execute_e1_impl<F: PrimeField32, CTX: ExecutionCtxTrait, const IS_NE:
129143
execute_e12_impl::<F, CTX, IS_NE>(pre_compute, vm_state);
130144
}
131145

146+
#[create_tco_handler]
132147
unsafe fn execute_e2_impl<F: PrimeField32, CTX: MeteredExecutionCtxTrait, const IS_NE: bool>(
133148
pre_compute: &[u8],
134149
vm_state: &mut VmExecState<F, GuestMemory, CTX>,

extensions/bigint/circuit/src/branch_lt.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ struct BranchLtPreCompute {
3535
b: u8,
3636
}
3737

38+
macro_rules! dispatch {
39+
($execute_impl:ident, $local_opcode:ident) => {
40+
Ok(match $local_opcode {
41+
BranchLessThanOpcode::BLT => $execute_impl::<_, _, BltOp>,
42+
BranchLessThanOpcode::BLTU => $execute_impl::<_, _, BltuOp>,
43+
BranchLessThanOpcode::BGE => $execute_impl::<_, _, BgeOp>,
44+
BranchLessThanOpcode::BGEU => $execute_impl::<_, _, BgeuOp>,
45+
})
46+
};
47+
}
48+
3849
impl<F: PrimeField32> Executor<F> for Rv32BranchLessThan256Executor {
3950
fn pre_compute_size(&self) -> usize {
4051
size_of::<BranchLtPreCompute>()
@@ -51,13 +62,7 @@ impl<F: PrimeField32> Executor<F> for Rv32BranchLessThan256Executor {
5162
{
5263
let data: &mut BranchLtPreCompute = data.borrow_mut();
5364
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
54-
let fn_ptr = match local_opcode {
55-
BranchLessThanOpcode::BLT => execute_e1_impl::<_, _, BltOp>,
56-
BranchLessThanOpcode::BLTU => execute_e1_impl::<_, _, BltuOp>,
57-
BranchLessThanOpcode::BGE => execute_e1_impl::<_, _, BgeOp>,
58-
BranchLessThanOpcode::BGEU => execute_e1_impl::<_, _, BgeuOp>,
59-
};
60-
Ok(fn_ptr)
65+
dispatch!(execute_e1_impl, local_opcode)
6166
}
6267

6368
#[cfg(feature = "tco")]
@@ -72,13 +77,7 @@ impl<F: PrimeField32> Executor<F> for Rv32BranchLessThan256Executor {
7277
{
7378
let data: &mut BranchLtPreCompute = data.borrow_mut();
7479
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
75-
let fn_ptr = match local_opcode {
76-
BranchLessThanOpcode::BLT => execute_e1_tco_handler::<_, _, BltOp>,
77-
BranchLessThanOpcode::BLTU => execute_e1_tco_handler::<_, _, BltuOp>,
78-
BranchLessThanOpcode::BGE => execute_e1_tco_handler::<_, _, BgeOp>,
79-
BranchLessThanOpcode::BGEU => execute_e1_tco_handler::<_, _, BgeuOp>,
80-
};
81-
Ok(fn_ptr)
80+
dispatch!(execute_e1_tco_handler, local_opcode)
8281
}
8382
}
8483

@@ -100,13 +99,24 @@ impl<F: PrimeField32> MeteredExecutor<F> for Rv32BranchLessThan256Executor {
10099
let data: &mut E2PreCompute<BranchLtPreCompute> = data.borrow_mut();
101100
data.chip_idx = chip_idx as u32;
102101
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
103-
let fn_ptr = match local_opcode {
104-
BranchLessThanOpcode::BLT => execute_e2_impl::<_, _, BltOp>,
105-
BranchLessThanOpcode::BLTU => execute_e2_impl::<_, _, BltuOp>,
106-
BranchLessThanOpcode::BGE => execute_e2_impl::<_, _, BgeOp>,
107-
BranchLessThanOpcode::BGEU => execute_e2_impl::<_, _, BgeuOp>,
108-
};
109-
Ok(fn_ptr)
102+
dispatch!(execute_e2_impl, local_opcode)
103+
}
104+
105+
#[cfg(feature = "tco")]
106+
fn metered_handler<Ctx>(
107+
&self,
108+
chip_idx: usize,
109+
pc: u32,
110+
inst: &Instruction<F>,
111+
data: &mut [u8],
112+
) -> Result<Handler<F, Ctx>, StaticProgramError>
113+
where
114+
Ctx: MeteredExecutionCtxTrait,
115+
{
116+
let data: &mut E2PreCompute<BranchLtPreCompute> = data.borrow_mut();
117+
data.chip_idx = chip_idx as u32;
118+
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
119+
dispatch!(execute_e2_tco_handler, local_opcode)
110120
}
111121
}
112122

@@ -137,6 +147,7 @@ unsafe fn execute_e1_impl<F: PrimeField32, CTX: ExecutionCtxTrait, OP: BranchLes
137147
execute_e12_impl::<F, CTX, OP>(pre_compute, vm_state);
138148
}
139149

150+
#[create_tco_handler]
140151
unsafe fn execute_e2_impl<F: PrimeField32, CTX: MeteredExecutionCtxTrait, OP: BranchLessThanOp>(
141152
pre_compute: &[u8],
142153
vm_state: &mut VmExecState<F, GuestMemory, CTX>,

extensions/bigint/circuit/src/less_than.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ struct LessThanPreCompute {
3232
c: u8,
3333
}
3434

35+
macro_rules! dispatch {
36+
($execute_impl:ident, $local_opcode:ident) => {
37+
Ok(match $local_opcode {
38+
LessThanOpcode::SLT => $execute_impl::<_, _, false>,
39+
LessThanOpcode::SLTU => $execute_impl::<_, _, true>,
40+
})
41+
};
42+
}
43+
3544
impl<F: PrimeField32> Executor<F> for Rv32LessThan256Executor {
3645
fn pre_compute_size(&self) -> usize {
3746
size_of::<LessThanPreCompute>()
@@ -48,11 +57,7 @@ impl<F: PrimeField32> Executor<F> for Rv32LessThan256Executor {
4857
{
4958
let data: &mut LessThanPreCompute = data.borrow_mut();
5059
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
51-
let fn_ptr = match local_opcode {
52-
LessThanOpcode::SLT => execute_e1_impl::<_, _, false>,
53-
LessThanOpcode::SLTU => execute_e1_impl::<_, _, true>,
54-
};
55-
Ok(fn_ptr)
60+
dispatch!(execute_e1_impl, local_opcode)
5661
}
5762

5863
#[cfg(feature = "tco")]
@@ -67,11 +72,7 @@ impl<F: PrimeField32> Executor<F> for Rv32LessThan256Executor {
6772
{
6873
let data: &mut LessThanPreCompute = data.borrow_mut();
6974
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
70-
let fn_ptr = match local_opcode {
71-
LessThanOpcode::SLT => execute_e1_tco_handler::<_, _, false>,
72-
LessThanOpcode::SLTU => execute_e1_tco_handler::<_, _, true>,
73-
};
74-
Ok(fn_ptr)
75+
dispatch!(execute_e1_tco_handler, local_opcode)
7576
}
7677
}
7778

@@ -93,11 +94,24 @@ impl<F: PrimeField32> MeteredExecutor<F> for Rv32LessThan256Executor {
9394
let data: &mut E2PreCompute<LessThanPreCompute> = data.borrow_mut();
9495
data.chip_idx = chip_idx as u32;
9596
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
96-
let fn_ptr = match local_opcode {
97-
LessThanOpcode::SLT => execute_e2_impl::<_, _, false>,
98-
LessThanOpcode::SLTU => execute_e2_impl::<_, _, true>,
99-
};
100-
Ok(fn_ptr)
97+
dispatch!(execute_e2_impl, local_opcode)
98+
}
99+
100+
#[cfg(feature = "tco")]
101+
fn metered_handler<Ctx>(
102+
&self,
103+
chip_idx: usize,
104+
pc: u32,
105+
inst: &Instruction<F>,
106+
data: &mut [u8],
107+
) -> Result<Handler<F, Ctx>, StaticProgramError>
108+
where
109+
Ctx: MeteredExecutionCtxTrait,
110+
{
111+
let data: &mut E2PreCompute<LessThanPreCompute> = data.borrow_mut();
112+
data.chip_idx = chip_idx as u32;
113+
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
114+
dispatch!(execute_e2_tco_handler, local_opcode)
101115
}
102116
}
103117

@@ -133,6 +147,7 @@ unsafe fn execute_e1_impl<F: PrimeField32, CTX: ExecutionCtxTrait, const IS_U256
133147
execute_e12_impl::<F, CTX, IS_U256>(pre_compute, vm_state);
134148
}
135149

150+
#[create_tco_handler]
136151
unsafe fn execute_e2_impl<F: PrimeField32, CTX: MeteredExecutionCtxTrait, const IS_U256: bool>(
137152
pre_compute: &[u8],
138153
vm_state: &mut VmExecState<F, GuestMemory, CTX>,

extensions/bigint/circuit/src/mult.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ impl<F: PrimeField32> MeteredExecutor<F> for Rv32Multiplication256Executor {
8787
self.pre_compute_impl(pc, inst, &mut data.data)?;
8888
Ok(execute_e2_impl)
8989
}
90+
91+
#[cfg(feature = "tco")]
92+
fn metered_handler<Ctx>(
93+
&self,
94+
chip_idx: usize,
95+
pc: u32,
96+
inst: &Instruction<F>,
97+
data: &mut [u8],
98+
) -> Result<Handler<F, Ctx>, StaticProgramError>
99+
where
100+
Ctx: MeteredExecutionCtxTrait,
101+
{
102+
let data: &mut E2PreCompute<MultPreCompute> = data.borrow_mut();
103+
data.chip_idx = chip_idx as u32;
104+
self.pre_compute_impl(pc, inst, &mut data.data)?;
105+
Ok(execute_e2_tco_handler)
106+
}
90107
}
91108

92109
#[inline(always)]
@@ -115,6 +132,7 @@ unsafe fn execute_e1_impl<F: PrimeField32, CTX: ExecutionCtxTrait>(
115132
execute_e12_impl(pre_compute, vm_state);
116133
}
117134

135+
#[create_tco_handler]
118136
unsafe fn execute_e2_impl<F: PrimeField32, CTX: MeteredExecutionCtxTrait>(
119137
pre_compute: &[u8],
120138
vm_state: &mut VmExecState<F, GuestMemory, CTX>,

0 commit comments

Comments
 (0)