Skip to content

Commit

Permalink
feat: working on ringct integration. does not build
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-da authored and dirvine committed Feb 17, 2022
1 parent 80ca4cf commit 411d8d3
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 496 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ authors = [ "MaidSafe Developers <dev@maidsafe.net>" ]
edition = "2018"

[features]
dkg = [ "bls_dkg" ]
#dkg = [ "bls_dkg" ]

[dependencies]
serde_json = "1.0.64"
thiserror = "1.0.24"
quickcheck = "1"
quickcheck_macros = "1"
rand = "0.7.1"
bulletproofs = "4.0.0"
curve25519-dalek-ng = "4.0.1"
merlin = "3.0.0"
blst_ringct = {git="https://github.com/maidsafe/blst-ringct"}
blsttc = "3.3.0"
hex = "0.4.3"
blstrs = "0.4.1"

[dependencies.rand8]
package = "rand"
Expand Down Expand Up @@ -50,7 +49,7 @@ bincode = "1.3.3"

[dev-dependencies.sn_dbc]
path = "."
features = [ "dkg" ]
# features = [ "dkg" ]

[target."cfg(unix)".dev-dependencies]
termios = "0.3.3"
Expand Down
28 changes: 14 additions & 14 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ use blsttc::{PublicKeySet, SignatureShare};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::iter::FromIterator;

use curve25519_dalek_ng::scalar::Scalar;
// use curve25519_dalek_ng::scalar::Scalar;
use blst_ringct::Output;

use crate::{
Amount, AmountSecrets, Dbc, DbcContent, Error, NodeSignature, PublicKey, ReissueRequest,
ReissueShare, ReissueTransaction, Result, SpendKey, SpentProof, SpentProofShare,
Amount, Dbc, DbcContent, Error, NodeSignature, PublicKey, ReissueRequest,
ReissueShare, ReissueTransaction, Result, SpentProof, SpentProofShare,
};

// note: Use blst_ringct::Output instead.

///! Unblinded data for creating sn_dbc::DbcContent
pub struct Output {
pub amount: Amount,
pub owner: PublicKey,
}
// pub struct Output {
// pub amount: Amount,
// pub owner: PublicKey,
// }

#[derive(Default)]
pub struct TransactionBuilder {
pub inputs: HashMap<Dbc, AmountSecrets>,
pub outputs: Vec<Output>,
}
pub struct TransactionBuilder(RingCtMaterial);

