generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
236 lines (205 loc) · 5.54 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
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
#![deny(clippy::all)]
#![allow(dead_code)]
use std::collections::HashMap;
use ark_bn254::Bn254;
use ark_bls12_381::Bls12_381;
use std::{
fs::{write}
};
use napi_derive::napi;
mod setup;
mod keys;
mod prover;
mod verifier;
// path : pk, vk ans saving
// pk : circuit_bn128_pk.bin
// vk : circuit_bn128_vk.bin
// proof: circuit_bn128_proof.bin
#[napi]
pub fn setup_from_circom_r1cs_bn128(
r1cs_file_path : String,
commit_witness_count : u32,
seed:u32,
path_pk : String,
path_vk : String,
) {
let(_, proving_key, _) = setup::setup_from_circom_r1cs_bn128(
r1cs_file_path,
commit_witness_count as usize,
seed as u64
);
keys::write_bn128_proving_key(proving_key.clone(), &path_pk.as_str(),&path_vk.as_str()).unwrap();
}
#[napi]
pub fn setup_from_circom_r1cs_bls12_381(
r1cs_file_path : String,
commit_witness_count : u32,
seed : u32,
path_pk : String,
path_vk : String,
) {
let(_, proving_key, _) = setup::setup_from_circom_r1cs_bls12_381(
r1cs_file_path,
commit_witness_count as usize,
seed as u64
);
keys::write_bls12_381_proving_key(proving_key.clone(), &path_pk.as_str(),&path_vk.as_str()).unwrap();
}
#[napi]
pub fn prove_range_bn128(
r1cs_file_path : String,
wasm_file_path : String,
pk_file_path : String,
proof_file_path : String,
input_string: String,
seed : u32
) {
let value = prover::make_range_inputs::<Bn254>(input_string);
let mut inputs= HashMap::new();
inputs.insert("value".to_string(), vec![value]);
let opening_key = prover::prove::<Bn254, _>(
r1cs_file_path.as_str(),
pk_file_path.as_str(),
wasm_file_path.as_str(),
proof_file_path.as_str(),
1usize,
inputs.clone(),
seed as u64
);
let opening_key_path = format!("{}{}",proof_file_path.as_str().trim_end_matches(".bin"), "_opening_key.json");
write(
keys::abs_path(opening_key_path.as_str()),
opening_key.as_bytes()
).unwrap();
}
#[napi]
pub fn prove_range_bls12_381(
r1cs_file_path : String,
wasm_file_path : String,
pk_file_path : String,
proof_file_path : String,
input_string: String,
seed : u32
) {
let value = prover::make_range_inputs::<Bls12_381>(input_string);
let mut inputs= HashMap ::new();
inputs.insert("value".to_string(), vec![value]);
let opening_key = prover::prove::<Bls12_381, _>(
r1cs_file_path.as_str(),
pk_file_path.as_str(),
wasm_file_path.as_str(),
proof_file_path.as_str(),
1usize,
inputs.clone(),
seed as u64
);
println!("opening_key: {}", opening_key);
let opening_key_path = format!("{}{}",proof_file_path.as_str().trim_end_matches(".bin"), "_opening_key.bin");
println!("opening_key_path : {}", opening_key_path);
}
#[napi]
pub fn verify_range_bn128(
vk_path : String,
proof_file_path : String,
) -> bool{
verifier::verify::<Bn254>(
vk_path.as_str(),
proof_file_path.as_str(),
vec![]
)
}
#[napi]
pub fn verify_range_bls12_381(
vk_path : String,
proof_file_path : String
) -> bool {
verifier::verify::<Bls12_381>(
vk_path.as_str(),
proof_file_path.as_str(),
vec![]
)
}
#[napi]
pub fn get_proof_bn128(
proof_file_path : String
) -> String {
prover::proof_to_string_from_file::<Bn254>(proof_file_path.as_str())
}
#[napi]
pub fn aggregate_proof_commitment_bn128(
proof_file_paths : Vec<String>,
save_file_path : String
){
prover::aggregate_proof_commitment::<Bn254>(proof_file_paths, &save_file_path.as_str());
}
#[napi]
pub fn aggregate_proof_commitment_bls12_381(
proof_file_paths : Vec<String>,
save_file_path : String
){
prover::aggregate_proof_commitment::<Bls12_381>(proof_file_paths, &save_file_path.as_str());
}
#[napi]
pub fn get_aggregated_commitment_bn128(
aggregated_commitment_file_path : String
) -> String {
prover::get_aggregated_commitment::<Bn254>(aggregated_commitment_file_path.as_str())
}
#[napi]
pub fn get_aggregated_commitment_bls12_381(
aggregated_commitment_file_path : String
) -> String {
prover::get_aggregated_commitment::<Bls12_381>(aggregated_commitment_file_path.as_str())
}
#[napi]
pub fn aggregate_opening_keys_bn128(
opening_key_file_paths : Vec<String>,
save_file_path : String
){
prover::aggregated_pedersen_commitment_opening_keys::<Bn254>(opening_key_file_paths, &save_file_path.as_str());
}
#[napi]
pub fn aggregate_opening_keys_bls12_381(
opening_key_file_paths : Vec<String>,
save_file_path : String
){
prover::aggregated_pedersen_commitment_opening_keys::<Bls12_381>(opening_key_file_paths, &save_file_path.as_str());
}
#[napi]
pub fn update_aggregated_commitment_bn128(
r1cs_file_path : String,
key_file_path : String,
wasm_file_path : String,
proof_file_path : String,
aggregated_commitment_file_path : String,
aggregated_opening_key_file_path : String,
update_value : String,
seed : u32
){
prover::update_aggregated_commitment::<Bn254>(
r1cs_file_path.as_str(),
key_file_path.as_str(),
wasm_file_path.as_str(),
proof_file_path.as_str(),
aggregated_commitment_file_path.as_str(),
aggregated_opening_key_file_path.as_str(),
update_value.clone(),
seed as u64
);
}
#[napi]
pub fn update_aggregated_commitment_bls12_381(
){
}
#[napi]
pub fn calculate_pedersen_commitment_bn128(
proving_file_path: String,
m : String,
v : String
) -> String {
let proving_key = keys::read_compressed_proving_key_from_file::<Bn254>(&proving_file_path.as_str());
let m = prover::hex_string_to_scalar_field::<Bn254>(m);
let v = prover::hex_string_to_scalar_field::<Bn254>(v);
let commitment = prover::calculate_pedersen_commitment::<Bn254>(proving_key, m, v);
format!("{:#?}", commitment)
}