zcash_primitives: Drop `byteorder::LittleEndian` usage in sighash

This commit is contained in:
Jack Grigg 2023-05-15 20:44:02 +00:00
parent 31e5a5188e
commit 57a3914e3a
2 changed files with 12 additions and 22 deletions

View File

@ -1,5 +1,4 @@
use blake2b_simd::{Hash as Blake2bHash, Params as Blake2bParams};
use byteorder::{LittleEndian, WriteBytesExt};
use ff::PrimeField;
use crate::consensus::BranchId;
@ -22,13 +21,6 @@ const ZCASH_JOINSPLITS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashJSplitsHash";
const ZCASH_SHIELDED_SPENDS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashSSpendsHash";
const ZCASH_SHIELDED_OUTPUTS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashSOutputHash";
macro_rules! update_u32 {
($h:expr, $value:expr, $tmp:expr) => {
(&mut $tmp[..4]).write_u32::<LittleEndian>($value).unwrap();
$h.update(&$tmp[..4]);
};
}
macro_rules! update_hash {
($h:expr, $cond:expr, $value:expr) => {
if $cond {
@ -53,7 +45,7 @@ fn prevout_hash<TA: transparent::Authorization>(vin: &[TxIn<TA>]) -> Blake2bHash
fn sequence_hash<TA: transparent::Authorization>(vin: &[TxIn<TA>]) -> Blake2bHash {
let mut data = Vec::with_capacity(vin.len() * 4);
for t_in in vin {
data.write_u32::<LittleEndian>(t_in.sequence).unwrap();
data.extend_from_slice(&t_in.sequence.to_le_bytes());
}
Blake2bParams::new()
.hash_length(32)
@ -145,18 +137,15 @@ pub fn v4_signature_hash<
if tx.version.has_overwinter() {
let mut personal = [0; 16];
personal[..12].copy_from_slice(ZCASH_SIGHASH_PERSONALIZATION_PREFIX);
(&mut personal[12..])
.write_u32::<LittleEndian>(tx.consensus_branch_id.into())
.unwrap();
personal[12..].copy_from_slice(&u32::from(tx.consensus_branch_id).to_le_bytes());
let mut h = Blake2bParams::new()
.hash_length(32)
.personal(&personal)
.to_state();
let mut tmp = [0; 8];
update_u32!(h, tx.version.header(), tmp);
update_u32!(h, tx.version.version_group_id(), tmp);
h.update(&tx.version.header().to_le_bytes());
h.update(&tx.version.version_group_id().to_le_bytes());
update_hash!(
h,
hash_type & SIGHASH_ANYONECANPAY == 0,
@ -231,12 +220,12 @@ pub fn v4_signature_hash<
shielded_outputs_hash(tx.sapling_bundle.as_ref().unwrap().shielded_outputs())
);
}
update_u32!(h, tx.lock_time, tmp);
update_u32!(h, tx.expiry_height.into(), tmp);
h.update(&tx.lock_time.to_le_bytes());
h.update(&u32::from(tx.expiry_height).to_le_bytes());
if tx.version.has_sapling() {
h.update(&tx.sapling_value_balance().to_i64_le_bytes());
}
update_u32!(h, hash_type.into(), tmp);
h.update(&u32::from(hash_type).to_le_bytes());
match signable_input {
SignableInput::Shielded => (),
@ -251,8 +240,7 @@ pub fn v4_signature_hash<
bundle.vin[*index].prevout.write(&mut data).unwrap();
script_code.write(&mut data).unwrap();
data.extend_from_slice(&value.to_i64_le_bytes());
data.write_u32::<LittleEndian>(bundle.vin[*index].sequence)
.unwrap();
data.extend_from_slice(&bundle.vin[*index].sequence.to_le_bytes());
h.update(&data);
} else {
panic!(

View File

@ -1,7 +1,6 @@
use std::io::Write;
use blake2b_simd::{Hash as Blake2bHash, Params, State};
use byteorder::{LittleEndian, WriteBytesExt};
use zcash_encoding::Array;
use crate::transaction::{
@ -17,6 +16,9 @@ use crate::transaction::{
Authorization, TransactionData, TransparentDigests, TxDigests,
};
#[cfg(feature = "zfuture")]
use byteorder::WriteBytesExt;
#[cfg(feature = "zfuture")]
use zcash_encoding::{CompactSize, Vector};
@ -121,7 +123,7 @@ fn transparent_sig_digest<A: TransparentAuthorizingContext>(
txin.prevout.write(&mut ch).unwrap();
ch.write_all(&value.to_i64_le_bytes()).unwrap();
script_pubkey.write(&mut ch).unwrap();
ch.write_u32::<LittleEndian>(txin.sequence).unwrap();
ch.write_all(&txin.sequence.to_le_bytes()).unwrap();
}
let txin_sig_digest = ch.finalize();