Skip to content

Commit

Permalink
Merge pull request #42 from afilini/rgb-validation-develop
Browse files Browse the repository at this point in the history
Rgb validation develop
  • Loading branch information
dr-orlovsky authored Apr 20, 2020
2 parents b1b49ae + b2f792c commit 56a9f08
Show file tree
Hide file tree
Showing 20 changed files with 1,285 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Cargo.lock
**/*.rs.bk

.idea

*.swp
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ grin_secp256k1zkp = { git = "https://github.com/lnp-bp/rust-secp256k1-zkp", bran
# bitcoin = { version = "0.23.0", features = [ "use-serde" ] }
rand = "0.5" # Required by grin_secp256k1zkp
derive_wrapper = "0.1.3"
num-integer = "0.1.42"
num-traits = "0.2.11"
num-derive = "0.3.0"
tokio = { version = "~0.2", features = ["tcp"], optional = true }
Expand All @@ -36,6 +35,7 @@ parse_arg = { version = "0.1.4", optional = true }
# This strange naming is a workaround for not being able to define required features for a dependency
# See https://github.com/rust-lang/api-guidelines/issues/180 for the explanation and references.
serde_crate = { package = "serde", version = "1.0.106", features = ["derive"], optional = true }
petgraph = { version = "0.5", optional = true }

[features]
default = []
Expand All @@ -45,7 +45,7 @@ use-log = ["log"]
use-tor = ["torut/v3"]
use-tokio = ["use-lightning", "tokio/tcp", "lightning-net-tokio"]
use-bulletproofs = ["grin_secp256k1zkp"]
use-rgb = ["use-bulletproofs"]
use-rgb = ["use-bulletproofs", "petgraph"]
use-api = ["zmq"]
use-daemons = ["async-trait", "use-api"]
use-lightning = ["lightning"]
Expand Down
2 changes: 1 addition & 1 deletion src/bp/blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bitcoin::hashes::{Hash, HashEngine, sha256d};

/// Data required to generate or reveal the information about blinded
/// transaction outpoint
#[derive(Clone, PartialEq, PartialOrd, Debug, Display, Default)]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Debug, Display, Default)]
#[display_from(Debug)]
pub struct OutpointReveal {
/// Blinding factor preventing rainbow table bruteforce attack based on
Expand Down
2 changes: 1 addition & 1 deletion src/bp/short_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Descriptor {
}


#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Display)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, Display)]
#[display_from(Debug)]
pub struct ShortId(u64);

Expand Down
8 changes: 7 additions & 1 deletion src/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ macro_rules! bytes {

#[macro_export]
macro_rules! map {
{ } => {
{
::std::collections::HashMap::new()
}
};

{ $($key:expr => $value:expr),+ } => {
{
let mut m = ::std::collections::HashMap::new();
Expand Down Expand Up @@ -59,4 +65,4 @@ macro_rules! hlist {
m
}
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#![feature(arbitrary_enum_discriminant)]
#![feature(bool_to_option)]
#![feature(str_strip)]
#![feature(bindings_after_at)]

// Coding conventions
#![deny(non_upper_case_globals)]
Expand All @@ -38,7 +39,6 @@
#[macro_use]
pub extern crate derive_wrapper;
extern crate rand;
extern crate num_integer;
extern crate num_derive;
extern crate num_traits;
#[macro_use]
Expand Down
69 changes: 68 additions & 1 deletion src/rgb/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.


pub mod amount {
use std::ops::Add;
use rand;

// We do not import particular modules to keep aware with namespace prefixes that we do not use
// the standard secp256k1zkp library
use secp256k1zkp::*;
pub use secp256k1zkp::pedersen::Commitment as PedersenCommitment;

// TODO: Convert Amount into a wrapper type later
//wrapper!(Amount, _AmountPhantom, u64, doc="64-bit data for amounts");
Expand All @@ -32,6 +36,14 @@ pub mod amount {
#[display_from(Debug)]
pub struct Proof(secp256k1zkp::key::SecretKey);

impl std::ops::Deref for Proof {
type Target = secp256k1zkp::key::SecretKey;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive(Clone, PartialEq, Debug, Display)]
#[display_from(Debug)]
pub struct Confidential {
Expand All @@ -57,15 +69,70 @@ pub mod amount {
}
}
}

pub fn commit_last_item(amount: Amount, blinding_factors: Vec<secp256k1zkp::key::SecretKey>) -> Confidential {
// TODO: refactor duplicated code

let secp = secp256k1zkp::Secp256k1::with_caps(ContextFlag::Commit);
let blinding = secp.blind_sum(vec![secp256k1zkp::key::ONE_KEY], blinding_factors).unwrap(); // FIXME: that's probably broken, but it works

let value = amount;
let commitment = secp.commit(value, blinding.clone())
.expect("Internal inconsistency in Grin secp256k1zkp library Pedersen commitments");
let bulletproof = secp.bullet_proof(
value, blinding.clone(),
blinding.clone(), blinding.clone(),
None, None
);
Confidential {
commitment: Commitment { commitment, bulletproof },
proof: Proof(blinding)
}
}

pub fn zero_pedersen_commitment() -> PedersenCommitment {
let secp = secp256k1zkp::Secp256k1::with_caps(ContextFlag::Commit);

secp
.commit_value(0)
.expect("Internal inconsistency in Grin secp256k1zkp library Pedersen commitments")
}

impl Add<pedersen::Commitment> for Commitment {
type Output = pedersen::Commitment;

fn add(self, other: pedersen::Commitment) -> Self::Output {
let secp = secp256k1zkp::Secp256k1::with_caps(ContextFlag::Commit);

secp
.commit_sum(vec![self.commitment, other], vec![])
.expect("Failed to add Pedersen commitments")
}
}

pub fn verify_bullet_proof(commitment: &Commitment) -> Result<pedersen::ProofRange, secp256k1zkp::Error> {
let secp = secp256k1zkp::Secp256k1::with_caps(ContextFlag::Commit);

secp.
verify_bullet_proof(commitment.commitment.clone(), commitment.bulletproof.clone(), None)
}

pub fn verify_commit_sum(positive: Vec<pedersen::Commitment>, negative: Vec<pedersen::Commitment>) -> bool {
let secp = secp256k1zkp::Secp256k1::with_caps(ContextFlag::Commit);

secp.
verify_commit_sum(positive, negative)
}
}

pub use amount::Amount;
pub use amount::{Amount, PedersenCommitment};

#[non_exhaustive]
#[derive(Clone, PartialEq, Debug, Display)]
#[display_from(Debug)]
pub enum Data {
Balance(amount::Commitment),
Binary(Box<[u8]>),
None,
// TODO: Add other supported bound state types according to the schema
}
Loading

0 comments on commit 56a9f08

Please sign in to comment.