Skip to content

Commit

Permalink
chore(core): remove commitment, re-organize types again
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Jul 26, 2024
1 parent 60fdebd commit 301b2db
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 273 deletions.
175 changes: 0 additions & 175 deletions core/src/crypto/commitment.rs

This file was deleted.

40 changes: 17 additions & 23 deletions core/src/crypto/dh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use rand::rngs::OsRng;

use super::*;
use crate::error::Error;

pub use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use crate::{error::Error, types::*, G};

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
Expand All @@ -15,23 +12,20 @@ pub struct DLEQProof {
s: Scalar,
}

pub fn blind(secret_message: &[u8]) -> (RistrettoPoint, Scalar, RistrettoPoint) {
pub fn blind(secret_message: &[u8]) -> (Point, Scalar, Point) {
let y = hash_to_curve(secret_message);
let r = Scalar::random(&mut OsRng);
let b_prime = y + (RISTRETTO_BASEPOINT_POINT * r);
let b_prime = y + (*G * r);
(y, r, b_prime)
}

pub fn sign_blinded(
private_key: &Scalar,
blinded_point: &RistrettoPoint,
) -> (RistrettoPoint, DLEQProof) {
pub fn sign_blinded(private_key: &Scalar, blinded_point: &Point) -> (Point, DLEQProof) {
let signed_point = blinded_point * private_key;
let public_key = RISTRETTO_BASEPOINT_POINT * private_key;
let public_key = *G * private_key;

// Generate DLEQ proof
let r = Scalar::random(&mut OsRng);
let r1 = RISTRETTO_BASEPOINT_POINT * r;
let r1 = *G * r;
let r2 = blinded_point * r;
let e = hash_to_scalar(&[
r1.compress().as_bytes(),
Expand All @@ -45,12 +39,12 @@ pub fn sign_blinded(
}

pub fn verify_dleq_proof(
public_key: &RistrettoPoint,
blinded_point: &RistrettoPoint,
signed_point: &RistrettoPoint,
public_key: &Point,
blinded_point: &Point,
signed_point: &Point,
proof: &DLEQProof,
) -> Result<(), Error> {
let r1 = (RISTRETTO_BASEPOINT_POINT * proof.s) - (public_key * proof.e);
let r1 = (*G * proof.s) - (public_key * proof.e);
let r2 = (blinded_point * proof.s) - (signed_point * proof.e);
let e = hash_to_scalar(&[
r1.compress().as_bytes(),
Expand All @@ -67,12 +61,12 @@ pub fn verify_dleq_proof(
}

pub fn unblind_and_verify_signature(
signed_point: &RistrettoPoint,
signed_point: &Point,
blinding_factor: &Scalar,
public_key: &RistrettoPoint,
public_key: &Point,
proof: &DLEQProof,
blinded_point: &RistrettoPoint,
) -> Result<RistrettoPoint, Error> {
blinded_point: &Point,
) -> Result<Point, Error> {
verify_dleq_proof(public_key, blinded_point, signed_point, proof)?;

Ok(signed_point - (public_key * blinding_factor))
Expand All @@ -81,7 +75,7 @@ pub fn unblind_and_verify_signature(
pub fn verify_unblinded_point(
private_key: &Scalar,
message: &[u8],
unblinded_point: &RistrettoPoint,
unblinded_point: &Point,
) -> Result<(), Error> {
let y = hash_to_curve(message);

Expand All @@ -100,7 +94,7 @@ mod tests {

#[proptest]
fn test_blind_diffie_hellman_flow(
#[strategy(keypair())] a: (Scalar, RistrettoPoint),
#[strategy(keypair())] a: (Scalar, Point),
secret_message: Vec<u8>,
) {
// Alice initializes
Expand All @@ -122,7 +116,7 @@ mod tests {
#[proptest]
#[should_panic]
fn test_schnorr_signature_tampering(
#[strategy(keypair())] a: (Scalar, RistrettoPoint),
#[strategy(keypair())] a: (Scalar, Point),
secret_message: Vec<u8>,
) {
// Alice initializes
Expand Down
32 changes: 4 additions & 28 deletions core/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
pub mod commitment;
pub mod dh;
pub mod schnorr;

use blake2::{Blake2b, Digest};
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use lazy_static::lazy_static;
use rand::rngs::OsRng;

pub use bulletproofs::RangeProof;
pub use curve25519_dalek::{
ristretto::{CompressedRistretto, RistrettoPoint},
scalar::Scalar,
traits::*,
};

pub use schnorr::Signature;

pub const DOMAIN_SEPARATOR: &[u8] = b"MUGRAPH_V1_CURVE_25519_HASH_TO_CURVE_";
pub const DLEQ_DOMAIN_SEPARATOR: &[u8] = b"MUGRAPH_V1_CURVE_25519_DLEQ_PROOF_";
pub const COMMITMENT_TRANSCRIPT_LABEL: &[u8] = b"MUGRAPH_V1_CURVE_25519_COMMITMENT_";
pub const COMMITMENT_VERIFIER_LABEL: &[u8] = b"MUGRAPH_V1_CURVE_25519_COMMITMENT_VERIFIER_";
pub const RANGE_PROOF_DOMAIN_SEPARATOR: &[u8] = b"MUGRAPH_V1_CURVE_25519_RANGE_PROOF_";

pub type PublicKey = RistrettoPoint;
pub type SecretKey = Scalar;

lazy_static! {
pub static ref G: RistrettoPoint = RISTRETTO_BASEPOINT_POINT;
pub static ref H: RistrettoPoint = RistrettoPoint::random(&mut OsRng);
}
use crate::{types::*, DOMAIN_SEPARATOR, G};

pub fn hash_to_scalar(data: &[&[u8]]) -> Scalar {
let mut hasher = Blake2b::new();
Expand All @@ -40,13 +16,13 @@ pub fn hash_to_scalar(data: &[&[u8]]) -> Scalar {
Scalar::from_bytes_mod_order(hasher.finalize().into())
}

pub fn hash_to_curve(message: &[u8]) -> RistrettoPoint {
pub fn hash_to_curve(message: &[u8]) -> Point {
let scalar = hash_to_scalar(&[DOMAIN_SEPARATOR, message]);
RISTRETTO_BASEPOINT_POINT * scalar
*G * scalar
}

pub fn generate_keypair() -> (SecretKey, PublicKey) {
let privkey = Scalar::random(&mut OsRng);
let pubkey = RISTRETTO_BASEPOINT_POINT * privkey;
let pubkey = *G * privkey;
(privkey, pubkey)
}
22 changes: 4 additions & 18 deletions core/src/crypto/schnorr.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, RistrettoPoint, Scalar};
use rand::rngs::OsRng;

use super::hash_to_scalar;
use crate::error::Error;

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct Signature {
#[cfg_attr(test, strategy(crate::testing::point()))]
pub r: RistrettoPoint,
#[cfg_attr(test, strategy(crate::testing::scalar()))]
pub s: Scalar,
}
use crate::{error::Error, types::*, G};

pub fn sign(private_key: &Scalar, message: &[u8]) -> Signature {
let k = Scalar::random(&mut OsRng);
let r = RISTRETTO_BASEPOINT_POINT * k;
let r = *G * k;
let e = hash_to_scalar(&[&r.compress().to_bytes(), message]);
let s = k + e * private_key;

Signature { r, s }
}

pub fn verify(
public_key: &RistrettoPoint,
signature: &Signature,
message: &[u8],
) -> Result<(), Error> {
pub fn verify(public_key: &Point, signature: &Signature, message: &[u8]) -> Result<(), Error> {
let e = hash_to_scalar(&[&signature.r.compress().to_bytes(), message]);
let lhs = RISTRETTO_BASEPOINT_POINT * signature.s;
let lhs = *G * signature.s;
let rhs = signature.r + public_key * e;

if lhs == rhs {
Expand Down
2 changes: 1 addition & 1 deletion core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use miette::Diagnostic;
use thiserror::Error;

use crate::Hash;
use crate::types::Hash;

#[derive(Error, Debug, Diagnostic)]
pub enum Error {
Expand Down
Loading

0 comments on commit 301b2db

Please sign in to comment.