Skip to content

Commit

Permalink
feat: include AmountSecrets ciphertext in DbcContent
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 26c05dd commit 90f8a01
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 82 deletions.
10 changes: 10 additions & 0 deletions src/amount_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use blsttc::{DecryptionShare, IntoFr, SecretKey, SecretKeySet, SecretKeyShare, C
use std::convert::TryFrom;
use std::collections::BTreeMap;
use rand_core::OsRng;
use std::convert::Into;

use crate::{Amount, Error};

Expand All @@ -28,6 +29,7 @@ use crate::{Amount, Error};
const AMT_SIZE: usize = 8; // Amount size: 8 bytes (u64)
const BF_SIZE: usize = 32; // Blinding factor size: 32 bytes (Scalar)

#[derive(Clone)]
pub struct AmountSecrets(RevealedCommitment);

impl AmountSecrets {
Expand Down Expand Up @@ -113,6 +115,14 @@ impl From<RevealedCommitment> for AmountSecrets {
}
}

#[allow(clippy::from_over_into)]
impl Into<RevealedCommitment> for AmountSecrets {
fn into(self) -> RevealedCommitment {
self.0
}
}


impl From<Amount> for AmountSecrets {
/// create AmountSecrets from an amount and a randomly generated blinding factor
fn from(amount: Amount) -> Self {
Expand Down
71 changes: 51 additions & 20 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use blsttc::{PublicKeySet, SignatureShare};
use std::collections::{BTreeMap, HashSet};
pub use blstrs::G1Affine;
pub use blst_ringct::{MlsagMaterial, Output, RevealedCommitment};
pub use blstrs::{G1Affine, Scalar};
pub use blst_ringct::{MlsagMaterial, Output, RevealedCommitment, TrueInput};
use blstrs::group::Curve;
use blst_ringct::ringct::{RingCtTransaction, RingCtMaterial};
use rand_core::OsRng;
use bulletproofs::PedersenGens;

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

Expand All @@ -32,6 +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();
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_output(mut self, output: Output) -> Self {
self.0.outputs.push(output);
self
Expand Down Expand Up @@ -148,14 +163,16 @@ impl ReissueRequestBuilder {
#[derive(Debug)]
pub struct DbcBuilder {
pub transaction: RingCtTransaction,
pub revealed_commitments: Vec<RevealedCommitment>,
pub reissue_shares: Vec<ReissueShare>,
}

impl DbcBuilder {
/// Create a new DbcBuilder from a ReissueTransaction
pub fn new(transaction: RingCtTransaction) -> Self {
pub fn new(transaction: RingCtTransaction, revealed_commitments: Vec<RevealedCommitment>) -> Self {
Self {
transaction,
revealed_commitments,
reissue_shares: Default::default(),
}
}
Expand Down Expand Up @@ -245,27 +262,41 @@ impl DbcBuilder {
// Combine signatures from all the mint nodes to obtain Mint's Signature.
let mint_sig = mint_public_key_set.combine_signatures(mint_sig_shares_ref)?;

let pc_gens = PedersenGens::default();
let output_commitments: Vec<(G1Affine, RevealedCommitment)> = self.revealed_commitments.iter()
.map(|r| (r.commit(&pc_gens).to_affine(), r.clone()) )
.collect();

// Form the final output DBCs, with Mint's Signature for each.
let output_dbcs: Vec<Dbc> = self
.transaction
.outputs
.iter()
.map(|proof| Dbc {
content: DbcContent {
owner: *proof.public_key(),
},
transaction: transaction.clone(),
transaction_sigs: self
.transaction
.mlsags
.iter()
.map(|mlsag| {
(
mlsag.key_image.to_compressed(),
(mint_public_key_set.public_key(), mint_sig.clone()),
)
})
.collect(),
.map(|proof| {
let amount_secrets_list: Vec<AmountSecrets> = output_commitments.iter()
.filter(|(c,_)| *c == proof.commitment())
.map(|(_, r)| AmountSecrets::from((*r).clone()))
.collect();
assert!(amount_secrets_list.len() == 1);

Dbc {
content: DbcContent::from((
*proof.public_key(),
amount_secrets_list[0].clone(),
)),
transaction: transaction.clone(),
transaction_sigs: self
.transaction
.mlsags
.iter()
.map(|mlsag| {
(
mlsag.key_image.to_compressed(),
(mint_public_key_set.public_key(), mint_sig.clone()),
)
})
.collect(),
}
})
.collect();

Expand Down
34 changes: 30 additions & 4 deletions src/dbc_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
// 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 blsttc::PublicKey;
use blsttc::{Ciphertext, PublicKey};
use blstrs::group::GroupEncoding;
use blstrs::G1Affine;
use serde::{Deserialize, Serialize};
use crate::{AmountSecrets, DbcHelper};
// use tiny_keccak::{Hasher, Sha3};

use crate::Hash;
Expand All @@ -24,16 +25,41 @@ pub type OwnerPublicKey = G1Affine;
pub struct DbcContent {
// pub owner: PublicKey,
pub owner: OwnerPublicKey, // Todo: what should this type be?
pub amount_secrets_cipher: Ciphertext,
}

/// Represents the content of a DBC.
impl From<OwnerPublicKey> for DbcContent {
impl From<(OwnerPublicKey, Ciphertext)> for DbcContent {
// Create a new DbcContent for signing.
fn from(owner: OwnerPublicKey) -> Self {
Self { owner }
fn from(params: (OwnerPublicKey, Ciphertext)) -> Self {
let (owner, amount_secrets_cipher) = params;
Self { owner, amount_secrets_cipher }
}
}

impl From<(OwnerPublicKey, AmountSecrets)> for DbcContent {
// Create a new DbcContent for signing.
fn from(params: (OwnerPublicKey, AmountSecrets)) -> Self {
let (owner, amount_secrets) = params;
let pubkey = DbcHelper::blstrs_to_blsttc_pubkey(&owner);
let amount_secrets_cipher = pubkey.encrypt(&amount_secrets.to_bytes());

Self { owner, amount_secrets_cipher }
}
}

impl From<(PublicKey, AmountSecrets)> for DbcContent {
// Create a new DbcContent for signing.
fn from(params: (PublicKey, AmountSecrets)) -> Self {
let (pubkey, amount_secrets) = params;
let amount_secrets_cipher = pubkey.encrypt(&amount_secrets.to_bytes());
let owner = DbcHelper::blsttc_to_blstrs_pubkey(&pubkey);

Self { owner, amount_secrets_cipher }
}
}


impl DbcContent {
pub fn hash(&self) -> Hash {
Hash::hash(self.owner.to_bytes().as_ref())
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use serde::{Deserialize, Serialize};
use std::fmt;
use blstrs::G1Affine;

mod builder;
mod amount_secrets;
Expand Down Expand Up @@ -105,6 +106,17 @@ pub struct DbcHelper {}

#[cfg(feature = "dkg")]
impl DbcHelper {

pub(crate) fn blsttc_to_blstrs_pubkey(pk: &PublicKey) -> G1Affine {
let bytes = pk.to_bytes();
G1Affine::from_compressed(&bytes).unwrap()
}

pub(crate) fn blstrs_to_blsttc_pubkey(pk: &G1Affine) -> PublicKey {
let bytes = pk.to_compressed();
PublicKey::from_bytes(bytes).unwrap()
}

pub fn decrypt_amount_secrets(
owner: &bls_dkg::outcome::Outcome,
ciphertext: &Ciphertext,
Expand Down
Loading

0 comments on commit 90f8a01

Please sign in to comment.