Skip to content

Commit

Permalink
added failure unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
10d9e committed Nov 2, 2023
1 parent fc33a4a commit 9a0243c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 27 deletions.
40 changes: 16 additions & 24 deletions src/chaum_pedersen/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,21 @@ lazy_static! {
params
};

// Defining `EC25519_BASEPOINT_POINT_G` as a lazy static variable. This variable represents a base point on the elliptic curve Curve25519, specifically for the Ristretto group.
pub static ref EC25519_BASEPOINT_POINT_G: RistrettoPoint = {
// Creating the base point 'G' from its byte representation, decoded from the hexadecimal string.
// The unwrap() method is called to handle the Result type, assuming the input is always valid.
RistrettoPoint::from_bytes(
&hex::decode("2aea1fc8034016ac0e9be8c357421a6a3afba883fd10d0f842f4ef6df6fb347a").unwrap()
)
.unwrap() // Unwrap is called again to handle the Option type, assuming the decoded bytes represent a valid point.
};

// Defining `EC25519_BASEPOINT_POINT_H` as a lazy static variable. This variable represents another distinct point on the elliptic curve Curve25519, specifically for the Ristretto group.
pub static ref EC25519_BASEPOINT_POINT_H: RistrettoPoint = {
// Creating the base point 'H' from its byte representation, decoded from the hexadecimal string.
// Similar to the previous definition, unwrap() is used to handle the Result and Option types.
RistrettoPoint::from_bytes(
&hex::decode("ae0855e254e43f00ad816c82b3a801f9995fe0717c826eb776b7a29f13e04c78").unwrap()
)
.unwrap() // Unwrap is called again to ensure the decoded bytes represent a valid point.
// Defining `EC25519_GROUP_PARAMS` as a lazy static variable. This variable represents the group parameters for the elliptic curve Curve25519, specifically for the Ristretto group.
pub static ref EC25519_GROUP_PARAMS: GroupParams<RistrettoPoint> = {
let params = GroupParams::<RistrettoPoint> {
g: RistrettoPoint::from_bytes(
&hex::decode("2aea1fc8034016ac0e9be8c357421a6a3afba883fd10d0f842f4ef6df6fb347a").unwrap()
)
.unwrap().to_owned(),
h: RistrettoPoint::from_bytes(
&hex::decode("ae0855e254e43f00ad816c82b3a801f9995fe0717c826eb776b7a29f13e04c78").unwrap()
)
.unwrap().to_owned(),
p: RISTRETTO_BASEPOINT_POINT.to_owned(),
q: RISTRETTO_BASEPOINT_POINT.to_owned(),
};
params
};

}
Expand Down Expand Up @@ -109,12 +106,7 @@ impl FromStr for GroupParams<RistrettoPoint> {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
// Matching the string "ec25519" and returning the corresponding group parameters.
"ec25519" => Ok(GroupParams {
g: EC25519_BASEPOINT_POINT_G.to_owned(),
h: EC25519_BASEPOINT_POINT_H.to_owned(),
p: RISTRETTO_BASEPOINT_POINT.to_owned(),
q: RISTRETTO_BASEPOINT_POINT.to_owned(),
}),
"ec25519" => Ok(EC25519_GROUP_PARAMS.to_owned()),
_ => Err(()), // Returning an error for unrecognized strings.
}
}
Expand Down
45 changes: 44 additions & 1 deletion src/chaum_pedersen/curve25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ impl ChaumPedersen for EllipticCurveChaumPedersen {
#[cfg(test)]
mod test {
use super::*;
use crate::chaum_pedersen::constants::EC25519_GROUP_PARAMS;
use crate::chaum_pedersen::test::test_execute_protocol;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto::CompressedRistretto;
use crate::rand::RandomGenerator;

fn serialize_ristretto_point(point: &RistrettoPoint) -> String {
// Compress the RistrettoPoint
Expand Down Expand Up @@ -175,7 +177,7 @@ mod test {

/// Tests the verification process in the Elliptic Curve Chaum-Pedersen protocol.
#[test]
fn test_elliptic_curve_verification() {
fn test_elliptic_curve_random_point_verification() {
// Initializing random number generator.
let mut rng = OsRng;
// Creating a secret value x.
Expand All @@ -196,6 +198,47 @@ mod test {
assert!(test_execute_protocol::<EllipticCurveChaumPedersen>(&params, &x));
}

#[test]
fn test_elliptic_curve_standard_verification() {
// Initializing random number generator.
// Creating a secret value x.
let x = Scalar::from(3u32);
// Setting up the group parameters.
let params = EC25519_GROUP_PARAMS.to_owned();

// Executing the protocol and asserting the verification is successful.
assert!(test_execute_protocol::<EllipticCurveChaumPedersen>(&params, &x));
}

#[test]
fn test_fail_elliptic_curve_verification() {
let mut rng = OsRng;
// Setting up the group parameters.
let params = EC25519_GROUP_PARAMS.to_owned();
let x = Scalar::random(&mut rng);

let (cp, _) = EllipticCurveChaumPedersen::commitment(&params, &x);
let c = EllipticCurveChaumPedersen::challenge(&params);
let fake_response = Scalar::generate_random().unwrap();
let verified = EllipticCurveChaumPedersen::verify(&params, &fake_response, &c, &cp);
assert!(!verified);
}

#[test]
fn test_fail_elliptic_curve_verification_with_ec_params() {
let mut rng = OsRng;
// Setting up the group parameters.
let params = EC25519_GROUP_PARAMS.to_owned();
// Creating a secret value x.
let x = Scalar::random(&mut rng);

let (cp, _) = EllipticCurveChaumPedersen::commitment(&params, &x);
let c = EllipticCurveChaumPedersen::challenge(&params);
let fake_response = Scalar::generate_random().unwrap();
let verified = EllipticCurveChaumPedersen::verify(&params, &fake_response, &c, &cp);
assert!(!verified);
}

/// Tests the serialization and deserialization of Ristretto points, simulating sending over a wire.
#[test]
fn test_wire_serialization() {
Expand Down
40 changes: 40 additions & 0 deletions src/chaum_pedersen/discretelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ mod tests {
RFC5114_MODP_2048_256_BIT_PARAMS,
};
use crate::chaum_pedersen::test::test_execute_protocol;
use crate::rand::RandomGenerator;

#[test]
fn test_discrete_log_commitment() {
Expand Down Expand Up @@ -190,6 +191,45 @@ mod tests {
assert!(test_execute_protocol::<DiscreteLogChaumPedersen>(&params, &x));
}

#[test]
fn test_fail_rfc_1024_160_bits_params() {
let params = RFC5114_MODP_1024_160_BIT_PARAMS.to_owned();
let mut rng = OsRng;
let x = rng.gen_biguint_below(&params.p);

let (cp, _) = DiscreteLogChaumPedersen::commitment(&params, &x);
let c = DiscreteLogChaumPedersen::challenge(&params);
let fake_response = BigUint::generate_random().unwrap();
let verified = DiscreteLogChaumPedersen::verify(&params, &fake_response, &c, &cp);
assert!(!verified);
}

#[test]
fn test_fail_rfc_2048_224_bits_params() {
let params = RFC5114_MODP_2048_224_BIT_PARAMS.to_owned();
let mut rng = OsRng;
let x = rng.gen_biguint_below(&params.p);

let (cp, _) = DiscreteLogChaumPedersen::commitment(&params, &x);
let c = DiscreteLogChaumPedersen::challenge(&params);
let fake_response = BigUint::generate_random().unwrap();
let verified = DiscreteLogChaumPedersen::verify(&params, &fake_response, &c, &cp);
assert!(!verified);
}

#[test]
fn test_fail_rfc_2048_256_bits_params() {
let params = RFC5114_MODP_2048_256_BIT_PARAMS.to_owned();
let mut rng = OsRng;
let x = rng.gen_biguint_below(&params.p);

let (cp, _) = DiscreteLogChaumPedersen::commitment(&params, &x);
let c = DiscreteLogChaumPedersen::challenge(&params);
let fake_response = BigUint::generate_random().unwrap();
let verified = DiscreteLogChaumPedersen::verify(&params, &fake_response, &c, &cp);
assert!(!verified);
}

#[test]
fn test_verify() {
let g = BigUint::from(4u32);
Expand Down
5 changes: 3 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tonic::transport::Channel;
use crate::chaum_pedersen::GroupParams;
use crate::chaum_pedersen::ChaumPedersen;
use crate::conversion::ByteConvertible;
use crate::rand::RandomGenerator;
use std::error::Error;

/// A module that contains the auto-generated gRPC code for the Zero-Knowledge Proof (ZKP) authentication service.
Expand Down Expand Up @@ -128,8 +129,8 @@ where
Response = S,
Challenge = S,
>,
P: ByteConvertible<P>,
S: ByteConvertible<S>,
P: ByteConvertible<P> + RandomGenerator<P>,
S: ByteConvertible<S> + RandomGenerator<S>,
{
// Client calculates the commitment.
let ((y1, y2, r1, r2), k) = T::commitment(params, x);
Expand Down
1 change: 1 addition & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use crate::chaum_pedersen::curve25519::EllipticCurveChaumPedersen;
use crate::conversion::ByteConvertible;
use crate::repository::daoimpl::InMemoryUserDao;
Expand Down

0 comments on commit 9a0243c

Please sign in to comment.