Make initialization of sprout validation key optional

This makes the C interface behave like `zcash_proofs` and allows to
init the library without downloading the heavy sprout parameters.
In the special case where `librustzcash_init_zksnark_params` is called
with the sprout arguments path set to NULL and length set to 0, the
arguments are passed as None to `load_parameters`.
This commit is contained in:
Marco Stronati
2019-08-06 23:41:33 +02:00
parent 4255b44b21
commit 3891fe57d4

View File

@@ -143,9 +143,13 @@ pub extern "system" fn librustzcash_init_zksnark_params(
let output_path = Path::new(OsStr::from_bytes(unsafe { let output_path = Path::new(OsStr::from_bytes(unsafe {
slice::from_raw_parts(output_path, output_path_len) slice::from_raw_parts(output_path, output_path_len)
})); }));
let sprout_path = Path::new(OsStr::from_bytes(unsafe { let sprout_path = if sprout_path.is_null() && sprout_path_len == 0 {
slice::from_raw_parts(sprout_path, sprout_path_len) None
})); } else {
Some(Path::new(OsStr::from_bytes(unsafe {
slice::from_raw_parts(sprout_path, sprout_path_len)
})))
};
init_zksnark_params( init_zksnark_params(
spend_path, spend_path,
@@ -174,8 +178,13 @@ pub extern "system" fn librustzcash_init_zksnark_params(
OsString::from_wide(unsafe { slice::from_raw_parts(spend_path, spend_path_len) }); OsString::from_wide(unsafe { slice::from_raw_parts(spend_path, spend_path_len) });
let output_path = let output_path =
OsString::from_wide(unsafe { slice::from_raw_parts(output_path, output_path_len) }); OsString::from_wide(unsafe { slice::from_raw_parts(output_path, output_path_len) });
let sprout_path = let sprout_path = if sprout_path.is_null() && sprout_path_len == 0 {
OsString::from_wide(unsafe { slice::from_raw_parts(sprout_path, sprout_path_len) }); None
} else {
Some(OsStr::from_wide(unsafe {
slice::from_raw_parts(sprout_path, sprout_path_len)
}))
};
init_zksnark_params( init_zksnark_params(
Path::new(&spend_path), Path::new(&spend_path),
@@ -192,7 +201,7 @@ fn init_zksnark_params(
spend_hash: *const c_char, spend_hash: *const c_char,
output_path: &Path, output_path: &Path,
output_hash: *const c_char, output_hash: *const c_char,
sprout_path: &Path, sprout_path: Option<&Path>,
sprout_hash: *const c_char, sprout_hash: *const c_char,
) { ) {
// Initialize jubjub parameters here // Initialize jubjub parameters here
@@ -206,9 +215,15 @@ fn init_zksnark_params(
.to_str() .to_str()
.expect("hash should be a valid string"); .expect("hash should be a valid string");
let sprout_hash = unsafe { CStr::from_ptr(sprout_hash) } let sprout_hash_option = if sprout_path.is_none() {
.to_str() None
.expect("hash should be a valid string"); } else {
Some(
unsafe { CStr::from_ptr(sprout_hash) }
.to_str()
.expect("hash should be a valid string"),
)
};
// Load params // Load params
let (spend_params, spend_vk, output_params, output_vk, sprout_vk) = load_parameters( let (spend_params, spend_vk, output_params, output_vk, sprout_vk) = load_parameters(
@@ -216,8 +231,8 @@ fn init_zksnark_params(
spend_hash, spend_hash,
output_path, output_path,
output_hash, output_hash,
Some(sprout_path), sprout_path,
Some(sprout_hash), sprout_hash_option,
); );
// Caller is responsible for calling this function once, so // Caller is responsible for calling this function once, so
@@ -225,11 +240,11 @@ fn init_zksnark_params(
unsafe { unsafe {
SAPLING_SPEND_PARAMS = Some(spend_params); SAPLING_SPEND_PARAMS = Some(spend_params);
SAPLING_OUTPUT_PARAMS = Some(output_params); SAPLING_OUTPUT_PARAMS = Some(output_params);
SPROUT_GROTH16_PARAMS_PATH = Some(sprout_path.to_owned()); SPROUT_GROTH16_PARAMS_PATH = sprout_path.map(|p| p.to_owned());
SAPLING_SPEND_VK = Some(spend_vk); SAPLING_SPEND_VK = Some(spend_vk);
SAPLING_OUTPUT_VK = Some(output_vk); SAPLING_OUTPUT_VK = Some(output_vk);
SPROUT_GROTH16_VK = Some(sprout_vk.unwrap()); SPROUT_GROTH16_VK = sprout_vk;
} }
} }