From 616d82faafcb5d49a6d6f974982c09650a5fd8c1 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Thu, 13 Aug 2020 22:58:03 -0400 Subject: [PATCH] Use the jubjub::AffinePoint in the canonical, encoded little-endian byte form in Pedersen tests Keeps the librustzcash test vectors with their big-endian byte order as comments, for greppability. --- Cargo.lock | 16 ---- .../src/commitments/sapling/test_vectors.rs | 94 +++++++++++-------- zebra-chain/src/transaction/serialize.rs | 2 +- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 727e2174..9ab56743 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,16 +222,6 @@ dependencies = [ "radium", ] -[[package]] -name = "bitvec" -version = "0.17.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41262f11d771fd4a61aa3ce019fca363b4b6c282fca9da2a31186d3965a47a5c" -dependencies = [ - "either", - "radium", -] - [[package]] name = "blake2b_simd" version = "0.5.10" @@ -698,12 +688,6 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" -[[package]] -name = "either" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" - [[package]] name = "equihash" version = "0.1.0" diff --git a/zebra-chain/src/commitments/sapling/test_vectors.rs b/zebra-chain/src/commitments/sapling/test_vectors.rs index 6895970e..0a8c0852 100644 --- a/zebra-chain/src/commitments/sapling/test_vectors.rs +++ b/zebra-chain/src/commitments/sapling/test_vectors.rs @@ -5,65 +5,63 @@ // enum variant from the original source. // // The Python hex-encoded outputs for these test vectors were output in -// big-endian byte order, so to parse them, we have to reverse their order; in +// big-endian byte order, so to parse them, we reversed their order; in // librustzcash, they match their Display impl to match the Python hex strings // and that's what they compare in their unit tests, not the bytes. #![allow(dead_code)] use bitvec::prelude::*; -use jubjub::{AffinePoint, Fq}; use lazy_static::lazy_static; -fn point_from_hex>(u_in_hex: T, v_in_hex: T) -> AffinePoint { - let mut u_bytes = [0u8; 32]; - let _ = hex::decode_to_slice(u_in_hex, &mut u_bytes); - // The test vectors were/are output in reverse-endian order. - u_bytes.reverse(); - let u_scalar = Fq::from_bytes(&u_bytes).unwrap(); +fn point_from_hex>(point_in_hex: T) -> jubjub::AffinePoint { + let mut bytes = [0u8; 32]; + let _ = hex::decode_to_slice(point_in_hex, &mut bytes); - let mut v_bytes = [0u8; 32]; - let _ = hex::decode_to_slice(v_in_hex, &mut v_bytes); - // The test vectors were/are output in reverse-endian order. - v_bytes.reverse(); - let v_scalar = Fq::from_bytes(&v_bytes).unwrap(); - - AffinePoint::from_raw_unchecked(u_scalar, v_scalar) + jubjub::AffinePoint::from_bytes(bytes).unwrap() } pub struct TestVector { pub input_bits: BitVec, - pub output_point: AffinePoint, + pub output_point: jubjub::AffinePoint, } lazy_static! { pub static ref TEST_VECTORS: [TestVector; 12] = [ TestVector { input_bits: bitvec![Lsb0, u8; 1, 1, 1, 1, 1, 1], + // original librustzcash affine point test vector (in reversed-endian byte order): + // "06b1187c11ca4fb4383b2e0d0dbbde3ad3617338b5029187ec65a5eaed5e4d0b", + // "3ce70f536652f0dea496393a1e55c4e08b9d55508e16d11e5db40d4810cbc982" output_point: point_from_hex( - "06b1187c11ca4fb4383b2e0d0dbbde3ad3617338b5029187ec65a5eaed5e4d0b", - "3ce70f536652f0dea496393a1e55c4e08b9d55508e16d11e5db40d4810cbc982" + "82c9cb10480db45d1ed1168e50559d8be0c4551e3a3996a4def05266530fe7bc" ) }, TestVector { input_bits: bitvec![Lsb0, u8; 1, 1, 1, 1, 1, 1, 0], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "2fc3bc454c337f71d4f04f86304262fcbfc9ecd808716b92fc42cbe6827f7f1a", + // "46d0d25bf1a654eedc6a9b1e5af398925113959feac31b7a2c036ff9b9ec0638" output_point: point_from_hex( - "2fc3bc454c337f71d4f04f86304262fcbfc9ecd808716b92fc42cbe6827f7f1a", - "46d0d25bf1a654eedc6a9b1e5af398925113959feac31b7a2c036ff9b9ec0638" + "3806ecb9f96f032c7a1bc3ea9f9513519298f35a1e9b6adcee54a6f15bd2d046" ) }, TestVector { input_bits: bitvec![Lsb0, u8; 1, 1, 1, 1, 1, 1, 1], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "4f8ce0e0a9e674b3ab9606a7d7aefba386e81583d81918127814cde41d209d97", + // "312b5ab93b14c9b9af334fe1fe3c50fffb53fbd074fa40ca600febde7c97e346" output_point: point_from_hex( - "4f8ce0e0a9e674b3ab9606a7d7aefba386e81583d81918127814cde41d209d97", - "312b5ab93b14c9b9af334fe1fe3c50fffb53fbd074fa40ca600febde7c97e346" + "46e3977cdeeb0f60ca40fa74d0fb53fbff503cfee14f33afb9c9143bb95a2bb1" ) }, TestVector { input_bits: bitvec![Lsb0, u8; 1, 1, 1, 1, 1, 1, 1, 0, 0], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "4f8ce0e0a9e674b3ab9606a7d7aefba386e81583d81918127814cde41d209d97", + // "312b5ab93b14c9b9af334fe1fe3c50fffb53fbd074fa40ca600febde7c97e346" output_point: point_from_hex( - "4f8ce0e0a9e674b3ab9606a7d7aefba386e81583d81918127814cde41d209d97", - "312b5ab93b14c9b9af334fe1fe3c50fffb53fbd074fa40ca600febde7c97e346" + "46e3977cdeeb0f60ca40fa74d0fb53fbff503cfee14f33afb9c9143bb95a2bb1" ), }, TestVector { @@ -76,9 +74,11 @@ lazy_static! { 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "599ab788360ae8c6d5bb7618aec37056d6227408d857fdc394078a3d7afdfe0f", + // "4320c373da670e28d168f4ffd72b43208e8c815f40841682c57a3ee1d005a527" output_point: point_from_hex( - "599ab788360ae8c6d5bb7618aec37056d6227408d857fdc394078a3d7afdfe0f", - "4320c373da670e28d168f4ffd72b43208e8c815f40841682c57a3ee1d005a527" + "27a505d0e13e7ac5821684405f818c8e20432bd7fff468d1280e67da73c320c3" ), }, TestVector { @@ -91,9 +91,11 @@ lazy_static! { 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "2da510317620f5dfdce1f31db6019f947eedcf02ff2972cff597a5c3ad21f5dd", + // "198789969c0c33e6c359b9da4a51771f4d50863f36beef90436944fe568399f2" output_point: point_from_hex( - "2da510317620f5dfdce1f31db6019f947eedcf02ff2972cff597a5c3ad21f5dd", - "198789969c0c33e6c359b9da4a51771f4d50863f36beef90436944fe568399f2" + "f2998356fe44694390efbe363f86504d1f77514adab959c3e6330c9c96898799" ), }, TestVector { @@ -106,9 +108,11 @@ lazy_static! { 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "601247c7e640992d193dfb51df6ed93446687a7f2bcd0e4a598e6feb1ef20c40", + // "371931733b73e7b95c2cad55a6cebd15c83619f697c64283e54e5ef61442a743" output_point: point_from_hex( - "601247c7e640992d193dfb51df6ed93446687a7f2bcd0e4a598e6feb1ef20c40", - "371931733b73e7b95c2cad55a6cebd15c83619f697c64283e54e5ef61442a743" + "43a74214f65e4ee58342c697f61936c815bdcea655ad2c5cb9e7733b73311937" ) }, TestVector { @@ -141,9 +145,11 @@ lazy_static! { 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "314192ecb1f2d8806a8108704c875a25d9fb7e444f9f373919adedebe8f2ae27", + // "6b12b32f1372ad574799dee9eb591d961b704bf611f55fcc71f7e82cd3330b74" output_point: point_from_hex( - "314192ecb1f2d8806a8108704c875a25d9fb7e444f9f373919adedebe8f2ae27", - "6b12b32f1372ad574799dee9eb591d961b704bf611f55fcc71f7e82cd3330b74" + "740b33d32ce8f771cc5ff511f64b701b961d59ebe9de994757ad72132fb312eb" ), }, TestVector { @@ -177,9 +183,11 @@ lazy_static! { 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "0666c2bce7f362a2b807d212e9a577f116891a932affd7addec39fbf372c494e", + // "6758bccfaf2e47c07756b96edea23aa8d10c33b38220bd1c411af612eeec18ab" output_point: point_from_hex( - "0666c2bce7f362a2b807d212e9a577f116891a932affd7addec39fbf372c494e", - "6758bccfaf2e47c07756b96edea23aa8d10c33b38220bd1c411af612eeec18ab" + "ab18ecee12f61a411cbd2082b3330cd1a83aa2de6eb95677c0472eafcfbc5867" ), }, TestVector { @@ -219,9 +227,11 @@ lazy_static! { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "130afe02b99375484efb0998f5331d2178e1d00e803049bb0769099420624f5f", + // "5e2fc6970554ffe358652aa7968ac4fcf3de0c830e6ea492e01a38fafb68cd71" output_point: point_from_hex( - "130afe02b99375484efb0998f5331d2178e1d00e803049bb0769099420624f5f", - "5e2fc6970554ffe358652aa7968ac4fcf3de0c830e6ea492e01a38fafb68cd71" + "71cd68fbfa381ae092a46e0e830cdef3fcc48a96a72a6558e3ff540597c62fde" ), }, TestVector { @@ -261,9 +271,11 @@ lazy_static! { 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "67914ebd539961b70f468fa23d4cb42133693a8ac57cd35a1e6369fe34fbedf7", + // "44770870c0f0cfe59a10df95d6c21e6f1514a2f464b66377599438c126052d9f" output_point: point_from_hex( - "67914ebd539961b70f468fa23d4cb42133693a8ac57cd35a1e6369fe34fbedf7", - "44770870c0f0cfe59a10df95d6c21e6f1514a2f464b66377599438c126052d9f" + "9f2d0526c13894597763b664f4a214156f1ec2d695df109ae5cff0c0700877c4" ), }, TestVector { @@ -276,9 +288,11 @@ lazy_static! { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ], + // Original librustzcash affine point test vector (in reversed-endian byte order): + // "329e3bb2ca31ea6e13a986730237f6fd16b842a510cbabe851bdbcf57d75ee0d", + // "471d2109656afcb96d0609b371b132b97efcf72c6051064dd19fdc004799bfa9" output_point: point_from_hex( - "329e3bb2ca31ea6e13a986730237f6fd16b842a510cbabe851bdbcf57d75ee0d", - "471d2109656afcb96d0609b371b132b97efcf72c6051064dd19fdc004799bfa9" + "a9bf994700dc9fd14d0651602cf7fc7eb932b171b309066db9fc6a6509211dc7" ), }, ]; diff --git a/zebra-chain/src/transaction/serialize.rs b/zebra-chain/src/transaction/serialize.rs index 91931305..f597b19c 100644 --- a/zebra-chain/src/transaction/serialize.rs +++ b/zebra-chain/src/transaction/serialize.rs @@ -246,7 +246,7 @@ impl ZcashDeserialize for TransparentOutput { fn zcash_deserialize(mut reader: R) -> Result { Ok(TransparentOutput { value: reader.read_u64::()?.try_into()?, - lock_script: tyoes::Script::zcash_deserialize(&mut reader)?, + lock_script: types::Script::zcash_deserialize(&mut reader)?, }) } }