From e21be37042bc2a56ec44e1af8fa2eb3db75d3546 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 11 Oct 2018 23:14:46 +0100 Subject: [PATCH 1/9] Block header representation --- zcash_primitives/src/block.rs | 32 ++++++++++++++++++++++++++++++++ zcash_primitives/src/lib.rs | 1 + 2 files changed, 33 insertions(+) create mode 100644 zcash_primitives/src/block.rs diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs new file mode 100644 index 000000000..816975d0b --- /dev/null +++ b/zcash_primitives/src/block.rs @@ -0,0 +1,32 @@ +use std::ops::Deref; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct BlockHash(pub [u8; 32]); + +/// A Zcash block header. +pub struct BlockHeader(BlockHeaderData); + +impl Deref for BlockHeader { + type Target = BlockHeaderData; + + fn deref(&self) -> &BlockHeaderData { + &self.0 + } +} + +pub struct BlockHeaderData { + pub version: i32, + pub prev_block: BlockHash, + pub merkle_root: [u8; 32], + pub final_sapling_root: [u8; 32], + pub time: u32, + pub bits: u32, + pub nonce: [u8; 32], + pub solution: Vec, +} + +impl BlockHeaderData { + pub fn freeze(self) -> BlockHeader { + BlockHeader(self) + } +} diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index 247520938..97b826f50 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -10,6 +10,7 @@ extern crate sapling_crypto; use sapling_crypto::jubjub::JubjubBls12; +pub mod block; pub mod sapling; mod serialize; pub mod transaction; From 20d5cdc571f2da638f4af61b35237bee6ed3b675 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 11 Oct 2018 23:15:29 +0100 Subject: [PATCH 2/9] TxId struct --- zcash_primitives/src/transaction/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 70934e1e2..2993dfd69 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -20,6 +20,9 @@ const OVERWINTER_TX_VERSION: u32 = 3; const SAPLING_VERSION_GROUP_ID: u32 = 0x892F2085; const SAPLING_TX_VERSION: u32 = 4; +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct TxId(pub [u8; 32]); + /// A Zcash transaction. #[derive(Debug)] pub struct Transaction(TransactionData); From a1664c6bbc99aa2df7f1d3f261b6fd82ff90c504 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 11 Oct 2018 23:16:48 +0100 Subject: [PATCH 3/9] impl Display for BlockHash and TxId --- Cargo.lock | 7 +++++++ zcash_primitives/Cargo.toml | 1 + zcash_primitives/src/block.rs | 10 ++++++++++ zcash_primitives/src/lib.rs | 1 + zcash_primitives/src/transaction/mod.rs | 10 ++++++++++ 5 files changed, 29 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 33d7675ef..baffb57db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,6 +190,11 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hex-literal" version = "0.1.1" @@ -435,6 +440,7 @@ dependencies = [ "blake2-rfc 0.2.18 (git+https://github.com/gtank/blake2-rfc?rev=7a5b5fc99ae483a0043db7547fb79a6fa44b88a9)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ff 0.4.0", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "pairing 0.14.2", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -493,6 +499,7 @@ dependencies = [ "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4da5f0e01bd8a71a224a4eedecaacfcabda388dbb7a80faf04d3514287572d95" "checksum hex-literal-impl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d340b6514f232f6db1bd16db65302a5278a04fef9ce867cb932e7e5fa21130a" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index 01e5243ea..ab0d87b32 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -8,6 +8,7 @@ authors = [ [dependencies] byteorder = "1" ff = { path = "../ff" } +hex = "0.3" lazy_static = "1" pairing = { path = "../pairing" } rand = "0.4" diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs index 816975d0b..d0c47b300 100644 --- a/zcash_primitives/src/block.rs +++ b/zcash_primitives/src/block.rs @@ -1,8 +1,18 @@ +use hex; +use std::fmt; use std::ops::Deref; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct BlockHash(pub [u8; 32]); +impl fmt::Display for BlockHash { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let mut data = self.0.to_vec(); + data.reverse(); + formatter.write_str(&hex::encode(data)) + } +} + /// A Zcash block header. pub struct BlockHeader(BlockHeaderData); diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index 97b826f50..ba76dc739 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -4,6 +4,7 @@ extern crate lazy_static; extern crate blake2_rfc; extern crate byteorder; extern crate ff; +extern crate hex; extern crate pairing; extern crate rand; extern crate sapling_crypto; diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 2993dfd69..6aea0cfca 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -1,5 +1,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use hex; use sapling_crypto::redjubjub::Signature; +use std::fmt; use std::io::{self, Read, Write}; use std::ops::Deref; @@ -23,6 +25,14 @@ const SAPLING_TX_VERSION: u32 = 4; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct TxId(pub [u8; 32]); +impl fmt::Display for TxId { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let mut data = self.0.to_vec(); + data.reverse(); + formatter.write_str(&hex::encode(data)) + } +} + /// A Zcash transaction. #[derive(Debug)] pub struct Transaction(TransactionData); From 670bb277e997f6298c503ebdbb8a0662a35cd1b0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 22 Oct 2018 23:51:04 +0100 Subject: [PATCH 4/9] Block header serialisation --- zcash_primitives/src/block.rs | 167 ++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs index d0c47b300..7ca889110 100644 --- a/zcash_primitives/src/block.rs +++ b/zcash_primitives/src/block.rs @@ -1,7 +1,11 @@ +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use hex; use std::fmt; +use std::io::{self, Read, Write}; use std::ops::Deref; +use serialize::Vector; + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct BlockHash(pub [u8; 32]); @@ -40,3 +44,166 @@ impl BlockHeaderData { BlockHeader(self) } } + +impl BlockHeader { + pub fn read(mut reader: R) -> io::Result { + let version = reader.read_i32::()?; + + let mut prev_block = BlockHash([0; 32]); + reader.read_exact(&mut prev_block.0)?; + + let mut merkle_root = [0; 32]; + reader.read_exact(&mut merkle_root)?; + + let mut final_sapling_root = [0; 32]; + reader.read_exact(&mut final_sapling_root)?; + + let time = reader.read_u32::()?; + let bits = reader.read_u32::()?; + + let mut nonce = [0; 32]; + reader.read_exact(&mut nonce)?; + + let solution = Vector::read(&mut reader, |r| r.read_u8())?; + + Ok(BlockHeader(BlockHeaderData { + version, + prev_block, + merkle_root, + final_sapling_root, + time, + bits, + nonce, + solution, + })) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_i32::(self.version)?; + writer.write_all(&self.prev_block.0)?; + writer.write_all(&self.merkle_root)?; + writer.write_all(&self.final_sapling_root)?; + writer.write_u32::(self.time)?; + writer.write_u32::(self.bits)?; + writer.write_all(&self.nonce)?; + Vector::write(&mut writer, &self.solution, |w, b| w.write_u8(*b))?; + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::BlockHeader; + + const HEADER_MAINNET_415000: [u8; 1487] = [ + 0x04, 0x00, 0x00, 0x00, 0x52, 0x74, 0xb4, 0x3b, 0x9e, 0x4a, 0xd8, 0xf4, 0x3e, 0x93, 0xf7, + 0x84, 0x63, 0xd2, 0x4d, 0xcf, 0xe5, 0x31, 0xae, 0xb4, 0x71, 0x98, 0x19, 0xf4, 0xf9, 0x7f, + 0x7e, 0x03, 0x00, 0x00, 0x00, 0x00, 0x66, 0x30, 0x73, 0xbc, 0x4b, 0xfa, 0x95, 0xc9, 0xbe, + 0xc3, 0x6a, 0xad, 0x72, 0x68, 0xa5, 0x73, 0x04, 0x97, 0x97, 0xbd, 0xfc, 0x5a, 0xa4, 0xc7, + 0x43, 0xfb, 0xe4, 0x82, 0x0a, 0xa3, 0x93, 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xbe, 0xcc, 0x5b, 0xe1, + 0xab, 0x03, 0x1c, 0xc2, 0xfd, 0x60, 0x7c, 0x77, 0x6a, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x3e, 0xb2, 0x18, 0x19, 0xfd, 0x40, 0x05, 0x00, 0x94, 0x9d, 0x55, 0xde, 0x0c, 0xc6, + 0x33, 0xe0, 0xcc, 0xe4, 0x1e, 0x46, 0x49, 0xef, 0x4a, 0xa3, 0x34, 0x9f, 0x01, 0x00, 0x29, + 0x0f, 0xfe, 0x28, 0x1b, 0x94, 0x7b, 0x3b, 0x53, 0xfb, 0xd2, 0xf3, 0x5b, 0x1c, 0xe2, 0x92, + 0x64, 0x9b, 0x96, 0xac, 0x6e, 0x08, 0x83, 0xaf, 0x3a, 0x68, 0x44, 0xb9, 0x55, 0x92, 0xe7, + 0x45, 0x56, 0xda, 0x34, 0x4b, 0x47, 0x01, 0x96, 0x1c, 0xd4, 0x13, 0x0c, 0x68, 0x21, 0x9c, + 0xfa, 0x13, 0x41, 0xd5, 0xaf, 0xb5, 0x04, 0x9e, 0xb0, 0xe8, 0xbe, 0x4a, 0x2d, 0x92, 0xd6, + 0x78, 0xc4, 0x07, 0x85, 0xe3, 0x37, 0x05, 0x54, 0x8b, 0x5f, 0x3a, 0x54, 0xf0, 0xa4, 0xc3, + 0x9a, 0x2f, 0x58, 0xee, 0x78, 0x4a, 0x24, 0x16, 0x3c, 0xd8, 0x6f, 0x54, 0x81, 0x23, 0x27, + 0xdf, 0x55, 0xe1, 0xd5, 0x5c, 0xa8, 0x4b, 0x6e, 0x7b, 0x88, 0x7a, 0x7c, 0xb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3a, 0x52, 0x41, 0x96, 0xcf, 0x05, 0xd3, 0xe5, 0x24, 0x66, 0x05, 0x49, 0xff, 0xe7, 0xbd, + 0x65, 0x68, 0x05, 0x71, 0x35, 0xff, 0xd5, 0xaf, 0xd9, 0x43, 0xf6, 0xda, 0x11, 0xcb, 0xb5, + 0x97, 0xe8, 0xcc, 0xec, 0xd7, 0x7e, 0xcb, 0xe9, 0x09, 0xde, 0x06, 0x31, 0xbf, 0xa2, 0x9c, + 0xd3, 0xe3, 0xd5, 0x54, 0x46, 0x71, 0xba, 0x80, 0x25, 0x61, 0x53, 0xd6, 0xe9, 0x99, 0x0b, + 0x88, 0xad, 0x8e, 0x0c, 0xf4, 0x98, 0x9b, 0xef, 0x4b, 0xe4, 0x57, 0xf9, 0xc7, 0xb0, 0xf1, + 0xaa, 0xcd, 0x6e, 0x0e, 0xf3, 0x20, 0x60, 0x5c, 0x29, 0xed, 0x0c, 0xd2, 0xeb, 0x6c, 0xfc, + 0xe2, 0x16, 0xc5, 0x2a, 0x31, 0x75, 0x80, 0x20, 0x1c, 0xad, 0x7a, 0x09, 0x43, 0xd2, 0x4b, + 0x7b, 0x06, 0xd5, 0xbf, 0x75, 0x87, 0x61, 0xdd, 0x96, 0xe1, 0x19, 0x70, 0xb5, 0xde, 0xd6, + 0x97, 0x22, 0x2b, 0x2c, 0x77, 0xe7, 0xf2, 0x56, 0xa6, 0x05, 0xac, 0x75, 0x55, 0x49, 0xc1, + 0x65, 0x1f, 0x25, 0xad, 0xfc, 0x9d, 0x53, 0xd9, 0x11, 0x7e, 0x3a, 0x0b, 0xb4, 0x09, 0xee, + 0xe4, 0xa6, 0x00, 0x12, 0x04, 0x72, 0x94, 0x9c, 0x7d, 0xda, 0x1c, 0x2e, 0xdb, 0x3c, 0x33, + 0x0c, 0x7f, 0x96, 0x17, 0x99, 0x82, 0x91, 0x64, 0x57, 0xd3, 0x31, 0xe9, 0x63, 0x09, 0xdd, + 0x24, 0xdf, 0x74, 0xee, 0xdd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22, 0x87, 0x27, 0x46, + 0xda, 0x10, 0xa1, 0x77, 0x1c, 0xeb, 0x02, 0xdd, 0x8a, 0xac, 0x01, 0xba, 0x18, 0x6b, 0xf1, + 0x48, 0x86, 0x30, 0x47, 0x9e, 0x12, 0x84, 0xda, 0x01, 0x90, 0xfc, 0xe8, 0xb5, 0x9a, 0xc6, + 0xb0, 0xfd, 0x41, 0x6b, 0xee, 0x56, 0xb7, 0x2f, 0x0a, 0x58, 0x45, 0x15, 0x35, 0x57, 0xff, + 0x0f, 0x49, 0x50, 0xa0, 0xdc, 0x5b, 0xe6, 0x5c, 0xe9, 0x42, 0xd2, 0x2e, 0x18, 0x53, 0x4c, + 0x4e, 0x0e, 0xfa, 0xbb, 0x2d, 0x15, 0x25, 0xdc, 0x48, 0x58, 0xb9, 0xb0, 0xf7, 0x7d, 0x47, + 0x4a, 0x12, 0x5e, 0xbc, 0x25, 0x0e, 0x08, 0xfe, 0xdb, 0xfa, 0xa6, 0x6f, 0x45, 0x3d, 0x90, + 0x93, 0x2c, 0xab, 0x3f, 0xf4, 0x52, 0x21, 0x90, 0x99, 0x68, 0xe5, 0x1e, 0x6b, 0xc2, 0x54, + 0xd5, 0x09, 0xad, 0xeb, 0x75, 0xcb, 0xa7, 0x6d, 0x48, 0xfe, 0x02, 0x4e, 0x3e, 0x66, 0xd8, + 0xdf, 0x5e, + ]; + + #[test] + fn header_read_write() { + let header = BlockHeader::read(&HEADER_MAINNET_415000[..]).unwrap(); + let mut encoded = Vec::with_capacity(HEADER_MAINNET_415000.len()); + header.write(&mut encoded).unwrap(); + assert_eq!(&HEADER_MAINNET_415000[..], &encoded[..]); + } +} From 4289843852e2d59b4addf5627492be3277f3870f Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 3 Dec 2018 12:54:11 +0000 Subject: [PATCH 5/9] Compute TxId for Transaction --- Cargo.lock | 70 +++++++++++++++++++++++ zcash_primitives/Cargo.toml | 1 + zcash_primitives/src/lib.rs | 1 + zcash_primitives/src/transaction/mod.rs | 25 ++++++-- zcash_primitives/src/transaction/tests.rs | 33 ++--------- 5 files changed, 98 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index baffb57db..da35e288f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,6 +72,17 @@ dependencies = [ "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "block-buffer" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-padding 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "block-cipher-trait" version = "0.5.3" @@ -80,11 +91,24 @@ dependencies = [ "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "block-padding" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "byte-tools" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byte-tools" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "1.2.2" @@ -108,6 +132,19 @@ dependencies = [ "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "digest" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ff" version = "0.4.0" @@ -182,6 +219,14 @@ dependencies = [ "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "generic-array" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "group" version = "0.1.0" @@ -280,6 +325,11 @@ name = "opaque-debug" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "opaque-debug" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pairing" version = "0.14.2" @@ -376,6 +426,17 @@ dependencies = [ "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sha2" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "stream-cipher" version = "0.1.1" @@ -445,6 +506,7 @@ dependencies = [ "pairing 0.14.2", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "sapling-crypto 0.0.1", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -486,18 +548,24 @@ dependencies = [ "checksum bit-vec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "02b4ff8b16e6076c3e14220b39fbc1fabb6737522281a388998046859400895f" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" "checksum blake2-rfc 0.2.18 (git+https://github.com/gtank/blake2-rfc?rev=7a5b5fc99ae483a0043db7547fb79a6fa44b88a9)" = "" +"checksum block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49665c62e0e700857531fa5d3763e91b539ff1abeebd56808d378b495870d60d" "checksum block-cipher-trait 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "370424437b9459f3dfd68428ed9376ddfe03d8b70ede29cc533b3557df186ab4" +"checksum block-padding 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc4358306e344bf9775d0197fd00d2603e5afb0771bb353538630f022068ea3" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" +"checksum byte-tools 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "980479e6fde23246dfb54d47580d66b4e99202e7579c5eaa9fe10ecb5ebd2182" "checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" "checksum crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24ce9782d4d5c53674646a6a4c1863a21a8fc0cb649b3c94dfc16e45071dea19" "checksum digest 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "00a49051fef47a72c9623101b19bd71924a45cca838826caae3eaa4d00772603" +"checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" +"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum fpe 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce3371c82bfbd984f624cab093f55e7336f5a6e589f8518e1258f54f011b89ad" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "1a70b146671de62ec8c8ed572219ca5d594d9b06c0b364d5e67b722fc559b48c" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" +"checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4da5f0e01bd8a71a224a4eedecaacfcabda388dbb7a80faf04d3514287572d95" @@ -510,6 +578,7 @@ dependencies = [ "checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d620c9c26834b34f039489ac0dfdb12c7ac15ccaf818350a64c9b5334a452ad7" +"checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" "checksum proc-macro-hack 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ba8d4f9257b85eb6cdf13f055cea3190520aab1409ca2ab43493ea4820c25f0" "checksum proc-macro-hack-impl 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5cb6f960ad471404618e9817c0e5d10b1ae74cfdf01fab89ea0641fe7fb2892" "checksum proc-macro2 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)" = "b331c6ad3411474cd55540398dc7ad89fc41488e64ec71fdecc9c9b86de96fb0" @@ -519,6 +588,7 @@ dependencies = [ "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum stream-cipher 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "30dc6118470d69ce0fdcf7e6f95e95853f7f4f72f80d835d4519577c323814ab" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index ab0d87b32..aac2e3b7c 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -13,6 +13,7 @@ lazy_static = "1" pairing = { path = "../pairing" } rand = "0.4" sapling-crypto = { path = "../sapling-crypto" } +sha2 = "0.8" [dependencies.blake2-rfc] git = "https://github.com/gtank/blake2-rfc" diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index ba76dc739..727d446b9 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -8,6 +8,7 @@ extern crate hex; extern crate pairing; extern crate rand; extern crate sapling_crypto; +extern crate sha2; use sapling_crypto::jubjub::JubjubBls12; diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 6aea0cfca..4f5d2a633 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -1,6 +1,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use hex; use sapling_crypto::redjubjub::Signature; +use sha2::{Digest, Sha256}; use std::fmt; use std::io::{self, Read, Write}; use std::ops::Deref; @@ -35,7 +36,7 @@ impl fmt::Display for TxId { /// A Zcash transaction. #[derive(Debug)] -pub struct Transaction(TransactionData); +pub struct Transaction(TransactionData, TxId); impl Deref for Transaction { type Target = TransactionData; @@ -125,12 +126,26 @@ impl TransactionData { header } - pub fn freeze(self) -> Transaction { - Transaction(self) + pub fn freeze(self) -> io::Result { + Transaction::from_data(self) } } impl Transaction { + fn from_data(data: TransactionData) -> io::Result { + let mut tx = Transaction(data, TxId([0; 32])); + let mut raw = vec![]; + tx.write(&mut raw)?; + (tx.1) + .0 + .copy_from_slice(&Sha256::digest(&Sha256::digest(&raw))); + Ok(tx) + } + + pub fn txid(&self) -> TxId { + self.1 + } + pub fn read(mut reader: R) -> io::Result { let header = reader.read_u32::()?; let overwintered = (header >> 31) == 1; @@ -195,7 +210,7 @@ impl Transaction { false => None, }; - Ok(Transaction(TransactionData { + Transaction::from_data(TransactionData { overwintered, version, version_group_id, @@ -210,7 +225,7 @@ impl Transaction { joinsplit_pubkey, joinsplit_sig, binding_sig, - })) + }) } pub fn write(&self, mut writer: W) -> io::Result<()> { diff --git a/zcash_primitives/src/transaction/tests.rs b/zcash_primitives/src/transaction/tests.rs index db3a4dfa1..80513afe6 100644 --- a/zcash_primitives/src/transaction/tests.rs +++ b/zcash_primitives/src/transaction/tests.rs @@ -159,51 +159,33 @@ fn tx_read_write() { #[test] fn tx_write_rejects_unexpected_joinsplit_pubkey() { // Succeeds without a JoinSplit pubkey - { - let tx = TransactionData::new().freeze(); - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_ok()); - } + assert!(TransactionData::new().freeze().is_ok()); // Fails with an unexpected JoinSplit pubkey { let mut tx = TransactionData::new(); tx.joinsplit_pubkey = Some([0; 32]); - let tx = tx.freeze(); - - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_err()); + assert!(tx.freeze().is_err()); } } #[test] fn tx_write_rejects_unexpected_joinsplit_sig() { // Succeeds without a JoinSplit signature - { - let tx = TransactionData::new().freeze(); - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_ok()); - } + assert!(TransactionData::new().freeze().is_ok()); // Fails with an unexpected JoinSplit signature { let mut tx = TransactionData::new(); tx.joinsplit_sig = Some([0; 64]); - let tx = tx.freeze(); - - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_err()); + assert!(tx.freeze().is_err()); } } #[test] fn tx_write_rejects_unexpected_binding_sig() { // Succeeds without a binding signature - { - let tx = TransactionData::new().freeze(); - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_ok()); - } + assert!(TransactionData::new().freeze().is_ok()); // Fails with an unexpected binding signature { @@ -218,10 +200,7 @@ fn tx_write_rejects_unexpected_binding_sig() { let mut tx = TransactionData::new(); tx.binding_sig = Some(sig); - let tx = tx.freeze(); - - let mut encoded = Vec::new(); - assert!(tx.write(&mut encoded).is_err()); + assert!(tx.freeze().is_err()); } } From b856d230691609de965aa5515300fceba7e167fb Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 7 Mar 2019 23:43:58 +0000 Subject: [PATCH 6/9] Reverse a clone of [u8; 32] instead of allocating --- zcash_primitives/src/block.rs | 2 +- zcash_primitives/src/transaction/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs index 7ca889110..05b65e045 100644 --- a/zcash_primitives/src/block.rs +++ b/zcash_primitives/src/block.rs @@ -11,7 +11,7 @@ pub struct BlockHash(pub [u8; 32]); impl fmt::Display for BlockHash { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - let mut data = self.0.to_vec(); + let mut data = self.0.clone(); data.reverse(); formatter.write_str(&hex::encode(data)) } diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 4f5d2a633..b1a877be7 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -28,7 +28,7 @@ pub struct TxId(pub [u8; 32]); impl fmt::Display for TxId { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - let mut data = self.0.to_vec(); + let mut data = self.0.clone(); data.reverse(); formatter.write_str(&hex::encode(data)) } From 663f9d619d751eed091577c2ec00ee1d0a8b5756 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 2 Apr 2019 01:29:22 +0100 Subject: [PATCH 7/9] Use named fields in Transaction struct --- zcash_primitives/src/transaction/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index b1a877be7..93c741d47 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -36,13 +36,16 @@ impl fmt::Display for TxId { /// A Zcash transaction. #[derive(Debug)] -pub struct Transaction(TransactionData, TxId); +pub struct Transaction { + txid: TxId, + data: TransactionData, +} impl Deref for Transaction { type Target = TransactionData; fn deref(&self) -> &TransactionData { - &self.0 + &self.data } } @@ -133,17 +136,20 @@ impl TransactionData { impl Transaction { fn from_data(data: TransactionData) -> io::Result { - let mut tx = Transaction(data, TxId([0; 32])); + let mut tx = Transaction { + txid: TxId([0; 32]), + data, + }; let mut raw = vec![]; tx.write(&mut raw)?; - (tx.1) + tx.txid .0 .copy_from_slice(&Sha256::digest(&Sha256::digest(&raw))); Ok(tx) } pub fn txid(&self) -> TxId { - self.1 + self.txid } pub fn read(mut reader: R) -> io::Result { From 3501365950ece709f7f741bbedce477c43d15569 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 2 Apr 2019 01:29:48 +0100 Subject: [PATCH 8/9] Test Transaction::txid() --- zcash_primitives/src/transaction/tests.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zcash_primitives/src/transaction/tests.rs b/zcash_primitives/src/transaction/tests.rs index 80513afe6..1275bbacd 100644 --- a/zcash_primitives/src/transaction/tests.rs +++ b/zcash_primitives/src/transaction/tests.rs @@ -150,6 +150,10 @@ fn tx_read_write() { 0x9b, 0x27, 0xdb, 0x5f, 0x95, 0xfd, 0x09, 0xa3, 0x6b, 0x05, ]; let tx = Transaction::read(&data[..]).unwrap(); + assert_eq!( + format!("{}", tx.txid()), + "64f0bd7fe30ce23753358fe3a2dc835b8fba9c0274c4e2c54a6f73114cb55639" + ); let mut encoded = Vec::with_capacity(data.len()); tx.write(&mut encoded).unwrap(); From 6c99d71d4f0c2980b4619f421abbb5be07c4efaf Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 2 Apr 2019 01:30:00 +0100 Subject: [PATCH 9/9] cargo fmt --- zcash_primitives/src/transaction/components.rs | 2 +- zcash_primitives/src/transaction/mod.rs | 6 +++--- zip32/src/lib.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/zcash_primitives/src/transaction/components.rs b/zcash_primitives/src/transaction/components.rs index 981dc4c6b..b8153f310 100644 --- a/zcash_primitives/src/transaction/components.rs +++ b/zcash_primitives/src/transaction/components.rs @@ -218,7 +218,7 @@ impl SpendDescription { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Missing spend auth signature", - )) + )); } } } diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 93c741d47..35e042a15 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -275,7 +275,7 @@ impl Transaction { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Missing JoinSplit pubkey", - )) + )); } } match self.joinsplit_sig { @@ -284,7 +284,7 @@ impl Transaction { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Missing JoinSplit signature", - )) + )); } } } @@ -312,7 +312,7 @@ impl Transaction { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Missing binding signature", - )) + )); } } } else if self.binding_sig.is_some() { diff --git a/zip32/src/lib.rs b/zip32/src/lib.rs index b0135e95f..0011b09cc 100644 --- a/zip32/src/lib.rs +++ b/zip32/src/lib.rs @@ -184,7 +184,7 @@ impl FullViewingKey { return Err(io::Error::new( io::ErrorKind::InvalidData, "ak not of prime order", - )) + )); } }; @@ -195,7 +195,7 @@ impl FullViewingKey { return Err(io::Error::new( io::ErrorKind::InvalidData, "nk not of prime order", - )) + )); } };