forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathmod.rs
153 lines (143 loc) · 4.64 KB
/
mod.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use eth_types::{GethExecStep, ToWord, Word};
use crate::{
circuit_input_builder::{
Call, CircuitInputStateRef, ExecState, ExecStep, PrecompileEvent, SHA256,
},
operation::CallContextField,
precompile::{PrecompileAuxData, PrecompileCalls},
Error,
};
mod ec_add;
mod ec_mul;
mod ec_pairing;
mod ecrecover;
mod modexp;
mod p256_verify;
use ec_add::opt_data as opt_data_ec_add;
use ec_mul::opt_data as opt_data_ec_mul;
use ec_pairing::opt_data as opt_data_ec_pairing;
use ecrecover::opt_data as opt_data_ecrecover;
use modexp::opt_data as opt_data_modexp;
use p256_verify::opt_data as opt_data_p256verify;
pub fn gen_associated_ops(
state: &mut CircuitInputStateRef,
geth_step: GethExecStep,
call: Call,
precompile: PrecompileCalls,
input_bytes: &[u8],
output_bytes: &[u8],
return_bytes: &[u8],
) -> Result<ExecStep, Error> {
let input_step = state.new_step(&geth_step)?;
gen_ops(
state,
input_step,
call,
precompile,
input_bytes,
output_bytes,
return_bytes,
)
}
/// generate an execstep for the input step
pub fn gen_ops(
state: &mut CircuitInputStateRef,
mut exec_step: ExecStep,
call: Call,
precompile: PrecompileCalls,
input_bytes: &[u8],
output_bytes: &[u8],
return_bytes: &[u8],
) -> Result<ExecStep, Error> {
assert_eq!(call.code_address(), Some(precompile.into()));
exec_step.exec_state = ExecState::Precompile(precompile);
common_call_ctx_reads(state, &mut exec_step, &call)?;
let (opt_event, aux_data) = match precompile {
PrecompileCalls::Ecrecover => opt_data_ecrecover(input_bytes, output_bytes, return_bytes),
PrecompileCalls::Bn128Add => opt_data_ec_add(input_bytes, output_bytes, return_bytes),
PrecompileCalls::Bn128Mul => opt_data_ec_mul(input_bytes, output_bytes, return_bytes),
PrecompileCalls::Bn128Pairing => {
opt_data_ec_pairing(input_bytes, output_bytes, return_bytes)
}
PrecompileCalls::Modexp => opt_data_modexp(input_bytes, output_bytes, return_bytes),
PrecompileCalls::Identity => (
None,
Some(PrecompileAuxData::Identity {
input_bytes: input_bytes.to_vec(),
output_bytes: output_bytes.to_vec(),
return_bytes: return_bytes.to_vec(),
}),
),
PrecompileCalls::Sha256 => (
if output_bytes.is_empty() {
None
} else {
Some(PrecompileEvent::SHA256(SHA256 {
input: input_bytes.to_vec(),
digest: output_bytes
.try_into()
.expect("output bytes must be 32 bytes"),
}))
},
Some(PrecompileAuxData::SHA256 {
input_bytes: input_bytes.to_vec(),
output_bytes: output_bytes.to_vec(),
return_bytes: return_bytes.to_vec(),
}),
),
PrecompileCalls::P256Verify => opt_data_p256verify(input_bytes, output_bytes, return_bytes),
_ => {
log::warn!("precompile {:?} unsupported in circuits", precompile);
(
None,
Some(PrecompileAuxData::Base {
input_bytes: input_bytes.to_vec(),
output_bytes: output_bytes.to_vec(),
return_bytes: return_bytes.to_vec(),
}),
)
}
};
log::trace!("precompile event {opt_event:?}, aux data {aux_data:?}");
if let Some(event) = opt_event {
state.push_precompile_event(event);
}
exec_step.aux_data = aux_data;
Ok(exec_step)
}
fn common_call_ctx_reads(
state: &mut CircuitInputStateRef,
exec_step: &mut ExecStep,
call: &Call,
) -> Result<(), Error> {
for (field, value) in [
(
CallContextField::IsSuccess,
Word::from(call.is_success as u64),
),
(
CallContextField::CalleeAddress,
call.code_address().unwrap().to_word(),
),
(CallContextField::IsRoot, Word::from(call.is_root as u64)),
(
CallContextField::CallDataOffset,
call.call_data_offset.into(),
),
(
CallContextField::CallDataLength,
call.call_data_length.into(),
),
(
CallContextField::ReturnDataOffset,
call.return_data_offset.into(),
),
(
CallContextField::ReturnDataLength,
call.return_data_length.into(),
),
] {
state.call_context_read(exec_step, call.call_id, field, value)?;
}
Ok(())
}