diff --git a/Cargo.lock b/Cargo.lock index b1409e6aa..3cac18976 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,7 +118,7 @@ dependencies = [ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", "pairing 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", - "sapling-crypto 0.0.1 (git+https://github.com/zcash-hackworks/sapling-crypto?rev=e554b473dd10885d232f42237c13282f5b6fee43)", + "sapling-crypto 0.0.1 (git+https://github.com/zcash-hackworks/sapling-crypto?rev=7beeb52730e24724ee10ea2458ecf7776cb59c58)", ] [[package]] @@ -156,7 +156,7 @@ dependencies = [ [[package]] name = "sapling-crypto" version = "0.0.1" -source = "git+https://github.com/zcash-hackworks/sapling-crypto?rev=e554b473dd10885d232f42237c13282f5b6fee43#e554b473dd10885d232f42237c13282f5b6fee43" +source = "git+https://github.com/zcash-hackworks/sapling-crypto?rev=7beeb52730e24724ee10ea2458ecf7776cb59c58#7beeb52730e24724ee10ea2458ecf7776cb59c58" dependencies = [ "bellman 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "blake2-rfc 0.2.18 (git+https://github.com/gtank/blake2-rfc?rev=7a5b5fc99ae483a0043db7547fb79a6fa44b88a9)", @@ -211,7 +211,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum pairing 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "06f21a403a78257de696b59a5bfafad56a3b3ab8f27741c8122750bf0ebbb9fa" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" -"checksum sapling-crypto 0.0.1 (git+https://github.com/zcash-hackworks/sapling-crypto?rev=e554b473dd10885d232f42237c13282f5b6fee43)" = "" +"checksum sapling-crypto 0.0.1 (git+https://github.com/zcash-hackworks/sapling-crypto?rev=7beeb52730e24724ee10ea2458ecf7776cb59c58)" = "" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index 18c28e0cc..2bb4e14ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ bellman = "0.1" [dependencies.sapling-crypto] git = "https://github.com/zcash-hackworks/sapling-crypto" -rev = "e554b473dd10885d232f42237c13282f5b6fee43" +rev = "7beeb52730e24724ee10ea2458ecf7776cb59c58" [profile.release] lto = true diff --git a/src/rustzcash.rs b/src/rustzcash.rs index 649a0217b..754a507af 100644 --- a/src/rustzcash.rs +++ b/src/rustzcash.rs @@ -8,7 +8,7 @@ extern crate lazy_static; use pairing::{BitIterator, PrimeField, PrimeFieldRepr, bls12_381::{Bls12, Fr, FrRepr}}; -use sapling_crypto::{jubjub::JubjubBls12, pedersen_hash::{pedersen_hash, Personalization}}; +use sapling_crypto::{jubjub::JubjubBls12, pedersen_hash::{pedersen_hash, Personalization}, util::swap_bits_u64}; use bellman::groth16::{prepare_verifying_key, Parameters, PreparedVerifyingKey, VerifyingKey}; @@ -28,12 +28,45 @@ static mut SAPLING_SPEND_PARAMS: Option> = None; static mut SAPLING_OUTPUT_PARAMS: Option> = None; static mut SPROUT_GROTH16_PARAMS_PATH: Option = None; +/// Writes an FrRepr to [u8] of length 32 +fn write_le(mut f: FrRepr, to: &mut [u8]) { + assert_eq!(to.len(), 32); + + f.as_mut().reverse(); + for b in f.as_mut() { + *b = swap_bits_u64(*b); + } + + f.write_be(to).expect("length is 32 bytes"); +} + +/// Reads an FrRepr from a [u8] of length 32. +/// This will panic (abort) if length provided is +/// not correct. +fn read_le(from: &[u8]) -> FrRepr +{ + assert_eq!(from.len(), 32); + + let mut f = FrRepr::default(); + f.read_be(from).expect("length is 32 bytes"); + + f.as_mut().reverse(); + for b in f.as_mut() { + *b = swap_bits_u64(*b); + } + + f +} + #[no_mangle] pub extern "system" fn librustzcash_init_zksnark_params( spend_path: *const c_char, output_path: *const c_char, sprout_path: *const c_char, ) { + // Initialize jubjub parameters here + lazy_static::initialize(&JUBJUB); + // These should be valid CStr's, but the decoding may fail on Windows // so we may need to use OSStr or something. let spend_path = unsafe { CStr::from_ptr(spend_path) } @@ -90,7 +123,7 @@ pub extern "system" fn librustzcash_tree_uncommitted(result: *mut [c_uchar; 32]) // is a valid pointer to 32 bytes that can be mutated. let result = unsafe { &mut *result }; - tmp.write_be(&mut result[..]).unwrap(); + write_le(tmp, &mut result[..]); } #[no_mangle] @@ -100,18 +133,15 @@ pub extern "system" fn librustzcash_merkle_hash( b: *const [c_uchar; 32], result: *mut [c_uchar; 32], ) { - let mut a_repr = FrRepr::default(); - let mut b_repr = FrRepr::default(); + // Should be okay, because caller is responsible for ensuring + // the pointer is a valid pointer to 32 bytes, and that is the + // size of the representation + let a_repr = read_le(unsafe { &(&*a)[..] }); // Should be okay, because caller is responsible for ensuring // the pointer is a valid pointer to 32 bytes, and that is the // size of the representation - a_repr.read_be(unsafe { &(&*a)[..] }).unwrap(); - - // Should be okay, because caller is responsible for ensuring - // the pointer is a valid pointer to 32 bytes, and that is the - // size of the representation - b_repr.read_be(unsafe { &(&*b)[..] }).unwrap(); + let b_repr = read_le(unsafe { &(&*b)[..] }); let mut lhs = [false; 256]; let mut rhs = [false; 256]; @@ -139,7 +169,7 @@ pub extern "system" fn librustzcash_merkle_hash( // is a valid pointer to 32 bytes that can be mutated. let result = unsafe { &mut *result }; - tmp.write_be(&mut result[..]).unwrap(); + write_le(tmp, &mut result[..]); } /// XOR two uint64_t values and return the result, used