Apply suggestions from code review.

This commit is contained in:
Kris Nuttycombe 2021-06-04 12:22:00 -06:00
parent e71a1ce345
commit 28d3f481cd
8 changed files with 57 additions and 63 deletions

View File

@ -628,7 +628,7 @@ mod tests {
}
fn tx_tze_outputs(&self) -> &[TzeOut] {
match self.tx.tze_bundle().as_ref() {
match self.tx.tze_bundle() {
Some(b) => &b.vout,
None => &[],
}
@ -764,8 +764,8 @@ mod tests {
let ctx = Ctx { tx: &tx_b };
assert_eq!(
Program.verify(
&tx_a.tze_bundle().as_ref().unwrap().vout[0].precondition,
&tx_b.tze_bundle().as_ref().unwrap().vin[0].witness,
&tx_a.tze_bundle().unwrap().vout[0].precondition,
&tx_b.tze_bundle().unwrap().vin[0].witness,
&ctx
),
Ok(())
@ -777,8 +777,8 @@ mod tests {
let ctx = Ctx { tx: &tx_c };
assert_eq!(
Program.verify(
&tx_b.tze_bundle().as_ref().unwrap().vout[0].precondition,
&tx_c.tze_bundle().as_ref().unwrap().vin[0].witness,
&tx_b.tze_bundle().unwrap().vout[0].precondition,
&tx_c.tze_bundle().unwrap().vin[0].witness,
&ctx
),
Ok(())

View File

@ -374,9 +374,9 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
// for now we need to continue to compute it here.
let shielded_sig_commitment = signature_hash(
&unauthed_tx,
SIGHASH_ALL,
&SignableInput::Shielded,
&txid_parts,
SIGHASH_ALL,
);
let (sapling_bundle, tx_metadata) = match unauthed_tx

View File

@ -203,9 +203,9 @@ impl Bundle<Unauthorized> {
.map(|(i, info)| {
let sighash = signature_hash(
mtx,
SIGHASH_ALL,
&SignableInput::transparent(i, &info.coin.script_pubkey, info.coin.value),
txid_parts_cache,
SIGHASH_ALL,
);
let msg = secp256k1::Message::from_slice(sighash.as_ref()).expect("32 bytes");

View File

@ -119,18 +119,18 @@ pub fn signature_hash<
A: Authorization<SaplingAuth = SA>,
>(
tx: &TransactionData<A>,
hash_type: u32,
signable_input: &SignableInput<'a>,
txid_parts: &TxDigests<Blake2bHash>,
hash_type: u32,
) -> SignatureHash {
SignatureHash(match tx.version {
TxVersion::Sprout(_) | TxVersion::Overwinter | TxVersion::Sapling => {
v4_signature_hash(tx, signable_input, hash_type)
v4_signature_hash(tx, hash_type, signable_input)
}
TxVersion::Zip225 => v5_signature_hash(tx, txid_parts, signable_input, hash_type),
TxVersion::Zip225 => v5_signature_hash(tx, hash_type, signable_input, txid_parts),
#[cfg(feature = "zfuture")]
TxVersion::ZFuture => v5_signature_hash(tx, txid_parts, signable_input, hash_type),
TxVersion::ZFuture => v5_signature_hash(tx, hash_type, signable_input, txid_parts),
})
}

View File

@ -140,8 +140,8 @@ pub fn v4_signature_hash<
A: Authorization<SaplingAuth = SA>,
>(
tx: &TransactionData<A>,
signable_input: &SignableInput<'_>,
hash_type: u32,
signable_input: &SignableInput<'_>,
) -> Blake2bHash {
if tx.version.has_overwinter() {
let mut personal = [0; 16];

View File

@ -119,9 +119,9 @@ fn tze_input_sigdigests<A: tze::Authorization>(
pub fn v5_signature_hash<A: Authorization>(
tx: &TransactionData<A>,
txid_parts: &TxDigests<Blake2bHash>,
signable_input: &SignableInput<'_>,
hash_type: u32,
signable_input: &SignableInput<'_>,
txid_parts: &TxDigests<Blake2bHash>,
) -> Blake2bHash {
match signable_input {
SignableInput::Shielded => to_hash(

View File

@ -129,7 +129,7 @@ fn zip_0143() {
};
assert_eq!(
v4_signature_hash(tx.deref(), &signable_input, tv.hash_type).as_ref(),
v4_signature_hash(tx.deref(), tv.hash_type, &signable_input).as_ref(),
tv.sighash
);
}
@ -149,7 +149,7 @@ fn zip_0243() {
};
assert_eq!(
v4_signature_hash(tx.deref(), &signable_input, tv.hash_type).as_ref(),
v4_signature_hash(tx.deref(), tv.hash_type, &signable_input).as_ref(),
tv.sighash
);
}
@ -172,19 +172,19 @@ fn zip_0244() {
);
assert_eq!(
v5_signature_hash(tx.deref(), &txid_parts, &signable_input, SIGHASH_ALL)
v5_signature_hash(tx.deref(), SIGHASH_ALL, &signable_input, &txid_parts)
.as_ref(),
&tv.sighash_all
);
assert_eq!(
v5_signature_hash(tx.deref(), &txid_parts, &signable_input, SIGHASH_NONE)
v5_signature_hash(tx.deref(), SIGHASH_NONE, &signable_input, &txid_parts)
.as_ref(),
&tv.sighash_none.unwrap()
);
assert_eq!(
v5_signature_hash(tx.deref(), &txid_parts, &signable_input, SIGHASH_SINGLE)
v5_signature_hash(tx.deref(), SIGHASH_SINGLE, &signable_input, &txid_parts)
.as_ref(),
&tv.sighash_single.unwrap()
);
@ -192,9 +192,9 @@ fn zip_0244() {
assert_eq!(
v5_signature_hash(
tx.deref(),
&txid_parts,
SIGHASH_ALL | SIGHASH_ANYONECANPAY,
&signable_input,
SIGHASH_ALL | SIGHASH_ANYONECANPAY
&txid_parts,
)
.as_ref(),
&tv.sighash_all_anyone.unwrap()
@ -203,9 +203,9 @@ fn zip_0244() {
assert_eq!(
v5_signature_hash(
tx.deref(),
&txid_parts,
SIGHASH_NONE | SIGHASH_ANYONECANPAY,
&signable_input,
SIGHASH_NONE | SIGHASH_ANYONECANPAY
&txid_parts,
)
.as_ref(),
&tv.sighash_none_anyone.unwrap()
@ -214,9 +214,9 @@ fn zip_0244() {
assert_eq!(
v5_signature_hash(
tx.deref(),
&txid_parts,
SIGHASH_SINGLE | SIGHASH_ANYONECANPAY,
&signable_input,
SIGHASH_SINGLE | SIGHASH_ANYONECANPAY
&txid_parts,
)
.as_ref(),
&tv.sighash_single_anyone.unwrap()
@ -226,7 +226,7 @@ fn zip_0244() {
let signable_input = SignableInput::Shielded;
assert_eq!(
v5_signature_hash(tx.deref(), &txid_parts, &signable_input, SIGHASH_ALL)
v5_signature_hash(tx.deref(), SIGHASH_ALL, &signable_input, &txid_parts)
.as_ref(),
tv.sighash_all
);

View File

@ -145,19 +145,19 @@ pub(crate) fn hash_tze_outputs(tze_outputs: &[TzeOut]) -> Blake2bHash {
pub(crate) fn hash_sapling_spends<A: sapling::Authorization>(
shielded_spends: &[SpendDescription<A>],
) -> Blake2bHash {
let mut ch = hasher(ZCASH_SAPLING_SPENDS_COMPACT_HASH_PERSONALIZATION);
let mut nh = hasher(ZCASH_SAPLING_SPENDS_NONCOMPACT_HASH_PERSONALIZATION);
for s_spend in shielded_spends {
// we build the hash of nullifiers separately for compact blocks.
ch.write_all(&s_spend.nullifier.as_ref()).unwrap();
nh.write_all(&s_spend.cv.to_bytes()).unwrap();
nh.write_all(&s_spend.anchor.to_repr()).unwrap();
s_spend.rk.write(&mut nh).unwrap();
}
let mut h = hasher(ZCASH_SAPLING_SPENDS_HASH_PERSONALIZATION);
if !shielded_spends.is_empty() {
let mut ch = hasher(ZCASH_SAPLING_SPENDS_COMPACT_HASH_PERSONALIZATION);
let mut nh = hasher(ZCASH_SAPLING_SPENDS_NONCOMPACT_HASH_PERSONALIZATION);
for s_spend in shielded_spends {
// we build the hash of nullifiers separately for compact blocks.
ch.write_all(&s_spend.nullifier.as_ref()).unwrap();
nh.write_all(&s_spend.cv.to_bytes()).unwrap();
nh.write_all(&s_spend.anchor.to_repr()).unwrap();
s_spend.rk.write(&mut nh).unwrap();
}
let compact_digest = ch.finalize();
h.write_all(&compact_digest.as_bytes()).unwrap();
let noncompact_digest = nh.finalize();
@ -173,23 +173,23 @@ pub(crate) fn hash_sapling_spends<A: sapling::Authorization>(
///
/// Then, hash these together personalized with ZCASH_SAPLING_OUTPUTS_HASH_PERSONALIZATION
pub(crate) fn hash_sapling_outputs<A>(shielded_outputs: &[OutputDescription<A>]) -> Blake2bHash {
let mut ch = hasher(ZCASH_SAPLING_OUTPUTS_COMPACT_HASH_PERSONALIZATION);
let mut mh = hasher(ZCASH_SAPLING_OUTPUTS_MEMOS_HASH_PERSONALIZATION);
let mut nh = hasher(ZCASH_SAPLING_OUTPUTS_NONCOMPACT_HASH_PERSONALIZATION);
for s_out in shielded_outputs {
ch.write_all(&s_out.cmu.to_repr().as_ref()).unwrap();
ch.write_all(&s_out.ephemeral_key.to_bytes()).unwrap();
ch.write_all(&s_out.enc_ciphertext[..52]).unwrap();
mh.write_all(&s_out.enc_ciphertext[52..564]).unwrap();
nh.write_all(&s_out.cv.to_bytes()).unwrap();
nh.write_all(&s_out.enc_ciphertext[564..]).unwrap();
nh.write_all(&s_out.out_ciphertext).unwrap();
}
let mut h = hasher(ZCASH_SAPLING_OUTPUTS_HASH_PERSONALIZATION);
if !shielded_outputs.is_empty() {
let mut ch = hasher(ZCASH_SAPLING_OUTPUTS_COMPACT_HASH_PERSONALIZATION);
let mut mh = hasher(ZCASH_SAPLING_OUTPUTS_MEMOS_HASH_PERSONALIZATION);
let mut nh = hasher(ZCASH_SAPLING_OUTPUTS_NONCOMPACT_HASH_PERSONALIZATION);
for s_out in shielded_outputs {
ch.write_all(&s_out.cmu.to_repr().as_ref()).unwrap();
ch.write_all(&s_out.ephemeral_key.to_bytes()).unwrap();
ch.write_all(&s_out.enc_ciphertext[..52]).unwrap();
mh.write_all(&s_out.enc_ciphertext[52..564]).unwrap();
nh.write_all(&s_out.cv.to_bytes()).unwrap();
nh.write_all(&s_out.enc_ciphertext[564..]).unwrap();
nh.write_all(&s_out.out_ciphertext).unwrap();
}
h.write_all(&ch.finalize().as_bytes()).unwrap();
h.write_all(&mh.finalize().as_bytes()).unwrap();
h.write_all(&nh.finalize().as_bytes()).unwrap();
@ -229,19 +229,13 @@ fn hash_header_txid_data(
) -> Blake2bHash {
let mut h = hasher(ZCASH_HEADERS_HASH_PERSONALIZATION);
(&mut h)
.write_u32::<LittleEndian>(version.header())
h.write_u32::<LittleEndian>(version.header()).unwrap();
h.write_u32::<LittleEndian>(version.version_group_id())
.unwrap();
(&mut h)
.write_u32::<LittleEndian>(version.version_group_id())
.unwrap();
(&mut h)
.write_u32::<LittleEndian>(consensus_branch_id.into())
.unwrap();
(&mut h).write_u32::<LittleEndian>(lock_time).unwrap();
(&mut h)
.write_u32::<LittleEndian>(expiry_height.into())
h.write_u32::<LittleEndian>(consensus_branch_id.into())
.unwrap();
h.write_u32::<LittleEndian>(lock_time).unwrap();
h.write_u32::<LittleEndian>(expiry_height.into()).unwrap();
h.finalize()
}