forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.rs
More file actions
634 lines (523 loc) · 22.3 KB
/
execute.rs
File metadata and controls
634 lines (523 loc) · 22.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#[allow(unused_imports)]
#[cfg(test)]
mod tests {
use acvm::FieldElement;
use acvm::acir::circuit::Program;
// Some of these imports are consumed by the injected tests
use assert_cmd::prelude::*;
use base64::Engine;
use insta::assert_snapshot;
use insta::internals::Content;
use insta::internals::Redaction;
use noirc_artifacts::contract::ContractArtifact;
use noirc_artifacts::program::ProgramArtifact;
use predicates::prelude::*;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::fs;
use std::io::BufWriter;
use std::path::{Path, PathBuf};
use std::process::Command;
use super::*;
// Utilities to keep the test matrix labels more intuitive.
#[derive(Debug, Clone, Copy)]
struct ForceBrillig(pub bool);
#[derive(Debug, Clone, Copy)]
struct Inliner(pub i64);
fn setup_nargo(
test_program_dir: &Path,
test_command: &str,
force_brillig: ForceBrillig,
inliner_aggressiveness: Inliner,
) -> Command {
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg(test_command).arg("--force");
nargo.arg("--inliner-aggressiveness").arg(inliner_aggressiveness.0.to_string());
// Allow more bytecode in exchange to catch illegal states.
nargo.arg("--enable-brillig-debug-assertions");
// Enable pedantic solving
let skip_pedantic_solving = [
// TODO(https://github.com/noir-lang/noir/issues/8098): all of these are failing with:
// ```
// Failed to solve program:
// \'Failed to solve blackbox function: embedded_curve_add, reason: Infinite input: embedded_curve_add(infinity, infinity)\'
// ```
"execution_success/multi_scalar_mul",
"execution_success/regression_5045",
"execution_success/regression_7744",
];
if !skip_pedantic_solving
.into_iter()
.any(|test_to_skip| test_program_dir.ends_with(test_to_skip))
{
nargo.arg("--pedantic-solving");
}
// Enable enums and ownership as unstable features
nargo.arg("-Zenums");
if force_brillig.0 {
{
nargo.arg("--force-brillig");
// Set the maximum increase so that part of the optimization is exercised (it might fail).
nargo.arg("--max-bytecode-increase-percent");
nargo.arg("50");
}
}
nargo
}
fn remove_noise_lines(string: String) -> String {
string
.lines()
.filter(|line| {
!line.contains("Witness saved to")
&& !line.contains("Circuit witness successfully solved")
&& !line.contains("Waiting for lock")
})
.collect::<Vec<&str>>()
.join("\n")
}
fn delete_test_program_dir_occurrences(string: String, test_program_dir: &Path) -> String {
// Assuming `test_program_dir` is "/projects/noir/test_programs/compile_failure/some_program"...
// `test_program_base_dir` is "/projects/noir/test_programs"
let test_program_base_dir = test_program_dir.parent().unwrap().parent().unwrap();
// `root_dir` is "/projects/noir"
let root_dir = test_program_base_dir.parent().unwrap();
// Here we turn the paths into strings and ensure they end with a `/`
let mut test_program_base_dir = test_program_base_dir.to_string_lossy().to_string();
if !test_program_base_dir.ends_with('/') {
test_program_base_dir.push('/');
}
let mut test_program_dir = test_program_dir.to_string_lossy().to_string();
if !test_program_dir.ends_with('/') {
test_program_dir.push('/');
}
// `test_program_dir_without_root is "test_programs/compile_failure".
// This one is needed because tests might run from the root of the project and paths
// will end up starting with "test_programs/compile_failure/...".
let test_program_dir_without_root =
test_program_dir.strip_prefix(&root_dir.to_string_lossy().to_string()).unwrap();
let test_program_dir_without_root = test_program_dir_without_root
.strip_prefix('/')
.unwrap_or(test_program_dir_without_root);
// We replace all of these:
// - test_program_dir ("/projects/noir/test_programs/compile_failure/foo")
// - test_program_base_dir ("/projects/noir/test_programs")
// - test_program_dir_without_root ("test_programs/compile_failure")
string
.lines()
.map(|line| {
line.replace(&test_program_dir, "")
.replace(&test_program_base_dir, "test_programs/")
.replace(test_program_dir_without_root, "")
})
.collect::<Vec<String>>()
.join("\n")
}
fn execution_success(
mut nargo: Command,
test_program_dir: PathBuf,
check_stdout: bool,
check_artifact: bool,
force_brillig: ForceBrillig,
inliner: Inliner,
) {
let target_dir = tempfile::tempdir().unwrap().keep();
nargo.arg(format!("--target-dir={}", target_dir.to_string_lossy()));
nargo.assert().success();
let mut has_circuit_output = false;
if check_stdout {
let output = nargo.output().unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
has_circuit_output = stdout.contains("Circuit output:");
let stdout = remove_noise_lines(stdout);
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = "stdout";
insta::with_settings!(
{
snapshot_path => format!("./snapshots/execution_success/{test_name}")
},
{
insta::assert_snapshot!(snapshot_name, stdout);
});
}
if has_circuit_output {
let prover_toml_path = find_prover_toml_in_dir(&test_program_dir).expect(
"Expected a Prover.toml file to exist because the program produced an output",
);
let prover_toml_contents =
fs::read_to_string(prover_toml_path).expect("Failed to read Prover.toml");
let prover_toml: toml::Value =
toml::from_str(&prover_toml_contents).expect("Failed to parse Prover.toml");
let toml::Value::Table(table) = prover_toml else {
panic!("Expected Prover.toml to be a table");
};
if !table.contains_key("return") {
panic!(
"Expected Prover.toml to contain a `return` key because the program produced an output"
);
}
}
if check_artifact {
check_program_artifact(
"execution_success",
&test_program_dir,
&target_dir,
force_brillig,
inliner,
);
}
}
fn execution_failure(mut nargo: Command) {
nargo
.assert()
.failure()
.stderr(predicate::str::contains("The application panicked (crashed).").not());
}
fn execution_panic(mut nargo: Command) {
nargo
.assert()
.failure()
.stderr(predicate::str::contains("The application panicked (crashed)."));
}
fn noir_test_success(mut nargo: Command) {
nargo.assert().success();
}
fn noir_test_failure(mut nargo: Command) {
nargo
.assert()
.failure()
.stderr(predicate::str::contains("The application panicked (crashed).").not());
}
fn compile_success_empty(
mut nargo: Command,
test_program_dir: PathBuf,
no_warnings: bool,
force_brillig: ForceBrillig,
inliner: Inliner,
) {
let target_dir = test_program_dir
.join(format!("target_force_brillig_{}_inliner_{}", force_brillig.0, inliner.0));
nargo.arg(format!("--target-dir={}", target_dir.to_string_lossy()));
nargo.arg("--json");
let output = nargo.output().expect("Failed to execute command");
if !output.status.success() {
{
panic!(
"`nargo info` failed with: {}",
String::from_utf8(output.stderr).unwrap_or_default()
);
}
}
if no_warnings {
nargo.assert().success().stderr(predicate::str::contains("warning:").not());
}
// `compile_success_empty` tests should be able to compile down to an empty circuit.
let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {
{
panic!(
"JSON was not well-formatted {:?}\n\n{:?}",
e,
std::str::from_utf8(&output.stdout)
)
}
});
let num_opcodes = &json["programs"][0]["functions"][0]["opcodes"];
assert_eq!(
num_opcodes.as_u64().expect("number of opcodes should fit in a u64"),
0,
"expected the number of opcodes to be 0"
);
check_program_artifact(
"compile_success_empty",
&test_program_dir,
&target_dir,
force_brillig,
inliner,
);
}
fn compile_success_contract(
mut nargo: Command,
test_program_dir: PathBuf,
force_brillig: ForceBrillig,
inliner: Inliner,
) {
let target_dir = test_program_dir
.join(format!("target_force_brillig_{}_inliner_{}", force_brillig.0, inliner.0));
nargo.arg(format!("--target-dir={}", target_dir.to_string_lossy()));
nargo.assert().success().stderr(predicate::str::contains("warning:").not());
check_contract_artifact(
"compile_success_contract",
&test_program_dir,
&target_dir,
force_brillig,
inliner,
);
}
fn compile_success_no_bug(mut nargo: Command) {
nargo.assert().success().stderr(predicate::str::contains("bug:").not());
}
fn compile_success_with_bug(mut nargo: Command, test_program_dir: PathBuf) {
nargo.assert().success().stderr(predicate::str::contains("bug:"));
let output = nargo.output().unwrap();
let stderr = String::from_utf8(output.stderr).unwrap();
let stderr = remove_noise_lines(stderr);
let stderr = delete_test_program_dir_occurrences(stderr, &test_program_dir);
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = "stderr";
insta::with_settings!(
{
snapshot_path => format!("./snapshots/compile_success_with_bug/{test_name}")
},
{
insta::assert_snapshot!(snapshot_name, stderr);
});
}
fn compile_failure(mut nargo: Command, test_program_dir: PathBuf) {
nargo
.assert()
.failure()
.stderr(predicate::str::contains("The application panicked (crashed).").not());
let output = nargo.output().unwrap();
let stderr = String::from_utf8(output.stderr).unwrap();
let stderr = remove_noise_lines(stderr);
let stderr = delete_test_program_dir_occurrences(stderr, &test_program_dir);
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = "stderr";
insta::with_settings!(
{
snapshot_path => format!("./snapshots/compile_failure/{test_name}")
},
{
insta::assert_snapshot!(snapshot_name, stderr);
});
}
fn interpret_execution_success(mut nargo: Command) {
nargo.assert().success();
}
fn nargo_expand_execute(test_program_dir: PathBuf) {
// First run `nargo execute` on the original code to get the output
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir.clone());
nargo.arg("execute").arg("--force").arg("--disable-comptime-printing");
// Enable enums as an unstable feature
nargo.arg("-Zenums");
// Enable pedantic solving
nargo.arg("--pedantic-solving");
nargo.assert().success();
let original_output = nargo.output().unwrap();
let original_output: String = String::from_utf8(original_output.stdout).unwrap();
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir.clone());
nargo.arg("expand").arg("--force").arg("--disable-comptime-printing");
// Enable enums as an unstable feature
nargo.arg("-Zenums");
// Enable pedantic solving
nargo.arg("--pedantic-solving");
nargo.assert().success();
let expanded_code = nargo.output().unwrap();
let expanded_code: String = String::from_utf8(expanded_code.stdout).unwrap();
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = "expanded";
insta::with_settings!(
{
snapshot_path => format!("./snapshots/execution_success/{test_name}")
},
{
insta::assert_snapshot!(snapshot_name, expanded_code);
});
// Create a new directory where we'll put the expanded code
let temp_dir = tempfile::tempdir().unwrap().keep();
// Copy everything from the original directory to the new directory
// (because some dependencies might be there and might be needed for the expanded code to work)
copy_dir_all(test_program_dir.clone(), temp_dir.clone()).unwrap();
// Create a main file for the expanded code
fs::write(temp_dir.join("src").join("main.nr"), expanded_code).unwrap();
// First check if `nargo fmt` works on the expanded code. If not, it means the code is not valid.
run_nargo_fmt(temp_dir.clone());
// Now we can run `nargo execute` on the expanded code
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(temp_dir);
nargo.arg("execute").arg("--force").arg("--disable-comptime-printing");
// Enable enums as an unstable feature
nargo.arg("-Zenums");
// Enable pedantic solving
nargo.arg("--pedantic-solving");
nargo.assert().success();
let expanded_output = nargo.output().unwrap();
let expanded_output: String = String::from_utf8(expanded_output.stdout).unwrap();
let original_output = remove_noise_lines(original_output);
let expanded_output = remove_noise_lines(expanded_output);
assert_eq!(original_output, expanded_output);
}
fn nargo_expand_compile(test_program_dir: PathBuf, prefix: &'static str) {
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir.clone());
nargo.arg("expand").arg("--force").arg("--disable-comptime-printing");
// Enable enums as an unstable feature
nargo.arg("-Zenums");
// Enable pedantic solving
nargo.arg("--pedantic-solving");
nargo.assert().success();
let expanded_code = nargo.output().unwrap();
let expanded_code: String = String::from_utf8(expanded_code.stdout).unwrap();
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = "expanded";
insta::with_settings!(
{
snapshot_path => format!("./snapshots/{prefix}/{test_name}")
},
{
insta::assert_snapshot!(snapshot_name, expanded_code);
});
// Create a new directory where we'll put the expanded code
let temp_dir = tempfile::tempdir().unwrap().keep();
// Copy everything from the original directory to the new directory
// (because some dependencies might be there and might be needed for the expanded code to work)
copy_dir_all(test_program_dir.clone(), temp_dir.clone()).unwrap();
// Create a main file for the expanded code
fs::write(temp_dir.join("src").join("main.nr"), expanded_code).unwrap();
// First check if `nargo fmt` works on the expanded code. If not, it means the code is not valid.
run_nargo_fmt(temp_dir.clone());
// Now we can run `nargo compile` on the expanded code
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(temp_dir);
nargo.arg("compile").arg("--force");
// Enable enums as an unstable feature
nargo.arg("-Zenums");
// Enable pedantic solving
nargo.arg("--pedantic-solving");
nargo.assert().success();
}
fn run_nargo_fmt(target_dir: PathBuf) {
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(target_dir);
nargo.arg("fmt");
nargo.assert().success();
}
fn check_program_artifact(
prefix: &'static str,
test_program_dir: &Path,
target_dir: &PathBuf,
force_brillig: ForceBrillig,
inliner: Inliner,
) {
let artifact_filename =
find_program_artifact_in_dir(target_dir).expect("Expected an artifact to exist");
let artifact_file = fs::File::open(&artifact_filename).unwrap();
let artifact: ProgramArtifact = serde_json::from_reader(artifact_file).unwrap();
let _ = fs::remove_dir_all(target_dir);
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
if test_name == "workspace" {
// workspace outputs multiple artifacts so we get a non-deterministic result.
return;
}
let snapshot_name = format!("force_brillig_{}_inliner_{}", force_brillig.0, inliner.0);
insta::with_settings!(
{
snapshot_path => format!("./snapshots/{prefix}/{test_name}")
},
{
insta::assert_json_snapshot!(snapshot_name, artifact, {
".noir_version" => "[noir_version]",
".hash" => "[hash]",
".bytecode" => insta::dynamic_redaction(|value, _path| {
// assert that the value looks like a uuid here
let bytecode_b64 = value.as_str().unwrap();
let bytecode = base64::engine::general_purpose::STANDARD
.decode(bytecode_b64)
.unwrap();
let program = Program::<FieldElement>::deserialize_program(&bytecode).unwrap();
Content::Seq(program.to_string().split("\n").filter(|line: &&str| !line.is_empty()).map(Content::from).collect::<Vec<Content>>())
}),
".file_map.**.path" => file_map_path_redaction(),
});
});
}
fn check_contract_artifact(
prefix: &'static str,
test_program_dir: &Path,
target_dir: &PathBuf,
force_brillig: ForceBrillig,
inliner: Inliner,
) {
let artifact_filename =
find_program_artifact_in_dir(target_dir).expect("Expected an artifact to exist");
let artifact_file = fs::File::open(&artifact_filename).unwrap();
let artifact: ContractArtifact = serde_json::from_reader(artifact_file).unwrap();
fs::remove_dir_all(target_dir).expect("Could not remove target dir");
let test_name = test_program_dir.file_name().unwrap().to_string_lossy().to_string();
let snapshot_name = format!("force_brillig_{}_inliner_{}", force_brillig.0, inliner.0);
insta::with_settings!(
{
snapshot_path => format!("./snapshots/{prefix}/{test_name}")
},
{
insta::assert_json_snapshot!(snapshot_name, artifact, {
".noir_version" => "[noir_version]",
".functions[].hash" => "[hash]",
".file_map.**.path" => file_map_path_redaction(),
});
});
}
fn file_map_path_redaction() -> Redaction {
insta::dynamic_redaction(|value, _path| {
// Some paths are absolute: clear those out.
let value = value.as_str().expect("Expected a string value in a path entry");
if value.starts_with("/") { String::new() } else { value.to_string() }
})
}
fn find_program_artifact_in_dir(dir: &PathBuf) -> Option<PathBuf> {
if !dir.exists() {
return None;
}
for entry in fs::read_dir(dir).unwrap() {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
if path.extension().is_none_or(|ext| ext != "json") {
continue;
};
return Some(path);
}
None
}
fn find_prover_toml_in_dir(dir: &PathBuf) -> Option<PathBuf> {
if !dir.exists() {
return None;
}
for entry in fs::read_dir(dir).unwrap() {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
if entry.file_type().is_ok_and(|file_type| file_type.is_dir()) {
if let Some(prover_toml) = find_prover_toml_in_dir(&path) {
return Some(prover_toml);
}
}
if path.file_name().is_none_or(|name| name != "Prover.toml") {
continue;
};
return Some(path);
}
None
}
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
{
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
}
// include tests generated by `build.rs`
include!(concat!(env!("OUT_DIR"), "/execute.rs"));
}