Simplify creation of FVKFingerprint and FVKTag

This commit is contained in:
Jack Grigg 2018-08-22 23:59:20 +01:00
parent e27fc674f5
commit 770c4bef25
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
1 changed files with 14 additions and 22 deletions

View File

@ -205,6 +205,14 @@ impl<E: JubjubEngine> FullViewingKey<E> {
.expect("should be able to serialize a FullViewingKey"); .expect("should be able to serialize a FullViewingKey");
result result
} }
fn fingerprint(&self) -> FVKFingerprint {
let mut h = Blake2b::with_params(32, &[], &[], ZIP32_SAPLING_FVFP_PERSONALIZATION);
h.update(&self.to_bytes());
let mut fvfp = [0u8; 32];
fvfp.copy_from_slice(h.finalize().as_bytes());
FVKFingerprint(fvfp)
}
} }
// ZIP 32 structures // ZIP 32 structures
@ -212,34 +220,18 @@ impl<E: JubjubEngine> FullViewingKey<E> {
/// A Sapling full viewing key fingerprint /// A Sapling full viewing key fingerprint
struct FVKFingerprint([u8; 32]); struct FVKFingerprint([u8; 32]);
impl<'a, E: JubjubEngine> From<&'a FullViewingKey<E>> for FVKFingerprint {
fn from(fvk: &FullViewingKey<E>) -> Self {
let mut h = Blake2b::with_params(32, &[], &[], ZIP32_SAPLING_FVFP_PERSONALIZATION);
h.update(&fvk.to_bytes());
let mut fvfp = [0u8; 32];
fvfp.copy_from_slice(h.finalize().as_bytes());
FVKFingerprint(fvfp)
}
}
/// A Sapling full viewing key tag /// A Sapling full viewing key tag
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
struct FVKTag([u8; 4]); struct FVKTag([u8; 4]);
impl<'a> From<&'a FVKFingerprint> for FVKTag { impl FVKFingerprint {
fn from(fingerprint: &FVKFingerprint) -> Self { fn tag(&self) -> FVKTag {
let mut tag = [0u8; 4]; let mut tag = [0u8; 4];
tag.copy_from_slice(&fingerprint.0[..4]); tag.copy_from_slice(&self.0[..4]);
FVKTag(tag) FVKTag(tag)
} }
} }
impl From<FVKFingerprint> for FVKTag {
fn from(fingerprint: FVKFingerprint) -> Self {
(&fingerprint).into()
}
}
impl FVKTag { impl FVKTag {
fn master() -> Self { fn master() -> Self {
FVKTag([0u8; 4]) FVKTag([0u8; 4])
@ -493,7 +485,7 @@ impl ExtendedSpendingKey {
ExtendedSpendingKey { ExtendedSpendingKey {
depth: self.depth + 1, depth: self.depth + 1,
parent_fvk_tag: FVKFingerprint::from(&fvk).into(), parent_fvk_tag: fvk.fingerprint().tag(),
child_index: i, child_index: i,
chain_code: ChainCode(c_i), chain_code: ChainCode(c_i),
expsk: self.expsk.derive_child(i_l), expsk: self.expsk.derive_child(i_l),
@ -570,7 +562,7 @@ impl ExtendedFullViewingKey {
Ok(ExtendedFullViewingKey { Ok(ExtendedFullViewingKey {
depth: self.depth + 1, depth: self.depth + 1,
parent_fvk_tag: FVKFingerprint::from(&self.fvk).into(), parent_fvk_tag: self.fvk.fingerprint().tag(),
child_index: i, child_index: i,
chain_code: ChainCode(c_i), chain_code: ChainCode(c_i),
fvk: self.fvk.derive_child(i_l, &JUBJUB), fvk: self.fvk.derive_child(i_l, &JUBJUB),
@ -1200,7 +1192,7 @@ mod tests {
let mut ser = vec![]; let mut ser = vec![];
xfvk.write(&mut ser).unwrap(); xfvk.write(&mut ser).unwrap();
assert_eq!(&ser[..], &tv.xfvk[..]); assert_eq!(&ser[..], &tv.xfvk[..]);
assert_eq!(FVKFingerprint::from(&xfvk.fvk).0, tv.fp); assert_eq!(xfvk.fvk.fingerprint().0, tv.fp);
// d0 // d0
let mut di = DiversifierIndex::new(); let mut di = DiversifierIndex::new();