From f5a18b8fa5e9400babad48a60f352c3182b4d0ef Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Wed, 31 Aug 2022 13:36:24 -0600 Subject: [PATCH] Add conversions from u32 and u64 to DiversifierIndex --- zcash_primitives/src/zip32.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/zcash_primitives/src/zip32.rs b/zcash_primitives/src/zip32.rs index 9d3bb836c..66f6ceb99 100644 --- a/zcash_primitives/src/zip32.rs +++ b/zcash_primitives/src/zip32.rs @@ -134,6 +134,20 @@ impl Default for DiversifierIndex { } } +impl From for DiversifierIndex { + fn from(i: u32) -> Self { + u64::from(i).into() + } +} + +impl From for DiversifierIndex { + fn from(i: u64) -> Self { + let mut result = DiversifierIndex([0; 11]); + (&mut result.0[..8]).copy_from_slice(&i.to_le_bytes()); + result + } +} + impl DiversifierIndex { pub fn new() -> Self { DiversifierIndex([0; 11]) @@ -734,6 +748,20 @@ mod tests { assert_eq!(dk.diversifier_index(&Diversifier(d_3)), j_3); } + #[test] + fn diversifier_index_from() { + let di32: u32 = 0xa0b0c0d0; + assert_eq!( + DiversifierIndex::from(di32), + DiversifierIndex([0xd0, 0xc0, 0xb0, 0xa0, 0, 0, 0, 0, 0, 0, 0]) + ); + let di64: u64 = 0x0102030405060708; + assert_eq!( + DiversifierIndex::from(di64), + DiversifierIndex([8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0]) + ); + } + #[test] fn find_diversifier() { let dk = DiversifierKey([0; 32]);