-
Notifications
You must be signed in to change notification settings - Fork 14
/
lib.rs
109 lines (88 loc) · 3.74 KB
/
lib.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
use std::{env, fs::File};
use std::process;
use std::io::Read;
use std::fs;
use bellman::{Circuit, groth16};
use bellman::groth16::{Proof, Parameters};
use bls12_381::{Bls12, Scalar};
use clap::Parser;
use rand::rngs::OsRng;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct BinaryArgs {
#[arg(short, long)]
pub input: String,
#[arg(short, long)]
pub phase: String,
#[arg(short, long)]
pub params: Option<String>,
#[arg(short, long)]
pub proof: Option<String>,
}
pub fn read_file_from_env_var(env_var_name: String) -> String {
let input_file = env::var(env_var_name.clone()).unwrap_or_else(|_| {
println!("Please set the {} environment variable to point to the input file", env_var_name);
process::exit(1);
});
return read_file_contents(input_file);
}
pub fn read_env_variable(env_var_name: String) -> String {
let variable_str = env::var(env_var_name.clone()).unwrap_or_else(|_| {
println!("Please set the {} environment variable", env_var_name);
process::exit(1);
});
return variable_str;
}
pub fn read_file_contents(file_name: String) -> String {
let mut file = File::open(file_name).expect("Cannot load file");
let mut file_str = String::new();
file.read_to_string(&mut file_str).expect("Cannot read file");
return file_str;
}
pub fn measure_size_in_bytes(proof: &Proof<Bls12>) -> usize {
// TODO: Should we serialize the proof in another format?
// Serialize data and save it to a temporary file
let temp_file_path = "temp_file.bin";
let mut file = File::create(temp_file_path).expect("Could not create temp file");
proof.write(&mut file).expect("Could not write proof to file");
// Measure the size of the file
let file_size: usize = fs::metadata(&temp_file_path).expect("Cannot read the size of temp file").len() as usize;
// Convert file size to MB
let size_in_mb = file_size;
// Remove the temporary file
fs::remove_file(&temp_file_path).expect("Cannot remove temp file");
return size_in_mb;
}
pub fn save_params(params_file: String, params: Parameters<Bls12>) {
let mut file = File::create(¶ms_file).expect("Failed to create file");
// Write the init_params to the file
params.write(&mut file).expect("Failed to write params to file");
}
pub fn load_params(params_file: String) -> Parameters<Bls12> {
let mut file = File::open(¶ms_file).expect("Failed to open file");
Parameters::read(&mut file, true).expect("Failed to read params from file")
}
pub fn save_proof(proof_file: String, proof: Proof<Bls12>) {
let mut file = File::create(&proof_file).expect("Failed to create file");
// Write the proof to the file
proof.write(&mut file).expect("Failed to write proof to file");
}
pub fn load_proof(proof_file: String) -> Proof<Bls12> {
let mut file = File::open(&proof_file).expect("Failed to open file");
Proof::read(&mut file).expect("Failed to read proof from file")
}
pub fn f_setup<C: Circuit<Scalar> + Clone>(circuit: C, params_file: String) {
let params = groth16::generate_random_parameters::<Bls12, _, _>(circuit.clone(), &mut OsRng).unwrap();
save_params(params_file, params);
}
pub fn f_prove<C: Circuit<Scalar> + Clone>(circuit: C, params_file: String, proof_file: String) {
let params = load_params(params_file);
let proof = groth16::create_random_proof(circuit, ¶ms, &mut OsRng).unwrap();
save_proof(proof_file, proof);
}
pub fn f_verify(params_file: String, proof_file: String, public_input: Vec<Scalar>) {
let params = load_params(params_file);
let pvk = groth16::prepare_verifying_key(¶ms.vk);
let proof = load_proof(proof_file);
assert!(groth16::verify_proof(&pvk, &proof, &public_input).is_ok());
}