Skip to content
Draft
100 changes: 90 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ parking_lot = "0.12.1"
paste = "1.0.14"
pbkdf2 = "0.12.2"
portable-atomic = { version = "1.5.1", features = ["float"] }
pqcrypto-falcon = { version = "0.4.1" }
pqcrypto-traits = "0.3.5"
prost = "0.13.2"
rand = "0.8.5"
rand_chacha = "0.3.1"
Expand Down Expand Up @@ -339,4 +341,14 @@ strip = false

[workspace.lints.clippy]
empty_docs = "allow"
uninlined_format_args = "allow"
uninlined_format_args = "allow"

[patch.crates-io.pqcrypto-falcon]
git = "https://github.com/rustpq/pqcrypto"
package = "pqcrypto-falcon"
rev = "4d95901b90512bc5c38a4c72642af2309e46d8a1"

[patch.crates-io.pqcrypto-traits]
git = "https://github.com/rustpq/pqcrypto"
package = "pqcrypto-traits"
rev = "4d95901b90512bc5c38a4c72642af2309e46d8a1"
31 changes: 26 additions & 5 deletions consensus/core/src/hashing/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,22 @@ pub fn hash_script_public_key(hasher: &mut impl Hasher, script_public_key: &Scri
hasher.write_var_bytes(script_public_key.script());
}

pub fn calc_schnorr_signature_hash(
pub fn schnorr_hash_input_fields(
hasher: &mut impl Hasher,
verifiable_tx: &impl VerifiableTransaction,
input_index: usize,
hash_type: SigHashType,
reused_values: &impl SigHashReusedValues,
) -> Hash {
) {
let input = verifiable_tx.populated_input(input_index);
let tx = verifiable_tx.tx();
let mut hasher = TransactionSigningHash::new();
hasher
.write_u16(tx.version)
.update(previous_outputs_hash(tx, hash_type, reused_values))
.update(sequences_hash(tx, hash_type, reused_values))
.update(sig_op_counts_hash(tx, hash_type, reused_values));
hash_outpoint(&mut hasher, input.0.previous_outpoint);
hash_script_public_key(&mut hasher, &input.1.script_public_key);
hash_outpoint(hasher, input.0.previous_outpoint);
hash_script_public_key(hasher, &input.1.script_public_key);
hasher
.write_u64(input.1.amount)
.write_u64(input.0.sequence)
Expand All @@ -261,6 +261,16 @@ pub fn calc_schnorr_signature_hash(
.write_u64(tx.gas)
.update(payload_hash(tx, reused_values))
.write_u8(hash_type.to_u8());
}

pub fn calc_schnorr_signature_hash(
verifiable_tx: &impl VerifiableTransaction,
input_index: usize,
hash_type: SigHashType,
reused_values: &impl SigHashReusedValues,
) -> Hash {
let mut hasher = TransactionSigningHash::new();
schnorr_hash_input_fields(&mut hasher, verifiable_tx, input_index, hash_type, reused_values);
hasher.finalize()
}

Expand All @@ -276,6 +286,17 @@ pub fn calc_ecdsa_signature_hash(
hasher.finalize()
}

pub fn calc_falcon_signature_hash(
tx: &impl VerifiableTransaction,
input_index: usize,
hash_type: SigHashType,
reused_values: &impl SigHashReusedValues,
) -> Hash {
let mut hasher = TransactionSigningHash::new();
schnorr_hash_input_fields(&mut hasher, tx, input_index, hash_type, reused_values);
hasher.finalize()
}

#[cfg(test)]
mod tests {
use std::{str::FromStr, vec};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn check_scripts(sig_cache: &Cache<SigCacheKey, bool>, tx: &(impl Verifiable
pub fn check_scripts_sequential(sig_cache: &Cache<SigCacheKey, bool>, tx: &impl VerifiableTransaction) -> TxResult<()> {
let reused_values = SigHashReusedValuesUnsync::new();
for (i, (input, entry)) in tx.populated_inputs().enumerate() {
TxScriptEngine::from_transaction_input(tx, input, i, entry, &reused_values, sig_cache)
TxScriptEngine::from_transaction_input(tx, input, i, entry, &reused_values, sig_cache, false) // todo falcon
.execute()
.map_err(|err| map_script_err(err, input))?;
}
Expand All @@ -194,7 +194,7 @@ pub fn check_scripts_par_iter(sig_cache: &Cache<SigCacheKey, bool>, tx: &(impl V
let reused_values = SigHashReusedValuesSync::new();
(0..tx.inputs().len()).into_par_iter().try_for_each(|idx| {
let (input, utxo) = tx.populated_input(idx);
TxScriptEngine::from_transaction_input(tx, input, idx, utxo, &reused_values, sig_cache)
TxScriptEngine::from_transaction_input(tx, input, idx, utxo, &reused_values, sig_cache, false) // todo falcon
.execute()
.map_err(|err| map_script_err(err, input))
})
Expand Down
2 changes: 2 additions & 0 deletions crypto/txscript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ kaspa-utils.workspace = true
kaspa-wasm-core.workspace = true
log.workspace = true
parking_lot.workspace = true
pqcrypto-falcon = { workspace = true, features = ["getrandom_wasm_js"] } # todo should only be included in case of target wasm32-unknown-unknown
pqcrypto-traits.workspace = true
rand.workspace = true
secp256k1.workspace = true
serde_json.workspace = true
Expand Down
Loading