Skip to content

Tests checking that TrezorSigner produces the same signatures as SoftwareSigner #1928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: fill_order_dest_tests
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion blockprod/src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl BlockProduction {
block_timestamp,
block_height,
make_ancestor_getter(cs),
randomness::make_true_rng(),
&mut randomness::make_true_rng(),
)?;
let consensus_data = ConsensusData::PoS(Box::new(consensus_data));

Expand Down Expand Up @@ -591,6 +591,7 @@ impl BlockProduction {
max_block_timestamp_for_pos,
stop_flag,
finalize_block_data,
&mut randomness::make_true_rng(),
)
.map_err(BlockProductionError::FailedConsensusInitialization);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
const MESSAGE_MAGIC_PREFIX: &str = "===MINTLAYER MESSAGE BEGIN===\n";
const MESSAGE_MAGIC_SUFFIX: &str = "\n===MINTLAYER MESSAGE END===";

use randomness::{CryptoRng, Rng};
use thiserror::Error;

use crypto::key::SigAuxDataProvider;
use serialization::Encode;

use crate::{
Expand Down Expand Up @@ -99,21 +99,21 @@ impl ArbitraryMessageSignature {
self.as_ref().verify_signature(chain_config, destination, challenge)
}

pub fn produce_uniparty_signature<R: Rng + CryptoRng>(
pub fn produce_uniparty_signature<AuxP: SigAuxDataProvider + ?Sized>(
private_key: &crypto::key::PrivateKey,
destination: &Destination,
message: &[u8],
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<Self, SignArbitraryMessageError> {
let challenge = produce_message_challenge(message);
let signature =
match destination {
Destination::PublicKeyHash(pubkeyhash) => {
let sig = sign_public_key_hash_spending(private_key, pubkeyhash, &challenge, rng)?;
let sig = sign_public_key_hash_spending(private_key, pubkeyhash, &challenge, sig_aux_data_provider)?;
sig.encode()
}
Destination::PublicKey(pubkey) => {
let sig = sign_public_key_spending(private_key, pubkey, &challenge, rng)?;
let sig = sign_public_key_spending(private_key, pubkey, &challenge, sig_aux_data_provider)?;
sig.encode()
}
Destination::ScriptHash(_) => return Err(SignArbitraryMessageError::Unsupported),
Expand All @@ -132,13 +132,19 @@ impl ArbitraryMessageSignature {
})
}

pub fn produce_uniparty_signature_as_pub_key_hash_spending<R: Rng + CryptoRng>(
pub fn produce_uniparty_signature_as_pub_key_hash_spending<
AuxP: SigAuxDataProvider + ?Sized,
>(
private_key: &crypto::key::PrivateKey,
message: &[u8],
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<Self, SignArbitraryMessageError> {
let challenge = produce_message_challenge(message);
let signature = sign_public_key_hash_spending_unchecked(private_key, &challenge, rng)?;
let signature = sign_public_key_hash_spending_unchecked(
private_key,
&challenge,
sig_aux_data_provider,
)?;
let signature = signature.encode();

Ok(Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn produce_uniparty_signature_as_pub_key_hash_spending_matches_produce_uniparty_
&private_key,
&destination_addr,
&message,
test_utils::random::make_seedable_rng(signer_rng_seed),
&mut test_utils::random::make_seedable_rng(signer_rng_seed),
)
.unwrap();
sig1.verify_signature(&chain_config, &destination_addr, &message_challenge)
Expand All @@ -101,7 +101,7 @@ fn produce_uniparty_signature_as_pub_key_hash_spending_matches_produce_uniparty_
let sig2 = ArbitraryMessageSignature::produce_uniparty_signature_as_pub_key_hash_spending(
&private_key,
&message,
test_utils::random::make_seedable_rng(signer_rng_seed),
&mut test_utils::random::make_seedable_rng(signer_rng_seed),
)
.unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crypto::key::Signature;
use randomness::{CryptoRng, Rng};
use crypto::key::{SigAuxDataProvider, Signature};
use serialization::{Decode, DecodeAll, Encode};

use crate::{chain::signature::DestinationSigError, primitives::H256};
Expand Down Expand Up @@ -48,19 +47,19 @@ pub fn verify_public_key_spending(
Ok(())
}

pub fn sign_public_key_spending<R: Rng + CryptoRng>(
pub fn sign_public_key_spending<AuxP: SigAuxDataProvider + ?Sized>(
private_key: &crypto::key::PrivateKey,
spendee_pubkey: &crypto::key::PublicKey,
sighash: &H256,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<AuthorizedPublicKeySpend, DestinationSigError> {
let calculated_public_key = crypto::key::PublicKey::from_private_key(private_key);
if *spendee_pubkey != calculated_public_key {
return Err(DestinationSigError::SpendeePrivatePublicKeyMismatch);
}
let msg = sighash.encode();
let signature = private_key
.sign_message(&msg, rng)
.sign_message(&msg, sig_aux_data_provider)
.map_err(DestinationSigError::ProducingSignatureFailed)?;

Ok(AuthorizedPublicKeySpend::new(signature))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crypto::key::{PrivateKey, PublicKey, Signature};
use randomness::{CryptoRng, Rng};
use crypto::key::{PrivateKey, PublicKey, SigAuxDataProvider, Signature};
use serialization::{Decode, DecodeAll, Encode};

use crate::{
Expand Down Expand Up @@ -58,40 +57,40 @@ pub fn verify_public_key_hash_spending(
Ok(())
}

pub fn sign_public_key_hash_spending<R: Rng + CryptoRng>(
pub fn sign_public_key_hash_spending<AuxP: SigAuxDataProvider + ?Sized>(
private_key: &PrivateKey,
spendee_addr: &PublicKeyHash,
sighash: &H256,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<AuthorizedPublicKeyHashSpend, DestinationSigError> {
let public_key = PublicKey::from_private_key(private_key);
let calculated_addr = PublicKeyHash::from(&public_key);
if calculated_addr != *spendee_addr {
return Err(DestinationSigError::PublicKeyToHashMismatch);
}
sign_public_key_hash_spending_impl(private_key, public_key, sighash, rng)
sign_public_key_hash_spending_impl(private_key, public_key, sighash, sig_aux_data_provider)
}

pub fn sign_public_key_hash_spending_unchecked<R: Rng + CryptoRng>(
pub fn sign_public_key_hash_spending_unchecked<AuxP: SigAuxDataProvider + ?Sized>(
private_key: &PrivateKey,
sighash: &H256,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<AuthorizedPublicKeyHashSpend, DestinationSigError> {
let public_key = PublicKey::from_private_key(private_key);
sign_public_key_hash_spending_impl(private_key, public_key, sighash, rng)
sign_public_key_hash_spending_impl(private_key, public_key, sighash, sig_aux_data_provider)
}

fn sign_public_key_hash_spending_impl<R: Rng + CryptoRng>(
fn sign_public_key_hash_spending_impl<AuxP: SigAuxDataProvider + ?Sized>(
private_key: &PrivateKey,
public_key: PublicKey,
sighash: &H256,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<AuthorizedPublicKeyHashSpend, DestinationSigError> {
debug_assert_eq!(public_key, PublicKey::from_private_key(private_key));

let msg = sighash.encode();
let signature = private_key
.sign_message(&msg, rng)
.sign_message(&msg, sig_aux_data_provider)
.map_err(DestinationSigError::ProducingSignatureFailed)?;

Ok(AuthorizedPublicKeyHashSpend::new(public_key, signature))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

use std::collections::BTreeMap;

use crypto::key::Signature;
use randomness::{CryptoRng, Rng};
use crypto::key::{SigAuxDataProvider, Signature};
use serialization::{Decode, DecodeAll, Encode};

use crate::{
Expand Down Expand Up @@ -176,14 +175,14 @@ pub enum ClassicalMultisigSigningError {
/// A signature cannot be added more than once. Also, in every iteration, all the signatures must be valid,
/// and obviously the challenge must be valid too, since there is no point in adding signatures to anything
/// that is considered invalid.
pub fn sign_classical_multisig_spending(
pub fn sign_classical_multisig_spending<AuxP: SigAuxDataProvider + ?Sized>(
chain_config: &ChainConfig,
key_index: u8,
private_key: &crypto::key::PrivateKey,
challenge: &ClassicMultisigChallenge,
sighash: &H256,
current_signatures: AuthorizedClassicalMultisigSpend,
rng: &mut (impl Rng + CryptoRng),
sig_aux_data_provider: &mut AuxP,
) -> Result<ClassicalMultisigCompletionStatus, ClassicalMultisigSigningError> {
// ensure the challenge is valid before signing it
if let Err(ch_err) = challenge.is_valid(chain_config) {
Expand Down Expand Up @@ -240,7 +239,7 @@ pub fn sign_classical_multisig_spending(
return Err(ClassicalMultisigSigningError::SpendeePrivateChallengePublicKeyMismatch);
}
let signature = private_key
.sign_message(&msg, rng)
.sign_message(&msg, sig_aux_data_provider)
.map_err(ClassicalMultisigSigningError::ProducingSignatureFailed)?;

let mut current_signatures = current_signatures;
Expand Down Expand Up @@ -687,7 +686,7 @@ mod tests {
signatures.insert(
*tampered_with_key_index,
new_random_private_key
.sign_message(&sighash.encode(), randomness::make_true_rng())
.sign_message(&sighash.encode(), &mut randomness::make_true_rng())
.unwrap(),
);

Expand Down Expand Up @@ -920,7 +919,7 @@ mod tests {
let (new_random_private_key, _) =
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
let sig = new_random_private_key
.sign_message(&sighash.encode(), randomness::make_true_rng())
.sign_message(&sighash.encode(), &mut randomness::make_true_rng())
.unwrap();
let new_sigs = BTreeMap::from([(key_index, sig)]);
let tampered_with_signatures = AuthorizedClassicalMultisigSpend::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ mod tests {
let tampered_signature = priv_keys[tampered_index as usize]
.sign_message(
&H256::random_using(rng).encode(),
randomness::make_true_rng(),
&mut randomness::make_true_rng(),
)
.unwrap();
// replace the signatures with a tampered one
Expand Down Expand Up @@ -368,7 +368,7 @@ mod tests {
.0
.sign_message(
&H256::random_using(rng).encode(),
randomness::make_true_rng(),
&mut randomness::make_true_rng(),
)
.unwrap();
// replace the signatures with a tampered one
Expand Down
8 changes: 4 additions & 4 deletions common/src/chain/transaction/signature/inputsig/htlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use randomness::{CryptoRng, Rng};
use crypto::key::SigAuxDataProvider;
use serialization::Encode;

use standard_signature::StandardInputSignature;
Expand All @@ -28,15 +28,15 @@ use super::{
};

#[allow(clippy::too_many_arguments)]
pub fn produce_uniparty_signature_for_htlc_input<T: Signable, R: Rng + CryptoRng>(
pub fn produce_uniparty_signature_for_htlc_input<T: Signable, AuxP: SigAuxDataProvider + ?Sized>(
private_key: &crypto::key::PrivateKey,
sighash_type: SigHashType,
outpoint_destination: Destination,
tx: &T,
inputs_utxos: &[Option<&TxOutput>],
input_num: usize,
htlc_secret: HtlcSecret,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<StandardInputSignature, DestinationSigError> {
let sig = StandardInputSignature::produce_uniparty_signature_for_input(
private_key,
Expand All @@ -45,7 +45,7 @@ pub fn produce_uniparty_signature_for_htlc_input<T: Signable, R: Rng + CryptoRng
tx,
inputs_utxos,
input_num,
rng,
sig_aux_data_provider,
)?;

let sig_with_secret =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use std::io::BufWriter;

use randomness::{CryptoRng, Rng};
use crypto::key::SigAuxDataProvider;
use serialization::{Decode, DecodeAll, Encode};

use crate::{
Expand Down Expand Up @@ -100,23 +100,23 @@ impl StandardInputSignature {
Ok(())
}

pub fn produce_uniparty_signature_for_input<T: Signable, R: Rng + CryptoRng>(
pub fn produce_uniparty_signature_for_input<T: Signable, AuxP: SigAuxDataProvider + ?Sized>(
private_key: &crypto::key::PrivateKey,
sighash_type: SigHashType,
outpoint_destination: Destination,
tx: &T,
inputs_utxos: &[Option<&TxOutput>],
input_num: usize,
rng: R,
sig_aux_data_provider: &mut AuxP,
) -> Result<Self, DestinationSigError> {
let sighash = signature_hash(sighash_type, tx, inputs_utxos, input_num)?;
let serialized_sig = match outpoint_destination {
Destination::PublicKeyHash(ref addr) => {
let sig = sign_public_key_hash_spending(private_key, addr, &sighash, rng)?;
let sig = sign_public_key_hash_spending(private_key, addr, &sighash, sig_aux_data_provider)?;
sig.encode()
}
Destination::PublicKey(ref pubkey) => {
let sig = sign_public_key_spending(private_key, pubkey, &sighash, rng)?;
let sig = sign_public_key_spending(private_key, pubkey, &sighash, sig_aux_data_provider)?;
sig.encode()
}
Destination::ScriptHash(_) => return Err(DestinationSigError::Unsupported),
Expand Down
Loading