Skip to content

Commit

Permalink
Mc crypto ring signature no alloc improvements (#2297)
Browse files Browse the repository at this point in the history
* move generator cache back to transaction core

this is just code movement, no functional change

* Create mc-account-keys-types, break dep alloc in mc-crypto-ring-signature

This adds a trait for a subset of the functionality of `PublicAddress` to
a trait called `RingCtAddress`. This is then used in `mc-crypto-ring-signature`
which allows us to break the dependency of that crate on `mc-account-keys`.

This is helpful because `mc-account-keys` depends on `alloc`, but we want
`mc-crypto-ring-signature` not to depend on `alloc`.

* cargo fmt

* Update account-keys/types/src/traits.rs

Co-authored-by: sugargoat <sugargoat@mobilecoin.com>

Co-authored-by: sugargoat <sugargoat@mobilecoin.com>
  • Loading branch information
cbeck88 and sugargoat committed Jul 24, 2022
1 parent 281353e commit 302bf9f
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 18 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cargo-features = ["named-profiles"]
members = [
"account-keys",
"account-keys/slip10",
"account-keys/types",
"admin-http-gateway",
"android-bindings",
"api",
Expand Down
1 change: 1 addition & 0 deletions account-keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ subtle = { version = "2", default-features = false }
zeroize = { version = "1", default-features = false }

# MobileCoin dependencies
mc-account-keys-types = { path = "types" }
mc-crypto-digestible = { path = "../crypto/digestible" }
mc-crypto-hashes = { path = "../crypto/hashes" }
mc-crypto-keys = { path = "../crypto/keys", default-features = false }
Expand Down
11 changes: 11 additions & 0 deletions account-keys/src/account_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use core::{
hash::{Hash, Hasher},
};
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use mc_account_keys_types::RingCtAddress;
use mc_crypto_digestible::Digestible;
use mc_crypto_hashes::{Blake2b512, Digest};
use mc_crypto_keys::{RistrettoPrivate, RistrettoPublic};
Expand Down Expand Up @@ -187,6 +188,16 @@ impl PublicAddress {
}
}

impl RingCtAddress for PublicAddress {
fn view_public_key(&self) -> &RistrettoPublic {
&self.view_public_key
}

fn spend_public_key(&self) -> &RistrettoPublic {
&self.spend_public_key
}
}

impl AuthorityVerifier for PublicAddress {
type Sig = <RistrettoPublic as AuthorityVerifier>::Sig;
type Error = <RistrettoPublic as AuthorityVerifier>::Error;
Expand Down
10 changes: 10 additions & 0 deletions account-keys/types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "mc-account-keys-types"
version = "1.3.0-pre0"
authors = ["MobileCoin"]
edition = "2021"
readme = "README.md"

[dependencies]
# MobileCoin dependencies
mc-crypto-keys = { path = "../../crypto/keys", default-features = false }
12 changes: 12 additions & 0 deletions account-keys/types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2018-2022 The MobileCoin Foundation

//! Traits and wrapper types connected to MobileCoin account keys.
//! This crate is intended to have a small footprint and be maximally portable.

#![no_std]
#![deny(missing_docs)]
#![deny(unsafe_code)]

mod traits;

pub use traits::RingCtAddress;
10 changes: 10 additions & 0 deletions account-keys/types/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use mc_crypto_keys::RistrettoPublic;

/// An object which represents a subaddress, and has RingCT-style
/// view and spend public keys.
pub trait RingCtAddress {
/// Get the subaddress' view public key
fn view_public_key(&self) -> &RistrettoPublic;
/// Get the subaddress' spend public key
fn spend_public_key(&self) -> &RistrettoPublic;
}
10 changes: 9 additions & 1 deletion consensus/enclave/trusted/Cargo.lock

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

3 changes: 2 additions & 1 deletion crypto/ring-signature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ subtle = { version = "2.4.1", default-features = false, features = ["i128"] }
zeroize = { version = "1", default-features = false }

# MobileCoin dependencies
mc-account-keys = { path = "../../account-keys", default-features = false }
mc-account-keys-types = { path = "../../account-keys/types", default-features = false }
mc-crypto-digestible = { path = "../../crypto/digestible", features = ["dalek", "derive"] }
mc-crypto-hashes = { path = "../../crypto/hashes" }
mc-crypto-keys = { path = "../../crypto/keys", default-features = false }
Expand All @@ -36,6 +36,7 @@ curve25519-dalek = { version = "4.0.0-pre.2", default-features = false, features
proptest = { version = "1.0", default-features = false, features = ["default-code-coverage"] }
tempdir = "0.3"

mc-account-keys = { path = "../../account-keys", default-features = false }
mc-crypto-digestible-test-utils = { path = "../../crypto/digestible/test-utils" }
mc-util-serial = { path = "../../util/serial", features = ["std"] }
mc-util-test-helper = { path = "../../util/test-helper" }
4 changes: 2 additions & 2 deletions crypto/ring-signature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub mod proptest_fixtures;

pub use amount::{Commitment, CompressedCommitment};
pub use ring_signature::{
generators, CryptoRngCore, CurveScalar, Error, GeneratorCache, KeyImage, PedersenGens,
ReducedTxOut, RingMLSAG, Scalar,
generators, CryptoRngCore, CurveScalar, Error, KeyImage, PedersenGens, ReducedTxOut, RingMLSAG,
Scalar,
};

/// Get the shared secret for a transaction output.
Expand Down
6 changes: 3 additions & 3 deletions crypto/ring-signature/src/onetime_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use crate::domain_separators::HASH_TO_SCALAR_DOMAIN_TAG;
use curve25519_dalek::{
constants::RISTRETTO_BASEPOINT_POINT, ristretto::RistrettoPoint, scalar::Scalar,
};
use mc_account_keys::PublicAddress;
use mc_account_keys_types::RingCtAddress;
use mc_crypto_hashes::{Blake2b512, Digest};
use mc_crypto_keys::{RistrettoPrivate, RistrettoPublic};

Expand All @@ -98,7 +98,7 @@ fn hash_to_scalar(point: RistrettoPoint) -> Scalar {
/// * `recipient` - The recipient subaddress `(C,D)`.
pub fn create_tx_out_target_key(
tx_private_key: &RistrettoPrivate,
recipient: &PublicAddress,
recipient: &impl RingCtAddress,
) -> RistrettoPublic {
// `Hs( r * C)`
let Hs: Scalar = {
Expand Down Expand Up @@ -198,7 +198,7 @@ pub fn create_shared_secret(
#[cfg(test)]
mod tests {
use super::*;
use mc_account_keys::AccountKey;
use mc_account_keys::{AccountKey, PublicAddress};
use mc_util_from_random::FromRandom;
use mc_util_test_helper::run_with_several_seeds;

Expand Down
2 changes: 0 additions & 2 deletions crypto/ring-signature/src/ring_signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ pub use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};

mod curve_scalar;
mod error;
mod generator_cache;
mod key_image;
mod mlsag;

pub use self::{
curve_scalar::CurveScalar,
error::Error,
generator_cache::GeneratorCache,
key_image::KeyImage,
mlsag::{CryptoRngCore, ReducedTxOut, RingMLSAG},
};
Expand Down
10 changes: 9 additions & 1 deletion fog/ingest/enclave/trusted/Cargo.lock

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

10 changes: 9 additions & 1 deletion fog/ledger/enclave/trusted/Cargo.lock

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

10 changes: 9 additions & 1 deletion fog/view/enclave/trusted/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

//! A simple generator cache

use super::{generators, PedersenGens};
use alloc::collections::BTreeMap;
use mc_crypto_ring_signature::{generators, PedersenGens};
use mc_transaction_types::TokenId;

/// GeneratorCache is a simple object which caches computations of
Expand Down
2 changes: 2 additions & 0 deletions transaction/core/src/ring_ct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//! MobileCoin RingCT implementation

mod error;
mod generator_cache;
mod rct_bulletproofs;

pub use self::{
error::Error,
generator_cache::GeneratorCache,
rct_bulletproofs::{
InputRing, OutputSecret, PresignedInputRing, SignatureRctBulletproofs, SignedInputRing,
},
Expand Down
4 changes: 2 additions & 2 deletions transaction/core/src/ring_ct/rct_bulletproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use curve25519_dalek::{
use mc_common::HashSet;
use mc_crypto_digestible::{DigestTranscript, Digestible, MerlinTranscript};
use mc_crypto_ring_signature::{
Commitment, CompressedCommitment, GeneratorCache, KeyImage, ReducedTxOut, RingMLSAG, Scalar,
Commitment, CompressedCommitment, KeyImage, ReducedTxOut, RingMLSAG, Scalar,
};
use mc_crypto_ring_signature_signer::{RingSigner, SignableInputRing};
use mc_util_serial::prost::Message;
Expand All @@ -31,7 +31,7 @@ use crate::{
constants::FEE_BLINDING,
domain_separators::EXTENDED_MESSAGE_DOMAIN_TAG,
range_proofs::{check_range_proofs, generate_range_proofs},
ring_ct::Error,
ring_ct::{Error, GeneratorCache},
Amount, BlockVersion,
};

Expand Down
5 changes: 2 additions & 3 deletions transaction/core/src/signed_contingent_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
//! A signed contingent input as described in MCIP #31

use crate::{
ring_ct::{OutputSecret, PresignedInputRing, SignedInputRing},
ring_ct::{GeneratorCache, OutputSecret, PresignedInputRing, SignedInputRing},
tx::TxIn,
Amount, TokenId,
};
use alloc::vec::Vec;
use displaydoc::Display;
use mc_crypto_ring_signature::{
Commitment, CompressedCommitment, CurveScalar, Error as RingSignatureError, GeneratorCache,
KeyImage, RingMLSAG,
Commitment, CompressedCommitment, CurveScalar, Error as RingSignatureError, KeyImage, RingMLSAG,
};
use prost::Message;

Expand Down

0 comments on commit 302bf9f

Please sign in to comment.