Skip to content

Commit

Permalink
feat: use safer bulletproofs instead of bls_bulletproofs
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach authored and davidrusu committed Feb 8, 2023
1 parent 02eace0 commit e6cb255
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 85 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ mock = [ ]
[dependencies]
bincode = "1.3.3"
blsttc = "8.0.1"
bls_bulletproofs = "1.1.1"
bulletproofs = "4.0.0"
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4.1.1" }
hex = "0.4.3"
merlin = "3.0.0"
thiserror = "1.0.24"

[dependencies.serde]
Expand Down
18 changes: 9 additions & 9 deletions src/amount_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use crate::transaction::{Amount, RevealedCommitment};
use crate::{rand::RngCore, BlindingFactor, Error, Token};
use blsttc::rand::CryptoRng;
use blsttc::{
Ciphertext, DecryptionShare, IntoFr, PublicKey, PublicKeySet, SecretKey, SecretKeySet,
SecretKeyShare,
Expand Down Expand Up @@ -57,11 +58,10 @@ impl AmountSecrets {
b
});
let mut b = [0u8; BF_SIZE];
let blinding_factor = BlindingFactor::from_bytes_le({
let blinding_factor = BlindingFactor::from_bytes_mod_order({
b.copy_from_slice(&bytes[AMT_SIZE..]);
&b
})
.unwrap();
b
});