impl TransactionBuilder {
pub fn add_input(mut self, dbc: Dbc, amount_secrets: AmountSecrets) -> Self {
Expand Down Expand Up @@ -99,8 +99,8 @@ impl TransactionBuilder {
/// any number of (input) DBC spent proof shares.
#[derive(Debug)]
pub struct ReissueRequestBuilder {
pub reissue_transaction: ReissueTransaction,
pub spent_proof_shares: BTreeMap<SpendKey, HashSet<SpentProofShare>>,
pub transaction: RingCtTransaction,
pub spent_proof_shares: BTreeMap<KeyImage, HashSet<SpentProofShare>>,
}

impl ReissueRequestBuilder {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl ReissueRequestBuilder {
/// generate the final Dbc outputs.
#[derive(Debug)]
pub struct DbcBuilder {
pub reissue_transaction: ReissueTransaction,
pub transaction: RingCtTransaction,
pub reissue_shares: Vec<ReissueShare>,
}

Expand Down
81 changes: 47 additions & 34 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,54 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
DbcContent, DbcTransaction, Error, KeyManager, PublicKey, Result, Signature, SpendKey,
DbcContent, Error, KeyManager, PublicKey, Result, Signature,
};

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tiny_keccak::{Hasher, Sha3};
use blst_ringct::ringct::RingCtTransaction;
use blstrs::G1Projective;

// note: typedef should be moved into blst_ringct crate

pub type KeyImage = G1Projective;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct Dbc {
pub content: DbcContent,
pub transaction: DbcTransaction,
pub transaction_sigs: BTreeMap<SpendKey, (PublicKey, Signature)>,
content: DbcContent,
ringct_tx: RingCtTransaction,
}

impl Dbc {

// pub fn key_image() -> KeyImage {
// unimplemented!()
// }

/// Derive the (public) spend key for this DBC.
pub fn spend_key(&self) -> SpendKey {
let index = self.spend_key_index();
SpendKey(self.owner().derive_child(&index))
}
// pub fn spend_key(&self) -> SpendKey {
// let index = self.spend_key_index();
// SpendKey(self.owner().derive_child(&index))
// }

/// Read the DBC owner
pub fn owner(&self) -> PublicKey {
self.content.owner
}

/// Calculate the spend key index, this index is used to derive the spend key.
pub fn spend_key_index(&self) -> [u8; 32] {
pub fn hash(&self) -> [u8; 32] {
let mut sha3 = Sha3::v256();

sha3.update(&self.content.hash().0);
sha3.update(&self.transaction.hash().0);
sha3.update(&self.ringct_tx.hash().0);

for (in_key, (mint_key, mint_sig)) in self.transaction_sigs.iter() {
sha3.update(&in_key.0.to_bytes());
sha3.update(&mint_key.to_bytes());
sha3.update(&mint_sig.to_bytes());
}
// for (in_key, (mint_key, mint_sig)) in self.transaction_sigs.iter() {
// sha3.update(&in_key.0.to_bytes());
// sha3.update(&mint_key.to_bytes());
// sha3.update(&mint_sig.to_bytes());
// }

let mut hash = [0u8; 32];
sha3.finalize(&mut hash);
Expand All @@ -54,26 +64,29 @@ impl Dbc {
// Check there exists a DbcTransaction with the output containing this Dbc
// Check there DOES NOT exist a DbcTransaction with this Dbc as parent (already minted)
pub fn confirm_valid<K: KeyManager>(&self, verifier: &K) -> Result<(), Error> {
for (input, (mint_key, mint_sig)) in self.transaction_sigs.iter() {
if !self.transaction.inputs.contains(input) {
return Err(Error::UnknownInput);
}
println!("Dbc::confirm_valid() unimplemented");
Ok(())

verifier
.verify(&self.transaction.hash(), mint_key, mint_sig)
.map_err(|e| Error::Signing(e.to_string()))?;
}
if self.transaction.inputs.is_empty() {
Err(Error::TransactionMustHaveAnInput)
} else if self.transaction_sigs.len() < self.transaction.inputs.len() {
Err(Error::MissingSignatureForInput)
} else if self.transaction.inputs != self.content.parents {
Err(Error::DbcContentParentsDifferentFromTransactionInputs)
} else if !self.transaction.outputs.contains(&self.owner()) {
Err(Error::DbcContentNotPresentInTransactionOutput)
} else {
Ok(())
}
// for (input, (mint_key, mint_sig)) in self.transaction_sigs.iter() {
// if !self.transaction.inputs.contains(input) {
// return Err(Error::UnknownInput);
// }

// verifier
// .verify(&self.transaction.hash(), mint_key, mint_sig)
// .map_err(|e| Error::Signing(e.to_string()))?;
// }
// if self.transaction.inputs.is_empty() {
// Err(Error::TransactionMustHaveAnInput)
// } else if self.transaction_sigs.len() < self.transaction.inputs.len() {
// Err(Error::MissingSignatureForInput)
// } else if self.transaction.inputs != self.content.parents {
// Err(Error::DbcContentParentsDifferentFromTransactionInputs)
// } else if !self.transaction.outputs.contains(&self.owner()) {
// Err(Error::DbcContentNotPresentInTransactionOutput)
// } else {
// Ok(())
// }
}
}

Expand Down
Loading

0 comments on commit 411d8d3

Please sign in to comment.