Basic conversions

This commit is contained in:
Jack Grigg 2018-07-11 01:26:07 +01:00
parent 1b8da6b12d
commit 8db848139b
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 67 additions and 1 deletions

View File

@ -12,6 +12,7 @@ homepage = "https://github.com/zcash-hackworks/zip32"
repository = "https://github.com/zcash-hackworks/zip32"
[dependencies]
lazy_static = "1.0"
pairing = "0.14.2"
[dependencies.sapling-crypto]

View File

@ -1,12 +1,21 @@
#[macro_use]
extern crate lazy_static;
extern crate pairing;
extern crate sapling_crypto;
use pairing::bls12_381::Bls12;
use sapling_crypto::{jubjub::JubjubEngine, primitives::ViewingKey};
use sapling_crypto::{
jubjub::{FixedGenerators, JubjubBls12, JubjubEngine, JubjubParams}, primitives::ViewingKey,
};
lazy_static! {
static ref JUBJUB: JubjubBls12 = { JubjubBls12::new() };
}
// Sapling key components
/// An outgoing viewing key
#[derive(Clone, Copy)]
struct OutgoingViewingKey([u8; 32]);
/// A Sapling expanded spending key
@ -22,24 +31,67 @@ struct FullViewingKey<E: JubjubEngine> {
ovk: OutgoingViewingKey,
}
impl<E: JubjubEngine> FullViewingKey<E> {
fn from_expanded_spending_key(xsk: &ExpandedSpendingKey<E>, params: &E::Params) -> Self {
FullViewingKey {
vk: ViewingKey {
ak: params
.generator(FixedGenerators::SpendingKeyGenerator)
.mul(xsk.ask, params),
nk: params
.generator(FixedGenerators::ProofGenerationKey)
.mul(xsk.nsk, params),
},
ovk: xsk.ovk,
}
}
}
// ZIP 32 structures
/// A Sapling full viewing key fingerprint
struct FVKFingerprint([u8; 32]);
/// A Sapling full viewing key tag
#[derive(Clone, Copy)]
struct FVKTag([u8; 4]);
impl<'a> From<&'a FVKFingerprint> for FVKTag {
fn from(fingerprint: &FVKFingerprint) -> Self {
let mut tag = [0u8; 4];
tag.copy_from_slice(&fingerprint.0[..4]);
FVKTag(tag)
}
}
impl From<FVKFingerprint> for FVKTag {
fn from(fingerprint: FVKFingerprint) -> Self {
(&fingerprint).into()
}
}
/// A child index for a derived key
#[derive(Clone, Copy)]
pub enum ChildIndex {
NonHardened(u32),
Hardened(u32), // Hardened(n) == n + (1 << 31) == n' in path notation
}
impl ChildIndex {
pub fn from_index(i: u32) -> Self {
match i {
n if n >= (1 << 31) => ChildIndex::Hardened(n - (1 << 31)),
n => ChildIndex::NonHardened(n),
}
}
}
/// A chain code
#[derive(Clone, Copy)]
struct ChainCode([u8; 32]);
/// A key used to derive diversifiers for a particular child key
#[derive(Clone, Copy)]
struct DiversifierKey([u8; 32]);
/// A Sapling extended spending key
@ -62,6 +114,19 @@ pub struct ExtendedFullViewingKey {
dk: DiversifierKey,
}
impl<'a> From<&'a ExtendedSpendingKey> for ExtendedFullViewingKey {
fn from(xsk: &ExtendedSpendingKey) -> Self {
ExtendedFullViewingKey {
depth: xsk.depth,
parent_fvk_tag: xsk.parent_fvk_tag,
child_index: xsk.child_index,
chain_code: xsk.chain_code,
fvk: FullViewingKey::from_expanded_spending_key(&xsk.xsk, &JUBJUB),
dk: xsk.dk,
}
}
}
#[cfg(test)]
mod tests {
#[test]