Skip to content

Commit

Permalink
Make PrivateKey and PublicKey type aliases
Browse files Browse the repository at this point in the history
To minimise breaking changes, we introduce extension traits that
provide most of the old functionality that was defined on the
wrapper types.

Fixes #13.
  • Loading branch information
thomaseizinger committed May 19, 2021
1 parent ded0176 commit 49e0383
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 587 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,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", rev = "8cc9ad36bd30ceab6073ff64655473c6b5aa4aab" }
64 changes: 27 additions & 37 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,7 +166,7 @@ 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();
Expand Down Expand Up @@ -327,7 +327,9 @@ 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.display_hex())
}
SubField::Nonce(nonce) => {
let nonce_str = hex_encode(serialize(nonce));
writeln!(fmt, "Nonce: {}", nonce_str)
Expand All @@ -337,7 +339,7 @@ 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.display_hex())?;
}
Ok(())
}
Expand Down Expand Up @@ -445,9 +447,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 +971,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,14 +1023,13 @@ mod tests {

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

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

Expand Down Expand Up @@ -1062,7 +1060,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 +1074,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 +1114,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 +1128,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 49e0383

Please sign in to comment.