Merge pull request #474 from nuttycom/enforce_typecode_order

Unified container parsing enforces typecode order.
This commit is contained in:
Kris Nuttycombe 2022-01-04 16:09:55 -07:00 committed by GitHub
commit 7801dddf35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 196 deletions

View File

@ -25,9 +25,9 @@ pub enum Typecode {
Unknown(u32),
}
impl Ord for Typecode {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match (self, other) {
impl Typecode {
pub fn preference_order(a: &Self, b: &Self) -> cmp::Ordering {
match (a, b) {
// Trivial equality checks.
(Self::Orchard, Self::Orchard)
| (Self::Sapling, Self::Sapling)
@ -55,11 +55,9 @@ impl Ord for Typecode {
(_, Self::P2pkh) => cmp::Ordering::Greater,
}
}
}
impl PartialOrd for Typecode {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
pub fn encoding_order(a: &Self, b: &Self) -> cmp::Ordering {
u32::from(*a).cmp(&u32::from(*b))
}
}
@ -109,6 +107,8 @@ pub enum ParseError {
InvalidTypecodeValue(u64),
/// The string is an invalid encoding.
InvalidEncoding(String),
/// The items in the unified container are not in typecode order.
InvalidTypecodeOrder,
/// The unified container only contains transparent items.
OnlyTransparent,
/// The string is not Bech32m encoded, and so cannot be a unified address.
@ -124,6 +124,7 @@ impl fmt::Display for ParseError {
ParseError::DuplicateTypecode(c) => write!(f, "Duplicate typecode {}", u32::from(*c)),
ParseError::InvalidTypecodeValue(v) => write!(f, "Typecode value out of range {}", v),
ParseError::InvalidEncoding(msg) => write!(f, "Invalid encoding: {}", msg),
ParseError::InvalidTypecodeOrder => write!(f, "Items are out of order."),
ParseError::OnlyTransparent => write!(f, "UA only contains transparent items"),
ParseError::NotUnified => write!(f, "Address is not Bech32m encoded"),
ParseError::UnknownPrefix(s) => {
@ -140,18 +141,29 @@ pub(crate) mod private {
use crate::Network;
use std::{
cmp,
collections::HashSet,
convert::{TryFrom, TryInto},
io::Write,
};
use zcash_encoding::CompactSize;
/// A raw address or viewing key.
pub trait SealedItem:
for<'a> TryFrom<(u32, &'a [u8]), Error = ParseError> + cmp::Ord + cmp::PartialOrd + Clone
{
pub trait SealedItem: for<'a> TryFrom<(u32, &'a [u8]), Error = ParseError> + Clone {
fn typecode(&self) -> Typecode;
fn data(&self) -> &[u8];
fn preference_order(a: &Self, b: &Self) -> cmp::Ordering {
match Typecode::preference_order(&a.typecode(), &b.typecode()) {
cmp::Ordering::Equal => a.data().cmp(b.data()),
res => res,
}
}
fn encoding_order(a: &Self, b: &Self) -> cmp::Ordering {
match Typecode::encoding_order(&a.typecode(), &b.typecode()) {
cmp::Ordering::Equal => a.data().cmp(b.data()),
res => res,
}
}
}
/// A Unified Container containing addresses or viewing keys.
@ -283,23 +295,30 @@ pub(crate) mod private {
}
/// A private function that constructs a unified container with the
/// items in their given order.
/// specified items, which must be in ascending typecode order.
fn try_from_items_internal(items: Vec<Self::Item>) -> Result<Self, ParseError> {
let mut typecodes = HashSet::with_capacity(items.len());
assert!(u32::from(Typecode::P2sh) == u32::from(Typecode::P2pkh) + 1);
let mut only_transparent = true;
let mut prev_code = None; // less than any Some
for item in &items {
let t = item.typecode();
if typecodes.contains(&t) {
let t_code = Some(u32::from(t));
if t_code < prev_code {
return Err(ParseError::InvalidTypecodeOrder);
} else if t_code == prev_code {
return Err(ParseError::DuplicateTypecode(t));
} else if (t == Typecode::P2pkh && typecodes.contains(&Typecode::P2sh))
|| (t == Typecode::P2sh && typecodes.contains(&Typecode::P2pkh))
{
} else if t == Typecode::P2sh && prev_code == Some(u32::from(Typecode::P2pkh)) {
// P2pkh and P2sh can only be in that order and next to each other,
// otherwise we would detect an out-of-order or duplicate typecode.
return Err(ParseError::BothP2phkAndP2sh);
} else {
typecodes.insert(t);
prev_code = t_code;
only_transparent = only_transparent && t.is_transparent();
}
}
if typecodes.iter().all(|t| t.is_transparent()) {
if only_transparent {
Err(ParseError::OnlyTransparent)
} else {
// All checks pass!
@ -325,23 +344,10 @@ pub trait Encoding: private::SealedContainer {
/// invariants concerning the composition of a unified container are
/// violated:
/// * the item list may not contain two items having the same typecode
/// * the item list may not contain only a single transparent item
/// * the item list may not contain only transparent items (or no items)
/// * the item list may not contain both P2PKH and P2SH items.
fn try_from_items(mut items: Vec<Self::Item>) -> Result<Self, ParseError> {
items.sort_unstable_by_key(|i| i.typecode());
Self::try_from_items_internal(items)
}
/// Constructs a value of a unified container type from a vector
/// of container items, preserving the order of the provided vector
/// in the serialized form, potentially contravening the ordering
/// recommended by ZIP 316.
///
/// This function will return an error in the case that the following ZIP 316
/// invariants concerning the composition of a unified container are
/// violated:
/// * the item list may not contain two items having the same typecode
/// * the item list may not contain only a single transparent item
fn try_from_items_preserving_order(items: Vec<Self::Item>) -> Result<Self, ParseError> {
items.sort_unstable_by(Self::Item::encoding_order);
Self::try_from_items_internal(items)
}
@ -389,7 +395,7 @@ pub trait Container {
let mut items = self.items_as_parsed().to_vec();
// Unstable sorting is fine, because all items are guaranteed by construction
// to have distinct typecodes.
items.sort_unstable_by_key(|r| r.typecode());
items.sort_unstable_by(Self::Item::preference_order);
items
}

View File

@ -1,7 +1,6 @@
use super::{private::SealedItem, ParseError, Typecode};
use crate::kind;
use std::cmp;
use std::convert::{TryFrom, TryInto};
/// The set of known Receivers for Unified Addresses.
@ -14,21 +13,6 @@ pub enum Receiver {
Unknown { typecode: u32, data: Vec<u8> },
}
impl cmp::Ord for Receiver {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match self.typecode().cmp(&other.typecode()) {
cmp::Ordering::Equal => self.data().cmp(other.data()),
res => res,
}
}
}
impl cmp::PartialOrd for Receiver {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl TryFrom<(u32, &[u8])> for Receiver {
type Error = ParseError;
@ -152,15 +136,15 @@ mod tests {
/// A strategy to generate an arbitrary valid set of typecodes without
/// duplication and containing only one of P2sh and P2pkh transparent
/// typecodes.
/// typecodes. The resulting vector will be sorted in encoding order.
fn arb_typecodes() -> impl Strategy<Value = Vec<Typecode>> {
prop::option::of(arb_transparent_typecode())
.prop_flat_map(|transparent| {
prop::collection::hash_set(arb_shielded_typecode(), 1..4)
.prop_map(move |xs| xs.into_iter().chain(transparent).collect())
.boxed()
prop::option::of(arb_transparent_typecode()).prop_flat_map(|transparent| {
prop::collection::hash_set(arb_shielded_typecode(), 1..4).prop_map(move |xs| {
let mut typecodes: Vec<_> = xs.into_iter().chain(transparent).collect();
typecodes.sort_unstable_by(Typecode::encoding_order);
typecodes
})
.prop_shuffle()
})
}
fn arb_unified_address_for_typecodes(
@ -296,6 +280,19 @@ mod tests {
);
}
#[test]
fn addresses_out_of_order() {
// Construct and serialize an invalid UA. This must be done using private
// methods, as the public API does not permit construction of such invalid values.
let ua = Address(vec![Receiver::Sapling([0; 43]), Receiver::P2pkh([0; 20])]);
let encoded = ua.to_jumbled_bytes(Address::MAINNET);
// ensure that decoding catches the error
assert_eq!(
Address::parse_internal(Address::MAINNET, &encoded[..]),
Err(ParseError::InvalidTypecodeOrder)
);
}
#[test]
fn only_transparent() {
// Encoding of `Address(vec![Receiver::P2pkh([0; 20])])`.

View File

@ -22,17 +22,17 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0xae,
]),
unified_addr: &[
0x75, 0x31, 0x74, 0x37, 0x79, 0x33, 0x6e, 0x64, 0x76, 0x34, 0x36, 0x74, 0x76, 0x63,
0x64, 0x65, 0x37, 0x78, 0x34, 0x36, 0x38, 0x35, 0x61, 0x6a, 0x6d, 0x67, 0x6b, 0x35,
0x39, 0x70, 0x64, 0x30, 0x39, 0x32, 0x6c, 0x68, 0x73, 0x34, 0x77, 0x61, 0x6e, 0x77,
0x6c, 0x64, 0x68, 0x34, 0x66, 0x6d, 0x37, 0x75, 0x63, 0x6a, 0x72, 0x30, 0x74, 0x34,
0x34, 0x68, 0x66, 0x7a, 0x35, 0x6a, 0x63, 0x75, 0x66, 0x7a, 0x75, 0x61, 0x6b, 0x65,
0x66, 0x7a, 0x75, 0x71, 0x76, 0x7a, 0x37, 0x6a, 0x30, 0x61, 0x79, 0x74, 0x73, 0x6e,
0x66, 0x77, 0x34, 0x70, 0x65, 0x79, 0x64, 0x78, 0x38, 0x75, 0x38, 0x74, 0x6a, 0x73,
0x71, 0x34, 0x77, 0x38, 0x75, 0x6b, 0x73, 0x36, 0x72, 0x79, 0x73, 0x63, 0x36, 0x35,
0x35, 0x6d, 0x78, 0x6b, 0x6a, 0x36, 0x77, 0x78, 0x78, 0x68, 0x70, 0x67, 0x6b, 0x6e,
0x79, 0x73, 0x35, 0x33, 0x65, 0x79, 0x78, 0x70, 0x6b, 0x38, 0x39, 0x78, 0x79, 0x73,
0x63,
0x75, 0x31, 0x36, 0x75, 0x74, 0x37, 0x33, 0x6b, 0x36, 0x34, 0x6a, 0x75, 0x7a, 0x75,
0x36, 0x6a, 0x75, 0x30, 0x77, 0x61, 0x73, 0x36, 0x71, 0x75, 0x37, 0x67, 0x64, 0x37,
0x71, 0x7a, 0x30, 0x33, 0x72, 0x61, 0x77, 0x73, 0x68, 0x77, 0x6e, 0x74, 0x6d, 0x30,
0x30, 0x78, 0x39, 0x37, 0x30, 0x6b, 0x32, 0x63, 0x74, 0x35, 0x6d, 0x77, 0x6a, 0x35,
0x36, 0x72, 0x64, 0x78, 0x73, 0x78, 0x63, 0x38, 0x38, 0x71, 0x70, 0x6e, 0x35, 0x6b,
0x76, 0x71, 0x6e, 0x35, 0x33, 0x7a, 0x63, 0x68, 0x6c, 0x6e, 0x74, 0x78, 0x6b, 0x38,
0x34, 0x78, 0x6b, 0x6b, 0x68, 0x6e, 0x34, 0x66, 0x6a, 0x74, 0x33, 0x64, 0x66, 0x79,
0x6e, 0x6c, 0x35, 0x76, 0x79, 0x6d, 0x64, 0x66, 0x65, 0x68, 0x30, 0x6d, 0x39, 0x78,
0x65, 0x30, 0x77, 0x38, 0x30, 0x66, 0x76, 0x6d, 0x6a, 0x6d, 0x70, 0x78, 0x34, 0x6d,
0x65, 0x66, 0x6a, 0x74, 0x77, 0x6a, 0x38, 0x78, 0x67, 0x6e, 0x67, 0x70, 0x68, 0x77,
0x70,
],
},
TestVector {
@ -54,22 +54,22 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0x9f,
]),
unified_addr: &[
0x75, 0x31, 0x66, 0x65, 0x68, 0x6b, 0x65, 0x6a, 0x64, 0x6d, 0x36, 0x30, 0x61, 0x38,
0x71, 0x35, 0x35, 0x67, 0x73, 0x65, 0x76, 0x71, 0x6c, 0x78, 0x67, 0x66, 0x32, 0x39,
0x6a, 0x38, 0x67, 0x6c, 0x37, 0x73, 0x64, 0x39, 0x34, 0x36, 0x75, 0x6e, 0x6a, 0x73,
0x34, 0x6e, 0x6c, 0x34, 0x34, 0x73, 0x63, 0x34, 0x34, 0x30, 0x72, 0x74, 0x65, 0x74,
0x35, 0x75, 0x67, 0x71, 0x37, 0x32, 0x64, 0x63, 0x78, 0x39, 0x70, 0x6a, 0x74, 0x68,
0x39, 0x6c, 0x32, 0x33, 0x72, 0x70, 0x6a, 0x75, 0x66, 0x75, 0x7a, 0x63, 0x77, 0x68,
0x37, 0x35, 0x79, 0x71, 0x65, 0x7a, 0x75, 0x32, 0x35, 0x61, 0x75, 0x32, 0x38, 0x39,
0x70, 0x71, 0x72, 0x74, 0x61, 0x6c, 0x71, 0x36, 0x37, 0x77, 0x6e, 0x34, 0x30, 0x66,
0x78, 0x71, 0x77, 0x30, 0x6c, 0x6e, 0x37, 0x7a, 0x78, 0x77, 0x7a, 0x39, 0x79, 0x73,
0x78, 0x75, 0x75, 0x30, 0x7a, 0x39, 0x33, 0x77, 0x72, 0x67, 0x6e, 0x6d, 0x66, 0x71,
0x37, 0x6b, 0x33, 0x66, 0x6e, 0x36, 0x74, 0x7a, 0x63, 0x30, 0x76, 0x37, 0x72, 0x7a,
0x39, 0x67, 0x33, 0x70, 0x6a, 0x67, 0x77, 0x75, 0x71, 0x74, 0x6c, 0x6b, 0x7a, 0x78,
0x68, 0x73, 0x78, 0x38, 0x39, 0x61, 0x7a, 0x39, 0x65, 0x68, 0x68, 0x33, 0x6c, 0x35,
0x6a, 0x35, 0x71, 0x75, 0x75, 0x75, 0x72, 0x77, 0x6e, 0x37, 0x6e, 0x33, 0x74, 0x6a,
0x78, 0x6c, 0x61, 0x6e, 0x32, 0x35, 0x72, 0x6e, 0x71, 0x34, 0x67, 0x6a, 0x7a, 0x61,
0x38, 0x74, 0x36,
0x75, 0x31, 0x67, 0x72, 0x35, 0x33, 0x37, 0x65, 0x70, 0x6b, 0x32, 0x74, 0x78, 0x6b,
0x78, 0x7a, 0x74, 0x61, 0x72, 0x37, 0x72, 0x72, 0x76, 0x34, 0x35, 0x70, 0x6b, 0x70,
0x63, 0x70, 0x65, 0x6c, 0x38, 0x39, 0x6e, 0x61, 0x37, 0x32, 0x6e, 0x38, 0x67, 0x70,
0x35, 0x72, 0x65, 0x34, 0x39, 0x61, 0x6c, 0x6d, 0x7a, 0x71, 0x34, 0x38, 0x35, 0x6e,
0x36, 0x72, 0x37, 0x61, 0x33, 0x65, 0x61, 0x34, 0x30, 0x6a, 0x71, 0x32, 0x33, 0x32,
0x78, 0x37, 0x39, 0x75, 0x37, 0x37, 0x65, 0x64, 0x6b, 0x6c, 0x7a, 0x6e, 0x73, 0x35,
0x65, 0x66, 0x38, 0x36, 0x30, 0x75, 0x6e, 0x78, 0x32, 0x33, 0x71, 0x39, 0x67, 0x73,
0x77, 0x72, 0x76, 0x33, 0x6d, 0x33, 0x7a, 0x78, 0x32, 0x6a, 0x76, 0x66, 0x64, 0x61,
0x66, 0x36, 0x76, 0x75, 0x70, 0x35, 0x35, 0x75, 0x7a, 0x73, 0x33, 0x34, 0x7a, 0x37,
0x61, 0x75, 0x63, 0x75, 0x38, 0x30, 0x37, 0x67, 0x30, 0x79, 0x6c, 0x6b, 0x75, 0x63,
0x76, 0x79, 0x76, 0x77, 0x76, 0x35, 0x74, 0x63, 0x79, 0x38, 0x68, 0x34, 0x38, 0x6b,
0x65, 0x67, 0x67, 0x70, 0x6e, 0x32, 0x72, 0x38, 0x79, 0x70, 0x35, 0x63, 0x6c, 0x70,
0x36, 0x66, 0x30, 0x32, 0x34, 0x39, 0x36, 0x61, 0x79, 0x73, 0x38, 0x6a, 0x6c, 0x64,
0x38, 0x6a, 0x35, 0x38, 0x75, 0x67, 0x76, 0x68, 0x65, 0x32, 0x72, 0x78, 0x63, 0x72,
0x73, 0x77, 0x79, 0x72, 0x6a, 0x6b, 0x66, 0x35, 0x72, 0x6d, 0x37, 0x6d, 0x36, 0x74,
0x77, 0x79, 0x73,
],
},
TestVector {
@ -86,17 +86,17 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0x93,
]),
unified_addr: &[
0x75, 0x31, 0x64, 0x6a, 0x68, 0x67, 0x78, 0x77, 0x35, 0x79, 0x74, 0x7a, 0x74, 0x63,
0x35, 0x6d, 0x75, 0x32, 0x32, 0x72, 0x34, 0x6d, 0x73, 0x35, 0x67, 0x79, 0x6d, 0x38,
0x64, 0x77, 0x68, 0x67, 0x38, 0x34, 0x32, 0x32, 0x66, 0x67, 0x75, 0x78, 0x79, 0x76,
0x6c, 0x74, 0x65, 0x78, 0x76, 0x38, 0x78, 0x75, 0x72, 0x67, 0x73, 0x38, 0x63, 0x72,
0x30, 0x75, 0x6b, 0x72, 0x64, 0x79, 0x73, 0x37, 0x66, 0x76, 0x68, 0x71, 0x6d, 0x6d,
0x61, 0x73, 0x61, 0x66, 0x67, 0x66, 0x65, 0x30, 0x67, 0x74, 0x35, 0x77, 0x6e, 0x30,
0x6d, 0x75, 0x6a, 0x6e, 0x63, 0x33, 0x34, 0x67, 0x74, 0x6d, 0x63, 0x77, 0x34, 0x39,
0x75, 0x65, 0x64, 0x6b, 0x68, 0x70, 0x7a, 0x79, 0x78, 0x7a, 0x34, 0x39, 0x7a, 0x76,
0x78, 0x35, 0x63, 0x39, 0x75, 0x71, 0x30, 0x68, 0x6a, 0x64, 0x35, 0x6e, 0x37, 0x74,
0x72, 0x67, 0x73, 0x6e, 0x65, 0x6a, 0x68, 0x6a, 0x71, 0x6d, 0x73, 0x76, 0x72, 0x36,
0x63,
0x75, 0x31, 0x6e, 0x6b, 0x35, 0x37, 0x30, 0x61, 0x61, 0x6d, 0x79, 0x34, 0x7a, 0x6d,
0x68, 0x65, 0x6c, 0x7a, 0x36, 0x6b, 0x61, 0x30, 0x33, 0x66, 0x7a, 0x79, 0x34, 0x73,
0x37, 0x66, 0x39, 0x72, 0x34, 0x6b, 0x65, 0x76, 0x66, 0x6d, 0x67, 0x63, 0x65, 0x33,
0x79, 0x35, 0x36, 0x71, 0x6e, 0x6a, 0x71, 0x7a, 0x6d, 0x68, 0x74, 0x36, 0x68, 0x79,
0x37, 0x72, 0x33, 0x38, 0x74, 0x6b, 0x77, 0x64, 0x74, 0x67, 0x39, 0x61, 0x34, 0x63,
0x63, 0x78, 0x65, 0x33, 0x64, 0x78, 0x74, 0x34, 0x6b, 0x74, 0x6a, 0x34, 0x61, 0x6a,
0x6e, 0x39, 0x6b, 0x79, 0x34, 0x6c, 0x73, 0x36, 0x6a, 0x65, 0x6a, 0x63, 0x39, 0x72,
0x67, 0x66, 0x64, 0x75, 0x6e, 0x73, 0x75, 0x67, 0x75, 0x68, 0x78, 0x64, 0x6d, 0x70,
0x6a, 0x30, 0x35, 0x75, 0x72, 0x36, 0x64, 0x75, 0x63, 0x64, 0x77, 0x6c, 0x68, 0x6a,
0x6d, 0x39, 0x32, 0x6a, 0x78, 0x6c, 0x6a, 0x72, 0x79, 0x37, 0x74, 0x67, 0x32, 0x64,
0x6b,
],
},
TestVector {
@ -139,22 +139,22 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0xad,
]),
unified_addr: &[
0x75, 0x31, 0x35, 0x65, 0x32, 0x6e, 0x32, 0x35, 0x36, 0x74, 0x6b, 0x63, 0x68, 0x37,
0x39, 0x66, 0x77, 0x79, 0x7a, 0x76, 0x6c, 0x66, 0x33, 0x73, 0x78, 0x66, 0x6c, 0x66,
0x72, 0x77, 0x72, 0x66, 0x6d, 0x36, 0x64, 0x33, 0x6d, 0x65, 0x39, 0x30, 0x6e, 0x76,
0x39, 0x32, 0x66, 0x6b, 0x65, 0x75, 0x77, 0x6e, 0x65, 0x34, 0x77, 0x73, 0x79, 0x75,
0x65, 0x7a, 0x65, 0x6e, 0x71, 0x35, 0x6a, 0x73, 0x68, 0x71, 0x6e, 0x67, 0x30, 0x39,
0x6e, 0x77, 0x6c, 0x79, 0x30, 0x35, 0x37, 0x35, 0x71, 0x73, 0x70, 0x37, 0x36, 0x68,
0x68, 0x78, 0x78, 0x77, 0x6b, 0x6a, 0x63, 0x73, 0x73, 0x6b, 0x63, 0x65, 0x66, 0x6c,
0x38, 0x35, 0x6c, 0x30, 0x74, 0x65, 0x79, 0x33, 0x38, 0x77, 0x32, 0x36, 0x74, 0x6c,
0x6c, 0x65, 0x75, 0x78, 0x7a, 0x66, 0x67, 0x6d, 0x38, 0x70, 0x37, 0x32, 0x72, 0x74,
0x35, 0x37, 0x30, 0x33, 0x63, 0x72, 0x38, 0x78, 0x75, 0x79, 0x30, 0x75, 0x77, 0x37,
0x6c, 0x72, 0x63, 0x74, 0x6e, 0x67, 0x67, 0x39, 0x6b, 0x37, 0x7a, 0x75, 0x38, 0x7a,
0x75, 0x66, 0x75, 0x76, 0x39, 0x71, 0x78, 0x63, 0x36, 0x74, 0x79, 0x6a, 0x72, 0x78,
0x33, 0x78, 0x33, 0x67, 0x6a, 0x76, 0x79, 0x6b, 0x64, 0x6d, 0x79, 0x61, 0x6d, 0x34,
0x79, 0x76, 0x33, 0x79, 0x34, 0x71, 0x37, 0x6a, 0x66, 0x6c, 0x78, 0x30, 0x70, 0x30,
0x6d, 0x39, 0x34, 0x70, 0x66, 0x73, 0x77, 0x39, 0x68, 0x7a, 0x6b, 0x37, 0x61, 0x32,
0x38, 0x70, 0x79,
0x75, 0x31, 0x65, 0x6a, 0x70, 0x6e, 0x33, 0x67, 0x6e, 0x34, 0x30, 0x39, 0x73, 0x72,
0x38, 0x33, 0x34, 0x66, 0x63, 0x77, 0x71, 0x32, 0x6b, 0x68, 0x36, 0x79, 0x34, 0x6a,
0x61, 0x70, 0x66, 0x39, 0x68, 0x71, 0x72, 0x73, 0x36, 0x36, 0x33, 0x78, 0x6a, 0x30,
0x74, 0x79, 0x6e, 0x78, 0x75, 0x63, 0x33, 0x64, 0x67, 0x76, 0x6b, 0x78, 0x67, 0x67,
0x77, 0x75, 0x75, 0x30, 0x6d, 0x64, 0x6c, 0x79, 0x38, 0x38, 0x63, 0x79, 0x63, 0x38,
0x67, 0x76, 0x74, 0x33, 0x30, 0x6b, 0x76, 0x34, 0x36, 0x35, 0x76, 0x39, 0x76, 0x65,
0x6d, 0x6b, 0x72, 0x32, 0x32, 0x77, 0x71, 0x65, 0x78, 0x61, 0x73, 0x72, 0x77, 0x34,
0x39, 0x76, 0x79, 0x6c, 0x34, 0x68, 0x6e, 0x61, 0x6c, 0x6c, 0x6a, 0x64, 0x63, 0x75,
0x36, 0x32, 0x75, 0x32, 0x73, 0x61, 0x34, 0x64, 0x32, 0x61, 0x35, 0x74, 0x63, 0x65,
0x68, 0x72, 0x66, 0x6c, 0x75, 0x79, 0x74, 0x6a, 0x7a, 0x32, 0x70, 0x7a, 0x6a, 0x39,
0x6d, 0x61, 0x39, 0x38, 0x63, 0x78, 0x33, 0x30, 0x63, 0x6b, 0x32, 0x71, 0x30, 0x6d,
0x6b, 0x35, 0x30, 0x6b, 0x64, 0x36, 0x6d, 0x65, 0x76, 0x70, 0x39, 0x68, 0x70, 0x79,
0x6d, 0x6d, 0x39, 0x70, 0x37, 0x72, 0x73, 0x75, 0x79, 0x38, 0x70, 0x76, 0x30, 0x70,
0x61, 0x66, 0x6c, 0x66, 0x6a, 0x68, 0x6c, 0x30, 0x6c, 0x70, 0x7a, 0x6e, 0x32, 0x32,
0x38, 0x33, 0x77, 0x6b, 0x68, 0x64, 0x6c, 0x6a, 0x68, 0x71, 0x6a, 0x7a, 0x30, 0x6e,
0x63, 0x75, 0x6b,
],
},
TestVector {
@ -173,19 +173,19 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0xae,
]),
unified_addr: &[
0x75, 0x31, 0x6b, 0x61, 0x6c, 0x6b, 0x6d, 0x32, 0x76, 0x71, 0x72, 0x67, 0x71, 0x6d,
0x38, 0x6c, 0x6c, 0x71, 0x65, 0x75, 0x68, 0x35, 0x33, 0x72, 0x77, 0x7a, 0x6c, 0x65,
0x74, 0x35, 0x67, 0x65, 0x61, 0x34, 0x6d, 0x6b, 0x64, 0x77, 0x39, 0x37, 0x6a, 0x39,
0x70, 0x61, 0x33, 0x72, 0x63, 0x65, 0x74, 0x76, 0x7a, 0x76, 0x33, 0x6c, 0x6b, 0x6b,
0x33, 0x65, 0x30, 0x75, 0x33, 0x35, 0x34, 0x33, 0x76, 0x70, 0x74, 0x67, 0x74, 0x32,
0x6b, 0x30, 0x75, 0x61, 0x66, 0x38, 0x38, 0x36, 0x30, 0x38, 0x66, 0x75, 0x33, 0x75,
0x63, 0x65, 0x66, 0x76, 0x77, 0x65, 0x77, 0x79, 0x7a, 0x39, 0x74, 0x71, 0x77, 0x65,
0x61, 0x6a, 0x33, 0x77, 0x75, 0x6c, 0x6c, 0x30, 0x70, 0x64, 0x37, 0x30, 0x6d, 0x74,
0x6e, 0x68, 0x79, 0x77, 0x65, 0x6b, 0x30, 0x6c, 0x35, 0x76, 0x61, 0x7a, 0x63, 0x61,
0x36, 0x65, 0x77, 0x66, 0x34, 0x72, 0x64, 0x6d, 0x77, 0x78, 0x71, 0x38, 0x66, 0x32,
0x73, 0x34, 0x70, 0x6d, 0x35, 0x68, 0x71, 0x75, 0x36, 0x6d, 0x65, 0x67, 0x39, 0x7a,
0x67, 0x74, 0x67, 0x7a, 0x76, 0x39, 0x38, 0x72, 0x78, 0x7a, 0x68, 0x32, 0x77, 0x78,
0x35, 0x76, 0x65, 0x76, 0x79, 0x73, 0x30, 0x34, 0x65, 0x73,
0x75, 0x31, 0x6a, 0x6d, 0x38, 0x6d, 0x65, 0x63, 0x32, 0x6c, 0x73, 0x72, 0x65, 0x33,
0x66, 0x66, 0x65, 0x65, 0x70, 0x6d, 0x74, 0x74, 0x73, 0x34, 0x37, 0x6b, 0x38, 0x33,
0x33, 0x6d, 0x33, 0x72, 0x71, 0x65, 0x30, 0x72, 0x68, 0x6d, 0x7a, 0x6a, 0x39, 0x37,
0x78, 0x72, 0x67, 0x37, 0x37, 0x61, 0x36, 0x66, 0x6c, 0x6a, 0x7a, 0x61, 0x33, 0x36,
0x66, 0x6a, 0x68, 0x77, 0x34, 0x64, 0x63, 0x63, 0x76, 0x6d, 0x39, 0x6c, 0x32, 0x6e,
0x61, 0x37, 0x6c, 0x70, 0x61, 0x66, 0x75, 0x6a, 0x66, 0x61, 0x35, 0x6b, 0x61, 0x74,
0x77, 0x38, 0x39, 0x79, 0x77, 0x36, 0x36, 0x68, 0x73, 0x30, 0x63, 0x61, 0x35, 0x74,
0x74, 0x36, 0x66, 0x65, 0x70, 0x73, 0x6a, 0x76, 0x36, 0x70, 0x30, 0x75, 0x75, 0x39,
0x73, 0x77, 0x64, 0x61, 0x76, 0x72, 0x63, 0x38, 0x70, 0x78, 0x6d, 0x6c, 0x34, 0x30,
0x66, 0x77, 0x38, 0x65, 0x76, 0x6b, 0x76, 0x32, 0x30, 0x76, 0x6a, 0x61, 0x38, 0x6e,
0x77, 0x78, 0x6e, 0x37, 0x36, 0x6e, 0x61, 0x30, 0x6d, 0x37, 0x6e, 0x67, 0x74, 0x32,
0x6c, 0x30, 0x79, 0x73, 0x36, 0x32, 0x35, 0x37, 0x30, 0x77, 0x61, 0x75, 0x6a, 0x71,
0x73, 0x74, 0x35, 0x71, 0x37, 0x79, 0x74, 0x35, 0x74, 0x6e,
],
},
TestVector {
@ -207,22 +207,22 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
0x94,
]),
unified_addr: &[
0x75, 0x31, 0x6d, 0x30, 0x76, 0x7a, 0x6e, 0x37, 0x38, 0x67, 0x68, 0x30, 0x79, 0x6d,
0x71, 0x35, 0x6b, 0x71, 0x7a, 0x34, 0x6c, 0x70, 0x64, 0x34, 0x30, 0x39, 0x35, 0x77,
0x70, 0x75, 0x65, 0x38, 0x68, 0x6e, 0x68, 0x7a, 0x73, 0x73, 0x35, 0x7a, 0x34, 0x73,
0x6c, 0x33, 0x38, 0x73, 0x7a, 0x6e, 0x38, 0x76, 0x77, 0x36, 0x68, 0x39, 0x34, 0x78,
0x75, 0x76, 0x63, 0x6a, 0x76, 0x35, 0x61, 0x68, 0x6b, 0x6e, 0x70, 0x6d, 0x6c, 0x72,
0x61, 0x36, 0x37, 0x75, 0x65, 0x63, 0x76, 0x6e, 0x6b, 0x6d, 0x73, 0x39, 0x79, 0x66,
0x71, 0x78, 0x34, 0x73, 0x37, 0x68, 0x73, 0x6e, 0x73, 0x7a, 0x7a, 0x6d, 0x63, 0x70,
0x67, 0x76, 0x78, 0x72, 0x32, 0x30, 0x6c, 0x68, 0x34, 0x35, 0x39, 0x70, 0x73, 0x33,
0x75, 0x33, 0x77, 0x66, 0x36, 0x6d, 0x72, 0x7a, 0x77, 0x65, 0x36, 0x78, 0x39, 0x6b,
0x64, 0x6a, 0x38, 0x74, 0x70, 0x30, 0x61, 0x36, 0x63, 0x73, 0x65, 0x64, 0x32, 0x65,
0x6d, 0x78, 0x70, 0x36, 0x71, 0x37, 0x71, 0x6e, 0x77, 0x61, 0x74, 0x64, 0x32, 0x78,
0x6d, 0x34, 0x70, 0x73, 0x6a, 0x68, 0x34, 0x72, 0x34, 0x67, 0x39, 0x70, 0x68, 0x66,
0x66, 0x35, 0x34, 0x74, 0x73, 0x75, 0x78, 0x65, 0x35, 0x37, 0x36, 0x38, 0x74, 0x68,
0x73, 0x78, 0x73, 0x73, 0x61, 0x76, 0x78, 0x72, 0x66, 0x79, 0x74, 0x70, 0x64, 0x70,
0x6d, 0x37, 0x32, 0x6b, 0x6a, 0x73, 0x72, 0x79, 0x70, 0x61, 0x79, 0x7a, 0x77, 0x67,
0x71, 0x39, 0x78,
0x75, 0x31, 0x61, 0x76, 0x73, 0x63, 0x33, 0x74, 0x61, 0x38, 0x38, 0x64, 0x68, 0x63,
0x34, 0x6a, 0x35, 0x37, 0x74, 0x64, 0x65, 0x70, 0x38, 0x6a, 0x68, 0x33, 0x66, 0x32,
0x73, 0x67, 0x33, 0x63, 0x75, 0x6e, 0x66, 0x70, 0x73, 0x6d, 0x36, 0x76, 0x6d, 0x63,
0x6a, 0x61, 0x61, 0x37, 0x35, 0x66, 0x30, 0x66, 0x64, 0x39, 0x37, 0x66, 0x71, 0x37,
0x63, 0x70, 0x30, 0x79, 0x71, 0x34, 0x63, 0x6b, 0x6d, 0x63, 0x6c, 0x35, 0x76, 0x63,
0x77, 0x78, 0x78, 0x77, 0x77, 0x33, 0x32, 0x73, 0x75, 0x73, 0x75, 0x74, 0x30, 0x76,
0x34, 0x6c, 0x30, 0x39, 0x37, 0x33, 0x76, 0x35, 0x73, 0x76, 0x6e, 0x37, 0x37, 0x75,
0x74, 0x30, 0x30, 0x7a, 0x61, 0x75, 0x38, 0x36, 0x6e, 0x39, 0x36, 0x79, 0x67, 0x63,
0x77, 0x76, 0x79, 0x7a, 0x32, 0x79, 0x35, 0x74, 0x79, 0x79, 0x6c, 0x36, 0x6e, 0x64,
0x79, 0x72, 0x36, 0x38, 0x38, 0x32, 0x36, 0x34, 0x6e, 0x72, 0x63, 0x34, 0x32, 0x73,
0x68, 0x38, 0x33, 0x32, 0x6c, 0x6c, 0x68, 0x61, 0x70, 0x68, 0x6a, 0x39, 0x33, 0x61,
0x6c, 0x30, 0x33, 0x6a, 0x6e, 0x64, 0x36, 0x36, 0x70, 0x37, 0x6e, 0x34, 0x34, 0x70,
0x70, 0x37, 0x68, 0x71, 0x38, 0x66, 0x6e, 0x6b, 0x75, 0x79, 0x6d, 0x6b, 0x79, 0x6a,
0x35, 0x36, 0x65, 0x35, 0x70, 0x39, 0x67, 0x72, 0x39, 0x78, 0x65, 0x65, 0x78, 0x34,
0x30, 0x38, 0x65, 0x35, 0x32, 0x6e, 0x37, 0x35, 0x35, 0x65, 0x37, 0x38, 0x63, 0x64,
0x6a, 0x6c, 0x73,
],
},
TestVector {
@ -260,17 +260,17 @@ pub(crate) const TEST_VECTORS: &[TestVector] = &[
]),
orchard_raw_addr: None,
unified_addr: &[
0x75, 0x31, 0x37, 0x76, 0x6a, 0x73, 0x6b, 0x6c, 0x72, 0x34, 0x75, 0x6a, 0x64, 0x63,
0x38, 0x6c, 0x30, 0x7a, 0x68, 0x6b, 0x64, 0x6d, 0x6b, 0x71, 0x71, 0x77, 0x78, 0x34,
0x38, 0x33, 0x67, 0x38, 0x61, 0x36, 0x34, 0x32, 0x65, 0x39, 0x37, 0x36, 0x6c, 0x74,
0x66, 0x73, 0x30, 0x73, 0x34, 0x6a, 0x65, 0x64, 0x37, 0x61, 0x67, 0x33, 0x76, 0x33,
0x61, 0x38, 0x72, 0x6e, 0x67, 0x72, 0x75, 0x36, 0x6e, 0x65, 0x34, 0x38, 0x68, 0x7a,
0x72, 0x33, 0x33, 0x6b, 0x6b, 0x7a, 0x70, 0x36, 0x6d, 0x61, 0x67, 0x7a, 0x33, 0x66,
0x79, 0x65, 0x6a, 0x77, 0x7a, 0x6a, 0x67, 0x34, 0x33, 0x6a, 0x64, 0x70, 0x65, 0x72,
0x74, 0x66, 0x70, 0x78, 0x65, 0x6e, 0x32, 0x66, 0x61, 0x74, 0x7a, 0x35, 0x6b, 0x38,
0x30, 0x63, 0x6a, 0x6b, 0x6c, 0x6e, 0x39, 0x71, 0x37, 0x72, 0x39, 0x6d, 0x30, 0x33,
0x64, 0x6e, 0x36, 0x67, 0x36, 0x30, 0x63, 0x32, 0x79, 0x6c, 0x33, 0x36, 0x39, 0x34,
0x74,
0x75, 0x31, 0x39, 0x67, 0x67, 0x38, 0x73, 0x71, 0x70, 0x65, 0x68, 0x75, 0x6d, 0x67,
0x6d, 0x73, 0x78, 0x7a, 0x67, 0x6a, 0x79, 0x6d, 0x6c, 0x39, 0x33, 0x36, 0x78, 0x6b,
0x32, 0x67, 0x78, 0x6d, 0x73, 0x66, 0x65, 0x35, 0x6a, 0x65, 0x37, 0x37, 0x7a, 0x6a,
0x61, 0x61, 0x30, 0x67, 0x67, 0x6e, 0x32, 0x72, 0x33, 0x30, 0x73, 0x32, 0x39, 0x34,
0x32, 0x66, 0x76, 0x6b, 0x61, 0x32, 0x75, 0x63, 0x74, 0x75, 0x36, 0x39, 0x6d, 0x70,
0x74, 0x76, 0x30, 0x63, 0x32, 0x39, 0x76, 0x68, 0x6a, 0x70, 0x34, 0x68, 0x61, 0x72,
0x63, 0x75, 0x30, 0x72, 0x32, 0x73, 0x36, 0x6e, 0x79, 0x37, 0x30, 0x30, 0x6c, 0x79,
0x7a, 0x78, 0x71, 0x68, 0x66, 0x38, 0x33, 0x35, 0x78, 0x6b, 0x71, 0x6a, 0x78, 0x73,
0x77, 0x6a, 0x6a, 0x77, 0x71, 0x30, 0x32, 0x61, 0x64, 0x6b, 0x71, 0x79, 0x6a, 0x6b,
0x6b, 0x39, 0x63, 0x77, 0x6a, 0x6e, 0x37, 0x70, 0x32, 0x73, 0x68, 0x64, 0x71, 0x33,
0x79,
],
},
TestVector {

View File

@ -1,4 +1,3 @@
use std::cmp;
use std::convert::{TryFrom, TryInto};
use super::{
@ -40,21 +39,6 @@ pub enum Fvk {
},
}
impl cmp::Ord for Fvk {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match self.typecode().cmp(&other.typecode()) {
cmp::Ordering::Equal => self.data().cmp(other.data()),
res => res,
}
}
}
impl cmp::PartialOrd for Fvk {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl TryFrom<(u32, &[u8])> for Fvk {
type Error = ParseError;
@ -142,7 +126,10 @@ mod tests {
use super::{Fvk, ParseError, Typecode, Ufvk};
use crate::{
kind::unified::{private::SealedContainer, Container, Encoding},
kind::unified::{
private::{SealedContainer, SealedItem},
Container, Encoding,
},
Network,
};
@ -187,7 +174,7 @@ mod tests {
prop_oneof![
vec![arb_sapling_fvk().boxed()],
vec![arb_orchard_fvk().boxed()],
vec![arb_orchard_fvk().boxed(), arb_sapling_fvk().boxed()],
vec![arb_sapling_fvk().boxed(), arb_orchard_fvk().boxed()],
]
}
@ -200,7 +187,9 @@ mod tests {
shielded in arb_shielded_fvk(),
transparent in prop::option::of(arb_transparent_fvk()),
) -> Ufvk {
Ufvk(shielded.into_iter().chain(transparent).collect())
let mut items: Vec<_> = transparent.into_iter().chain(shielded).collect();
items.sort_unstable_by(Fvk::encoding_order);
Ufvk(items)
}
}

View File

@ -1,4 +1,3 @@
use std::cmp;
use std::convert::{TryFrom, TryInto};
use super::{
@ -45,21 +44,6 @@ pub enum Ivk {
},
}
impl cmp::Ord for Ivk {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match self.typecode().cmp(&other.typecode()) {
cmp::Ordering::Equal => self.data().cmp(other.data()),
res => res,
}
}
}
impl cmp::PartialOrd for Ivk {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl TryFrom<(u32, &[u8])> for Ivk {
type Error = ParseError;
@ -151,7 +135,10 @@ mod tests {
use super::{Ivk, ParseError, Typecode, Uivk};
use crate::{
kind::unified::{private::SealedContainer, Container, Encoding},
kind::unified::{
private::{SealedContainer, SealedItem},
Container, Encoding,
},
Network,
};
@ -178,8 +165,8 @@ mod tests {
vec![uniform64().prop_map(Ivk::Sapling)],
vec![uniform64().prop_map(Ivk::Orchard)],
vec![
uniform64().prop_map(Ivk::Orchard as fn([u8; 64]) -> Ivk),
uniform64().prop_map(Ivk::Sapling)
uniform64().prop_map(Ivk::Sapling as fn([u8; 64]) -> Ivk),
uniform64().prop_map(Ivk::Orchard)
],
]
}
@ -193,7 +180,9 @@ mod tests {
shielded in arb_shielded_ivk(),
transparent in prop::option::of(arb_transparent_ivk()),
) -> Uivk {
Uivk(shielded.into_iter().chain(transparent).collect())
let mut items: Vec<_> = transparent.into_iter().chain(shielded).collect();
items.sort_unstable_by(Ivk::encoding_order);
Uivk(items)
}
}

View File

@ -20,10 +20,10 @@ fn unified() {
let addr_string = String::from_utf8(tv.unified_addr.to_vec()).unwrap();
let receivers = iter::empty()
.chain(tv.orchard_raw_addr.map(Receiver::Orchard))
.chain(tv.sapling_raw_addr.map(Receiver::Sapling))
.chain(tv.p2sh_bytes.map(Receiver::P2sh))
.chain(tv.p2pkh_bytes.map(Receiver::P2pkh))
.chain(tv.p2sh_bytes.map(Receiver::P2sh))
.chain(tv.sapling_raw_addr.map(Receiver::Sapling))
.chain(tv.orchard_raw_addr.map(Receiver::Orchard))
.collect();
let expected_addr = ZcashAddress::from_unified(Network::Main, unified::Address(receivers));

View File

@ -59,7 +59,7 @@ rand_xorshift = "0.3"
orchard = { version = "=0.1.0-beta.1", features = ["test-dependencies"] }
[target.'cfg(unix)'.dev-dependencies]
pprof = { version = "0.6", features = ["criterion", "flamegraph"] }
pprof = { version = "=0.6.1", features = ["criterion", "flamegraph"] }
[features]
transparent-inputs = ["ripemd160", "secp256k1"]