Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cron-daily-fuzz.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
######
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-fuzz.sh.
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-files.sh.
## Edit that script instead and re-run it.
######
name: Fuzz
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
######
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-fuzz.sh.
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-files.sh.
## Edit that script instead and re-run it.
######
[package]
Expand Down
5 changes: 3 additions & 2 deletions fuzz/generate-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source "$REPO_DIR/fuzz/fuzz-util.sh"
# 1. Generate fuzz/Cargo.toml
cat > "$REPO_DIR/fuzz/Cargo.toml" <<EOF
######
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-fuzz.sh.
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-files.sh.
## Edit that script instead and re-run it.
######
[package]
Expand All @@ -38,6 +38,7 @@ use_self = "warn"

[package.metadata.rbmt.lint]
allowed_duplicates = [
"bitcoin_hashes",
"bitcoin-internals",
"hex-conservative",
"getrandom",
Expand All @@ -61,7 +62,7 @@ done
# 2. Generate .github/workflows/cron-daily-fuzz.yml
cat > "$REPO_DIR/.github/workflows/cron-daily-fuzz.yml" <<EOF
######
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-fuzz.sh.
## DO NOT EDIT THIS FILE DIRECTLY. It is generated by generate-files.sh.
## Edit that script instead and re-run it.
######
name: Fuzz
Expand Down
81 changes: 81 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
ignore = [
"/",
"!/src/lib.rs",
"!/src/confidential/*.rs",
]
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
indent_style = "Block"

max_width = 100 # This is the number of characters.
# `use_small_heuristics` is ignored if the granular width config values are explicitly set.
use_small_heuristics = "Max" # "Max" == All granular width settings same as `max_width`.
# # Granular width configuration settings. These are percentages of `max_width`.
# fn_call_width = 60
# attr_fn_like_width = 70
# struct_lit_width = 18
# struct_variant_width = 35
# array_width = 60
# chain_width = 60
# single_line_if_else_max_width = 50

wrap_comments = false
format_code_in_doc_comments = false
comment_width = 100 # Default 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
hex_literal_case = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = true # Default false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Module" # Default "Preserve"
group_imports = "StdExternalCrate" # Default "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = false # Default true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2021"
style_edition = "2021"
inline_attribute_width = 0
format_generated_files = true
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = false
disable_all_formatting = false
skip_children = false
show_parse_errors = true
error_on_line_overflow = false
error_on_unformatted = false
emit_mode = "Files"
make_backup = false
138 changes: 8 additions & 130 deletions src/blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use secp256k1_zkp::{
rand::{CryptoRng, RngCore},
PedersenCommitment, SecretKey, Tag, Tweak, Verification, ZERO_TWEAK,
};
use secp256k1_zkp::{Generator, RangeProof, Secp256k1, Signing, SurjectionProof};
use secp256k1_zkp::{Generator, Secp256k1, Signing};

use crate::{AddressParams, Script, TxIn};
use crate::{AddressParams, RangeProof, Script, TxIn, SurjectionProof};

use crate::{
confidential::{Asset, AssetBlindingFactor, Nonce, Value, ValueBlindingFactor},
Expand Down Expand Up @@ -497,13 +497,7 @@ impl Asset {
})
.collect::<Result<Vec<_>, _>>()?;

let surjection_proof = SurjectionProof::new(
secp,
rng,
asset.into_tag(),
asset_bf.into_inner(),
inputs.as_ref(),
)?;
let surjection_proof = SurjectionProof::new(secp, rng, asset, asset_bf, inputs)?;

Ok((out_asset, surjection_proof))
}
Expand Down Expand Up @@ -657,7 +651,7 @@ impl TxOut {
out_secrets.asset_bf,
);
let exp_value = Value::Explicit(out_secrets.value);
let (out_value, nonce, range_proof) = exp_value.blind(
let (out_value, nonce, rangeproof) = exp_value.blind(
secp,
out_secrets.value_bf,
receiver_blinding_pk,
Expand All @@ -672,8 +666,8 @@ impl TxOut {
nonce,
script_pubkey: spk,
witness: TxOutWitness {
surjection_proof: Some(Box::new(surjection_proof)),
rangeproof: Some(Box::new(range_proof)),
surjection_proof,
rangeproof,
},
};
Ok(txout)
Expand Down Expand Up @@ -977,10 +971,10 @@ impl TxIn {
let (comm, prf) = v.blind_with_shared_secret(secp, bf, blind_sk, &spk, &msg)?;
if i == 0 {
self.asset_issuance.amount = comm;
self.witness.amount_rangeproof = Some(Box::new(prf));
self.witness.amount_rangeproof = prf;
} else {
self.asset_issuance.inflation_keys = comm;
self.witness.inflation_keys_rangeproof = Some(Box::new(prf));
self.witness.inflation_keys_rangeproof = prf;
}
}
Ok(())
Expand Down Expand Up @@ -1365,122 +1359,6 @@ impl From<ConfidentialTxOutError> for BlindError {
}
}

/// A trait to create and verify explicit rangeproofs
pub trait BlindValueProofs: Sized {
/// Outputs a `[RangeProof]` that blinded value
/// corresponfs to unblinded explicit value
fn blind_value_proof<C: secp256k1_zkp::Signing, R: RngCore + CryptoRng>(
rng: &mut R,
secp: &Secp256k1<C>,
explicit_val: u64,
value_commit: PedersenCommitment,
asset_gen: Generator,
vbf: ValueBlindingFactor,
) -> Result<Self, secp256k1_zkp::Error>;

/// Verify that the Rangeproof proves that commitment
/// is actually bound to the explicit value
fn blind_value_proof_verify<C: secp256k1_zkp::Verification>(
&self,
secp: &Secp256k1<C>,
explicit_val: u64,
asset_gen: Generator,
value_commit: PedersenCommitment,
) -> bool;
}

impl BlindValueProofs for RangeProof {
/// Outputs a [`RangeProof`] that blinded `value_commit`
/// corresponds to explicit value
fn blind_value_proof<C: secp256k1_zkp::Signing, R: RngCore + CryptoRng>(
rng: &mut R,
secp: &Secp256k1<C>,
explicit_val: u64,
value_commit: PedersenCommitment,
asset_gen: Generator,
vbf: ValueBlindingFactor,
) -> Result<Self, secp256k1_zkp::Error> {
RangeProof::new(
secp,
explicit_val, // min_value
value_commit, // value_commit
explicit_val, // value
vbf.into_inner(), // blinding factor
&[], // message
&[], // add commitment
SecretKey::new(rng), // nonce
-1, // exp
0, // min bits
asset_gen, // additional gen
)
}

/// Verify that the Rangeproof proves that commitment
/// is actually bound to the explicit value
fn blind_value_proof_verify<C: secp256k1_zkp::Verification>(
&self,
secp: &Secp256k1<C>,
explicit_val: u64,
asset_gen: Generator,
value_commit: PedersenCommitment,
) -> bool {
let r = self.verify(secp, value_commit, &[], asset_gen);
match r {
Ok(e) => e.start == explicit_val && e.end - 1 == explicit_val,
Err(..) => false,
}
}
}

/// A trait to create and verify explicit surjection proofs
pub trait BlindAssetProofs: Sized {
/// Outputs a `[SurjectionProof]` that blinded asset
/// corresponfs to unblinded explicit asset
fn blind_asset_proof<C: secp256k1_zkp::Signing, R: RngCore + CryptoRng>(
rng: &mut R,
secp: &Secp256k1<C>,
asset: AssetId,
abf: AssetBlindingFactor,
) -> Result<Self, secp256k1_zkp::Error>;

/// Verify that the Surjection proves that asset commitment
/// is actually bound to the explicit asset
fn blind_asset_proof_verify(
&self,
secp: &Secp256k1<secp256k1_zkp::All>,
asset: AssetId,
asset_commit: Generator,
) -> bool;
}

impl BlindAssetProofs for SurjectionProof {
fn blind_asset_proof<C: secp256k1_zkp::Signing, R: RngCore + CryptoRng>(
rng: &mut R,
secp: &Secp256k1<C>,
asset: AssetId,
abf: AssetBlindingFactor,
) -> Result<Self, secp256k1_zkp::Error> {
let gen = Generator::new_unblinded(secp, asset.into_tag());
SurjectionProof::new(
secp,
rng,
asset.into_tag(),
abf.into_inner(),
&[(gen, asset.into_tag(), ZERO_TWEAK)],
)
}

fn blind_asset_proof_verify(
&self,
secp: &Secp256k1<secp256k1_zkp::All>,
asset: AssetId,
asset_commit: Generator,
) -> bool {
let gen = Generator::new_unblinded(secp, asset.into_tag());
self.verify(secp, asset_commit, &[gen])
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading