diff --git a/Cargo.toml b/Cargo.toml index 8d82a97..dc646fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/dbc.rs b/src/dbc.rs index cf8917a..fd0d1ed 100644 --- a/src/dbc.rs +++ b/src/dbc.rs @@ -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, @@ -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()); @@ -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(&self, verifier: &K) -> Result<(), Error> { + pub fn confirm_valid(&self, _verifier: &K) -> Result<(), Error> { println!("Dbc::confirm_valid() unimplemented"); Ok(()) diff --git a/src/dbc_content.rs b/src/dbc_content.rs index b403a0e..628e9ee 100644 --- a/src/dbc_content.rs +++ b/src/dbc_content.rs @@ -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) diff --git a/src/mint.rs b/src/mint.rs index 4d87e3b..28d6e7f 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -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; pub type MintNodeSignatures = BTreeMap; @@ -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, pub dbc_content: DbcContent, pub transaction: RingCtTransaction, pub revealed_commitments: Vec, @@ -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, pub transaction: RingCtTransaction, pub spent_proofs: BTreeMap, } -#[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, @@ -166,7 +171,9 @@ impl MintNode { } pub fn issue_genesis_dbc(&mut self, amount: Amount) -> Result { - 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()))?; @@ -187,18 +194,19 @@ impl MintNode { 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"); @@ -213,7 +221,6 @@ impl MintNode { // .map_err(|e| Error::Signing(e.to_string()))?; Ok(GenesisDbcShare { - signed_message, dbc_content, transaction, revealed_commitments, // output commitments @@ -228,16 +235,16 @@ impl MintNode { pub fn reissue(&mut self, reissue_req: ReissueRequest) -> Result { - 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(); @@ -247,9 +254,10 @@ impl MintNode { // 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)), } } @@ -269,15 +277,18 @@ impl MintNode { ) -> Result> { 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 = transaction.mlsags.iter().map(|m| m.key_image.to_compressed()).collect(); + + Ok(BTreeMap::from_iter( + v.iter().cloned().zip(std::iter::repeat(( + pks, sig, ))), )) } diff --git a/src/spent_proof.rs b/src/spent_proof.rs index b5bf4b7..6162a4f 100644 --- a/src/spent_proof.rs +++ b/src/spent_proof.rs @@ -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};