Skip to content

Commit

Permalink
feat: integrate ringct into sn_dbc. wip: it now builds without warnings
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 411d8d3 commit 289b242
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 43 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ thiserror = "1.0.24"
quickcheck = "1"
quickcheck_macros = "1"
rand = "0.7.1"
blst_ringct = {git="https://github.com/maidsafe/blst-ringct"}
#blst_ringct = {git="https://github.com/maidsafe/blst-ringct"}
blst_ringct = {path="/home/danda/dev/maidsafe/blst-ringct"}
blsttc = "3.3.0"
hex = "0.4.3"
blstrs = "0.4.1"
#blstrs = "0.4.1"
blstrs = { git = "https://github.com/davidrusu/blstrs.git", branch="bulletproofs-fixes" }
bulletproofs = { git = "https://github.com/davidrusu/blst-bulletproofs.git", branch="bls12-381-curve" }
rand_core = "0.6.3"


[dependencies.rand8]
package = "rand"
Expand Down
15 changes: 7 additions & 8 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
// permissions and limitations relating to use of the SAFE Network Software.

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

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
// use serde::{Deserialize, Serialize};
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;
pub type KeyImage = [u8; 48]; // G1 compressed

#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
// #[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[derive(Debug, Clone)]
pub struct Dbc {
content: DbcContent,
ringct_tx: RingCtTransaction,
Expand Down Expand Up @@ -48,7 +47,7 @@ impl Dbc {
let mut sha3 = Sha3::v256();

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

// for (in_key, (mint_key, mint_sig)) in self.transaction_sigs.iter() {
// sha3.update(&in_key.0.to_bytes());
Expand All @@ -63,7 +62,7 @@ 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> {
pub fn confirm_valid<K: KeyManager>(&self, _verifier: &K) -> Result<(), Error> {
println!("Dbc::confirm_valid() unimplemented");
Ok(())

Expand Down
4 changes: 2 additions & 2 deletions src/dbc_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

use blsttc::PublicKey;
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};
// use tiny_keccak::{Hasher, Sha3};

use crate::{Error, Hash};
use crate::Hash;

// note: Amount should move into blst_ringct crate.
// (or else blst_ringct::RevealedCommitment should be made generic over Amount type)
Expand Down
71 changes: 41 additions & 30 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
// Outputs <= input value

use crate::{
Amount, Dbc, DbcContent, Error, KeyImage, KeyManager, NodeSignature,
PublicKey, PublicKeySet, Result, SpentProof,
Amount, DbcContent, Error, Hash, KeyImage, KeyManager, NodeSignature,
PublicKeySet, Result, SpentProof,
};
// use curve25519_dalek_ng::ristretto::RistrettoPoint;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet, HashSet},
collections::{BTreeMap},
iter::FromIterator,
};
use blst_ringct::ringct::{RingCtMaterial, RingCtTransaction, RevealedCommitment};
use blst_ringct::ringct::{RingCtMaterial, RingCtTransaction};
use blst_ringct::mlsag::{MlsagMaterial, TrueInput};
use blst_ringct::{Output};
use blstrs::Scalar;
use blst_ringct::{Output, RevealedCommitment};
use blstrs::group::{ff::Field, Group, Curve};
use blstrs::{Scalar, G1Projective};
use bulletproofs::{PedersenGens};
use rand_core::OsRng;

// pub type MintNodeSignatures = BTreeMap<SpendKey, (PublicKeySet, NodeSignature)>;
pub type MintNodeSignatures = BTreeMap<KeyImage, (PublicKeySet, NodeSignature)>;
Expand All @@ -35,13 +38,14 @@ pub fn genesis_dbc_input() -> KeyImage {
use blsttc::group::CurveProjective;
let gen_bytes = blsttc::convert::g1_to_be_bytes(blsttc::G1::one());

gen_bytes

// fixme: unwrap
KeyImage::from_bytes(gen_bytes).unwrap()
// G1Projective::from_compressed(&gen_bytes).unwrap().to_affine().
}

#[derive(Debug, Clone)]
pub struct GenesisDbcShare {
pub signed_message: Vec<u8>,
pub dbc_content: DbcContent,
pub transaction: RingCtTransaction,
pub revealed_commitments: Vec<RevealedCommitment>,
Expand Down Expand Up @@ -139,14 +143,15 @@ pub struct GenesisDbcShare {
// }
// }

#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)]
// #[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone)]
pub struct ReissueRequest {
pub signed_message: Vec<u8>,
pub transaction: RingCtTransaction,
pub spent_proofs: BTreeMap<KeyImage, SpentProof>,
}

#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)]
// #[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone)]
pub struct ReissueShare {
pub transaction: RingCtTransaction,
pub mint_node_signatures: MintNodeSignatures,
Expand All @@ -166,7 +171,9 @@ impl<K: KeyManager> MintNode<K> {
}

pub fn issue_genesis_dbc(&mut self, amount: Amount) -> Result<GenesisDbcShare> {
let mut rng = rand::thread_rng();
let mut rng = OsRng::default();
let pc_gens = PedersenGens::default();

let public_key_set = self.key_manager
.public_key_set()
.map_err(|e| Error::Signing(e.to_string()))?;
Expand All @@ -187,18 +194,19 @@ impl<K: KeyManager> MintNode<K> {
let decoy_inputs = vec![];

let ring_ct = RingCtMaterial {
inputs: vec![MlsagMaterial {
inputs: vec![MlsagMaterial::new(
true_input,
decoy_inputs,
}],
&mut rng,
)],
outputs: vec![Output {
// public_key: G1Projective::random(&mut rng).to_affine(),
public_key: dbc_content.owner, // Dbc owner
public_key: G1Projective::random(&mut rng).to_affine(),
// public_key: dbc_content.owner.into(), // Dbc owner. todo.
amount,
}],
};

let (signed_message, transaction, revealed_commitments) = ring_ct
let (transaction, revealed_commitments) = ring_ct
.sign(&pc_gens, rng)
.expect("Failed to sign transaction");

Expand All @@ -213,7 +221,6 @@ impl<K: KeyManager> MintNode<K> {
// .map_err(|e| Error::Signing(e.to_string()))?;

Ok(GenesisDbcShare {
signed_message,
dbc_content,
transaction,
revealed_commitments, // output commitments
Expand All @@ -228,16 +235,16 @@ impl<K: KeyManager> MintNode<K> {

pub fn reissue(&mut self, reissue_req: ReissueRequest) -> Result<ReissueShare> {

let public_commitments = reissue_req.spent_proofs.public_commitments;
// let public_commitments = reissue_req.spent_proofs.public_commitments;

// new
reissue_req.verify(&reissue_req.signed_message, &public_commitments)?;
// reissue_req.transaction.verify(&public_commitments)?;
// old
// reissue_req.transaction.validate(self.key_manager())?;

// new
let transaction = reissue_req.transaction;
let transaction_hash = transaction.hash();
let transaction_hash = Hash::from(transaction.hash());
// old
// let transaction = reissue_req.transaction.blinded();
// let transaction_hash = transaction.hash();
Expand All @@ -247,9 +254,10 @@ impl<K: KeyManager> MintNode<K> {
// iterate over mlsags. each has key_image()

for mlsag in transaction.mlsags.iter() {
match reissue_req.spent_proofs.get(&mlsag.key_image()) {
Some(proof) => proof.validate(mlsag.key_image(), transaction_hash, self.key_manager())?,
None => return Err(Error::MissingSpentProof(mlsag.key_image())),
let key_image = mlsag.key_image.to_compressed();
match reissue_req.spent_proofs.get(&key_image) {
Some(proof) => proof.validate(key_image, transaction_hash, self.key_manager())?,
None => return Err(Error::MissingSpentProof(key_image)),
}
}

Expand All @@ -269,15 +277,18 @@ impl<K: KeyManager> MintNode<K> {
) -> Result<BTreeMap<KeyImage, (PublicKeySet, NodeSignature)>> {
let sig = self
.key_manager
.sign(&transaction.hash())
.sign(&Hash::from(transaction.hash()))
.map_err(|e| Error::Signing(e.to_string()))?;

Ok(BTreeMap::from_iter(
transaction.mlsags.iter().copied().zip(std::iter::repeat((
self.key_manager
let pks = self.key_manager
.public_key_set()
.map_err(|e| Error::Signing(e.to_string()))?,
sig,
.map_err(|e| Error::Signing(e.to_string()))?;

let v: Vec<KeyImage> = transaction.mlsags.iter().map(|m| m.key_image.to_compressed()).collect();

Ok(BTreeMap::from_iter(
v.iter().cloned().zip(std::iter::repeat((
pks, sig,
))),
))
}
Expand Down
2 changes: 1 addition & 1 deletion src/spent_proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Dbc, Error, Hash, KeyImage, KeyManager, NodeSignature, PublicKey, PublicKeySet, Result, Signature,
Error, Hash, KeyImage, KeyManager, NodeSignature, PublicKey, PublicKeySet, Result, Signature,
};

use serde::{Deserialize, Serialize};
Expand Down

0 comments on commit 289b242

Please sign in to comment.