forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib-props.rs
More file actions
351 lines (324 loc) · 11.1 KB
/
Copy pathstdlib-props.rs
File metadata and controls
351 lines (324 loc) · 11.1 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
mod common;
use std::cell::RefCell;
use std::collections::BTreeMap;
use acvm::{FieldElement, acir::native_types::WitnessStack};
use nargo::{foreign_calls::DefaultForeignCallBuilder, ops::execute_program};
use noirc_abi::input_parser::InputValue;
use proptest::prelude::*;
/// Inputs and expected output of a snippet encoded in ABI format.
#[derive(Debug)]
struct SnippetInputOutput {
pub description: String,
pub inputs: BTreeMap<String, InputValue>,
pub expected_output: InputValue,
}
impl SnippetInputOutput {
fn new(inputs: Vec<(&str, InputValue)>, output: InputValue) -> Self {
Self {
description: "".to_string(),
inputs: inputs.into_iter().map(|(k, v)| (k.to_string(), v)).collect(),
expected_output: output,
}
}
}
/// Compile a snippet and run property tests against it by generating random input/output pairs
/// according to the strategy, executing the snippet with the input, and asserting that the
/// output it returns is the one we expect.
fn run_snippet_proptest(
source: String,
force_brillig: bool,
strategy: BoxedStrategy<SnippetInputOutput>,
) {
let program = match common::prepare_and_compile_snippet(source.clone(), force_brillig) {
Ok((program, _)) => program,
Err(e) => panic!("failed to compile program; brillig = {force_brillig}:\n{source}\n{e:?}"),
};
let blackbox_solver = bn254_blackbox_solver::Bn254BlackBoxSolver;
let foreign_call_executor = RefCell::new(DefaultForeignCallBuilder::default().build());
// Generate multiple input/output
proptest!(ProptestConfig::with_cases(100), |(io in strategy)| {
let initial_witness = program.abi.encode(&io.inputs, None).expect("failed to encode");
let mut foreign_call_executor = foreign_call_executor.borrow_mut();
let witness_stack: WitnessStack<FieldElement> = execute_program(
&program.program,
initial_witness,
&blackbox_solver,
&mut *foreign_call_executor,
)
.expect("failed to execute");
let main_witness = witness_stack.peek().expect("should have return value on witness stack");
let main_witness = &main_witness.witness;
let (_, return_value) = program.abi.decode(main_witness).expect("failed to decode");
let return_value = return_value.expect("should decode a return value");
prop_assert_eq!(return_value, io.expected_output, "{}", io.description);
});
}
/// This is just a simple test to check that property testing works.
#[test]
fn fuzz_basic() {
let program = "fn main(init: u32) -> pub u32 {
let mut x = init;
for i in 0 .. 6 {
x += i;
}
x
}";
let strategy = any::<u32>()
.prop_map(|init| {
let init = init / 2;
SnippetInputOutput::new(
vec![("init", InputValue::Field(init.into()))],
InputValue::Field((init + 15).into()),
)
})
.boxed();
run_snippet_proptest(program.to_string(), false, strategy);
}
fn make_field_cast_test(size: u32, signed: bool) -> String {
if signed {
format!(
"fn main(x: i{size}) -> pub Field {{
x as u{size} as Field
}}
"
)
} else {
format!(
"fn main(x: u{size}) -> pub Field {{
x as Field
}}"
)
}
}
fn get_unsigned_strategies() -> Vec<(u32, BoxedStrategy<SnippetInputOutput>)> {
let strategy_u8 = any::<u8>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_u16 = any::<u16>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_u32 = any::<u32>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_u64 = any::<u64>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_u128 = any::<u128>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(x.into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
vec![
(8, strategy_u8),
(16, strategy_u16),
(32, strategy_u32),
(64, strategy_u64),
(128, strategy_u128),
]
}
fn get_signed_strategies() -> Vec<(u32, BoxedStrategy<SnippetInputOutput>)> {
let strategy_i8 = any::<u8>()
.prop_map(|mut x| {
if x == 128 {
x = 0;
}
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_i16 = any::<u16>()
.prop_map(|mut x| {
if x == 32768 {
x = 0;
}
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
let strategy_i32 = any::<u32>()
.prop_map(|mut x| {
if x == 2147483648 {
x = 0;
}
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(0_u128.into()),
)
})
.boxed();
vec![(8, strategy_i8), (16, strategy_i16), (32, strategy_i32)]
}
fn get_truncate_strategies() -> Vec<(u32, BoxedStrategy<SnippetInputOutput>)> {
let strategy_u16 = any::<u16>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(u128::from(x).into()),
)
})
.boxed();
let strategy_u32 = any::<u32>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(u128::from(x).into()),
)
})
.boxed();
let strategy_u64 = any::<u64>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(u128::from(x).into()))],
InputValue::Field(u128::from(x).into()),
)
})
.boxed();
let strategy_u128 = any::<u128>()
.prop_map(|x| {
SnippetInputOutput::new(
vec![("x", InputValue::Field(x.into()))],
InputValue::Field(x.into()),
)
})
.boxed();
vec![(16, strategy_u16), (32, strategy_u32), (64, strategy_u64), (128, strategy_u128)]
}
/// The tests `fuzz_zero_extend()`, `fuzz_signed_unsigned_same_size()`, `fuzz_sign_extend()` and `fuzz_truncate()`
/// ensure that casting between integer types is correct, assuming casting to Field is correct.
/// Casting to Field is validated with the `fuzz_field_cast()` test.
/// Any casting between integer types will use a combination of: no-op, zero extension, sign extension, or truncation.
/// Testing these 4 primitives should be enough to guarantee that casting between any integer types is correct.
///
/// Check that casting to Field is a no-op
#[test]
fn fuzz_field_cast() {
for (size, strategy) in &get_truncate_strategies() {
if *size < 128 {
let signed_i = make_field_cast_test(*size, true);
run_snippet_proptest(signed_i.clone(), false, strategy.clone());
run_snippet_proptest(signed_i, true, strategy.clone());
}
let unsigned_i = make_field_cast_test(*size, false);
run_snippet_proptest(unsigned_i.clone(), false, strategy.clone());
run_snippet_proptest(unsigned_i, true, strategy.clone());
}
}
fn make_zero_extend_test(in_size: u32, out_size: u32) -> String {
format!(
"fn main(x: u{in_size}) -> pub Field {{
let y = x as u{out_size};
y as Field - x as Field
}}"
)
}
/// Check that up-casting unsigned types is correct
#[test]
fn fuzz_zero_extend() {
let strategies = get_unsigned_strategies();
// zero extend 8, 16, 32, 64, 128 bits
for (size, strategy) in &strategies {
for i in &[8, 16, 32, 64, 128] {
if *i >= *size {
let unsigned_j_i = make_zero_extend_test(*size, *i);
run_snippet_proptest(unsigned_j_i.clone(), false, strategy.clone());
run_snippet_proptest(unsigned_j_i, true, strategy.clone());
}
}
}
}
fn make_sign_unsigned_test(size: u32) -> String {
format!(
"fn main(x: i{size}) -> pub Field {{
let y = x as u{size};
let z = y as i{size};
assert(z == x);
0
}}"
)
}
/// Check that signed to unsigned and unsigned to signed, with the same bit size, do not change the inner value.
#[test]
fn fuzz_signed_unsigned_same_size() {
let strategies = get_unsigned_strategies();
for (size, strategy) in &strategies {
if *size < 128 {
let signed_unsigned_i_i = make_sign_unsigned_test(*size);
run_snippet_proptest(signed_unsigned_i_i.clone(), false, strategy.clone());
run_snippet_proptest(signed_unsigned_i_i, true, strategy.clone());
}
}
}
fn make_sign_extend_test(in_size: u32, out_size: u32) -> String {
format!(
"fn main(x: i{in_size}) -> pub Field {{
let neg = -x;
let y = x as i{out_size};
let neg_y = neg as i{out_size};
(neg_y+y) as u{out_size} as Field
}}"
)
}
#[test]
// Test sign extension
fn fuzz_sign_extend() {
for (size, strategy) in &get_signed_strategies() {
for i in [16, 32, 64] {
if i > *size {
// sign extend
let signed_i_i = make_sign_extend_test(*size, i);
run_snippet_proptest(signed_i_i.clone(), false, strategy.clone());
run_snippet_proptest(signed_i_i, true, strategy.clone());
}
}
}
}
fn make_truncate_test(size: u32, truncate: u32) -> String {
let max: u128 = 1 << truncate;
format!(
"fn main(x: u{size}) -> pub u{size} {{
let y = x as u{truncate};
let q = (x as Field - y as Field)/{max};
(q as u{size})*{max} + y as u{size}
}}"
)
}
/// Check that truncation between unsigned types is correct
#[test]
fn fuzz_truncate() {
for (size, strategy) in &get_truncate_strategies() {
for i in [8, 16, 32, 64] {
if i < *size {
let unsigned_j_i = make_truncate_test(*size, i);
run_snippet_proptest(unsigned_j_i.clone(), false, strategy.clone());
run_snippet_proptest(unsigned_j_i, true, strategy.clone());
}
}
}
}