Skip to content

Commit

Permalink
feat: remove hand wavy crypto, use blsttc
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach authored and davidrusu committed Feb 2, 2023
1 parent e06e9f0 commit c25ef94
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Safe Network DBC"
license = "MIT OR BSD-3-Clause"
repository = "https://github.com/maidsafe/sn_dbc"
documentation = "https://docs.rs/sn_dbc"
keywords = [ "Safe", "Network", "SafeNetwork", "SafeUrl", "XorUrl" ]
keywords = [ "Safe", "Network", "SafeNetwork", "DBC", "Digital Bearer Certificates" ]
authors = [ "MaidSafe Developers <dev@maidsafe.net>" ]
edition = "2018"

Expand Down
23 changes: 11 additions & 12 deletions examples/mint-repl/mint-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ impl MintInfo {
}
}

/// A RingCtTransaction with pubkey set for all the input and output Dbcs
#[derive(Debug, Clone, Serialize, Deserialize)]
struct RingCtTransactionRevealed {
/// A DBC Transaction with pubkey set for all the input and output Dbcs
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct DbcTransactionRevealed {
inner: DbcTransaction,
revealed_commitments: Vec<RevealedCommitment>,
ringct_material: RevealedTransaction,
revealed_tx: RevealedTransaction,
output_owner_map: OutputOwnerMap,
}

Expand Down Expand Up @@ -427,7 +428,7 @@ fn print_dbc_human(dbc: &Dbc, outputs: bool, secret_key_base: Option<SecretKey>)
/// handles decode command.
fn decode_input() -> Result<()> {
let t = readline_prompt(
"\n[d: DBC, rt: RingCtTransaction, pks: PublicKeySet, sks: SecretKeySet]\nType: ",
"\n[d: DBC, rt: DBC Transaction, pks: PublicKeySet, sks: SecretKeySet]\nType: ",
)?;
let input = readline_prompt_nl("\nPaste Data: ")?;
let bytes = decode(input)?;
Expand Down Expand Up @@ -488,8 +489,8 @@ fn decode_input() -> Result<()> {
println!("-- End SecretKeySet --\n");
}
"rt" => println!(
"\n\n-- RingCtTransaction --\n\n{:#?}",
from_be_bytes::<RingCtTransactionRevealed>(&bytes)?
"\n\n-- DBC Transaction --\n\n{:#?}",
from_be_bytes::<DbcTransactionRevealed>(&bytes)?
),
_ => println!("Unknown type!"),
}
Expand Down Expand Up @@ -539,11 +540,9 @@ fn verify(mintinfo: &MintInfo) -> Result<()> {
}
};

