Skip to content

Commit

Permalink
feat: integrate blsttc+blstrs
Browse files Browse the repository at this point in the history
This commit integrates Ian Coleman's recent branch of blsttc that uses
blstrs instead of bls12_381.  blsttc now implements SecretKey and
PublicKey as newtype wrappers around blstrs::Scalar and
blstrs::G1Affine respectively.

It also depends on changes added by Anselme and myself to blsttc,
ringct and their dependencies.  These changes primarily provide From
impls for the blsttc newtype wrappers so that blsttc keys can be used
cleanly with ringct functons that accept Into<Scalar> and
Into<PublicKey>.

Some notable sn_dbc changes:

* we only use blsttc keys, no more Scalar/G1Affine.
* Amount is now defined in ringct and re-exported in sn_dbc.
* get rid of rand7, now rand8 everywhere
* use ringct::Output::new() and TrueInput::new()
* gets rid of BlsHelper, SecretKeyBlst, PublicKeyBlst
  • Loading branch information
dan-da committed Mar 22, 2022
1 parent 56eeb6d commit ae4d0ca
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 242 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ ringct-serde = [ "blst_ringct/serde" ]
thiserror = "1.0.24"
quickcheck_macros = "1"
rand = "0.7.1"
blsttc = "3.3.0"
# blsttc = "3.3.0"
blsttc = {git = "https://github.com/dan-da/blsttc", branch = "sn_dbc_integration"}
hex = "0.4.3"
rand_core = "0.6.3"
xor_name = "3.1.0"
# xor_name = "3.1.0"
xor_name = {git = "https://github.com/iancoleman/xor_name", branch = "remove_osrng"}

[dependencies.quickcheck]
git = "https://github.com/davidrusu/quickcheck.git"
Expand All @@ -43,7 +45,9 @@ xor_name = "3.1.0"
version = "0.8.0"

[dependencies.bls_dkg]
version = "~0.9"
git = "https://github.com/dan-da/bls_dkg.git"
branch = "sn_dbc_integration"
version = "~0.9.1"
optional = true

[dependencies.tiny-keccak]
Expand Down
49 changes: 18 additions & 31 deletions benches/reissue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,32 @@ use sn_dbc::{
use blst_ringct::Output;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::SeedableRng;
use rand8::SeedableRng as SeedableRng8;

const N_OUTPUTS: u32 = 100;

fn bench_reissue_1_to_100(c: &mut Criterion) {
let mut rng8 = rand8::rngs::StdRng::from_seed([0u8; 32]);
let mut rng = rand::rngs::StdRng::from_seed([0u8; 32]);

let (mintnode, mut spentbook, starting_dbc) =
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng, &mut rng8).unwrap();
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng8).unwrap();

let (mut rr_builder, ..) = sn_dbc::TransactionBuilder::default()
.add_input_by_secrets(
starting_dbc
.owner_once_bearer()
.unwrap()
.secret_key_blst()
.secret_key()
.unwrap(),
starting_dbc.amount_secrets_bearer().unwrap(),
vec![], // never any decoys for genesis
&mut rng8,
)
.add_outputs((0..N_OUTPUTS).into_iter().map(|_| {
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng8);
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);
(
Output {
amount: 1,
public_key: owner_once.as_owner().public_key_blst(),
},
Output::new(owner_once.as_owner().public_key(), 1),
owner_once,
)
}))
Expand All @@ -68,31 +63,27 @@ fn bench_reissue_1_to_100(c: &mut Criterion) {

fn bench_reissue_100_to_1(c: &mut Criterion) {
let mut rng8 = rand8::rngs::StdRng::from_seed([0u8; 32]);
let mut rng = rand::rngs::StdRng::from_seed([0u8; 32]);
let num_decoys = 0;

let (mintnode, mut spentbook, starting_dbc) =
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng, &mut rng8).unwrap();
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng8).unwrap();

let (mut rr_builder, mut dbc_builder, ..) = sn_dbc::TransactionBuilder::default()
.add_input_by_secrets(
starting_dbc
.owner_once_bearer()
.unwrap()
.secret_key_blst()
.secret_key()
.unwrap(),
starting_dbc.amount_secrets_bearer().unwrap(),
vec![], // never any decoy inputs for genesis
&mut rng8,
)
.add_outputs((0..N_OUTPUTS).into_iter().map(|_| {
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng8);
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);
(
Output {
amount: 1,
public_key: owner_once.as_owner().public_key_blst(),
},
Output::new(owner_once.as_owner().public_key(), 1),
owner_once,
)
}))
Expand All @@ -111,14 +102,14 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
let dbcs = dbc_builder.build(mintnode.key_manager()).unwrap();

