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
use crate::{OpcodeNotSolvable, OpcodeResolutionError};
use acir::{
native_types::{Expression, Witness},
FieldElement,
};
use std::collections::BTreeMap;
use self::arithmetic::ArithmeticSolver;
pub mod arithmetic;
pub mod directives;
pub mod block;
pub mod hash;
pub mod logic;
pub mod range;
pub mod signature;
pub mod sorting;
pub fn witness_to_value(
initial_witness: &BTreeMap<Witness, FieldElement>,
witness: Witness,
) -> Result<&FieldElement, OpcodeResolutionError> {
match initial_witness.get(&witness) {
Some(value) => Ok(value),
None => Err(OpcodeNotSolvable::MissingAssignment(witness.0).into()),
}
}
pub fn get_value(
expr: &Expression,
initial_witness: &BTreeMap<Witness, FieldElement>,
) -> Result<FieldElement, OpcodeResolutionError> {
let expr = ArithmeticSolver::evaluate(expr, initial_witness);
match expr.to_const() {
Some(value) => Ok(value),
None => {
Err(OpcodeResolutionError::OpcodeNotSolvable(OpcodeNotSolvable::MissingAssignment(
ArithmeticSolver::any_witness_from_expression(&expr).unwrap().0,
)))
}
}
}
fn insert_value(
witness: &Witness,
value_to_insert: FieldElement,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
) -> Result<(), OpcodeResolutionError> {
let optional_old_value = initial_witness.insert(*witness, value_to_insert);
let old_value = match optional_old_value {
Some(old_value) => old_value,
None => return Ok(()),
};
if old_value != value_to_insert {
return Err(OpcodeResolutionError::UnsatisfiedConstrain);
}
Ok(())
}