Skip to content

Commit

Permalink
feat: remove ringct dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach authored and davidrusu committed Feb 2, 2023
1 parent a172a04 commit 746cec3
Show file tree
Hide file tree
Showing 17 changed files with 1,075 additions and 40 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ authors = [ "MaidSafe Developers <dev@maidsafe.net>" ]
edition = "2018"

[features]
serdes = [ "serde", "ringct-serde" ]
ringct-serde = [ "bls_ringct/serde" ]
serdes = [ "serde" ]
mock = [ ]

[dependencies]
bincode = "1.3.3"
blsttc = "8.0.1"
bls_ringct = "1.1.2"
bls_bulletproofs = "1.1.1"
hex = "0.4.3"
thiserror = "1.0.24"

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ $ cargo test

This crate depends most heavily on:

- [bls_ringct](https://github.com/maidsafe/bls_ringct/) - RingCt using bls curve
- [blsttc](https://github.com/maidsafe/blsttc/) - BLS keys


Expand Down
6 changes: 3 additions & 3 deletions src/amount_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::transaction::{output::Amount, RevealedCommitment};
use crate::{rand::RngCore, BlindingFactor, Error, Token};
use bls_ringct::{ringct::Amount, RevealedCommitment};
use blsttc::{
Ciphertext, DecryptionShare, IntoFr, PublicKey, PublicKeySet, SecretKey, SecretKeySet,
SecretKeyShare,
Expand All @@ -20,14 +20,14 @@ use serde::{Deserialize, Serialize};
const AMT_SIZE: usize = std::mem::size_of::<Token>(); // Amount size: 8 bytes (u64)
const BF_SIZE: usize = std::mem::size_of::<BlindingFactor>(); // Blinding factor size: 32 bytes (BlindingFactor)

/// AmountSecrets wraps bls_ringct::RevealedCommitment to provide some methods
/// AmountSecrets wraps crate::transaction::RevealedCommitment to provide some methods
/// for ergonomic usage, eg: decrypting from Ciphertext using various blsttc
/// components, eg SecretKey, SecretKeyShare, SecretKeySet, DecryptionShare
//
// todo: perhaps AmountSecrets should be renamed to be more consistent with
// RevealedCommitment, since it is just a NewType wrapper.
//
// Once bls_ringct uses blsttc, perhaps AmountSecrets functionality could
// Once crate::transaction uses blsttc, perhaps AmountSecrets functionality could
// move into RevealedCommitment, and AmountSecrets goes away entirely.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions src/blst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
//! crates consistent.
/// a Commitment
pub type Commitment = bls_ringct::blstrs::G1Affine;
pub type Commitment = crate::transaction::blstrs::G1Affine;

/// a BlindingFactor
pub type BlindingFactor = bls_ringct::blstrs::Scalar;
pub type BlindingFactor = crate::transaction::blstrs::Scalar;

/// A KeyImage can be thought of as a specific type
/// of public key. blsttc::PublicKey is a newtype
Expand Down
6 changes: 3 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use bls_ringct::{bls_bulletproofs::PedersenGens, group::Curve};
pub use bls_ringct::{
ringct::RingCtTransaction, DecoyInput, MlsagMaterial, Output, RevealedCommitment,
use crate::transaction::{bls_bulletproofs::PedersenGens, group::Curve};
pub use crate::transaction::{
output::RingCtTransaction, DecoyInput, MlsagMaterial, Output, RevealedCommitment,
RingCtMaterial, TrueInput,
};
use blsttc::{PublicKey, SecretKey};
Expand Down
20 changes: 11 additions & 9 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::transaction::{
group::Curve,
output::{OutputProof, RingCtTransaction},
{RevealedCommitment, TrueInput},
};
use crate::{
AmountSecrets, DbcContent, DerivationIndex, Error, Hash, KeyImage, Owner, Result, SpentProof,
SpentProofKeyVerifier, TransactionVerifier,
};
use bls_ringct::{
group::Curve,
ringct::{OutputProof, RingCtTransaction},
{RevealedCommitment, TrueInput},
};
use blsttc::SecretKey;
use std::{collections::BTreeSet, convert::TryFrom};
use tiny_keccak::{Hasher, Sha3};
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Dbc {
/// This is useful for checking if a Dbc has been spent.
pub fn key_image(&self, base_sk: &SecretKey) -> Result<KeyImage> {
let secret_key = self.owner_once(base_sk)?.secret_key()?;
Ok(bls_ringct::key_image(secret_key).to_affine().into())
Ok(crate::transaction::key_image(secret_key).to_affine().into())
}

/// returns KeyImage for the owner's derived public key
Expand Down Expand Up @@ -322,12 +322,12 @@ pub(crate) mod tests {
use quickcheck_macros::quickcheck;

use crate::tests::{NonZeroTinyInt, TinyInt, STD_DECOYS_PER_INPUT, STD_DECOYS_TO_FETCH};
use crate::transaction::{bls_bulletproofs::PedersenGens, output::RingCtMaterial, Output};
use crate::{
mock,
rand::{CryptoRng, RngCore},
AmountSecrets, DbcBuilder, Hash, Owner, OwnerOnce, SpentProofContent, Token,
};
use bls_ringct::{bls_bulletproofs::PedersenGens, ringct::RingCtMaterial, Output};
use blsttc::PublicKey;
use std::convert::TryInto;

Expand Down Expand Up @@ -505,7 +505,9 @@ pub(crate) mod tests {

assert!(matches!(
dbc.verify(&owner_once.owner_base().secret_key()?, &key_manager),
Err(Error::RingCt(bls_ringct::Error::TransactionMustHaveAnInput))
Err(Error::RingCt(
crate::transaction::Error::TransactionMustHaveAnInput
))
));

Ok(())
Expand Down Expand Up @@ -753,7 +755,7 @@ pub(crate) mod tests {
.iter()
.any(|o| dbc_owner.eq(o.public_key())));
}
Err(Error::RingCt(bls_ringct::Error::TransactionMustHaveAnInput)) => {
Err(Error::RingCt(crate::transaction::Error::TransactionMustHaveAnInput)) => {
assert_eq!(n_inputs.coerce::<u8>(), 0);
}
Err(Error::AmountCommitmentsDoNotMatch) => {
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.
use thiserror::Error;

use crate::transaction;
use crate::KeyImage;

/// Specialisation of `std::Result`.
Expand Down Expand Up @@ -104,7 +105,7 @@ pub enum Error {
Blsttc(#[from] blsttc::error::Error),

#[error("ringct error: {0}")]
RingCt(#[from] bls_ringct::Error),
RingCt(#[from] transaction::Error),

#[cfg(feature = "mock")]
#[error("mock object error")]
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ mod spent_proof;
mod token;
mod verification;

pub mod transaction;

#[cfg(feature = "mock")]
pub mod mock;

// re-export crates used in our public API
pub use bls_ringct;
pub use blsttc;
// note: both bls_ringct::rand and blsttc::rand are
// note: both transaction::rand and blsttc::rand are
// exposed in our public API. Here, by choosing
// just one, we are making an implicit promise that
// the two versions will remain compatible, or that
// our API will reconcile the difference. We do
// this knowingly and pledge to uphold that promise.
pub use bls_ringct::rand;
pub use transaction::rand;

pub use blsttc::{PublicKey, PublicKeySet, Signature, SignatureShare};

Expand All @@ -52,6 +53,7 @@ pub use crate::{
SpentProofShare,
},
token::Token,
// transaction::Transaction,
verification::{get_public_commitments_from_transaction, TransactionVerifier},
};

Expand Down
16 changes: 8 additions & 8 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ mod tests {
let check_error = |error: Error| -> Result<()> {
match error {
Error::RingCt(
bls_ringct::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
crate::transaction::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
) => {
// Verify that no outputs were present and we got correct verification error.
assert_eq!(n_outputs, 0);
Ok(())
}
Error::RingCt(bls_ringct::Error::InvalidHiddenCommitmentInRing) => {
Error::RingCt(crate::transaction::Error::InvalidHiddenCommitmentInRing) => {
// Verify that no outputs were present and we got correct verification error.
assert_eq!(n_outputs, 0);
Ok(())
Expand Down Expand Up @@ -188,7 +188,7 @@ mod tests {
let check_tx_error = |error: Error| -> Result<()> {
match error {
Error::RingCt(
bls_ringct::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
crate::transaction::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
) => {
// Verify that no inputs were present and we got correct verification error.
assert!(input_amounts.is_empty());
Expand Down Expand Up @@ -258,7 +258,7 @@ mod tests {
assert!(!invalid_spent_proofs.is_empty());
}
Error::RingCt(
bls_ringct::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
crate::transaction::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
) => {
if mock::GenesisMaterial::GENESIS_AMOUNT == output_total_amount {
// This can correctly occur if there are 0 outputs and inputs sum to zero.
Expand All @@ -271,10 +271,10 @@ mod tests {
assert!(!input_amounts.is_empty());
}
}
Error::RingCt(bls_ringct::Error::InvalidHiddenCommitmentInRing) => {
Error::RingCt(crate::transaction::Error::InvalidHiddenCommitmentInRing) => {
assert!(!invalid_spent_proofs.is_empty());
}
Error::RingCt(bls_ringct::Error::TransactionMustHaveAnInput) => {
Error::RingCt(crate::transaction::Error::TransactionMustHaveAnInput) => {
assert_eq!(input_amounts.len(), 0);
}
Error::FailedSignature => {
Expand Down Expand Up @@ -565,7 +565,7 @@ mod tests {

for (key_image, tx) in dbc_builder_fudged.inputs() {
match spentbook.log_spent(key_image, tx) {
Err(Error::RingCt(bls_ringct::Error::InvalidHiddenCommitmentInRing)) => {}
Err(Error::RingCt(crate::transaction::Error::InvalidHiddenCommitmentInRing)) => {}
_ => panic!("Expecting RingCt Error::InvalidHiddenCommitmentInRing"),
}
}
Expand All @@ -589,7 +589,7 @@ mod tests {
let result_fudged = dbc_builder_fudged.build(&spentbook.key_manager);

match result_fudged {
Err(Error::RingCt(bls_ringct::Error::InvalidHiddenCommitmentInRing)) => {}
Err(Error::RingCt(crate::transaction::Error::InvalidHiddenCommitmentInRing)) => {}
_ => panic!("Expecting RingCt Error::InvalidHiddenCommitmentInRing"),
}

Expand Down
8 changes: 4 additions & 4 deletions src/mock/genesis_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{KeyImage, Owner, OwnerOnce};
use bls_ringct::{
use crate::transaction::{
blstrs::Scalar,
group::Curve,
mlsag::{MlsagMaterial, TrueInput},
ringct::{Amount, RingCtMaterial},
input::{MlsagMaterial, TrueInput},
output::{Amount, RingCtMaterial},
{Output, RevealedCommitment},
};
use crate::{KeyImage, Owner, OwnerOnce};
use blsttc::IntoFr;

/// represents all the inputs required to build the Genesis Dbc.
Expand Down
4 changes: 2 additions & 2 deletions src/mock/spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use bls_ringct::{
use crate::transaction::{
bls_bulletproofs::PedersenGens,
group::Curve,
ringct::{OutputProof, RingCtTransaction},
output::{OutputProof, RingCtTransaction},
DecoyInput,
};
use blsttc::PublicKey;
Expand Down
33 changes: 33 additions & 0 deletions src/transaction/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022, MaidSafe.
// All rights reserved.
//
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use thiserror::Error;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq, Error)]
pub enum Error {
#[error("We need a corresponding public key for each MLSAG ring entry")]
ExpectedAPublicCommitmentsForEachRingEntry,
#[error("The hidden commitment in the MLSAG ring must be of the form: $C - C'$")]
InvalidHiddenCommitmentInRing,
#[error("InputPseudoCommitmentsDoNotSumToOutputCommitments")]
InputPseudoCommitmentsDoNotSumToOutputCommitments,
#[error("The MLSAG ring signature is not valid")]
InvalidRingSignature,
#[error("KeyImage is not on the BLS12-381 G1 Curve")]
KeyImageNotOnCurve,
#[error("BulletProofs Error: {0}")]
BulletProofs(#[from] bls_bulletproofs::ProofError),
#[error("The DBC transaction must have at least one input")]
TransactionMustHaveAnInput,
#[error("key image is not unique across all transaction inputs")]
KeyImageNotUniqueAcrossInputs,
#[error("public key is not unique across all transaction inputs")]
PublicKeyNotUniqueAcrossInputs,
}
Loading

0 comments on commit 746cec3

Please sign in to comment.