Skip to content

Commit

Permalink
Merge #143: Blinding API cleanups
Browse files Browse the repository at this point in the history
582325e rustfmt: add src/pset/mod.rs and src/blind.rs (sanket1729)
759d301 Add liquid testnet parameters (sanket1729)
2908bc1 Allow inserting inputs/outputs at specified positions (sanket1729)
cbc2d56 Add issuance surjection proof verification (sanket1729)
3583ae2 Fix pset key bug (sanket1729)
2815e52 Fix pset Tweak serde (sanket1729)
3107c25 Add support for issuance (sanket1729)
fa753b4 Cleanup bunch of blinding APIs (sanket1729)
d521aee Remove public has_issuance field from TxIn (sanket1729)

Pull request description:

  - This required the user to explicitly maintain whether the current TxIn
  has issuance or not whereas it can directly be calculated from
  assetIssuance field

  The blinding APIs are separated into smaller chunks. This allows
      - Blinding only the values or the assets but not both
      - Not relying on rng to set asset/value blinding factors
      - Better input parameters

  Note I had to edit the test vectors the order in which they were sampled from rng had changed.

ACKs for top commit:
  apoelstra:
    ACK 582325e

Tree-SHA512: 1d70b58bc136341d1933a872749982cac8507aa5f355774de2dcaecfe68f89926d938bdf24031e8e34782562c5c78bb10d00d5726a2113c5dea1ee139bb2de0e
  • Loading branch information
