-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtoy_pasta.rs
125 lines (108 loc) · 3.7 KB
/
toy_pasta.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{collections::HashMap, env::current_dir, time::Instant};
use nova_scotia::{
circom::reader::load_r1cs, create_public_params, create_recursive_circuit, FileLocation, F, S,
};
use nova_snark::{
provider,
traits::{circuit::StepCircuit, Group},
CompressedSNARK, PublicParams,
};
use serde_json::json;
fn run_test(circuit_filepath: String, witness_gen_filepath: String) {
type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point;
println!(
"Running test with witness generator: {} and group: {}",
witness_gen_filepath,
std::any::type_name::<G1>()
);
let iteration_count = 5;
let root = current_dir().unwrap();
let circuit_file = root.join(circuit_filepath);
let r1cs = load_r1cs::<G1, G2>(&FileLocation::PathBuf(circuit_file));
let witness_generator_file = root.join(witness_gen_filepath);
let mut private_inputs = Vec::new();
for i in 0..iteration_count {
let mut private_input = HashMap::new();
private_input.insert("adder".to_string(), json!(i));
private_inputs.push(private_input);
}
let start_public_input = [F::<G1>::from(10), F::<G1>::from(10)];
let pp: PublicParams<G1, G2, _, _> = create_public_params(r1cs.clone());
println!(
"Number of constraints per step (primary circuit): {}",
pp.num_constraints().0
);
println!(
"Number of constraints per step (secondary circuit): {}",
pp.num_constraints().1
);
println!(
"Number of variables per step (primary circuit): {}",
pp.num_variables().0
);
println!(
"Number of variables per step (secondary circuit): {}",
pp.num_variables().1
);
println!("Creating a RecursiveSNARK...");
let start = Instant::now();
let recursive_snark = create_recursive_circuit(
FileLocation::PathBuf(witness_generator_file),
r1cs,
private_inputs,
start_public_input.to_vec(),
&pp,
)
.unwrap();
println!("RecursiveSNARK creation took {:?}", start.elapsed());
// TODO: empty?
let z0_secondary = [F::<G2>::from(0)];
// verify the recursive SNARK
println!("Verifying a RecursiveSNARK...");
let start = Instant::now();
let res = recursive_snark.verify(&pp, iteration_count, &start_public_input, &z0_secondary);
println!(
"RecursiveSNARK::verify: {:?}, took {:?}",
res,
start.elapsed()
);
assert!(res.is_ok());
// produce a compressed SNARK
println!("Generating a CompressedSNARK using Spartan with IPA-PC...");
let start = Instant::now();
let (pk, vk) = CompressedSNARK::<_, _, _, _, S<G1>, S<G2>>::setup(&pp).unwrap();
let res = CompressedSNARK::<_, _, _, _, S<G1>, S<G2>>::prove(&pp, &pk, &recursive_snark);
println!(
"CompressedSNARK::prove: {:?}, took {:?}",
res.is_ok(),
start.elapsed()
);
assert!(res.is_ok());
let compressed_snark = res.unwrap();
// verify the compressed SNARK
println!("Verifying a CompressedSNARK...");
let start = Instant::now();
let res = compressed_snark.verify(
&vk,
iteration_count,
start_public_input.to_vec(),
z0_secondary.to_vec(),
);
println!(
"CompressedSNARK::verify: {:?}, took {:?}",
res.is_ok(),
start.elapsed()
);
assert!(res.is_ok());
}
fn main() {
let group_name = "pasta";
let circuit_filepath = format!("examples/toy/{}/toy.r1cs", group_name);
for witness_gen_filepath in [
format!("examples/toy/{}/toy_cpp/toy", group_name),
format!("examples/toy/{}/toy_js/toy.wasm", group_name),
] {
run_test(circuit_filepath.clone(), witness_gen_filepath);
}
}