Skip to content

Commit

Permalink
Refactor Sapling spendAuthSig creation into the sapling module
Browse files Browse the repository at this point in the history
This should move into zcash_primitives once #35 has been merged.
  • Loading branch information
str4d committed Sep 11, 2018
1 parent 97dd7ae commit a6b3d14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
22 changes: 1 addition & 21 deletions librustzcash/src/rustzcash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,28 +1015,8 @@ pub extern "system" fn librustzcash_sapling_spend_sig(
Err(_) => return false,
};

// We compute `rsk`...
let rsk = ask.randomize(ar);

// We compute `rk` from there (needed for key prefixing)
let rk =
redjubjub::PublicKey::from_private(&rsk, FixedGenerators::SpendingKeyGenerator, &JUBJUB);

// Compute the signature's message for rk/spend_auth_sig
let mut data_to_be_signed = [0u8; 64];
rk.0
.write(&mut data_to_be_signed[0..32])
.expect("message buffer should be 32 bytes");
(&mut data_to_be_signed[32..64]).copy_from_slice(&(unsafe { &*sighash })[..]);

// Do the signing
let mut rng = OsRng::new().expect("should be able to construct RNG");
let sig = rsk.sign(
&data_to_be_signed,
&mut rng,
FixedGenerators::SpendingKeyGenerator,
&JUBJUB,
);
let sig = sapling::spend_sig(ask, ar, unsafe { &*sighash }, &JUBJUB);

// Write out the signature
sig.write(&mut (unsafe { &mut *result })[..])
Expand Down
32 changes: 32 additions & 0 deletions librustzcash/src/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ use sapling_crypto::{
};
use zcash_proofs::sapling::compute_value_balance;

/// Create the spendAuthSig for a Sapling SpendDescription.
pub fn spend_sig(
ask: PrivateKey<Bls12>,
ar: Fs,
sighash: &[u8; 32],
params: &JubjubBls12,
) -> Signature {
// Initialize secure RNG
let mut rng = OsRng::new().expect("should be able to construct RNG");

// We compute `rsk`...
let rsk = ask.randomize(ar);

// We compute `rk` from there (needed for key prefixing)
let rk = PublicKey::from_private(&rsk, FixedGenerators::SpendingKeyGenerator, params);

// Compute the signature's message for rk/spend_auth_sig
let mut data_to_be_signed = [0u8; 64];
rk.0
.write(&mut data_to_be_signed[0..32])
.expect("message buffer should be 32 bytes");
(&mut data_to_be_signed[32..64]).copy_from_slice(&sighash[..]);

// Do the signing
rsk.sign(
&data_to_be_signed,
&mut rng,
FixedGenerators::SpendingKeyGenerator,
params,
)
}

/// A context object for verifying the Sapling components of a Zcash transaction.
pub struct SaplingVerificationContext {
bvk: edwards::Point<Bls12, Unknown>,
Expand Down

0 comments on commit a6b3d14

Please sign in to comment.