Skip to content

Commit

Permalink
feat: upgrade tari_crypto sign api (#4932)
Browse files Browse the repository at this point in the history
Description
---
Upgrade the tari crypto signing api to version 0.16.1
Add back FFI signing for wallet to use push notification

How Has This Been Tested?
---
Unit tests
  • Loading branch information
SWvheerden authored Nov 18, 2022
1 parent 9acdf64 commit e2b7ad1
Show file tree
Hide file tree
Showing 35 changed files with 323 additions and 562 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion applications/tari_app_grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2018"
tari_common_types = { version = "^0.40", path = "../../base_layer/common_types" }
tari_comms = { path = "../../comms/core" }
tari_core = { path = "../../base_layer/core" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_script = { path = "../../infrastructure/tari_script" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }

Expand Down
2 changes: 1 addition & 1 deletion applications/tari_app_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "BSD-3-Clause"

[dependencies]
tari_comms = { path = "../../comms/core" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_common = { path = "../../common" }
tari_common_types = { path = "../../base_layer/common_types" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_base_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tari_comms = { path = "../../comms/core", features = ["rpc"] }
tari_common_types = { path = "../../base_layer/common_types" }
tari_comms_dht = { path = "../../comms/dht" }
tari_core = { path = "../../base_layer/core", default-features = false, features = ["transactions"] }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_libtor = { path = "../../infrastructure/libtor", optional = true }
tari_mmr = { path = "../../base_layer/mmr", features = ["native_bitmap"] }
tari_p2p = { path = "../../base_layer/p2p", features = ["auto-update"] }
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "BSD-3-Clause"

[dependencies]
tari_wallet = { path = "../../base_layer/wallet", features = ["bundled_sqlite"] }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_common = { path = "../../common" }
tari_app_utilities = { path = "../tari_app_utilities" }
tari_comms = { path = "../../comms/core" }
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_merge_mining_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tari_common = { path = "../../common" }
tari_comms = { path = "../../comms/core" }
tari_core = { path = "../../base_layer/core", default-features = false, features = ["transactions"] }
tari_app_utilities = { path = "../tari_app_utilities" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
tari_base_node_grpc_client = {path="../../clients/rust/base_node_grpc_client" }
tari_wallet_grpc_client = {path="../../clients/rust/wallet_grpc_client" }
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tari_common_types = { path = "../../base_layer/common_types" }
tari_comms = { path = "../../comms/core" }
tari_app_utilities = { path = "../tari_app_utilities" }
tari_app_grpc = { path = "../tari_app_grpc" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }

crossterm = { version = "0.25.0" }
Expand Down
2 changes: 1 addition & 1 deletion base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "0.40.1"
edition = "2018"

[dependencies]
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
tari_common = { version = "^0.40", path = "../../common" }

Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tari_common_types = { version = "^0.40", path = "../../base_layer/common_types"
tari_comms = { version = "^0.40", path = "../../comms/core" }
tari_comms_dht = { version = "^0.40", path = "../../comms/dht" }
tari_comms_rpc_macros = { version = "^0.40", path = "../../comms/rpc_macros" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_metrics = { path = "../../infrastructure/metrics" }
tari_mmr = { version = "^0.40", path = "../../base_layer/mmr", optional = true, features = ["native_bitmap"] }
tari_p2p = { version = "^0.40", path = "../../base_layer/p2p" }
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/transactions/coinbase_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl CoinbaseBuilder {
let metadata = TransactionMetadata::new_with_features(0.into(), 0, kernel_features);
let challenge =
TransactionKernel::build_kernel_challenge_from_tx_meta(&public_nonce, excess.as_public_key(), &metadata);
let sig = Signature::sign(spending_key.clone(), nonce, &challenge)
let sig = Signature::sign_raw(&spending_key, nonce, &challenge)
.map_err(|_| CoinbaseBuildError::BuildError("Challenge could not be represented as a scalar".into()))?;

let hasher =
Expand Down Expand Up @@ -554,7 +554,7 @@ mod test {
&KernelFeatures::empty(),
&None,
);
coinbase_kernel2.excess_sig = Signature::sign(output.spending_key, p2.nonce, &challenge).unwrap();
coinbase_kernel2.excess_sig = Signature::sign_raw(&output.spending_key, p2.nonce, &challenge).unwrap();

tx.body.add_output(coinbase2);
tx.body.add_kernel(coinbase_kernel2);
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/transactions/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ pub fn create_signature(k: PrivateKey, fee: MicroTari, lock_height: u64, feature
&PublicKey::from_secret_key(&k),
&tx_meta,
);
Signature::sign(k, r, &e).unwrap()
Signature::sign_raw(&k, r, &e).unwrap()
}

/// Generate a random transaction signature given a key, returning the public key (excess) and the signature.
Expand All @@ -294,7 +294,7 @@ pub fn create_random_signature_from_s_key(
let p = PK::from_secret_key(&s_key);
let tx_meta = TransactionMetadata::new_with_features(fee, lock_height, features);
let e = TransactionKernel::build_kernel_challenge_from_tx_meta(&PublicKey::from_secret_key(&r), &p, &tx_meta);
(p, Signature::sign(s_key, r, &e).unwrap())
(p, Signature::sign_raw(&s_key, r, &e).unwrap())
}

pub fn create_consensus_manager() -> ConsensusManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ValidatorNodeRegistration {
let (secret_nonce, public_nonce) = PublicKey::random_keypair(&mut OsRng);
let public_key = PublicKey::from_secret_key(private_key);
let challenge = Self::construct_challenge(&public_key, &public_nonce, msg);
let signature = Signature::sign(private_key.clone(), secret_nonce, &*challenge)
let signature = Signature::sign_raw(private_key, secret_nonce, &*challenge)
.expect("Sign cannot fail with 32-byte challenge and a RistrettoPublicKey");
Self { public_key, signature }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ mod tests {
MicroTari::from(100),
123,
test_params.commit_value(321.into()),
Signature::sign(
test_params.spend_key.clone(),
Signature::sign_raw(
&test_params.spend_key,
test_params.nonce.clone(),
test_params.nonce.as_bytes(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ mod test {
let r_sum = &msg.public_nonce + &p.public_nonce;
let excess = &msg.public_excess + &PublicKey::from_secret_key(&p.spend_key);
let e = TransactionKernel::build_kernel_challenge_from_tx_meta(&r_sum, &excess, &m);
let s = Signature::sign(p.spend_key.clone(), p.nonce, &e).unwrap();
let s = Signature::sign_raw(&p.spend_key, p.nonce, &e).unwrap();
assert_eq!(data.partial_signature, s);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ impl SenderTransactionProtocol {
// Create sender signature
let public_commitment_nonce = PublicKey::from_secret_key(private_commitment_nonce);
let e = output.get_metadata_signature_challenge(Some(&public_commitment_nonce));
let sender_signature =
Signature::sign(sender_offset_private_key.clone(), private_commitment_nonce.clone(), &e)?;
let sender_signature = Signature::sign_raw(sender_offset_private_key, private_commitment_nonce.clone(), &e)?;
let sender_signature = sender_signature.get_signature();
// Create aggregated metadata signature
let (r_pub, u, v) = output.metadata_signature.complete_signature_tuple();
Expand Down Expand Up @@ -591,7 +590,7 @@ impl SenderTransactionProtocol {

let k = info.offset_blinding_factor.clone();
let r = info.private_nonce.clone();
let s = Signature::sign(k, r, &e).map_err(TPE::SigningError)?;
let s = Signature::sign_raw(&k, r, &e).map_err(TPE::SigningError)?;
info.signatures.push(s);
Ok(())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl SingleReceiverTransactionProtocol {
&(&sender_info.public_excess + &public_spending_key),
&tx_meta,
);
let signature = Signature::sign(spending_key, nonce, &e).map_err(TPE::SigningError)?;
let signature = Signature::sign_raw(&spending_key, nonce, &e).map_err(TPE::SigningError)?;
let data = RD {
tx_id: sender_info.tx_id,
output,
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/tests/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ async fn consensus_validation_large_tx() {
let e = TransactionKernel::build_kernel_challenge_from_tx_meta(&public_nonce, &excess, &tx_meta);
let k = offset_blinding_factor;
let r = nonce;
let s = Signature::sign(k, r, &e).unwrap();
let s = Signature::sign_raw(&k, r, &e).unwrap();

let kernel = KernelBuilder::new()
.with_fee(fee)
Expand Down
2 changes: 1 addition & 1 deletion base_layer/key_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["lib", "cdylib"]
# NB: All dependencies must support or be gated for the WASM target.
[dependencies]
tari_common_types = { version = "^0.40", path = "../../base_layer/common_types", optional = true }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }

argon2 = { version = "0.4.1", features = ["std", "alloc"] }
Expand Down
2 changes: 1 addition & 1 deletion base_layer/mmr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ benches = ["criterion"]

[dependencies]
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_common = {path = "../../common"}
thiserror = "1.0.26"
digest = "0.9.0"
Expand Down
2 changes: 1 addition & 1 deletion base_layer/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
tari_comms = { version = "^0.40", path = "../../comms/core" }
tari_comms_dht = { version = "^0.40", path = "../../comms/dht" }
tari_common = { version = "^0.40", path = "../../common" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_service_framework = { version = "^0.40", path = "../service_framework" }
tari_shutdown = { version = "^0.40", path = "../../infrastructure/shutdown" }
tari_storage = { version = "^0.40", path = "../../infrastructure/storage" }
Expand Down
2 changes: 1 addition & 1 deletion base_layer/tari_mining_helper_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
tari_comms = { version = "^0.40", path = "../../comms/core" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_common = { path = "../../common" }
tari_core = { path = "../core", default-features = false, features = ["transactions"]}
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
Expand Down
2 changes: 1 addition & 1 deletion base_layer/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tari_common = { path = "../../common" }
tari_common_types = { version = "^0.40", path = "../../base_layer/common_types" }
tari_comms = { version = "^0.40", path = "../../comms/core" }
tari_comms_dht = { version = "^0.40", path = "../../comms/dht" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_key_manager = { version = "^0.40", path = "../key_manager" }
tari_p2p = { version = "^0.40", path = "../p2p", features = ["auto-update"] }
tari_script = { path = "../../infrastructure/tari_script" }
Expand Down
26 changes: 24 additions & 2 deletions base_layer/wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tari_common::configuration::bootstrap::ApplicationType;
use tari_common_types::{
tari_address::TariAddress,
transaction::{ImportStatus, TxId},
types::{ComSignature, Commitment, PrivateKey, PublicKey},
types::{ComSignature, Commitment, PrivateKey, PublicKey, Signature},
};
use tari_comms::{
multiaddr::Multiaddr,
Expand All @@ -47,7 +47,12 @@ use tari_core::{
CryptoFactories,
},
};
use tari_crypto::{hash::blake2::Blake256, tari_utilities::hex::Hex};
use tari_crypto::{
hash::blake2::Blake256,
ristretto::{RistrettoPublicKey, RistrettoSchnorr, RistrettoSecretKey},
signatures::{SchnorrSignature, SchnorrSignatureError},
tari_utilities::hex::Hex,
};
use tari_key_manager::{
cipher_seed::CipherSeed,
key_manager::KeyManager,
Expand Down Expand Up @@ -508,6 +513,23 @@ where
Ok(tx_id)
}

pub fn sign_message(
&mut self,
secret: &RistrettoSecretKey,
message: &str,
) -> Result<SchnorrSignature<RistrettoPublicKey, RistrettoSecretKey>, SchnorrSignatureError> {
RistrettoSchnorr::sign_message(secret, message.as_bytes())
}

pub fn verify_message_signature(
&mut self,
public_key: &RistrettoPublicKey,
signature: &Signature,
message: &str,
) -> bool {
signature.verify_message(public_key, message)
}

/// Appraise the expected outputs and a fee
pub async fn preview_coin_split_with_commitments_no_amount(
&mut self,
Expand Down
24 changes: 24 additions & 0 deletions base_layer/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,30 @@ async fn test_do_not_overwrite_master_key() {
.unwrap();
}

#[tokio::test]
async fn test_sign_message() {
let factories = CryptoFactories::default();
let dir = tempdir().unwrap();

let shutdown = Shutdown::new();
let mut wallet = create_wallet(
dir.path(),
"wallet_db",
factories.clone(),
shutdown.to_signal(),
None,
None,
)
.await
.unwrap();

let (secret, public_key) = PublicKey::random_keypair(&mut OsRng);
let message = "Tragedy will find us.";
let schnorr = wallet.sign_message(&secret, message).unwrap();

assert!(wallet.verify_message_signature(&public_key, &schnorr, message));
}

#[test]
fn test_many_iterations_store_and_forward_send_tx() {
for _n in 1..=10 {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/wallet_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tari_common = {path="../../common"}
tari_common_types = {path="../common_types"}
tari_comms = { version = "^0.40", path = "../../comms/core", features = ["c_integration"]}
tari_comms_dht = { version = "^0.40", path = "../../comms/dht", default-features = false }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.7" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.1" }
tari_key_manager = { version = "^0.40", path = "../key_manager" }
tari_p2p = { version = "^0.40", path = "../p2p" }
tari_script = { path = "../../infrastructure/tari_script" }
Expand Down
Loading

0 comments on commit e2b7ad1

Please sign in to comment.