From 60f3f35d894ab74d591dff376ad4c8ce4f5557cb Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 24 Jan 2020 13:33:22 -0800 Subject: [PATCH] Refine Ed25519 byte arrays to ed25519-zebra types. --- Cargo.lock | 27 ++++++++++++++++++++++++ zebra-chain/Cargo.toml | 4 +++- zebra-chain/src/transaction/joinsplit.rs | 7 ++---- zebra-chain/src/transaction/serialize.rs | 26 ++++------------------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2fffc7476..d4ed3ad13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,6 +268,19 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "curve25519-dalek" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26778518a7f6cffa1d25a44b602b62b979bd88adb9e99ffec546998cf3404839" +dependencies = [ + "byteorder", + "digest", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + [[package]] name = "darling" version = "0.10.2" @@ -312,6 +325,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "ed25519-zebra" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dd3113b0008c4cc23688f2a0b9bbffe08d71ade8db480b43e1635c469e1f22b" +dependencies = [ + "curve25519-dalek", + "rand_core 0.5.1", + "serde", + "sha2", + "thiserror", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -1644,6 +1670,7 @@ version = "0.1.0" dependencies = [ "byteorder", "chrono", + "ed25519-zebra", "futures", "hex", "proptest", diff --git a/zebra-chain/Cargo.toml b/zebra-chain/Cargo.toml index ed3155faa..4e575cd07 100644 --- a/zebra-chain/Cargo.toml +++ b/zebra-chain/Cargo.toml @@ -13,8 +13,10 @@ byteorder = "1.3" chrono = "0.4" hex = "0.4" sha2 = "0.8" -redjubjub = "0.1" futures = "0.3" +# ZF deps +redjubjub = "0.1" +ed25519-zebra = "0.1" [dev-dependencies] proptest = "0.9" diff --git a/zebra-chain/src/transaction/joinsplit.rs b/zebra-chain/src/transaction/joinsplit.rs index 8ca6bc9ee..2ac7ea932 100644 --- a/zebra-chain/src/transaction/joinsplit.rs +++ b/zebra-chain/src/transaction/joinsplit.rs @@ -69,12 +69,9 @@ pub struct JoinSplitData { /// all `JoinSplit`s. pub rest: Vec>, /// The public key for the JoinSplit signature. - // XXX refine to a Zcash-flavored Ed25519 pubkey. - pub pub_key: [u8; 32], + pub pub_key: ed25519_zebra::PublicKeyBytes, /// The JoinSplit signature. - // XXX refine to a Zcash-flavored Ed25519 signature. - // for now it's [u64; 8] rather than [u8; 64] to get trait impls - pub sig: [u64; 8], + pub sig: ed25519_zebra::Signature, } impl JoinSplitData

{ diff --git a/zebra-chain/src/transaction/serialize.rs b/zebra-chain/src/transaction/serialize.rs index 534d4dc0c..af578a034 100644 --- a/zebra-chain/src/transaction/serialize.rs +++ b/zebra-chain/src/transaction/serialize.rs @@ -125,18 +125,8 @@ impl ZcashSerialize for JoinSplitData

{ for joinsplit in self.joinsplits() { joinsplit.zcash_serialize(&mut writer)?; } - writer.write_all(&self.pub_key[..])?; - // XXX very ugly, this happens because we used a [u64; 8] instead of - // [u8; 64] to get trait impls and it will disappear when we refine to - // Zcash-flavored Ed25519. - writer.write_all( - &{ - use byteorder::ByteOrder; - let mut bytes = [0u8; 64]; - LittleEndian::write_u64_into(&self.sig[..], &mut bytes); - bytes - }[..], - )?; + writer.write_all(&<[u8; 32]>::from(self.pub_key)[..])?; + writer.write_all(&<[u8; 64]>::from(self.sig)[..])?; Ok(()) } } @@ -152,16 +142,8 @@ impl ZcashDeserialize for Option> { for _ in 0..(n - 1) { rest.push(JoinSplit::zcash_deserialize(&mut reader)?); } - let pub_key = reader.read_32_bytes()?; - // XXX this is horrible, see above, will be removed with type refinement - let sig = { - use byteorder::ByteOrder; - let mut bytes = [0u8; 64]; - reader.read_exact(&mut bytes[..])?; - let mut u64s = [0u64; 8]; - LittleEndian::read_u64_into(&bytes, &mut u64s[..]); - u64s - }; + let pub_key = reader.read_32_bytes()?.into(); + let sig = reader.read_64_bytes()?.into(); Ok(Some(JoinSplitData { first, rest,