mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-11-14 01:57:03 +00:00
Apply rustfmt
This commit is contained in:
102
src/rustzcash.rs
102
src/rustzcash.rs
@@ -1,47 +1,23 @@
|
|||||||
extern crate libc;
|
|
||||||
extern crate sapling_crypto;
|
|
||||||
extern crate pairing;
|
|
||||||
extern crate bellman;
|
extern crate bellman;
|
||||||
|
extern crate libc;
|
||||||
|
extern crate pairing;
|
||||||
|
extern crate sapling_crypto;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
use pairing::{
|
use pairing::{BitIterator, PrimeField, PrimeFieldRepr, bls12_381::{Bls12, Fr, FrRepr}};
|
||||||
BitIterator,
|
|
||||||
PrimeFieldRepr,
|
|
||||||
PrimeField,
|
|
||||||
bls12_381::{
|
|
||||||
Bls12,
|
|
||||||
Fr,
|
|
||||||
FrRepr
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
use sapling_crypto::{
|
use sapling_crypto::{jubjub::JubjubBls12, pedersen_hash::{pedersen_hash, Personalization}};
|
||||||
jubjub::JubjubBls12,
|
|
||||||
pedersen_hash::{
|
|
||||||
pedersen_hash,
|
|
||||||
Personalization
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
use bellman::{
|
use bellman::groth16::{prepare_verifying_key, Parameters, PreparedVerifyingKey, VerifyingKey};
|
||||||
groth16::{
|
|
||||||
Parameters,
|
|
||||||
PreparedVerifyingKey,
|
|
||||||
VerifyingKey,
|
|
||||||
prepare_verifying_key
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
use libc::{uint64_t, size_t, c_uchar, c_char};
|
use libc::{c_char, c_uchar, size_t, uint64_t};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref JUBJUB: JubjubBls12 = {
|
static ref JUBJUB: JubjubBls12 = { JubjubBls12::new() };
|
||||||
JubjubBls12::new()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static mut SAPLING_SPEND_VK: Option<PreparedVerifyingKey<Bls12>> = None;
|
static mut SAPLING_SPEND_VK: Option<PreparedVerifyingKey<Bls12>> = None;
|
||||||
@@ -56,24 +32,37 @@ static mut SPROUT_GROTH16_PARAMS_PATH: Option<String> = None;
|
|||||||
pub extern "system" fn librustzcash_init_zksnark_params(
|
pub extern "system" fn librustzcash_init_zksnark_params(
|
||||||
spend_path: *const c_char,
|
spend_path: *const c_char,
|
||||||
output_path: *const c_char,
|
output_path: *const c_char,
|
||||||
sprout_path: *const c_char
|
sprout_path: *const c_char,
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
// These should be valid CStr's, but the decoding may fail on Windows
|
// These should be valid CStr's, but the decoding may fail on Windows
|
||||||
// so we may need to use OSStr or something.
|
// so we may need to use OSStr or something.
|
||||||
let spend_path = unsafe { CStr::from_ptr(spend_path) }.to_str().expect("parameter path encoding error").to_string();
|
let spend_path = unsafe { CStr::from_ptr(spend_path) }
|
||||||
let output_path = unsafe { CStr::from_ptr(output_path) }.to_str().expect("parameter path encoding error").to_string();
|
.to_str()
|
||||||
let sprout_path = unsafe { CStr::from_ptr(sprout_path) }.to_str().expect("parameter path encoding error").to_string();
|
.expect("parameter path encoding error")
|
||||||
|
.to_string();
|
||||||
|
let output_path = unsafe { CStr::from_ptr(output_path) }
|
||||||
|
.to_str()
|
||||||
|
.expect("parameter path encoding error")
|
||||||
|
.to_string();
|
||||||
|
let sprout_path = unsafe { CStr::from_ptr(sprout_path) }
|
||||||
|
.to_str()
|
||||||
|
.expect("parameter path encoding error")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
// Load from each of the paths
|
// Load from each of the paths
|
||||||
let mut spend_fs = File::open(spend_path).expect("couldn't load Sapling spend parameters file");
|
let mut spend_fs = File::open(spend_path).expect("couldn't load Sapling spend parameters file");
|
||||||
let mut output_fs = File::open(output_path).expect("couldn't load Sapling output parameters file");
|
let mut output_fs =
|
||||||
let mut sprout_fs = File::open(&sprout_path).expect("couldn't load Sprout groth16 parameters file");
|
File::open(output_path).expect("couldn't load Sapling output parameters file");
|
||||||
|
let mut sprout_fs =
|
||||||
|
File::open(&sprout_path).expect("couldn't load Sprout groth16 parameters file");
|
||||||
|
|
||||||
// Deserialize params
|
// Deserialize params
|
||||||
let spend_params = Parameters::<Bls12>::read(&mut spend_fs, false).expect("couldn't deserialize Sapling spend parameters file");
|
let spend_params = Parameters::<Bls12>::read(&mut spend_fs, false)
|
||||||
let output_params = Parameters::<Bls12>::read(&mut output_fs, false).expect("couldn't deserialize Sapling spend parameters file");
|
.expect("couldn't deserialize Sapling spend parameters file");
|
||||||
let sprout_vk = VerifyingKey::<Bls12>::read(&mut sprout_fs).expect("couldn't deserialize Sprout Groth16 verifying key");
|
let output_params = Parameters::<Bls12>::read(&mut output_fs, false)
|
||||||
|
.expect("couldn't deserialize Sapling spend parameters file");
|
||||||
|
let sprout_vk = VerifyingKey::<Bls12>::read(&mut sprout_fs)
|
||||||
|
.expect("couldn't deserialize Sprout Groth16 verifying key");
|
||||||
|
|
||||||
// Prepare verifying keys
|
// Prepare verifying keys
|
||||||
let spend_vk = prepare_verifying_key(&spend_params.vk);
|
let spend_vk = prepare_verifying_key(&spend_params.vk);
|
||||||
@@ -94,10 +83,7 @@ pub extern "system" fn librustzcash_init_zksnark_params(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "system" fn librustzcash_tree_uncommitted(
|
pub extern "system" fn librustzcash_tree_uncommitted(result: *mut [c_uchar; 32]) {
|
||||||
result: *mut [c_uchar; 32]
|
|
||||||
)
|
|
||||||
{
|
|
||||||
let tmp = sapling_crypto::primitives::Note::<Bls12>::uncommitted().into_repr();
|
let tmp = sapling_crypto::primitives::Note::<Bls12>::uncommitted().into_repr();
|
||||||
|
|
||||||
// Should be okay, caller is responsible for ensuring the pointer
|
// Should be okay, caller is responsible for ensuring the pointer
|
||||||
@@ -113,8 +99,7 @@ pub extern "system" fn librustzcash_merkle_hash(
|
|||||||
a: *const [c_uchar; 32],
|
a: *const [c_uchar; 32],
|
||||||
b: *const [c_uchar; 32],
|
b: *const [c_uchar; 32],
|
||||||
result: *mut [c_uchar; 32],
|
result: *mut [c_uchar; 32],
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
let mut a_repr = FrRepr::default();
|
let mut a_repr = FrRepr::default();
|
||||||
let mut b_repr = FrRepr::default();
|
let mut b_repr = FrRepr::default();
|
||||||
|
|
||||||
@@ -138,14 +123,17 @@ pub extern "system" fn librustzcash_merkle_hash(
|
|||||||
for (a, b) in rhs.iter_mut().rev().zip(BitIterator::new(b_repr)) {
|
for (a, b) in rhs.iter_mut().rev().zip(BitIterator::new(b_repr)) {
|
||||||
*a = b;
|
*a = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tmp = pedersen_hash::<Bls12, _>(
|
let tmp = pedersen_hash::<Bls12, _>(
|
||||||
Personalization::MerkleTree(depth),
|
Personalization::MerkleTree(depth),
|
||||||
lhs.iter().map(|&x| x)
|
lhs.iter()
|
||||||
|
.map(|&x| x)
|
||||||
.take(Fr::NUM_BITS as usize)
|
.take(Fr::NUM_BITS as usize)
|
||||||
.chain(rhs.iter().map(|&x| x).take(Fr::NUM_BITS as usize)),
|
.chain(rhs.iter().map(|&x| x).take(Fr::NUM_BITS as usize)),
|
||||||
&JUBJUB
|
&JUBJUB,
|
||||||
).into_xy().0.into_repr();
|
).into_xy()
|
||||||
|
.0
|
||||||
|
.into_repr();
|
||||||
|
|
||||||
// Should be okay, caller is responsible for ensuring the pointer
|
// Should be okay, caller is responsible for ensuring the pointer
|
||||||
// is a valid pointer to 32 bytes that can be mutated.
|
// is a valid pointer to 32 bytes that can be mutated.
|
||||||
@@ -158,12 +146,14 @@ pub extern "system" fn librustzcash_merkle_hash(
|
|||||||
/// as a temporary mechanism for introducing Rust into
|
/// as a temporary mechanism for introducing Rust into
|
||||||
/// Zcash.
|
/// Zcash.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "system" fn librustzcash_xor(a: uint64_t, b: uint64_t) -> uint64_t
|
pub extern "system" fn librustzcash_xor(a: uint64_t, b: uint64_t) -> uint64_t {
|
||||||
{
|
|
||||||
a ^ b
|
a ^ b
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_xor() {
|
fn test_xor() {
|
||||||
assert_eq!(librustzcash_xor(0x0f0f0f0f0f0f0f0f, 0x1111111111111111), 0x1e1e1e1e1e1e1e1e);
|
assert_eq!(
|
||||||
|
librustzcash_xor(0x0f0f0f0f0f0f0f0f, 0x1111111111111111),
|
||||||
|
0x1e1e1e1e1e1e1e1e
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user