Skip to content

Commit

Permalink
fix: prop_dbc_transaction_many_to_many() is passing now
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 31e3bc0 commit d0e29f3
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 194 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ hex = "0.4.3"
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"
xor_name = "3.1.0"


[dependencies.rand8]
Expand Down
16 changes: 5 additions & 11 deletions src/amount_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use blst_ringct::RevealedCommitment;
use blsttc::{DecryptionShare, IntoFr, SecretKey, SecretKeySet, SecretKeyShare, Ciphertext, PublicKey, PublicKeySet};
use std::convert::TryFrom;
use std::collections::BTreeMap;
use rand_core::OsRng;
use rand_core::RngCore;
use std::convert::Into;

use crate::{Amount, Error};
Expand Down Expand Up @@ -91,6 +91,10 @@ impl AmountSecrets {
))
}

pub fn from_amount(amount: Amount, mut rng: impl RngCore) -> Self {
Self(RevealedCommitment::from_value(amount, &mut rng))
}

/// generate a pedersen commitment
// pub fn to_pedersen_commitment(&self) -> G1Projective {
// self.0.commit(&PedersenGens::default())
Expand All @@ -103,7 +107,6 @@ impl AmountSecrets {

// generate a random blinding factor
// pub fn random_blinding_factor() -> Scalar {
// let mut csprng: OsRng = OsRng::default();
// Scalar::random(&mut csprng)
// }
}
Expand All @@ -122,15 +125,6 @@ impl Into<RevealedCommitment> for AmountSecrets {
}
}


impl From<Amount> for AmountSecrets {
/// create AmountSecrets from an amount and a randomly generated blinding factor
fn from(amount: Amount) -> Self {
let mut rng = OsRng::default();
Self(RevealedCommitment::from_value(amount, &mut rng))
}
}