apoelstra committed Oct 5, 2022
2 parents 2547a2d + 582325e commit 55b6cce
Show file tree
Hide file tree
Showing 26 changed files with 1,587 additions and 775 deletions.
14 changes: 5 additions & 9 deletions examples/pset_blind_coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,16 @@ fn main() {
// ----------------------------------------------------------
// B Adds it's own outputs. Step 2 completed
// ----- Step 3: B to blind it's own outputs
let inp_txout_sec = [
None,
Some(&asset_txout_secrets.sec),
];
let mut inp_txout_sec = HashMap::new();
inp_txout_sec.insert(1, asset_txout_secrets.sec);

pset.blind_non_last(&mut rng, &secp, &inp_txout_sec).unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_B_blinded"]));

// Step 4: A blinds it's own inputs
let inp_txout_sec = [
Some(&btc_txout_secrets.sec),
None,
];
pset.blind_last(&mut rng, &secp, &inp_txout_sec).unwrap();
let mut inp_txout_sec_a = HashMap::new();
inp_txout_sec_a.insert(0, btc_txout_secrets.sec);
pset.blind_last(&mut rng, &secp, &inp_txout_sec_a).unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_blinded"]));

// check whether the blinding was correct
Expand Down
23 changes: 10 additions & 13 deletions examples/raw_blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use elements::{
bitcoin::PublicKey, pset::PartiallySignedTransaction as Pset, Address, AddressParams, OutPoint,
Script, TxOutSecrets, TxOutWitness, Txid, WScriptHash,
};
use elements::{pset, secp256k1_zkp};
use elements::{pset, secp256k1_zkp, SurjectionInput};

use elements::encode::{deserialize, serialize_hex};
use elements::hashes::hex::FromHex;
Expand Down Expand Up @@ -168,8 +168,8 @@ fn main() {
// Add outputs
// Send 5_000 worth of asset units to new address
let inputs = [
(btc_txout.asset, Some(&btc_txout_secrets.sec)),
(asset_txout.asset, Some(&asset_txout_secrets.sec)),
(SurjectionInput::from_txout_secrets(btc_txout_secrets.sec)),
(SurjectionInput::from_txout_secrets(asset_txout_secrets.sec)),
];

let dest_wsh =
Expand All @@ -179,7 +179,7 @@ fn main() {
let dest_blind_pk =
PublicKey::from_str("0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea")
.unwrap();
let (dest_asset_txout, dest_abf, dest_vbf) = TxOut::new_not_last_confidential(
let (dest_asset_txout, dest_abf, dest_vbf, _) = TxOut::new_not_last_confidential(
&mut rng,
&secp,
dest_amt,
Expand All @@ -200,7 +200,7 @@ fn main() {
let change_wsh =
WScriptHash::from_str("f6b43d56e004e9d0b1ec2fc3c95511d81af08420992be8dec7f86cdf8970b3e2")
.unwrap();
let (change_asset_txout, asset_change_abf, asset_change_vbf) =
let (change_asset_txout, asset_change_abf, asset_change_vbf, _) =
TxOut::new_not_last_confidential(
&mut rng,
&secp,
Expand Down Expand Up @@ -254,19 +254,16 @@ fn main() {

// For the last output we require all secrets.
let inputs = [
(btc_txout.asset, &btc_txout_secrets.sec),
(asset_txout.asset, &asset_txout_secrets.sec),
btc_txout_secrets.sec,
asset_txout_secrets.sec,
];
let (btc_change_txout, _abf, _vbf) = TxOut::new_last_confidential(
let (btc_change_txout, _abf, _vbf, _) = TxOut::new_last_confidential(
&mut rng,
&secp,
change_amt,
Address::p2wsh(
&Script::new_v0_wsh(&change_wsh),
Some(change_blind_pk.inner),
&PARAMS,
),
btc_txout_secrets.sec.asset,
Script::new_v0_wsh(&change_wsh),
change_blind_pk.inner,
&inputs,
&output_secrets,
)
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_one_inp_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_unsigned.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/extracted_tx.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/finalized.hex

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ impl AddressParams {
bech_hrp: "ert",
blech_hrp: "el",
};

/// The default liquid testnet network address parameters.
pub const LIQUID_TESTNET: AddressParams = AddressParams {
p2pkh_prefix: 36,
p2sh_prefix: 19,
blinded_prefix: 23,
bech_hrp: "tex",
blech_hrp: "tlq",
};
}

/// The method used to produce an address
Expand Down Expand Up @@ -439,7 +448,7 @@ impl Address {
if data.len() < 2 || data.len() > 40 + if blinded { 33 } else { 0 } {
return Err(AddressError::InvalidWitnessProgramLength(data.len() - if blinded { 33 } else { 0 }));
}

// Specific segwit v0 check.
if !blinded && version.to_u8() == 0 && data.len() != 20 && data.len() != 32 {
return Err(AddressError::InvalidSegwitV0ProgramLength(data.len()));
Expand Down Expand Up @@ -649,20 +658,19 @@ impl FromStr for Address {
// shorthands
let liq = &AddressParams::LIQUID;
let ele = &AddressParams::ELEMENTS;
let liq_test = &AddressParams::LIQUID_TESTNET;

let net_arr = [liq, ele, liq_test];

// Bech32.
let prefix = find_prefix(s);
if match_prefix(prefix, liq.bech_hrp) {
return Address::from_bech32(s, false, liq);
}
if match_prefix(prefix, liq.blech_hrp) {
return Address::from_bech32(s, true, liq);
}
if match_prefix(prefix, ele.bech_hrp) {
return Address::from_bech32(s, false, ele);
}
if match_prefix(prefix, ele.blech_hrp) {
return Address::from_bech32(s, true, ele);
for net in net_arr.iter() {
// Bech32.
if match_prefix(prefix, net.bech_hrp) {
return Address::from_bech32(s, false, net);
}
if match_prefix(prefix, net.blech_hrp) {
return Address::from_bech32(s, true, net);
}
}

// Base58.
Expand All @@ -675,11 +683,10 @@ impl FromStr for Address {
}

let p = data[0];
if p == liq.p2pkh_prefix || p == liq.p2sh_prefix || p == liq.blinded_prefix {
return Address::from_base58(&data, liq);
}
if p == ele.p2pkh_prefix || p == ele.p2sh_prefix || p == ele.blinded_prefix {
return Address::from_base58(&data, ele);
for net in net_arr.iter() {
if p == net.p2pkh_prefix || p == net.p2sh_prefix || p == net.blinded_prefix {
return Address::from_base58(&data, net);
}
}

Err(AddressError::InvalidAddress(s.to_owned()))
Expand Down
Loading

0 comments on commit 55b6cce

Please sign in to comment.