Skip to content

Commit

Permalink
Make PrivateKey and PublicKey type aliases
Browse files Browse the repository at this point in the history
Fixes #13.
  • Loading branch information
thomaseizinger committed May 18, 2021
1 parent 106cfaa commit b251126
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 627 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ serde_json = "1"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
curve25519-dalek = { git = "https://github.com/thomaseizinger/curve25519-dalek", branch = "try-from-impls" }
72 changes: 32 additions & 40 deletions src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::consensus::encode::{self, serialize, Decodable, Encodable, VarInt};
use crate::cryptonote::hash;
use crate::cryptonote::onetime_key::{KeyRecoverer, SubKeyChecker};
use crate::cryptonote::subaddress::Index;
use crate::util::key::{KeyPair, PrivateKey, PublicKey, ViewPair};
use crate::util::key::{EdwardsPointExt, KeyPair, PrivateKey, PublicKey, ViewPair};
use crate::util::ringct::{Opening, RctSig, RctSigBase, RctSigPrunable, RctType, Signature};

use hex::encode as hex_encode;
Expand All @@ -34,7 +34,7 @@ use std::ops::Range;
use std::{fmt, io};

use crate::cryptonote::hash::Hashable;
use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
use curve25519_dalek::edwards::EdwardsPoint;
use curve25519_dalek::scalar::Scalar;

