Skip to content

Commit

Permalink
ZIP-244: fix sighash test; add roundtrip test; update vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Jul 6, 2021
1 parent 4d91268 commit 4a27490
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
27 changes: 23 additions & 4 deletions zebra-chain/src/transaction/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,23 @@ fn fake_v5_librustzcash_round_trip_for_network(network: Network) {
}
}

#[test]
fn zip244_round_trip() -> Result<()> {
zebra_test::init();

for test in zip0244::TEST_VECTORS.iter() {
let transaction = test.tx.zcash_deserialize_into::<Transaction>()?;
let reencoded = transaction.zcash_serialize_to_vec()?;
assert_eq!(test.tx, reencoded);

let _alt_tx: zcash_primitives::transaction::Transaction = (&transaction)
.try_into()
.expect("librustzcash deserialization must work for zebra serialized transactions");
}

Ok(())
}

#[test]
fn zip244_txid() -> Result<()> {
zebra_test::init();
Expand Down Expand Up @@ -942,11 +959,12 @@ fn test_vec243_3() -> Result<()> {
fn zip244_sighash() -> Result<()> {
zebra_test::init();

for test in zip0244::TEST_VECTORS.iter() {
for (i, test) in zip0244::TEST_VECTORS.iter().enumerate() {
let transaction = test.tx.zcash_deserialize_into::<Transaction>()?;
let input = match test.amount {
Some(amount) => Some((
0,
test.transparent_input
.expect("test vector must have transparent_input when it has amount"),
transparent::Output {
value: amount.try_into()?,
lock_script: transparent::Script::new(
Expand All @@ -958,8 +976,9 @@ fn zip244_sighash() -> Result<()> {
)),
None => None,
};
let h = transaction.sighash(NetworkUpgrade::Nu5, HashType::ALL, input);
assert_eq!(h.as_ref(), test.sighash_all);
let result = hex::encode(transaction.sighash(NetworkUpgrade::Nu5, HashType::ALL, input));
let expected = hex::encode(test.sighash_all);
assert_eq!(expected, result, "test #{}: sighash does not match", i);
}

Ok(())
Expand Down
52 changes: 52 additions & 0 deletions zebra-test/src/zip0244.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub struct TestVector {
pub tx: Vec<u8>,
/// The expected transaction ID.
pub txid: [u8; 32],
/// The expected auth digest.
pub auth_digest: [u8; 32],
/// Which transparent input the ID refers to, if any.
pub transparent_input: Option<u32>,
/// The script code for the given transparent input, if any.
Expand Down Expand Up @@ -288,6 +290,11 @@ lazy_static! {
0x8b, 0x3c, 0x87, 0xc7, 0x4a, 0x2a, 0x63, 0xa2, 0x89, 0xd3, 0x00, 0x05, 0xda, 0xd6,
0x98, 0x3d, 0x95, 0x44,
],
auth_digest: [
0x1c, 0xbb, 0xe0, 0xd7, 0x25, 0x4c, 0xa6, 0x52, 0xcd, 0xda, 0xa7, 0xa9, 0xd2, 0x91,
0x5a, 0x60, 0x9e, 0x35, 0x73, 0xc0, 0x3d, 0x27, 0x05, 0xe9, 0xad, 0xd4, 0xe3, 0x2e,
0xec, 0x0d, 0x76, 0x9e,
],
transparent_input: Some(0),
script_code: Some(vec![0x65, 0x00, 0x51]),
amount: Some(570688904498311),
Expand Down Expand Up @@ -459,6 +466,11 @@ lazy_static! {
0xa5, 0xe4, 0xfc, 0xb7, 0xbf, 0xa7, 0xda, 0x79, 0x29, 0xbf, 0xb7, 0x31, 0xac, 0x10,
0xa5, 0x8a, 0xb0, 0x03,
],
auth_digest: [
0x64, 0xed, 0x51, 0x50, 0x16, 0x96, 0xf1, 0x14, 0x32, 0xb8, 0xa1, 0xe2, 0xe6, 0x87,
0xb8, 0x9e, 0x61, 0x25, 0x97, 0xfd, 0x47, 0x49, 0xf9, 0x2c, 0x0f, 0x5f, 0x51, 0x71,
0xfc, 0xad, 0x78, 0xbe,
],
transparent_input: None,
script_code: None,
amount: None,
Expand All @@ -483,6 +495,11 @@ lazy_static! {
0xf9, 0x57, 0x6a, 0xb7, 0x6b, 0xff, 0xc7, 0x1b, 0xcd, 0x98, 0x5b, 0x62, 0xdd, 0xd6,
0x9d, 0x29, 0x97, 0xcd,
],
auth_digest: [
0x04, 0xda, 0x78, 0xb6, 0x64, 0x11, 0x1d, 0xe8, 0xe4, 0xfc, 0xfc, 0x14, 0x93, 0x3d,
0x79, 0xf6, 0xd9, 0x60, 0xda, 0xd8, 0xf1, 0x08, 0xf1, 0xe0, 0xb4, 0x26, 0xcc, 0x20,
0x36, 0x29, 0x22, 0xed,
],
transparent_input: None,
script_code: None,
amount: None,
Expand All @@ -508,6 +525,11 @@ lazy_static! {
0xe3, 0x4f, 0xd5, 0xf9, 0xc7, 0x9c, 0x9f, 0x78, 0xdc, 0x65, 0x05, 0x1a, 0x9a, 0x14,
0xca, 0xc2, 0xb1, 0xbf,
],
auth_digest: [
0x04, 0xda, 0x78, 0xb6, 0x64, 0x11, 0x1d, 0xe8, 0xe4, 0xfc, 0xfc, 0x14, 0x93, 0x3d,
0x79, 0xf6, 0xd9, 0x60, 0xda, 0xd8, 0xf1, 0x08, 0xf1, 0xe0, 0xb4, 0x26, 0xcc, 0x20,
0x36, 0x29, 0x22, 0xed,
],
transparent_input: None,
script_code: None,
amount: None,
Expand Down Expand Up @@ -544,6 +566,11 @@ lazy_static! {
0x90, 0x93, 0x03, 0xd4, 0x58, 0x0e, 0x72, 0x3a, 0xd3, 0x16, 0x2c, 0x06, 0x09, 0x66,
0x48, 0xa2, 0x5b, 0x1e,
],
auth_digest: [
0x9e, 0x7c, 0x68, 0x39, 0xb3, 0x1f, 0xb3, 0xe9, 0x12, 0xb6, 0x93, 0xd1, 0x87, 0x9f,
0xbb, 0xad, 0xdb, 0xe7, 0xa7, 0x4a, 0x50, 0x8e, 0x7b, 0x6d, 0x1f, 0xe1, 0x93, 0x82,
0x68, 0xc0, 0x4a, 0xb3,
],
transparent_input: Some(1),
script_code: Some(vec![0xac, 0x00, 0x00]),
amount: Some(693972628630138),
Expand Down Expand Up @@ -800,6 +827,11 @@ lazy_static! {
0x1f, 0x14, 0x12, 0xe3, 0x8f, 0x89, 0x13, 0x07, 0x52, 0x80, 0xcd, 0x2b, 0x38, 0x02,
0xf5, 0xb2, 0x70, 0x4e,
],
auth_digest: [
0xee, 0x3a, 0x4a, 0x6d, 0xd3, 0x89, 0xf2, 0x81, 0xb6, 0xd6, 0xe3, 0xd8, 0xe0, 0xf7,
0x97, 0xa3, 0xd4, 0xfa, 0x01, 0x4e, 0x3f, 0x41, 0x6a, 0x0e, 0x47, 0x68, 0x4f, 0x76,
0x02, 0x15, 0x44, 0x58,
],
transparent_input: None,
script_code: None,
amount: None,
Expand Down Expand Up @@ -831,6 +863,11 @@ lazy_static! {
0xbc, 0xdf, 0x2d, 0x35, 0xd0, 0x13, 0x55, 0x80, 0x4b, 0xf0, 0xe3, 0xee, 0x1b, 0x17,
0xde, 0x9e, 0x46, 0xcd,
],
auth_digest: [
0x2c, 0xbe, 0xf7, 0x5b, 0x3e, 0x2c, 0xff, 0x88, 0xb6, 0xf1, 0x20, 0xbf, 0x70, 0xcb,
0x3e, 0xc7, 0x57, 0xec, 0x25, 0xd3, 0x11, 0xfa, 0xca, 0xfd, 0xfe, 0x2d, 0xb4, 0x2e,
0xd3, 0x42, 0x04, 0x0f,
],
transparent_input: Some(0),
script_code: Some(vec![0x63, 0x52, 0x51, 0x63, 0x53]),
amount: Some(107504874564564),
Expand Down Expand Up @@ -1092,6 +1129,11 @@ lazy_static! {
0x2a, 0xd9, 0x06, 0x50, 0x34, 0x22, 0x5d, 0xad, 0x9c, 0x89, 0xbf, 0xcb, 0x73, 0x32,
0x8d, 0x3d, 0x4f, 0xe6,
],
auth_digest: [
0xbb, 0x48, 0xcf, 0x27, 0x71, 0x54, 0x6c, 0x2c, 0x15, 0x57, 0x7d, 0xb2, 0x0d, 0x04,
0x86, 0x5a, 0x8c, 0xc0, 0x0c, 0x9c, 0x02, 0xec, 0xb3, 0x10, 0x24, 0x94, 0x31, 0x0f,
0x18, 0x68, 0x50, 0xcf,
],
transparent_input: None,
script_code: None,
amount: None,
Expand Down Expand Up @@ -1200,6 +1242,11 @@ lazy_static! {
0x01, 0x54, 0xeb, 0x2d, 0xeb, 0x76, 0x78, 0x74, 0xa3, 0x1b, 0x5d, 0x10, 0xaa, 0xf7,
0x6b, 0xa8, 0x4f, 0xae,
],
auth_digest: [
0xf1, 0x58, 0x29, 0x64, 0xb8, 0xc9, 0xec, 0x20, 0x29, 0xab, 0x97, 0xa2, 0x55, 0x98,
0xdb, 0xff, 0x28, 0x35, 0x23, 0xe6, 0xf3, 0x7a, 0xdb, 0x19, 0xcc, 0x57, 0x69, 0xb5,
0x13, 0xc7, 0x33, 0xc0,
],
transparent_input: None,
script_code: None,
amount: None,
Expand Down Expand Up @@ -1497,6 +1544,11 @@ lazy_static! {
0xde, 0x07, 0x15, 0x27, 0x5d, 0x15, 0x6c, 0xda, 0xb9, 0x6f, 0x68, 0xdc, 0x70, 0x10,
0x58, 0x3b, 0x02, 0xaa,
],
auth_digest: [
0x87, 0x46, 0xc9, 0x9d, 0x98, 0x57, 0x86, 0xb1, 0xb7, 0x7c, 0x40, 0x5f, 0x6e, 0xe8,
0x31, 0xe2, 0x98, 0x71, 0xca, 0x9d, 0x68, 0xe7, 0x72, 0x4f, 0xb7, 0xf8, 0x78, 0x6f,
0x28, 0x18, 0x42, 0xa6,
],
transparent_input: Some(0),
script_code: Some(vec![]),
amount: Some(1405243945822387),
Expand Down

0 comments on commit 4a27490

Please sign in to comment.