let pk = &dbc.public_key(&secret_key)?;
match dbc.verify(&secret_key, &mintinfo.spentbook()?.key_manager) {
Ok(_) => match mintinfo
.spentbook()?
.is_spent(&dbc.public_key(&secret_key)?)
{
Ok(_) => match mintinfo.spentbook()?.is_spent(pk) {
true => println!("\nThis DBC is unspendable. (valid but has already been spent)\n"),
false => println!("\nThis DBC is spendable. (valid and has not been spent)\n"),
},
Expand Down Expand Up @@ -655,7 +654,7 @@ fn prepare_tx() -> Result<DbcBuilder> {
i += 1;
}

println!("\n\nPreparing RingCtTransaction...\n\n");
println!("\n\nPreparing DBC Transaction...\n\n");

let dbc_builder = tx_builder.build(rng::thread_rng())?;

Expand Down
3 changes: 0 additions & 3 deletions src/blst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
//! and use its types directly.
//!
//! sn_dbc internally uses the type aliases rather than directly using the blstrs types.
//!
//! We could consider moving some or all of this lower into sn_ringct to make these
//! crates consistent.
/// a Commitment
pub type Commitment = crate::transaction::blstrs::G1Affine;
Expand Down
15 changes: 7 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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::{bls_bulletproofs::PedersenGens, group::Curve};
use crate::transaction::bls_bulletproofs::PedersenGens;
pub use crate::transaction::{
output::DbcTransaction, Output, RevealedCommitment, RevealedInput, RevealedTransaction,
};
Expand All @@ -27,7 +27,7 @@ use serde::{Deserialize, Serialize};

pub type OutputOwnerMap = BTreeMap<PublicKey, OwnerOnce>;

/// A builder to create a RingCt transaction from
/// A builder to create a DBC transaction from
/// inputs and outputs.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
Expand Down Expand Up @@ -104,8 +104,7 @@ impl TransactionBuilder {

/// add an output
pub fn add_output(mut self, output: Output, owner: OwnerOnce) -> Self {
self.output_owner_map
.insert(output.public_key().into(), owner);
self.output_owner_map.insert(output.public_key(), owner);
self.revealed_tx.outputs.push(output);
self
}
Expand Down Expand Up @@ -143,7 +142,7 @@ impl TransactionBuilder {
self.revealed_tx
.inputs
.iter()
.map(|t| t.public_key().into())
.map(|t| t.public_key())
.collect()
}

Expand Down Expand Up @@ -225,7 +224,7 @@ impl DbcBuilder {
self.transaction
.inputs
.iter()
.map(|input| (input.public_key().into(), self.transaction.clone()))
.map(|input| (input.public_key(), self.transaction.clone()))
.collect()
}

Expand Down Expand Up @@ -300,7 +299,7 @@ impl DbcBuilder {
let output_commitments: Vec<(Commitment, RevealedCommitment)> = self
.revealed_commitments
.iter()
.map(|r| (r.commit(&pc_gens).to_affine(), *r))
.map(|r| (r.commit(&pc_gens), *r))
.collect();

let owner_once_list: Vec<&OwnerOnce> = self
Expand All @@ -309,7 +308,7 @@ impl DbcBuilder {
.iter()
.map(|output| {
self.output_owner_map
.get(&(*output.public_key()).into())
.get(output.public_key())
.ok_or(Error::PublicKeyNotFound)
})
.collect::<Result<_>>()?;
Expand Down
17 changes: 7 additions & 10 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::transaction::{
group::Curve,
output::{DbcTransaction, OutputProof},
{RevealedCommitment, RevealedInput},
};
Expand Down Expand Up @@ -138,9 +137,7 @@ impl Dbc {
/// This is useful for checking if a Dbc has been spent.
pub fn public_key(&self, base_sk: &SecretKey) -> Result<PublicKey> {
let secret_key = self.owner_once(base_sk)?.secret_key()?;
Ok(crate::transaction::public_key(secret_key)
.to_affine()
.into())
Ok(secret_key.public_key())
}

/// returns PublicKey for the owner's derived public key
Expand Down Expand Up @@ -298,7 +295,7 @@ impl Dbc {
/// and no recourse.
pub(crate) fn verify_amount_matches_commitment(&self, base_sk: &SecretKey) -> Result<()> {
let rc: RevealedCommitment = self.amount_secrets(base_sk)?.into();
let secrets_commitment = rc.commit(&Default::default()).to_affine();
let secrets_commitment = rc.commit(&Default::default());
let tx_commitment = self.my_output_proof(base_sk)?.commitment();

match secrets_commitment == tx_commitment {
Expand Down Expand Up @@ -383,11 +380,11 @@ pub(crate) mod tests {
let amount = 100;
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng);
let ringct_material = RevealedTransaction {
let tx_material = RevealedTransaction {
inputs: vec![],
outputs: vec![Output::new(owner_once.as_owner().public_key(), amount)],
};
let (transaction, revealed_commitments) = ringct_material
let (transaction, revealed_commitments) = tx_material
.sign(&mut rng)
.expect("Failed to sign transaction");
let input_content = DbcContent::from((
Expand Down Expand Up @@ -473,12 +470,12 @@ pub(crate) mod tests {
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng);

let ringct_material = RevealedTransaction {
let tx_material = RevealedTransaction {
inputs: vec![],
outputs: vec![Output::new(owner_once.as_owner().public_key(), amount)],
};

let (transaction, revealed_commitments) = ringct_material
let (transaction, revealed_commitments) = tx_material
.sign(&mut rng)
.expect("Failed to sign transaction");

Expand Down Expand Up @@ -596,7 +593,7 @@ pub(crate) mod tests {
let output_commitments: Vec<(crate::Commitment, RevealedCommitment)> = dbc_builder
.revealed_commitments
.iter()
.map(|r| (r.commit(&pc_gens).to_affine(), *r))
.map(|r| (r.commit(&pc_gens), *r))
.collect();
let amount_secrets_list: Vec<AmountSecrets> = output_commitments
.iter()
Expand Down
6 changes: 3 additions & 3 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,15 @@ mod tests {
// Note that the new spentbook uses the same signing key as the original
let mut new_spentbook = mock::SpentBookNode::from(spentbook.key_manager);
let _genesis_spent_proof_share = new_spentbook.log_spent(
genesis_dbc.transaction.inputs[0].public_key.into(),
genesis_dbc.transaction.inputs[0].public_key,
genesis_dbc.transaction.clone(),
)?;
let _starting_spent_proof_share = new_spentbook.log_spent(
starting_dbc.transaction.inputs[0].public_key.into(),
starting_dbc.transaction.inputs[0].public_key,
starting_dbc.transaction.clone(),
)?;
let _spent_proof_share = new_spentbook.log_spent(
b_dbc.transaction.inputs[0].public_key.into(),
b_dbc.transaction.inputs[0].public_key,
b_dbc.transaction.clone(),
)?;

Expand Down
2 changes: 0 additions & 2 deletions src/mock/genesis_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ impl GenesisBuilder {
GenesisMaterial,
AmountSecrets,
)> {
// note: rng is necessary for RingCtMaterial::sign().

let genesis_material = GenesisMaterial::default();
let mut dbc_builder = TransactionBuilder::default()
.add_input(genesis_material.revealed_tx.inputs[0].clone())
Expand Down
7 changes: 3 additions & 4 deletions src/mock/genesis_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::transaction::{
{Output, RevealedCommitment},
};
use crate::{Owner, OwnerOnce, PublicKey};
use bls_bulletproofs::group::Curve;
use blsttc::IntoFr;

/// represents all the inputs required to build the Genesis Dbc.
Expand Down Expand Up @@ -60,10 +59,10 @@ impl Default for GenesisMaterial {
blinding: 42.into(), // just a random number
},
);
let input_public_key: PublicKey = revealed_input.public_key().to_affine().into();
let input_public_key: PublicKey = revealed_input.public_key();

// build the genesis Transaction
let ringct_material = RevealedTransaction {
let revealed_tx = RevealedTransaction {
inputs: vec![revealed_input],
outputs: vec![Output::new(
output_sk_once.public_key(),
Expand All @@ -72,7 +71,7 @@ impl Default for GenesisMaterial {
};

Self {
revealed_tx: ringct_material,
revealed_tx,
owner_once: output_owner_once,
input_public_key,
}
Expand Down
12 changes: 5 additions & 7 deletions src/mock/spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::transaction::{
bls_bulletproofs::PedersenGens,
output::{DbcTransaction, OutputProof},
};
use bls_bulletproofs::group::Curve;
use blsttc::PublicKey;
use std::collections::{BTreeMap, HashMap};

Expand Down Expand Up @@ -55,8 +54,7 @@ impl From<mock::KeyManager> for SpentBookNode {
let genesis_material = GenesisMaterial::default();
let public_commitment = genesis_material.revealed_tx.inputs[0]
.revealed_commitment()
.commit(&PedersenGens::default())
.to_affine();
.commit(&PedersenGens::default());

Self {
key_manager,
Expand Down Expand Up @@ -128,10 +126,10 @@ impl SpentBookNode {
.map(|input| {
// look up matching OutputProof
let pk = input.public_key();
let output_proof = self.outputs.get(&pk.into());
let output_proof = self.outputs.get(&pk);
match output_proof {
Some(p) => Ok((input.public_key.into(), p.commitment())),
None => Err(Error::MissingCommitmentForPubkey(pk.into())),
Some(p) => Ok((input.public_key, p.commitment())),
None => Err(Error::MissingCommitmentForPubkey(pk)),
}
})
.collect::<Result<_>>()?
Expand Down Expand Up @@ -172,7 +170,7 @@ impl SpentBookNode {

// Add public_key:output_proof to public_key index.
for output in existing_tx.outputs.iter() {
let pk = PublicKey::from(*output.public_key());
let pk = *output.public_key();
self.outputs.entry(pk).or_insert_with(|| output.clone());
}

Expand Down
Loading

0 comments on commit c25ef94

Please sign in to comment.