-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathmod.rs
107 lines (94 loc) · 3.26 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
mod toml;
use std::{collections::BTreeMap, path::Path};
use acvm::FieldElement;
use serde::Serialize;
use crate::errors::InputParserError;
use crate::{Abi, AbiType};
/// This is what all formats eventually transform into
/// For example, a toml file will parse into TomlTypes
/// and those TomlTypes will be mapped to Value
#[derive(Debug, Clone, Serialize, PartialEq)]
pub enum InputValue {
Field(FieldElement),
Vec(Vec<FieldElement>),
String(String),
Struct(BTreeMap<String, InputValue>),
}
impl InputValue {
/// Checks whether the ABI type matches the InputValue type
/// and also their arity
pub fn matches_abi(&self, abi_param: &AbiType) -> bool {
match (self, abi_param) {
(InputValue::Field(_), AbiType::Field) => true,
(InputValue::Field(field_element), AbiType::Integer { width, .. }) => {
field_element.num_bits() <= *width
}
(InputValue::Field(field_element), AbiType::Boolean) => {
field_element.is_one() || field_element.is_zero()
}
(InputValue::Vec(field_elements), AbiType::Array { length, typ, .. }) => {
if field_elements.len() != *length as usize {
return false;
}
// Check that all of the array's elements' values match the ABI as well.
field_elements
.iter()
.all(|field_element| Self::Field(*field_element).matches_abi(typ))
}
(InputValue::String(string), AbiType::String { length }) => {
string.len() == *length as usize
}
(InputValue::Struct(map), AbiType::Struct { fields, .. }) => {
if map.len() != fields.len() {
return false;
}
// Check that all of the struct's fields' values match the ABI as well.
map.iter().all(|(field_name, field_value)| {
if let Some(field_type) = fields.get(field_name) {
field_value.matches_abi(field_type)
} else {
false
}
})
}
// All other InputValue-AbiType combinations are fundamentally incompatible.
_ => false,
}
}
}
/// Parses the initial Witness Values that are needed to seed the
/// Partial Witness generator
pub trait InitialWitnessParser {
fn parse_initial_witness<P: AsRef<Path>>(&self, path: P) -> BTreeMap<String, InputValue>;
}
/// The different formats that are supported when parsing
/// the initial witness values
pub enum Format {
Toml,
}
impl Format {
pub fn ext(&self) -> &'static str {
match self {
Format::Toml => "toml",
}
}
}
impl Format {
pub fn parse(
&self,
input_string: &str,
abi: &Abi,
) -> Result<BTreeMap<String, InputValue>, InputParserError> {
match self {
Format::Toml => toml::parse_toml(input_string, abi),
}
}
pub fn serialize(
&self,
w_map: &BTreeMap<String, InputValue>,
) -> Result<String, InputParserError> {
match self {
Format::Toml => toml::serialize_to_toml(w_map),
}
}
}