Skip to content

Commit

Permalink
Update key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Sep 25, 2023
1 parent 6c8e2d3 commit b391c1a
Show file tree
Hide file tree
Showing 81 changed files with 424 additions and 329 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ overflow-checks = true

[patch.crates-io]
liblmdb-sys = { git = "https://github.com/tari-project/lmdb-rs", tag = "0.7.6-tari.1" }
tari_crypto = { git = "https://github.com/AaronFeickert/tari-crypto", branch = "scalar-byte-array" } # TODO: remove after merging
tari_utilities = { git = "https://github.com/AaronFeickert/tari_utilities", branch = "byte-array" } # TODO: remove after merging
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ impl TryFrom<grpc::BlockHeader> for BlockHeader {
type Error = String;

fn try_from(header: grpc::BlockHeader) -> Result<Self, Self::Error> {
let total_kernel_offset = PrivateKey::from_bytes(&header.total_kernel_offset).map_err(|err| err.to_string())?;
let total_kernel_offset =
PrivateKey::from_canonical_bytes(&header.total_kernel_offset).map_err(|err| err.to_string())?;

let total_script_offset = PrivateKey::from_bytes(&header.total_script_offset).map_err(|err| err.to_string())?;
let total_script_offset =
PrivateKey::from_canonical_bytes(&header.total_script_offset).map_err(|err| err.to_string())?;

let pow = match header.pow {
Some(p) => ProofOfWork::try_from(p)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ impl TryFrom<grpc::ComAndPubSignature> for ComAndPubSignature {
type Error = String;

fn try_from(sig: grpc::ComAndPubSignature) -> Result<Self, Self::Error> {
let ephemeral_commitment = Commitment::from_bytes(&sig.ephemeral_commitment)
let ephemeral_commitment = Commitment::from_canonical_bytes(&sig.ephemeral_commitment)
.map_err(|_| "Could not get ephemeral commitment".to_string())?;
let ephemeral_pubkey = PublicKey::from_bytes(&sig.ephemeral_pubkey)
let ephemeral_pubkey = PublicKey::from_canonical_bytes(&sig.ephemeral_pubkey)
.map_err(|_| "Could not get ephemeral public key".to_string())?;
let u_a = PrivateKey::from_bytes(&sig.u_a).map_err(|_| "Could not get partial signature u_a".to_string())?;
let u_x = PrivateKey::from_bytes(&sig.u_x).map_err(|_| "Could not get partial signature u_x".to_string())?;
let u_y = PrivateKey::from_bytes(&sig.u_y).map_err(|_| "Could not get partial signature u_y".to_string())?;
let u_a = PrivateKey::from_canonical_bytes(&sig.u_a)
.map_err(|_| "Could not get partial signature u_a".to_string())?;
let u_x = PrivateKey::from_canonical_bytes(&sig.u_x)
.map_err(|_| "Could not get partial signature u_x".to_string())?;
let u_y = PrivateKey::from_canonical_bytes(&sig.u_y)
.map_err(|_| "Could not get partial signature u_y".to_string())?;

Ok(Self::new(ephemeral_commitment, ephemeral_pubkey, u_a, u_x, u_y))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl TryFrom<grpc::CommitmentSignature> for CommitmentSignature<PublicKey, Priva
type Error = String;

fn try_from(sig: grpc::CommitmentSignature) -> Result<Self, Self::Error> {
let public_nonce = HomomorphicCommitment::<PublicKey>::from_bytes(&sig.public_nonce)
let public_nonce = HomomorphicCommitment::<PublicKey>::from_canonical_bytes(&sig.public_nonce)
.map_err(|_| "Could not get public nonce".to_string())?;
let u = PrivateKey::from_bytes(&sig.u).map_err(|_| "Could not get u_x".to_string())?;
let v = PrivateKey::from_bytes(&sig.v).map_err(|_| "Could not get v_x".to_string())?;
let u = PrivateKey::from_canonical_bytes(&sig.u).map_err(|_| "Could not get u_x".to_string())?;
let v = PrivateKey::from_canonical_bytes(&sig.v).map_err(|_| "Could not get v_x".to_string())?;

Ok(Self::new(public_nonce, u, v))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl TryFrom<grpc::NewBlockTemplate> for NewBlockTemplate {

fn try_from(block: grpc::NewBlockTemplate) -> Result<Self, Self::Error> {
let header = block.header.clone().ok_or_else(|| "No header provided".to_string())?;
let total_kernel_offset = PrivateKey::from_bytes(&header.total_kernel_offset)
let total_kernel_offset = PrivateKey::from_canonical_bytes(&header.total_kernel_offset)
.map_err(|err| format!("total_kernel_offset {}", err))?;
let total_script_offset = PrivateKey::from_bytes(&header.total_script_offset)
let total_script_offset = PrivateKey::from_canonical_bytes(&header.total_script_offset)
.map_err(|err| format!("total_script_offset {}", err))?;
let pow = match header.pow {
Some(p) => ProofOfWork::try_from(p)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl TryFrom<grpc::ValidatorNodeRegistration> for ValidatorNodeRegistration {

fn try_from(value: grpc::ValidatorNodeRegistration) -> Result<Self, Self::Error> {
Ok(ValidatorNodeRegistration::new(ValidatorNodeSignature::new(
PublicKey::from_bytes(&value.public_key).map_err(|e| e.to_string())?,
PublicKey::from_canonical_bytes(&value.public_key).map_err(|e| e.to_string())?,
value
.signature
.map(Signature::try_from)
Expand All @@ -112,7 +112,7 @@ impl TryFrom<grpc::TemplateRegistration> for CodeTemplateRegistration {

fn try_from(value: grpc::TemplateRegistration) -> Result<Self, Self::Error> {
Ok(Self {
author_public_key: PublicKey::from_bytes(&value.author_public_key).map_err(|e| e.to_string())?,
author_public_key: PublicKey::from_canonical_bytes(&value.author_public_key).map_err(|e| e.to_string())?,
author_signature: value
.author_signature
.map(Signature::try_from)
Expand Down Expand Up @@ -157,7 +157,7 @@ impl TryFrom<grpc::ConfidentialOutputData> for ConfidentialOutputData {

fn try_from(value: grpc::ConfidentialOutputData) -> Result<Self, Self::Error> {
Ok(ConfidentialOutputData {
claim_public_key: PublicKey::from_bytes(&value.claim_public_key).map_err(|e| e.to_string())?,
claim_public_key: PublicKey::from_canonical_bytes(&value.claim_public_key).map_err(|e| e.to_string())?,
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions applications/minotari_app_grpc/src/conversions/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ impl TryFrom<grpc::Signature> for Signature {

fn try_from(sig: grpc::Signature) -> Result<Self, Self::Error> {
let public_nonce =
PublicKey::from_bytes(&sig.public_nonce).map_err(|_| "Could not get public nonce".to_string())?;
let signature = PrivateKey::from_bytes(&sig.signature).map_err(|_| "Could not get signature".to_string())?;
PublicKey::from_canonical_bytes(&sig.public_nonce).map_err(|_| "Could not get public nonce".to_string())?;
let signature =
PrivateKey::from_canonical_bytes(&sig.signature).map_err(|_| "Could not get signature".to_string())?;

Ok(Self::new(public_nonce, signature))
}
Expand Down
5 changes: 3 additions & 2 deletions applications/minotari_app_grpc/src/conversions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ impl TryFrom<grpc::Transaction> for Transaction {

fn try_from(source: grpc::Transaction) -> Result<Self, Self::Error> {
Ok(Self {
offset: RistrettoSecretKey::from_bytes(&source.offset).map_err(|e| format!("Offset is not valid:{}", e))?,
offset: RistrettoSecretKey::from_canonical_bytes(&source.offset)
.map_err(|e| format!("Offset is not valid:{}", e))?,
body: source
.body
.ok_or_else(|| "Transaction body not provided".to_string())?
.try_into()?,
script_offset: RistrettoSecretKey::from_bytes(&source.script_offset)
script_offset: RistrettoSecretKey::from_canonical_bytes(&source.script_offset)
.map_err(|e| format!("Script offset is not valid:{}", e))?,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ impl TryFrom<grpc::TransactionInput> for TransactionInput {
script_signature,
))
} else {
let commitment = Commitment::from_bytes(&input.commitment).map_err(|e| e.to_string())?;
let commitment = Commitment::from_canonical_bytes(&input.commitment).map_err(|e| e.to_string())?;
let features = input
.features
.map(TryInto::try_into)
.ok_or_else(|| "transaction output features not provided".to_string())??;

let sender_offset_public_key =
PublicKey::from_bytes(input.sender_offset_public_key.as_bytes()).map_err(|err| format!("{:?}", err))?;
let sender_offset_public_key = PublicKey::from_canonical_bytes(input.sender_offset_public_key.as_bytes())
.map_err(|err| format!("{:?}", err))?;

let encrypted_data = EncryptedData::from_bytes(&input.encrypted_data).map_err(|err| err.to_string())?;
let minimum_value_promise = input.minimum_value_promise.into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl TryFrom<grpc::TransactionKernel> for TransactionKernel {
type Error = String;

fn try_from(kernel: grpc::TransactionKernel) -> Result<Self, Self::Error> {
let excess =
Commitment::from_bytes(&kernel.excess).map_err(|err| format!("Excess could not be converted:{}", err))?;
let excess = Commitment::from_canonical_bytes(&kernel.excess)
.map_err(|err| format!("Excess could not be converted:{}", err))?;

let excess_sig = kernel
.excess_sig
Expand All @@ -49,7 +49,7 @@ impl TryFrom<grpc::TransactionKernel> for TransactionKernel {
None
} else {
Some(
Commitment::from_bytes(&kernel.burn_commitment)
Commitment::from_canonical_bytes(&kernel.burn_commitment)
.map_err(|err| format!("Burn commitment could not be converted:{}", err))?,
)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ impl TryFrom<grpc::TransactionOutput> for TransactionOutput {
.map(TryInto::try_into)
.ok_or_else(|| "Transaction output features not provided".to_string())??;

let commitment =
Commitment::from_bytes(&output.commitment).map_err(|err| format!("Invalid output commitment: {}", err))?;
let sender_offset_public_key = PublicKey::from_bytes(output.sender_offset_public_key.as_bytes())
let commitment = Commitment::from_canonical_bytes(&output.commitment)
.map_err(|err| format!("Invalid output commitment: {}", err))?;
let sender_offset_public_key = PublicKey::from_canonical_bytes(output.sender_offset_public_key.as_bytes())
.map_err(|err| format!("Invalid sender_offset_public_key {:?}", err))?;

let range_proof = if let Some(proof) = output.range_proof {
Some(BulletRangeProof::from_bytes(&proof.proof_bytes).map_err(|err| err.to_string())?)
Some(BulletRangeProof::from_canonical_bytes(&proof.proof_bytes).map_err(|err| err.to_string())?)
} else {
None
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl TryFrom<grpc::UnblindedOutput> for UnblindedOutput {
type Error = String;

fn try_from(mut output: grpc::UnblindedOutput) -> Result<Self, Self::Error> {
let spending_key =
PrivateKey::from_bytes(output.spending_key.as_bytes()).map_err(|e| format!("spending_key: {:?}", e))?;
let spending_key = PrivateKey::from_canonical_bytes(output.spending_key.as_bytes())
.map_err(|e| format!("spending_key: {:?}", e))?;

let features = output
.features
Expand All @@ -80,10 +80,10 @@ impl TryFrom<grpc::UnblindedOutput> for UnblindedOutput {
let input_data =
ExecutionStack::from_bytes(output.input_data.as_bytes()).map_err(|e| format!("input_data: {:?}", e))?;

let script_private_key = PrivateKey::from_bytes(output.script_private_key.as_bytes())
let script_private_key = PrivateKey::from_canonical_bytes(output.script_private_key.as_bytes())
.map_err(|e| format!("script_private_key: {:?}", e))?;

let sender_offset_public_key = PublicKey::from_bytes(output.sender_offset_public_key.as_bytes())
let sender_offset_public_key = PublicKey::from_canonical_bytes(output.sender_offset_public_key.as_bytes())
.map_err(|err| format!("sender_offset_public_key {:?}", err))?;

let metadata_signature = output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
None
} else {
Some(
PublicKey::from_bytes(&message.claim_public_key)
PublicKey::from_canonical_bytes(&message.claim_public_key)
.map_err(|e| Status::invalid_argument(e.to_string()))?,
)
},
Expand Down Expand Up @@ -1005,7 +1005,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
) -> Result<Response<RegisterValidatorNodeResponse>, Status> {
let request = request.into_inner();
let mut transaction_service = self.get_transaction_service();
let validator_node_public_key = CommsPublicKey::from_bytes(&request.validator_node_public_key)
let validator_node_public_key = CommsPublicKey::from_canonical_bytes(&request.validator_node_public_key)
.map_err(|_| Status::internal("Destination address is malformed".to_string()))?;
let validator_node_signature = request
.validator_node_signature
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ async fn detect_local_base_node(network: Network) -> Option<SeedPeer> {
};
let resp = node_conn.identify(Empty {}).await.ok()?;
let identity = resp.get_ref();
let public_key = CommsPublicKey::from_bytes(&identity.public_key).ok()?;
let public_key = CommsPublicKey::from_canonical_bytes(&identity.public_key).ok()?;
let addresses = identity
.public_addresses
.iter()
Expand Down
24 changes: 11 additions & 13 deletions applications/minotari_console_wallet/src/ui/state/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use std::{convert::TryFrom, path::PathBuf};

use blake2::Blake2b;
use digest::consts::U32;
use digest::consts::U64;
use log::{error, warn};
use minotari_wallet::{
output_manager_service::UtxoSelectionCriteria,
Expand All @@ -33,7 +33,7 @@ use minotari_wallet::{
use rand::{random, rngs::OsRng};
use tari_common_types::{
tari_address::TariAddress,
types::{FixedHash, PublicKey, Signature},
types::{PublicKey, Signature},
};
use tari_core::{
consensus::{DomainSeparatedConsensusHasher, MaxSizeBytes, MaxSizeString},
Expand Down Expand Up @@ -421,7 +421,7 @@ pub async fn send_register_template_transaction_task(
// signing and sending code template registration request
// ----------------------------------------------------------------------------

let mut km = KeyManager::<RistrettoPublicKey, Blake2b<U32>>::new();
let mut km = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::new();

let author_private_key = match km.next_key() {
Ok(secret_key) => secret_key.key,
Expand All @@ -434,16 +434,14 @@ pub async fn send_register_template_transaction_task(

let author_public_key = PublicKey::from_secret_key(&author_private_key);
let (secret_nonce, public_nonce) = PublicKey::random_keypair(&mut OsRng);
let challenge = FixedHash::from(
DomainSeparatedConsensusHasher::<TransactionHashDomain>::new("template_registration")
.chain(&author_public_key)
.chain(&public_nonce)
.chain(&binary_sha)
.chain(&b"")
.finalize(),
);

let author_signature = Signature::sign_raw(&author_private_key, secret_nonce, &*challenge)
let challenge = DomainSeparatedConsensusHasher::<TransactionHashDomain, Blake2b<U64>>::new("template_registration")
.chain(&author_public_key)
.chain(&public_nonce)
.chain(&binary_sha)
.chain(&b"")
.finalize();

let author_signature = Signature::sign_raw_wide(&author_private_key, secret_nonce, &challenge)
.expect("Sign cannot fail with 32-byte challenge and a RistrettoPublicKey");

// ----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions applications/minotari_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let outputs = request
.commitments
.into_iter()
.map(|s| Commitment::from_bytes(&s))
.map(|s| Commitment::from_canonical_bytes(&s))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| Status::invalid_argument("Invalid commitments provided"))?;

Expand Down Expand Up @@ -1478,7 +1478,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let request = request.into_inner();
let report_error_flag = self.report_error_flag();
let mut handler = self.node_service.clone();
let public_key = PublicKey::from_bytes(&request.public_key)
let public_key = PublicKey::from_canonical_bytes(&request.public_key)
.map_err(|e| obscure_error_if_true(report_error_flag, Status::invalid_argument(e.to_string())))?;

let shard_key = handler.get_shard_key(request.height, public_key).await.map_err(|e| {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/common_types/src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl EmojiId {
bytes.pop();

// Convert to a public key
match PublicKey::from_bytes(&bytes) {
match PublicKey::from_canonical_bytes(&bytes) {
Ok(public_key) => Ok(Self(public_key)),
Err(_) => Err(EmojiIdError::CannotRecoverPublicKey),
}
Expand Down
6 changes: 4 additions & 2 deletions base_layer/common_types/src/tari_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ impl TariAddress {
if validate_checksum(&fixed_data).is_err() {
return Err(TariAddressError::InvalidNetworkOrChecksum);
}
let key = PublicKey::from_bytes(&bytes[0..32]).map_err(|_| TariAddressError::CannotRecoverPublicKey)?;
let key =
PublicKey::from_canonical_bytes(&bytes[0..32]).map_err(|_| TariAddressError::CannotRecoverPublicKey)?;
Ok(TariAddress {
public_key: key,
network,
Expand All @@ -151,7 +152,8 @@ impl TariAddress {
// if the network is a valid network number, we can assume that the checksum as valid
let network =
Network::try_from(checksum ^ bytes[32]).map_err(|_| TariAddressError::InvalidNetworkOrChecksum)?;
let key = PublicKey::from_bytes(&bytes[0..32]).map_err(|_| TariAddressError::CannotRecoverPublicKey)?;
let key =
PublicKey::from_canonical_bytes(&bytes[0..32]).map_err(|_| TariAddressError::CannotRecoverPublicKey)?;
Ok(TariAddress {
public_key: key,
network,
Expand Down
4 changes: 2 additions & 2 deletions base_layer/common_types/src/types/bullet_rangeproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ByteArray for BulletRangeProof {
Ok(BulletRangeProof(v.clone()))
}

fn from_bytes(bytes: &[u8]) -> Result<Self, ByteArrayError> {
fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, ByteArrayError> {
Ok(BulletRangeProof(bytes.to_vec()))
}

Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'de> Deserialize<'de> for BulletRangeProof {

fn visit_bytes<E>(self, v: &[u8]) -> Result<BulletRangeProof, E>
where E: de::Error {
BulletRangeProof::from_bytes(v).map_err(E::custom)
BulletRangeProof::from_canonical_bytes(v).map_err(E::custom)
}
}

Expand Down
Loading

0 comments on commit b391c1a

Please sign in to comment.