-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmod.rs
99 lines (88 loc) · 3.24 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
use int::IntStrategy;
use prop::collection::vec;
use proptest::prelude::*;
use acvm::{AcirField, FieldElement};
use noirc_abi::{input_parser::InputValue, Abi, AbiType, InputMap, Sign};
use std::collections::{BTreeMap, HashSet};
use uint::UintStrategy;
mod int;
mod uint;
pub(super) fn arb_value_from_abi_type(
abi_type: &AbiType,
dictionary: HashSet<FieldElement>,
) -> SBoxedStrategy<InputValue> {
match abi_type {
AbiType::Field => vec(any::<u8>(), 32)
.prop_map(|bytes| InputValue::Field(FieldElement::from_be_bytes_reduce(&bytes)))
.sboxed(),
AbiType::Integer { width, sign } if sign == &Sign::Unsigned => {
UintStrategy::new(*width as usize, dictionary)
.prop_map(|uint| InputValue::Field(uint.into()))
.sboxed()
}
AbiType::Integer { width, .. } => {
let shift = 2i128.pow(*width);
IntStrategy::new(*width as usize)
.prop_map(move |mut int| {
if int < 0 {
int += shift
}
InputValue::Field(int.into())
})
.sboxed()
}
AbiType::Boolean => {
any::<bool>().prop_map(|val| InputValue::Field(FieldElement::from(val))).sboxed()
}
AbiType::String { length } => {
// Strings only allow ASCII characters as each character must be able to be represented by a single byte.
let string_regex = format!("[[:ascii:]]{{{length}}}");
proptest::string::string_regex(&string_regex)
.expect("parsing of regex should always succeed")
.prop_map(InputValue::String)
.sboxed()
}
AbiType::Array { length, typ } => {
let length = *length as usize;
let elements = vec(arb_value_from_abi_type(typ, dictionary), length..=length);
elements.prop_map(InputValue::Vec).sboxed()
}
AbiType::Struct { fields, .. } => {
let fields: Vec<SBoxedStrategy<(String, InputValue)>> = fields
.iter()
.map(|(name, typ)| {
(Just(name.clone()), arb_value_from_abi_type(typ, dictionary.clone())).sboxed()
})
.collect();
fields
.prop_map(|fields| {
let fields: BTreeMap<_, _> = fields.into_iter().collect();
InputValue::Struct(fields)
})
.sboxed()
}
AbiType::Tuple { fields } => {
let fields: Vec<_> =
fields.iter().map(|typ| arb_value_from_abi_type(typ, dictionary.clone())).collect();
fields.prop_map(InputValue::Vec).sboxed()
}
}
}
pub(super) fn arb_input_map(
abi: &Abi,
dictionary: HashSet<FieldElement>,
) -> BoxedStrategy<InputMap> {
let values: Vec<_> = abi
.parameters
.iter()
.map(|param| {
(Just(param.name.clone()), arb_value_from_abi_type(¶m.typ, dictionary.clone()))
})
.collect();
values
.prop_map(|values| {
let input_map: InputMap = values.into_iter().collect();
input_map
})
.boxed()
}