-
Notifications
You must be signed in to change notification settings - Fork 983
/
masp.rs
202 lines (180 loc) · 6.44 KB
/
masp.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
//! MASP verification wrappers.
use std::env;
use std::fs::File;
use std::ops::Deref;
use std::path::PathBuf;
use bellman::groth16::{prepare_verifying_key, PreparedVerifyingKey};
use bls12_381::Bls12;
use masp_primitives::asset_type::AssetType;
use masp_primitives::consensus::BranchId::Sapling;
use masp_primitives::redjubjub::PublicKey;
use masp_primitives::transaction::components::{
ConvertDescription, OutputDescription, SpendDescription,
};
use masp_primitives::transaction::{
signature_hash_data, Transaction, SIGHASH_ALL,
};
use masp_proofs::sapling::SaplingVerificationContext;
/// Env var to point to a dir with MASP parameters. When not specified,
/// the default OS specific path is used.
pub const ENV_VAR_MASP_PARAMS_DIR: &str = "NAMADA_MASP_PARAMS_DIR";
// TODO these could be exported from masp_proof crate
/// Spend circuit name
pub const SPEND_NAME: &str = "masp-spend.params";
/// Output circuit name
pub const OUTPUT_NAME: &str = "masp-output.params";
/// Convert circuit name
pub const CONVERT_NAME: &str = "masp-convert.params";
/// Load Sapling spend params.
pub fn load_spend_params() -> (
bellman::groth16::Parameters<Bls12>,
bellman::groth16::PreparedVerifyingKey<Bls12>,
) {
let params_dir = get_params_dir();
let spend_path = params_dir.join(SPEND_NAME);
if !spend_path.exists() {
#[cfg(feature = "masp_proofs/download-params")]
masp_proofs::download_parameters()
.expect("MASP parameters not present or downloadable");
#[cfg(not(feature = "masp_proofs/download-params"))]
panic!("MASP parameters not present or downloadable");
}
let param_f = File::open(spend_path).unwrap();
let params = bellman::groth16::Parameters::read(¶m_f, false).unwrap();
let vk = prepare_verifying_key(¶ms.vk);
(params, vk)
}
/// Load Sapling convert params.
pub fn load_convert_params() -> (
bellman::groth16::Parameters<Bls12>,
bellman::groth16::PreparedVerifyingKey<Bls12>,
) {
let params_dir = get_params_dir();
let spend_path = params_dir.join(CONVERT_NAME);
if !spend_path.exists() {
#[cfg(feature = "masp_proofs/download-params")]
masp_proofs::download_parameters()
.expect("MASP parameters not present or downloadable");
#[cfg(not(feature = "masp_proofs/download-params"))]
panic!("MASP parameters not present or downloadable");
}
let param_f = File::open(spend_path).unwrap();
let params = bellman::groth16::Parameters::read(¶m_f, false).unwrap();
let vk = prepare_verifying_key(¶ms.vk);
(params, vk)
}
/// Load Sapling output params.
pub fn load_output_params() -> (
bellman::groth16::Parameters<Bls12>,
bellman::groth16::PreparedVerifyingKey<Bls12>,
) {
let params_dir = get_params_dir();
let output_path = params_dir.join(OUTPUT_NAME);
if !output_path.exists() {
#[cfg(feature = "masp_proofs/download-params")]
masp_proofs::download_parameters()
.expect("MASP parameters not present or downloadable");
#[cfg(not(feature = "masp_proofs/download-params"))]
panic!("MASP parameters not present or downloadable");
}
let param_f = File::open(output_path).unwrap();
let params = bellman::groth16::Parameters::read(¶m_f, false).unwrap();
let vk = prepare_verifying_key(¶ms.vk);
(params, vk)
}
/// check_spend wrapper
pub fn check_spend(
spend: &SpendDescription,
sighash: &[u8; 32],
ctx: &mut SaplingVerificationContext,
parameters: &PreparedVerifyingKey<Bls12>,
) -> bool {
let zkproof =
bellman::groth16::Proof::read(spend.zkproof.as_slice()).unwrap();
ctx.check_spend(
spend.cv,
spend.anchor,
&spend.nullifier,
PublicKey(spend.rk.0),
sighash,
spend.spend_auth_sig.unwrap(),
zkproof,
parameters,
)
}
/// check_output wrapper
pub fn check_output(
output: &OutputDescription,
ctx: &mut SaplingVerificationContext,
parameters: &PreparedVerifyingKey<Bls12>,
) -> bool {
let zkproof =
bellman::groth16::Proof::read(output.zkproof.as_slice()).unwrap();
ctx.check_output(
output.cv,
output.cmu,
output.ephemeral_key,
zkproof,
parameters,
)
}
/// check convert wrapper
pub fn check_convert(
convert: &ConvertDescription,
ctx: &mut SaplingVerificationContext,
parameters: &PreparedVerifyingKey<Bls12>,
) -> bool {
let zkproof =
bellman::groth16::Proof::read(convert.zkproof.as_slice()).unwrap();
ctx.check_convert(convert.cv, convert.anchor, zkproof, parameters)
}
/// Verify a shielded transaction.
pub fn verify_shielded_tx(transaction: &Transaction) -> bool {
tracing::info!("entered verify_shielded_tx()");
let mut ctx = SaplingVerificationContext::new();
let tx_data = transaction.deref();
let (_, spend_pvk) = load_spend_params();
let (_, convert_pvk) = load_convert_params();
let (_, output_pvk) = load_output_params();
let sighash: [u8; 32] =
signature_hash_data(tx_data, Sapling, SIGHASH_ALL, None)
.try_into()
.unwrap();
tracing::info!("sighash computed");
let spends_valid = tx_data
.shielded_spends
.iter()
.all(|spend| check_spend(spend, &sighash, &mut ctx, &spend_pvk));
let converts_valid = tx_data
.shielded_converts
.iter()
.all(|convert| check_convert(convert, &mut ctx, &convert_pvk));
let outputs_valid = tx_data
.shielded_outputs
.iter()
.all(|output| check_output(output, &mut ctx, &output_pvk));
if !(spends_valid && outputs_valid && converts_valid) {
return false;
}
tracing::info!("passed spend/output verification");
let assets_and_values: Vec<(AssetType, i64)> =
tx_data.value_balance.clone().into_components().collect();
tracing::info!("accumulated {} assets/values", assets_and_values.len());
let result = ctx.final_check(
assets_and_values.as_slice(),
&sighash,
tx_data.binding_sig.unwrap(),
);
tracing::info!("final check result {result}");
result
}
/// Get the path to MASP parameters from [`ENV_VAR_MASP_PARAMS_DIR`] env var or
/// use the default.
pub fn get_params_dir() -> PathBuf {
if let Ok(params_dir) = env::var(ENV_VAR_MASP_PARAMS_DIR) {
println!("Using {} as masp parameter folder.", params_dir);
PathBuf::from(params_dir)
} else {
masp_proofs::default_params_folder().unwrap()
}
}