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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#![warn(unused_crate_dependencies)]
#![warn(unreachable_pub)]
pub mod compiler;
pub mod pwg;
use crate::pwg::arithmetic::ArithmeticSolver;
use acir::{
circuit::{directives::Directive, opcodes::BlackBoxFuncCall, Circuit, Opcode},
native_types::{Expression, Witness},
BlackBoxFunc,
};
use pwg::block::Blocks;
use std::collections::BTreeMap;
use thiserror::Error;
pub use acir;
pub use acir::FieldElement;
#[derive(PartialEq, Eq, Debug, Error)]
pub enum OpcodeNotSolvable {
#[error("missing assignment for witness index {0}")]
MissingAssignment(u32),
#[error("expression has too many unknowns {0}")]
ExpressionHasTooManyUnknowns(Expression),
}
#[derive(PartialEq, Eq, Debug, Error)]
pub enum OpcodeResolutionError {
#[error("cannot solve opcode: {0}")]
OpcodeNotSolvable(#[from] OpcodeNotSolvable),
#[error("backend does not currently support the {0} opcode. ACVM does not currently have a fallback for this opcode.")]
UnsupportedBlackBoxFunc(BlackBoxFunc),
#[error("could not satisfy all constraints")]
UnsatisfiedConstrain,
#[error("unexpected opcode, expected {0}, but got {1}")]
UnexpectedOpcode(&'static str, BlackBoxFunc),
#[error("expected {0} inputs for function {1}, but got {2}")]
IncorrectNumFunctionArguments(usize, BlackBoxFunc, usize),
}
pub trait Backend: SmartContract + ProofSystemCompiler + PartialWitnessGenerator {}
pub trait PartialWitnessGenerator {
fn solve(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
mut opcode_to_solve: Vec<Opcode>,
) -> Result<(), OpcodeResolutionError> {
let mut unresolved_opcodes: Vec<Opcode> = Vec::new();
let mut blocks = Blocks::default();
while !opcode_to_solve.is_empty() {
unresolved_opcodes.clear();
for opcode in &opcode_to_solve {
let resolution = match opcode {
Opcode::Arithmetic(expr) => ArithmeticSolver::solve(initial_witness, expr),
Opcode::BlackBoxFuncCall(bb_func) => {
Self::solve_black_box_function_call(initial_witness, bb_func)
}
Opcode::Directive(directive) => {
Self::solve_directives(initial_witness, directive)
}
Opcode::Block(id, trace) => blocks.solve(*id, trace, initial_witness),
};
match resolution {
Ok(_) => {
}
Err(OpcodeResolutionError::OpcodeNotSolvable(_)) => {
unresolved_opcodes.push(opcode.clone());
}
Err(err) => return Err(err),
}
}
std::mem::swap(&mut opcode_to_solve, &mut unresolved_opcodes);
}
Ok(())
}
fn solve_black_box_function_call(
initial_witness: &mut BTreeMap<Witness, FieldElement>,
func_call: &BlackBoxFuncCall,
) -> Result<(), OpcodeResolutionError>;
fn all_func_inputs_assigned(
initial_witness: &mut BTreeMap<Witness, FieldElement>,
func_call: &BlackBoxFuncCall,
) -> bool {
!func_call.inputs.iter().any(|input| !initial_witness.contains_key(&input.witness))
}
fn solve_directives(
initial_witness: &mut BTreeMap<Witness, FieldElement>,
directive: &Directive,
) -> Result<(), OpcodeResolutionError> {
pwg::directives::solve_directives(initial_witness, directive)
}
}
pub trait SmartContract {
#[deprecated]
fn eth_contract_from_cs(&self, circuit: Circuit) -> String;
fn eth_contract_from_vk(&self, verification_key: &[u8]) -> String;
}
pub trait ProofSystemCompiler {
fn np_language(&self) -> Language;
fn black_box_function_supported(&self, opcode: &BlackBoxFunc) -> bool;
#[deprecated]
fn prove_with_meta(
&self,
circuit: Circuit,
witness_values: BTreeMap<Witness, FieldElement>,
) -> Vec<u8>;
#[deprecated]
fn verify_from_cs(
&self,
proof: &[u8],
public_inputs: Vec<FieldElement>,
circuit: Circuit,
) -> bool;
fn get_exact_circuit_size(&self, circuit: &Circuit) -> u32;
fn preprocess(&self, circuit: &Circuit) -> (Vec<u8>, Vec<u8>);
fn prove_with_pk(
&self,
circuit: &Circuit,
witness_values: BTreeMap<Witness, FieldElement>,
proving_key: &[u8],
) -> Vec<u8>;
fn verify_with_vk(
&self,
proof: &[u8],
public_inputs: BTreeMap<Witness, FieldElement>,
circuit: &Circuit,
verification_key: &[u8],
) -> bool;
}
#[derive(Debug, Clone)]
pub enum Language {
R1CS,
PLONKCSat { width: usize },
}
pub fn hash_constraint_system(cs: &Circuit) -> [u8; 32] {
let mut bytes = Vec::new();
cs.write(&mut bytes).expect("could not serialize circuit");
use sha2::{digest::FixedOutput, Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(bytes);
hasher.finalize_fixed().into()
}
#[deprecated(
note = "For backwards compatibility, this method allows you to derive _sensible_ defaults for black box function support based on the np language. \n Backends should simply specify what they support."
)]
pub fn default_is_black_box_supported(
language: Language,
) -> compiler::transformers::IsBlackBoxSupported {
fn r1cs_is_supported(_opcode: &BlackBoxFunc) -> bool {
false
}
fn plonk_is_supported(opcode: &BlackBoxFunc) -> bool {
!matches!(opcode, BlackBoxFunc::AES)
}
match language {
Language::R1CS => r1cs_is_supported,
Language::PLONKCSat { .. } => plonk_is_supported,
}
}