Self(RevealedCommitment {
value: amount,
Expand All @@ -80,11 +80,11 @@ impl AmountSecrets {
b
});
let mut b = [0u8; BF_SIZE];
let blinding_factor = BlindingFactor::from_bytes_le({
let blinding_factor = BlindingFactor::from_bytes_mod_order({
b.copy_from_slice(&bytes[AMT_SIZE..]);
&b
})
.unwrap();
b
});

Ok(Self(RevealedCommitment {
value: amount,
blinding: blinding_factor,
Expand All @@ -93,7 +93,7 @@ impl AmountSecrets {

/// build AmountSecrets from an Amount.
/// A blinding factor will be randomly generated.
pub fn from_amount(amount: Amount, rng: &mut impl RngCore) -> Self {
pub fn from_amount(amount: Amount, rng: &mut (impl RngCore + CryptoRng)) -> Self {
Self(RevealedCommitment::from_value(amount, rng))
}

Expand Down
4 changes: 2 additions & 2 deletions src/blst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! sn_dbc internally uses the type aliases rather than directly using the blstrs types.
/// a Commitment
pub type Commitment = bls_bulletproofs::blstrs::G1Affine;
pub type Commitment = curve25519_dalek::ristretto::RistrettoPoint;

/// a BlindingFactor
pub type BlindingFactor = bls_bulletproofs::blstrs::Scalar;
pub type BlindingFactor = curve25519_dalek::scalar::Scalar;
2 changes: 1 addition & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// 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 bls_bulletproofs::PedersenGens;
use blsttc::{PublicKey, SecretKey};
use bulletproofs::PedersenGens;
use std::{
borrow::Borrow,
collections::{BTreeMap, BTreeSet, HashSet},
Expand Down
4 changes: 2 additions & 2 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ pub(crate) mod tests {
rand::{CryptoRng, RngCore},
AmountSecrets, DbcBuilder, Hash, Owner, OwnerOnce, SpentProofContent, Token,
};
use bls_bulletproofs::PedersenGens;
use blsttc::PublicKey;
use bulletproofs::PedersenGens;
use quickcheck_macros::quickcheck;
use std::convert::TryInto;

Expand All @@ -338,7 +338,7 @@ pub(crate) mod tests {
})
}

const DBC_WITH_1_530_000_000: &str = "00000000000000000000000000000000b1d460d064d48592edc071c0ee40966eec954cdc0151a40d4445240a256cf73498da8f7693a58869079da8789403789652de1fc623c1b727925b5c0bbafcc3c06e6c7f2eda587320597c76ec0a797d15313ac18c9b4c4364789bc8e033c33c6512a659bf6be3166ff0c4a5f47cf2dfee2747b0a24172061926835e77aa2f4b415b04c1117c39011a66d4e68c53d3ab86f57a6237391374eca465d85e5a15c7b7df3484ac6879e96bbdc25a7355585af69ec5b10ef0585ff2e7b9bf4d2225bfa7c8c17974223995561e460ce524b9b5b5d5a70dbe88e7a9c08a05961d5430ccea53a5ed5a6bd01e151152ec5952a8abc35e926031239f426b3018fbd470d82f936bbb66b70284b8b45151d65491ab7b8474ad4eefd916c107f40cc8cb6a222ea57569640b96a75f45f78f9960990bef82192693271679e9b3cb859b9252bc76171cd5a55f2fafcd399249c7bd6a1e674e563114e312208b350aa99c6d30036fa66bce803dcd69d197701a19563d8a8d8420ed9967714ce8a17613dbe765e135f0429f0c05a0745e24162e492fe2ceb8b8b2ec07abf0e8f451dab46b2d85d00545beaefebf7be2d88556f32091c4cc255016e0203109dbfff4a2100435e26a7c920c9fc63fffac221bc44aca14557b87c830e0be5862d3ff279c7722c3722d46b34b87abd868501ce92cd105bf58676da1cc9224667ff1a3415d3fe55784788a4881cd0830664ba6270b4f1336f1917e848a6719609b84de20ff868d2f18ad65949ce3d1faa1c63e274a0ccd151bd826c02f53bbc62b6f6fa54232892840e884cefe3ee2e73db31a8825d98f0bfc1c1e9543fc7e50b98c9be093dda808672957847b7b8589a69de583d242ac096c73e4026ef8fd2edcc6775a17e97331f3ee3880122e83ff1048543e1c6f1145d17c950afaaba67cfa8bb8da0b4b8ca42e65f8b5c12988d5df261e3441023678e7958fb44cf33011912487e888ceab7909efd9ae6e100e042c2cfc027aaeed187a542f1958adbb52e193364171f1914b3b699b5f663a4d664d792e5aa36825feb9dd7c0c07d46094811b89903b5740fc71f662ea3a5df377e62458025e0a45f7e974ef786d0699b9b8d10d10f99ae127ef3fd649dd018fc2414b161840fa617aed0d03703d2ccb6e0841a3b75bc313eca9547587abbd7985f6714c6f9b409ee4792555523585c69dd3b8f6926d9e4a62456812cbebec89dd0d668831f19b88230f89dfb22bbecdc534cd3964c5aad33984614351e0342278e99a836bc5d1fac4ca5b5ab696f09c3461e51cae3f238679fb9002b7106300eefe46e093fc41ad3ef2d494306ab526d679f39c8610174ea4fc8f3a867b5f9e3fb1d3e957b119269b3080bd9000000000000003a0e1efaa9f098cf6af927d2a03b6b6735e23725f60a6412123f4b7c08c42e8d8067a2e00fd8c3b3af67d2929e1f7c739a300000000000000010000000000000000aef10b40280f6ff9b27cb4ff7c11211c3eac99bf8b4bfc8606fe3991a394dec3673ecb383ca63491b8ac1b294193250bd2d5fe3d8036ec140ee420adf9ebb9e56be5ef3824f3173f4f1a045bd9a082155986ac0305d269dc0fe32c2037b11bada69121f9ff8da8484f91f4ad6f00b63ef04d4d3985c93404411b052a3a6e549e5ba198863c9413bf00000000000000289dde07f9368f6e9e494bcbe35fffcef22ecfeb2beffa55af98778d08f90641fa3ffd5d32f15afec63fb9de44d1e0938104252cf84d1200cb64ba05ad272f018f0eeefba14bd1cea0d89bd80c14ff556db42c89f91cc98bfa4ff6df206bcb40104b45e2f77bb349d163af5a133277a36a14b8040110ff0810bd012a3d3f569df3e92d63e922b29fad6ce4236c9eb670aafe7222f907f13873bfb5a2d4bd2101598c21a894c2855e8bad5fdfaee4713074000000000000002069c61dadffc05c86fdf098afd3bbd96ec5fed102096f8f64329371a13441bb1afd61599d62ee6925181e2d5859eb2eb79bf49a6a0755f953811fce125f2683d50429c3bb49e074147e0089a52eae155f00000000";
const DBC_WITH_1_530_000_000: &str = "0000000000000000000000000000000025eb68773f856c5ff656e2f289cdce64228153754f733b753a3de2ddffd7af50077b2553ba337185d995753ebadf332a4173294a2776e2f36abd358c67f19e520e099a59502e76d6c7f71b436c3907464dc6aad4be7df774dc0c53cf3571c119244eddf689f66a51cac72bfe02277567e797751d090b6a53f07c2f0e1e78490a591349167bb1d5468fbf3c5be46040ee607a6ba5d84fd636fc73eaf8eb37b7141a574dff81b8f1ebae78107c3bd448cb18e79465cc280206379333aad73024e67b51e6eab0f1b4bf2c6545726bd4189047b68738b57eb3f188e882adb9f1e1b6275b12303bc37f01398a3599ccb6e4f4863c3723c3ed06255e488d4d0ca47a886895bf6fc208b5f2a5eaae27f2acbf77778e9b93be252f446c9a99621560ca724fb372a72883006a2d5a77894b41ff5fd44eb947cc4603febe9a463e1a944d0625c02b538baec65ac0a53d4cdf53b01f355f4a16f42132c432871b467cef1dd42c5d01f553ca77042f5d8438f8686328b62fd41f31ad3c0820827c24e703f3c24655cda7554621adf9dbbdb6f24640c8c71587848424f76bc9806414fc1a9454799a11d29318818a3c0ee6c8b9d9919a0205b78dee47658c6a364df07ebec59222e6700ccab6319649f7693f4af4c5c338e8131083a05d69dcba0f7c6c2692a40835058f6dfd23de1084343a2499ee6cbef872e960e7984aefdef93c645462c1037c62fdb1807fe19ce81c9340350913c957a613bef5592bc4cad21e45f3e14b0820b926e05b0ea11aad2b47a261ee7ce89c119f106dd9b235fc1b84323599a74b3c824ec8e300c3e3c2755af2af01b4b23046fa44d62cd36e5faa7c7d8b870a09be1cf1cc22682abf6fbfcb08d29562c9538d077c4a018a68a7254ce9cf44ac14aac406df62a31a49f0356485340733422c1027ee253c52287267b361d0404472821f7cf3d086b5fa8877f1d918699c368a6f5588452d517f436213d0104cb000000000000002a0e1efaa9f098cf6af927d2a03b6b6735e23725f60a6412123f4b7c08c42e8d8067a2e00fd8c3b3af67d2929e1f7c739a300000000000000010000000000000000a766ef7f2bdde5e7ccfb0cbb48e7efe8fb481bcebf1038242941ceedc9f4d2b9f486ca4ace93568c85abddb768219a0180abf10f3cb52422b019c2d750220ce841ca1c95d18b08b842fa64f627fce44a1aa506b0bcceea2b548a259db10555b879e5af5f3789f50da03334ed5ff263fb448386d140ad5343e41706b0f22b8c6b406202d50bb3babf00000000000000282c952c35ddc9af689cb0930c9ed8e8bda6aef8601f5650cddb9f8d60ae6ac1a8f4a3f2f9b66e20d864e4d0fcc5ebd8b5104926e3ff9a703f0525b46a42c91df892902cf2b28e6369e7b966dbdb6753f11aaa63bef27e79f4b453ee32bbb2c60bd9c328f79fefd147973bc842119c6f88ee60ec9060eb3e7f3727972411bb1f4ae744462baf8374a1c50893aea2dfa78c4271a698c25092c88577a0e815f511147a5ea1b41d50e73640035974a90e4f8400000000000000209c83fb66a1d6f0f7747dd902159faa4ab7bbbc2c847e904999b2c71349f5fa556a3fa9f3930a97f4eef81000ccb4ecac9bf49a6a0755f953811fce125f2683d50429c3bb49e074147e0089a52eae155f00000000";

fn prepare_even_split(
dbc_owner: SecretKey,
Expand Down
9 changes: 1 addition & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ pub mod mock;

// re-export crates used in our public API
pub use blsttc;
// note: both bls_bulletproofs::rand and blsttc::rand are
// exposed in our public API. Here, by choosing
// just one, we are making an implicit promise that
// the two versions will remain compatible, or that
// our API will reconcile the difference. We do
// this knowingly and pledge to uphold that promise.
pub use bls_bulletproofs::rand;

pub use blsttc::rand;
pub use blsttc::{PublicKey, PublicKeySet, Signature, SignatureShare};

pub use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/mock/genesis_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Default for GenesisMaterial {
input_sk,
RevealedCommitment {
value: Self::GENESIS_AMOUNT,
blinding: 42.into(), // just a random number
blinding: 42u32.into(), // just a random number
},
);
let input_public_key: PublicKey = revealed_input.public_key();
Expand Down
2 changes: 1 addition & 1 deletion src/mock/spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::transaction::{DbcTransaction, OutputProof};
use bls_bulletproofs::PedersenGens;
use blsttc::PublicKey;
use bulletproofs::PedersenGens;
use std::collections::{BTreeMap, HashMap};

use super::GenesisMaterial;
Expand Down
2 changes: 1 addition & 1 deletion src/spent_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl SpentProofContent {

bytes.extend(self.public_key.to_bytes());
bytes.extend(self.transaction_hash.as_ref());
bytes.extend(self.public_commitment.to_compressed());
bytes.extend(self.public_commitment.compress().to_bytes());
bytes
}

Expand Down
8 changes: 3 additions & 5 deletions src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@

use thiserror::Error;

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

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq, Error)]
pub enum Error {
#[error("Failed to decompress commitment")]
FailedToDecompressCommitment,
#[error("The commitment in the input doesn't match the public commitment")]
InvalidCommitment,
#[error("InputPseudoCommitmentsDoNotSumToOutputCommitments")]
InputPseudoCommitmentsDoNotSumToOutputCommitments,
#[error("The signature is not valid")]
InvalidSignature,
#[error("BulletProofs Error: {0}")]
BulletProofs(#[from] bls_bulletproofs::ProofError),
BulletProofs(#[from] bulletproofs::ProofError),
#[error("The DBC transaction must have at least one input")]
TransactionMustHaveAnInput,
#[error("public key is not unique across all transaction inputs")]
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use bls_bulletproofs::{group::GroupEncoding, PedersenGens};
use blsttc::{PublicKey, SecretKey, Signature};
use bulletproofs::PedersenGens;

#[cfg(feature = "serde")]
use serde::{self, Deserialize, Serialize};
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Input {
pub fn to_bytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = Default::default();
v.extend(self.public_key.to_bytes().as_ref());
v.extend(self.commitment.to_bytes().as_ref());
v.extend(self.commitment.compress().as_bytes());
v.extend(self.signature.to_bytes().as_ref());
v
}
Expand Down
16 changes: 6 additions & 10 deletions src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ mod output;

use crate::{BlindingFactor, Commitment};

use bls_bulletproofs::{
group::{ff::Field, Curve},
rand::RngCore,
PedersenGens,
};
use crate::rand::RngCore;
use blsttc::rand::CryptoRng;
use bulletproofs::PedersenGens;

pub(crate) use error::Error;
pub use input::{Input, RevealedInput};
Expand All @@ -36,22 +34,20 @@ impl RevealedCommitment {
pub fn to_bytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = Default::default();
v.extend(self.value.to_le_bytes());
v.extend(self.blinding.to_bytes_le());
v.extend(self.blinding.to_bytes());
v
}

/// Construct a revealed commitment from a value, generating a blinding randomly
pub fn from_value(value: u64, mut rng: impl RngCore) -> Self {
pub fn from_value(value: u64, mut rng: impl RngCore + CryptoRng) -> Self {
Self {
value,
blinding: BlindingFactor::random(&mut rng),
}
}

pub fn commit(&self, pc_gens: &PedersenGens) -> Commitment {
pc_gens
.commit(BlindingFactor::from(self.value), self.blinding)
.to_affine()
pc_gens.commit(BlindingFactor::from(self.value), self.blinding)
}

pub fn value(&self) -> u64 {
Expand Down
Loading

0 comments on commit e6cb255

Please sign in to comment.