Basic conversions
This commit is contained in:
@@ -12,6 +12,7 @@ homepage = "https://github.com/zcash-hackworks/zip32"
|
|||||||
repository = "https://github.com/zcash-hackworks/zip32"
|
repository = "https://github.com/zcash-hackworks/zip32"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
lazy_static = "1.0"
|
||||||
pairing = "0.14.2"
|
pairing = "0.14.2"
|
||||||
|
|
||||||
[dependencies.sapling-crypto]
|
[dependencies.sapling-crypto]
|
||||||
|
|||||||
67
src/lib.rs
67
src/lib.rs
@@ -1,12 +1,21 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
extern crate pairing;
|
extern crate pairing;
|
||||||
extern crate sapling_crypto;
|
extern crate sapling_crypto;
|
||||||
|
|
||||||
use pairing::bls12_381::Bls12;
|
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
|
// Sapling key components
|
||||||
|
|
||||||
/// An outgoing viewing key
|
/// An outgoing viewing key
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct OutgoingViewingKey([u8; 32]);
|
struct OutgoingViewingKey([u8; 32]);
|
||||||
|
|
||||||
/// A Sapling expanded spending key
|
/// A Sapling expanded spending key
|
||||||
@@ -22,24 +31,67 @@ struct FullViewingKey<E: JubjubEngine> {
|
|||||||
ovk: OutgoingViewingKey,
|
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
|
// ZIP 32 structures
|
||||||
|
|
||||||
/// A Sapling full viewing key fingerprint
|
/// A Sapling full viewing key fingerprint
|
||||||
struct FVKFingerprint([u8; 32]);
|
struct FVKFingerprint([u8; 32]);
|
||||||
|
|
||||||
/// A Sapling full viewing key tag
|
/// A Sapling full viewing key tag
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct FVKTag([u8; 4]);
|
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
|
/// A child index for a derived key
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum ChildIndex {
|
pub enum ChildIndex {
|
||||||
NonHardened(u32),
|
NonHardened(u32),
|
||||||
Hardened(u32), // Hardened(n) == n + (1 << 31) == n' in path notation
|
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
|
/// A chain code
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct ChainCode([u8; 32]);
|
struct ChainCode([u8; 32]);
|
||||||
|
|
||||||
/// A key used to derive diversifiers for a particular child key
|
/// A key used to derive diversifiers for a particular child key
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct DiversifierKey([u8; 32]);
|
struct DiversifierKey([u8; 32]);
|
||||||
|
|
||||||
/// A Sapling extended spending key
|
/// A Sapling extended spending key
|
||||||
@@ -62,6 +114,19 @@ pub struct ExtendedFullViewingKey {
|
|||||||
dk: DiversifierKey,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user