let output_owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng8);
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);

let (mut merge_rr_builder, ..) = sn_dbc::TransactionBuilder::default()
.add_inputs_by_secrets(
dbcs.into_iter()
.map(|(_dbc, owner_once, amount_secrets)| {
(
owner_once.as_owner().secret_key_blst().unwrap(),
owner_once.as_owner().secret_key().unwrap(),
amount_secrets,
spentbook.random_decoys(num_decoys, &mut rng8),
)
Expand All @@ -127,10 +118,10 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
&mut rng8,
)
.add_output(
Output {
amount: N_OUTPUTS as Amount,
public_key: output_owner_once.as_owner().public_key_blst(),
},
Output::new(
output_owner_once.as_owner().public_key(),
N_OUTPUTS as Amount,
),
output_owner_once,
)
.build(&mut rng8)
Expand All @@ -151,28 +142,24 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {

fn generate_dbc_of_value(
amount: Amount,
rng: &mut impl rand::RngCore,
rng8: &mut (impl rand8::RngCore + rand_core::CryptoRng),
) -> Result<(MintNode<SimpleKeyManager>, SpentBookNodeMock, Dbc)> {
let (mint_node, mut spentbook_node, genesis_dbc, _genesis_material, _amount_secrets) =
GenesisBuilderMock::init_genesis_single(rng, rng8)?;
GenesisBuilderMock::init_genesis_single(rng8)?;

let output_amounts = vec![amount, sn_dbc::GenesisMaterial::GENESIS_AMOUNT - amount];

let (mut rr_builder, mut dbc_builder, _material) = sn_dbc::TransactionBuilder::default()
.add_input_by_secrets(
genesis_dbc.owner_once_bearer()?.secret_key_blst()?,
genesis_dbc.owner_once_bearer()?.secret_key()?,
genesis_dbc.amount_secrets_bearer()?,
vec![], // never any decoys for genesis
rng8,
)
.add_outputs(output_amounts.into_iter().map(|amount| {
let owner_once = OwnerOnce::from_owner_base(Owner::from_random_secret_key(rng), rng8);
let owner_once = OwnerOnce::from_owner_base(Owner::from_random_secret_key(rng8), rng8);
(
Output {
amount,
public_key: owner_once.as_owner().public_key_blst(),
},
Output::new(owner_once.as_owner().public_key(), amount),
owner_once,
)
}))
Expand Down
17 changes: 5 additions & 12 deletions examples/mint-repl/mint-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ fn verify(mintinfo: &MintInfo) -> Result<()> {

/// Implements prepare_tx command.
fn prepare_tx(mintinfo: &MintInfo) -> Result<RingCtTransactionRevealed> {
let mut rng = rand::thread_rng();
let mut rng8 = rand8::thread_rng();
let mut tx_builder: TransactionBuilder = Default::default();

Expand Down Expand Up @@ -668,7 +667,7 @@ fn prepare_tx(mintinfo: &MintInfo) -> Result<RingCtTransactionRevealed> {
Some(Owner::from(public_key))
}
},
"r" => Some(Owner::from_random_secret_key(&mut rng)),
"r" => Some(Owner::from_random_secret_key(&mut rng8)),
"c" => return Err(anyhow!("Cancelled")),
_ => None,
};
Expand All @@ -680,10 +679,7 @@ fn prepare_tx(mintinfo: &MintInfo) -> Result<RingCtTransactionRevealed> {
let owner_once = OwnerOnce::from_owner_base(owner_base, &mut rng8);

tx_builder = tx_builder.add_output(
Output {
amount,
public_key: owner_once.as_owner().public_key_blst(),
},
Output::new(owner_once.as_owner().public_key(), amount),
owner_once,
);

Expand Down Expand Up @@ -819,13 +815,10 @@ fn reissue_auto_cli(mintinfo: &mut MintInfo) -> Result<()> {
let amount = rng.gen_range(0, range_max);

let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng), &mut rng8);
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);

tx_builder = tx_builder.add_output(
Output {
amount,
public_key: owner_once.as_owner().public_key_blst(),
},
Output::new(owner_once.as_owner().public_key(), amount),
owner_once,
);
}
Expand Down Expand Up @@ -909,7 +902,7 @@ fn reissue(mintinfo: &mut MintInfo, reissue_request: ReissueRequestRevealed) ->

/// Makes a new random SecretKeySet
fn mk_secret_key_set(threshold: usize) -> Result<(Poly, SecretKeySet)> {
let mut rng = rand::thread_rng();
let mut rng = rand8::thread_rng();
let poly = Poly::try_random(threshold, &mut rng).map_err(|e| anyhow!(e))?;
Ok((poly.clone(), SecretKeySet::from(poly)))
}
Expand Down
6 changes: 4 additions & 2 deletions src/amount_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// 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 crate::{BlindingFactor, Error};
use blst_ringct::RevealedCommitment;
use blsttc::{
Ciphertext, DecryptionShare, IntoFr, PublicKey, PublicKeySet, SecretKey, SecretKeySet,
Expand All @@ -15,7 +16,8 @@ use rand_core::RngCore;
use std::collections::BTreeMap;
use std::convert::TryFrom;

use crate::{Amount, BlindingFactor, Error, SecretKeyBlst};
// we re-export this.
pub use blst_ringct::ringct::Amount;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -43,7 +45,7 @@ impl AmountSecrets {
}

/// blinding factor getter
pub fn blinding_factor(&self) -> SecretKeyBlst {
pub fn blinding_factor(&self) -> BlindingFactor {
self.0.blinding
}

Expand Down
62 changes: 30 additions & 32 deletions src/blst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ use blstrs::{G1Affine, G1Projective, Scalar};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};

use blsttc::{PublicKey, SecretKey};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// a SecretKey in Blst format
pub type SecretKeyBlst = Scalar;
// pub type SecretKeyBlst = Scalar;

/// a PublicKey in Blst format
pub type PublicKeyBlst = G1Affine;
// pub type PublicKeyBlst = G1Affine;

/// a Commitment
pub type Commitment = G1Affine;
Expand Down Expand Up @@ -106,31 +104,31 @@ impl From<G1Affine> for PublicKeyBlstMappable {
// For this reason, we allow unwrap() in these methods so that
// it doesn't force higher APIs to return a Result when they should
// not need to do so once the integration is complete.
pub struct BlsHelper {}

impl BlsHelper {
#[allow(dead_code)]
pub fn blsttc_to_blstrs_secret_key(sk: SecretKey) -> SecretKeyBlst {
let bytes = sk.to_bytes();
// fixme: unwrap
SecretKeyBlst::from_bytes_be(&bytes).unwrap()
}

pub fn blsttc_to_blstrs_public_key(pk: &PublicKey) -> PublicKeyBlst {
let bytes = pk.to_bytes();
// fixme: unwrap
PublicKeyBlst::from_compressed(&bytes).unwrap()
}

pub fn blstrs_to_blsttc_public_key(pk: &PublicKeyBlst) -> PublicKey {
let bytes = pk.to_compressed();
// fixme: unwrap
PublicKey::from_bytes(bytes).unwrap()
}

pub fn blstrs_to_blsttc_secret_key(sk: SecretKeyBlst) -> SecretKey {
let bytes = sk.to_bytes_be();
// fixme: unwrap
SecretKey::from_bytes(bytes).unwrap()
}
}
// pub struct BlsHelper {}

// impl BlsHelper {
// #[allow(dead_code)]
// pub fn blsttc_to_blstrs_secret_key(sk: SecretKey) -> SecretKeyBlst {
// let bytes = sk.to_bytes();
// // fixme: unwrap
// SecretKeyBlst::from_bytes_be(&bytes).unwrap()
// }

// pub fn blsttc_to_blstrs_public_key(pk: &PublicKey) -> PublicKeyBlst {
// let bytes = pk.to_bytes();
// // fixme: unwrap
// PublicKeyBlst::from_compressed(&bytes).unwrap()
// }

// pub fn blstrs_to_blsttc_public_key(pk: &PublicKeyBlst) -> PublicKey {
// let bytes = pk.to_compressed();
// // fixme: unwrap
// PublicKey::from_bytes(bytes).unwrap()
// }

// pub fn blstrs_to_blsttc_secret_key(sk: SecretKeyBlst) -> SecretKey {
// let bytes = sk.to_bytes_be();
// // fixme: unwrap
// SecretKey::from_bytes(bytes).unwrap()
// }
// }
Loading

0 comments on commit ae4d0ca

Please sign in to comment.