#[cfg(feature = "serde_support")]
Expand Down Expand Up @@ -166,20 +166,20 @@ impl TxOut {
/// ```rust
/// use monero::blockdata::transaction::Transaction;
/// use monero::consensus::encode::deserialize;
/// use monero::util::key::{KeyPair, PrivateKey, PublicKey, ViewPair};
/// use monero::util::key::{KeyPair, PrivateKey, PublicKey, ViewPair, EdwardsPointExt, ScalarExt};
/// # use std::str::FromStr;
///
/// # let raw_tx = hex::decode("02000102000bb2e38c0189ea01a9bc02a533fe02a90705fd0540745f59f49374365304f8b4d5da63b444b2d74a40f8007ea44940c15cbbc80c9d106802000267f0f669ead579c1067cbffdf67c4af80b0287c549a10463122b4860fe215f490002b6a2e2f35a93d637ff7d25e20da326cee8e92005d3b18b3c425dabe8336568992c01d6c75cf8c76ac458123f2a498512eb65bb3cecba346c8fcfc516dc0c88518bb90209016f82359eb1fe71d604f0dce9470ed5fd4624bb9fce349a0e8317eabf4172f78a8b27dec6ea1a46da10ed8620fa8367c6391eaa8aabf4ebf660d9fe0eb7e9dfa08365a089ad2df7bce7ef776467898d5ca8947152923c54a1c5030e0c2f01035c555ff4285dcc44dfadd6bc37ec8b9354c045c6590446a81c7f53d8f199cace3faa7f17b3b8302a7cbb3881e8fdc23cca0275c9245fdc2a394b8d3ae73911e3541b10e7725cdeef5e0307bc218caefaafe97c102f39c8ce78f62cccf23c69baf0af55933c9d384ceaf07488f2f1ac7343a593449afd54d1065f6a1a4658845817e4b0e810afc4ca249096e463f9f368625fa37d5bbcbe87af68ce3c4d630f93a66defa4205b178f4e9fa04107bd535c7a4b2251df2dad255e470b611ffe00078c2916fc1eb2af1273e0df30dd1c74b6987b9885e7916b6ca711cbd4b7b50576e51af1439e9ed9e33eb97d8faba4e3bd46066a5026a1940b852d965c1db455d1401687ccaccc524e000b05966763564b7deb8fd64c7fb3d649897c94583dca1558893b071f5e6700dad139f3c6f973c7a43b207ee3e67dc7f7f18b52df442258200c7fe6d16685127da1df9b0d93d764c2659599bc6d300ae33bf8b7c2a504317da90ea2f0bb2af09bd531feae57cb4a0273d8add62fadfc6d43402372e5caf854e112b88417936f1a9c4045d48b5b0b7703d96801b35ff66c716cddbee1b92407aa069a162c163071710e28ccddf6fb560feea32485f2c54a477ae23fd8210427eabe4288cbe0ecbef4ed19ca049ceded424d9f839da957f56ffeb73060ea15498fcbc2d73606e85e963a667dafdb2641fb91862c07b98c1fdae8fadf514600225036dd63c22cdadb57d2125ebf30bc77f7ea0bc0dafb484bf01434954c5053b9c8a143f06972f80fa66788ea1e3425dc0104a9e3674729967b9819552ebb172418da0e4b3778ad4b3d6acd8f354ba09e54bbc8604540010e1e1e4d3066515aed457bd3399c0ce787236dbcd3923de4fb8faded10199b33c1251191612ab5526c1cf0cd55a0aeaed3f7a955ceced16dabdbeb0a2a19a9fdb5aa8c4fc8767cf70e4ad1838518bc6b9de7c420c1f57636579a14a5a8bdacd24e61a68adede8a2e07416c25409dd91ab78905bc99bab4ab4fb9e4ea628e09a271837769c4e67e580dcd5485e12e4e308cb4509686a7484a71f7dfe334499808c7122f07d45d89230b1f19ed86f675b7fec44ef5f3b178ae0af92ff114bd96baa264604fea5a762307bdce6cb483b7bc780d32ed5343fcc3aa306997f211dc075f6dfd66035c1db10bef8656fefbb45645264d401682e42fe3e05906f79d65481b87508f1a4c434e0d1dfc247d4276306f801a6b57e4e4a525177bae24e0bd88a216597d9db44f2604c29d8a5f74e7b934f55048690b5dcefd6489a81aa64c1edb49b320faab94130e603d99e455cfd828bca782176192ece95e9b967fe3dd698574cf0c0b6926970b156e1134658de657de42c4930e72b49c0d94da66c330ab188c10f0d2f578590f31bcac6fcff7e21f9ff67ae1a40d5a03b19301dcbbadc1aa9392795cf81f1401ec16d986a7f96fbb9e8e12ce04a2226e26b78117a4dfb757c6a44481ff68bb0909e7010988cd37146fb45d4cca4ba490aae323bb51a12b6864f88ea6897aa700ee9142eaf0880844083026f044a5e3dba4aae08578cb057976001beb27b5110c41fe336bf7879733739ce22fb31a1a6ac2c900d6d6c6facdbc60085e5c93d502542cfea90dbc62d4e061b7106f09f9c4f6c1b5506dd0550eb8b2bf17678b140de33a10ba676829092e6a13445d1857d06c715eea4492ff864f0b34d178a75a0f1353078f83cfee1440b0a20e64abbd0cab5c6e7083486002970a4904f8371805d1a0ee4aea8524168f0f39d2dfc55f545a98a031841a740e8422a62e123c8303021fb81afbb76d1120c0fbc4d3d97ba69f4e2fe086822ece2047c9ccea507008654c199238a5d17f009aa2dd081f7901d0688aa15311865a319ccba8de4023027235b5725353561c5f1185f6a063fb32fc65ef6e90339d406a6884d66be49d03daaf116ee4b65ef80dd3052a13157b929f98640c0bbe99c8323ce3419a136403dc3f7a95178c3966d2d7bdecf516a28eb2cf8cddb3a0463dc7a6248883f7be0a10aae1bb50728ec9b8880d6011b366a850798f6d7fe07103695dded3f371ca097c1d3596967320071d7f548938afe287cb9b8fae761fa592425623dcbf653028").unwrap();
/// # let tx = deserialize::<Transaction>(&raw_tx).expect("Raw tx deserialization failed");
/// # let secret_view_bytes =
/// # hex::decode("bcfdda53205318e1c14fa0ddca1a45df363bb427972981d0249d0f4652a7df07").unwrap();
/// # let secret_view = PrivateKey::from_slice(&secret_view_bytes).unwrap();
/// # let public_view = PublicKey::from_private_key(&secret_view);
/// # let public_view = PublicKey::from_scalar(&secret_view);
/// #
/// # let secret_spend_bytes =
/// # hex::decode("e5f4301d32f3bdaef814a835a18aaaa24b13cc76cf01a832a7852faf9322e907").unwrap();
/// # let secret_spend = PrivateKey::from_slice(&secret_spend_bytes).unwrap();
/// # let public_spend = PublicKey::from_private_key(&secret_spend);
/// # let public_spend = PublicKey::from_scalar(&secret_spend);
/// #
/// // Keypair used to recover the ephemeral spend key of an output
/// let keypair = KeyPair {
Expand Down Expand Up @@ -327,7 +327,10 @@ pub enum SubField {
impl fmt::Display for SubField {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
SubField::TxPublicKey(public_key) => writeln!(fmt, "Tx public Key: {}", public_key),
SubField::TxPublicKey(public_key) => {
writeln!(fmt, "Tx public Key: ")?;
public_key.fmt_as_hex(fmt)
}
SubField::Nonce(nonce) => {
let nonce_str = hex_encode(serialize(nonce));
writeln!(fmt, "Nonce: {}", nonce_str)
Expand All @@ -337,7 +340,8 @@ impl fmt::Display for SubField {
SubField::AdditionalPublickKey(keys) => {
writeln!(fmt, "Additional publick keys: ")?;
for key in keys {
writeln!(fmt, "key: {}", key)?;
writeln!(fmt, "key: ")?;
key.fmt_as_hex(fmt)?;
}
Ok(())
}
Expand Down Expand Up @@ -445,9 +449,6 @@ impl TransactionPrefix {
.ok_or(Error::MissingEcdhInfo)?;
let actual_commitment =
rct_sig_base.out_pk.get(i).ok_or(Error::MissingCommitment)?;
let actual_commitment = CompressedEdwardsY(actual_commitment.mask.key)
.decompress()
.ok_or(Error::InvalidCommitment)?;

let opening = ecdh_info
.open_commitment(pair, tx_pubkey, i, &actual_commitment)
Expand Down Expand Up @@ -972,15 +973,15 @@ impl Encodable for Transaction {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::{ExtraField, Transaction, TransactionPrefix};
use crate::blockdata::transaction::{SubField, TxIn, TxOutTarget};
use crate::consensus::encode::{deserialize, deserialize_partial, serialize, VarInt};
use crate::cryptonote::hash::Hashable;
use crate::util::key::{PrivateKey, PublicKey, ViewPair};
use crate::util::key::{EdwardsPointExt, PrivateKey, PublicKey, ViewPair};
use crate::util::ringct::{RctSig, RctSigBase, RctType};
use crate::TxOut;
use hex_literal::hex;
use std::convert::TryFrom;

#[test]
fn deserialize_transaction_prefix() {
Expand Down Expand Up @@ -1024,15 +1025,14 @@ mod tests {

#[test]
fn find_outputs() {
let view = PrivateKey::from_str(
"77916d0cd56ed1920aef6ca56d8a41bac915b68e4c46a589e0956e27a7b77404",
)
.unwrap();
let b = PrivateKey::from_str(
"8163466f1883598e6dd14027b8da727057165da91485834314f5500a65846f09",
)
.unwrap();
let spend = PublicKey::from_private_key(&b);
let view = PrivateKey::from_bits(hex!(
"77916d0cd56ed1920aef6ca56d8a41bac915b68e4c46a589e0956e27a7b77404"
));
let b = PrivateKey::from_bits(hex!(
"8163466f1883598e6dd14027b8da727057165da91485834314f5500a65846f09"
));

let spend = PublicKey::from_scalar(&b);
let viewpair = ViewPair { view, spend };

let hex = hex::decode("01f18d0601ffb58d0605efefead70202eb72f82bd8bdda51e0bdc25f04e99ffb90c6214e11b455abca7b116c7857738880e497d01202e87c65a22b78f4b7686ef3a30113674659a4fe769a7ded73d60e6f7c556a19858090dfc04a022ee52dca8845438995eb6d7af985ca07186cc34a7eb696937f78fc0fd9008e2280c0f9decfae0102cec392ffdcae05a370dc3c447465798d3688677f4a5937f1fef9661df99ac2fb80c0caf384a30202e2b6ce11475c2312d2de5c9f26fbd88b7fcac0dbbb7b31f49abe9bd631ed49e42b0104d46cf1a204ae727c14473d67ea95da3e97b250f3c63e0997198bfc812d7a81020800000000d8111b25").unwrap();
Expand Down Expand Up @@ -1062,7 +1062,7 @@ mod tests {
fn test_tx_hash() {
let tx = "f8ad7c58e6fce1792dd78d764ce88a11db0e3c3bb484d868ae05a7321fb6c6b0";

let pk_extra = vec![
let pk_extra = [
179, 155, 220, 223, 213, 23, 81, 160, 95, 232, 87, 102, 151, 63, 70, 249, 139, 40, 110,
16, 51, 193, 175, 208, 38, 120, 65, 191, 155, 139, 1, 4,
];
Expand All @@ -1076,19 +1076,15 @@ mod tests {
outputs: vec![TxOut {
amount: VarInt(1550800739964),
target: TxOutTarget::ToKey {
key: PublicKey::from_slice(
hex::decode(
"e2e19d8badb15e77c8e1f441cf6acd9bcde34a07cae82bbe5ff9629bf88e6e81",
)
.unwrap()
.as_slice(),
)
key: PublicKey::try_from(hex!(
"e2e19d8badb15e77c8e1f441cf6acd9bcde34a07cae82bbe5ff9629bf88e6e81"
))
.unwrap(),
},
}],
extra: ExtraField {
0: vec![
SubField::TxPublicKey(PublicKey::from_slice(pk_extra.as_slice()).unwrap()),
SubField::TxPublicKey(PublicKey::try_from(pk_extra).unwrap()),
SubField::Nonce(vec![
196, 37, 4, 0, 27, 37, 187, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]),
Expand Down Expand Up @@ -1120,7 +1116,7 @@ mod tests {
fn test_tx_hash_fail() {
let tx = "f8ad7c58e6fce1792dd78d764ce88a11db0e3c3bb484d868ae05a7321fb6c6b0";

let pk_extra = vec![
let pk_extra = [
179, 155, 220, 223, 213, 23, 81, 160, 95, 232, 87, 102, 151, 63, 70, 249, 139, 40, 110,
16, 51, 193, 175, 208, 38, 120, 65, 191, 155, 139, 1, 4,
];
Expand All @@ -1134,19 +1130,15 @@ mod tests {
outputs: vec![TxOut {
amount: VarInt(1550800739964),
target: TxOutTarget::ToKey {
key: PublicKey::from_slice(
hex::decode(
"e2e19d8badb15e77c8e1f441cf6acd9bcde34a07cae82bbe5ff9629bf88e6e81",
)
.unwrap()
.as_slice(),
)
key: PublicKey::try_from(hex!(
"e2e19d8badb15e77c8e1f441cf6acd9bcde34a07cae82bbe5ff9629bf88e6e81"
))
.unwrap(),
},
}],
extra: ExtraField {
0: vec![
SubField::TxPublicKey(PublicKey::from_slice(pk_extra.as_slice()).unwrap()),
SubField::TxPublicKey(PublicKey::try_from(pk_extra).unwrap()),
SubField::Nonce(vec![
196, 37, 4, 0, 27, 37, 187, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]),
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Hash {

/// Return the scalar of the hash as a little endian number modulo `l` (curve order).
pub fn as_scalar(&self) -> PrivateKey {
PrivateKey::from_scalar(Scalar::from_bytes_mod_order(self.0))
Scalar::from_bytes_mod_order(self.0)
}

/// Hash a stream of bytes and return its scalar representation.
Expand Down
Loading

0 comments on commit b251126

Please sign in to comment.