impl TryFrom<(&SecretKey, &Ciphertext)> for AmountSecrets {
type Error = Error;

Expand Down
41 changes: 25 additions & 16 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use blsttc::{PublicKeySet, SignatureShare};
use std::collections::{BTreeMap, HashSet};
pub use blstrs::{G1Affine, Scalar};
pub use blst_ringct::{MlsagMaterial, Output, RevealedCommitment, TrueInput};
pub use blst_ringct::{MlsagMaterial, Output, RevealedCommitment, TrueInput, DecoyInput};
use blstrs::group::Curve;
use blst_ringct::ringct::{RingCtTransaction, RingCtMaterial};
use rand_core::OsRng;
use rand_core::RngCore;
use bulletproofs::PedersenGens;

use crate::{
Expand Down Expand Up @@ -34,21 +34,19 @@ impl TransactionBuilder {
self
}

pub fn add_input_by_secrets(mut self, secret_key: Scalar, amount_secrets: AmountSecrets) -> Self {
let mut rng = OsRng::default();
pub fn add_input_by_secrets(mut self, secret_key: Scalar, amount_secrets: AmountSecrets, decoy_inputs: Vec<DecoyInput>, mut rng: impl RngCore) -> Self {
let true_input = TrueInput {
secret_key,
revealed_commitment: amount_secrets.into(),
};

let decoy_inputs = vec![]; // todo.
self.0.inputs.push(MlsagMaterial::new(true_input, decoy_inputs, &mut rng));
self
}

pub fn add_inputs_by_secrets(mut self, secrets: Vec<(Scalar, AmountSecrets)>) -> Self {
for (secret_key, amount_secrets) in secrets.into_iter() {
self = self.add_input_by_secrets(secret_key, amount_secrets);
pub fn add_inputs_by_secrets(mut self, secrets: Vec<(Scalar, AmountSecrets, Vec<DecoyInput>)>, mut rng: impl RngCore) -> Self {
for (secret_key, amount_secrets, decoy_inputs) in secrets.into_iter() {
self = self.add_input_by_secrets(secret_key, amount_secrets, decoy_inputs, &mut rng);
}
self
}
Expand Down Expand Up @@ -79,8 +77,7 @@ impl TransactionBuilder {
self.0.outputs.iter().map(|o| o.amount).sum()
}

pub fn build(self) -> Result<(RingCtTransaction, Vec<RevealedCommitment>)> {
let rng = OsRng::default();
pub fn build(self, rng: impl RngCore + rand_core::CryptoRng) -> Result<(RingCtTransaction, Vec<RevealedCommitment>)> {
self.0.sign(rng).map_err(|e| e.into())
}
}
Expand Down Expand Up @@ -111,6 +108,7 @@ impl ReissueRequestBuilder {
}

pub fn build(&self) -> Result<ReissueRequest> {

let spent_proofs: BTreeMap<KeyImage, SpentProof> = self
.spent_proof_shares
.iter()
Expand All @@ -128,7 +126,15 @@ impl ReissueRequestBuilder {
return Err(Error::ReissueRequestPublicKeySetMismatch);
}

let spent_sig = any_share.spent_sig.clone();
if shares
.iter()
.map(|s| &s.public_commitments)
.any(|pc| *pc != any_share.public_commitments)
{
return Err(Error::ReissueRequestPublicCommitmentMismatch);
}

// let spent_sig = any_share.spent_sig.clone();
let spentbook_pub_key = any_share.spentbook_public_key();
let spentbook_sig = any_share.spentbook_pks.combine_signatures(
shares
Expand All @@ -137,13 +143,16 @@ impl ReissueRequestBuilder {
.map(NodeSignature::threshold_crypto),
)?;

let public_commitments: Vec<G1Affine> = shares
.iter()
.flat_map(|s| s.public_commitments.clone())
.collect();
let public_commitments: Vec<G1Affine> = any_share.public_commitments.clone();

let index = match self.transaction.mlsags.iter().position(|m| m.key_image.to_compressed() == *key_image) {
Some(idx) => idx,
None => return Err(Error::SpentProofKeyImageMismatch),
};

let spent_proof = SpentProof {
spent_sig,
index,
// spent_sig,
spentbook_pub_key,
spentbook_sig,
public_commitments,
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ pub enum Error {
#[error("Dbc Content parents is not the same transaction inputs")]
DbcContentParentsDifferentFromTransactionInputs,

#[error("The number of SpentProof does not match the number of input MlsagSignature")]
SpentProofInputMismatch,

#[error("The SpentProof key-image is not found amongst transaction inputs")]
SpentProofKeyImageMismatch,

#[error("The PublicKeySet differs between ReissueRequest entries")]
ReissueRequestPublicKeySetMismatch,

#[error("The Public Commitments differ between ReissueRequest entries")]
ReissueRequestPublicCommitmentMismatch,

#[error("We need at least one spent proof share for {0:?} to build a SpentProof")]
ReissueRequestMissingSpentProofShare(KeyImage),

Expand Down
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,26 @@ impl AsRef<[u8]> for Hash {
}

#[cfg(feature = "dkg")]
pub fn bls_dkg_id() -> bls_dkg::outcome::Outcome {
use rand::RngCore;

#[cfg(feature = "dkg")]
pub fn bls_dkg_id(mut rng: impl RngCore) -> bls_dkg::outcome::Outcome {
use std::collections::BTreeSet;
use std::iter::FromIterator;

let owner_name = rand::random();
let mut owner_name = [0u8; 32];
rng.fill_bytes(&mut owner_name);
let owner_xorname = xor_name::XorName::from_content(&owner_name);

let threshold = 0;
let (mut key_gen, proposal) =
bls_dkg::KeyGen::initialize(owner_name, threshold, BTreeSet::from_iter([owner_name]))
bls_dkg::KeyGen::initialize(owner_xorname, threshold, BTreeSet::from_iter([owner_xorname]))
.expect("Failed to init key gen");

let mut msgs = vec![proposal];
while let Some(msg) = msgs.pop() {
let response_msgs = key_gen
.handle_message(&mut rand::thread_rng(), msg)
.handle_message(&mut rng, msg)
.expect("Error while generating BLS key");

msgs.extend(response_msgs);
Expand All @@ -109,9 +115,11 @@ pub struct DbcHelper {}
#[cfg(feature = "dkg")]
impl DbcHelper {

#[allow(dead_code)]
pub(crate) fn blsttc_to_blstrs_sk(sk: SecretKey) -> Scalar {
let bytes = sk.to_bytes();
Scalar::from_bytes_le(&bytes).unwrap()
println!("sk bytes: {:?}", bytes);
Scalar::from_bytes_be(&bytes).unwrap()
}

pub(crate) fn blsttc_to_blstrs_pubkey(pk: &PublicKey) -> G1Affine {
Expand Down
Loading

0 comments on commit d0e29f3

Please sign in to comment.