use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::scalar::Scalar;
use rand::rngs::OsRng;
use rand::TryRngCore;
use sigma_protocols::fiat_shamir::FiatShamirTransform;
use sigma_protocols::protocols::dleq::{DLEQProof, DLEQStatement, DLEQWitness};
use sigma_protocols::sigma::{Challenge, Commitment, ScalarChallenge, SigmaProtocol};
#[test]
fn test_dleq_interactive() {
let g = RISTRETTO_BASEPOINT_POINT;
let mut h_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut h_bytes)
.expect("Failed to generate random bytes");
let h = Scalar::from_bytes_mod_order(h_bytes) * g;
let mut alpha_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut alpha_bytes)
.expect("Failed to generate random bytes");
let alpha = Scalar::from_bytes_mod_order(alpha_bytes);
let x = alpha * g;
let y = alpha * h;
let statement = DLEQStatement { g, h, x, y };
let witness = DLEQWitness { alpha };
let (commitment, state) = DLEQProof::prover_commit(&statement, &witness);
let mut ch_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut ch_bytes)
.expect("Failed to generate random bytes");
let challenge = ScalarChallenge(Scalar::from_bytes_mod_order(ch_bytes));
let response = DLEQProof::prover_response(&statement, &witness, &state, &challenge)
.expect("Failed to generate response");
let result = DLEQProof::verifier(&statement, &commitment, &challenge, &response);
assert!(result.is_ok(), "DLEQ proof verification failed");
}
#[test]
fn test_dleq_non_interactive() {
let g = RISTRETTO_BASEPOINT_POINT;
let mut h_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut h_bytes)
.expect("Failed to generate random bytes");
let h = Scalar::from_bytes_mod_order(h_bytes) * g;
let mut alpha_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut alpha_bytes)
.expect("Failed to generate random bytes");
let alpha = Scalar::from_bytes_mod_order(alpha_bytes);
let x = alpha * g;
let y = alpha * h;
let statement = DLEQStatement { g, h, x, y };
let witness = DLEQWitness { alpha };
let (commitment, state) = DLEQProof::prover_commit(&statement, &witness);
let mut fs = FiatShamirTransform::new(b"dleq-proof", b"test-protocol", b"test-session");
fs.absorb_commitment(&commitment.to_bytes());
let challenge_bytes = fs.generate_challenge(32);
let challenge =
ScalarChallenge::from_bytes(&challenge_bytes).expect("Failed to create challenge");
let response = DLEQProof::prover_response(&statement, &witness, &state, &challenge)
.expect("Failed to generate response");
let result = DLEQProof::verifier(&statement, &commitment, &challenge, &response);
assert!(
result.is_ok(),
"Non-interactive DLEQ proof verification failed"
);
}
#[test]
fn test_dleq_invalid_proof() {
let g = RISTRETTO_BASEPOINT_POINT;
let mut h_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut h_bytes)
.expect("Failed to generate random bytes");
let h = Scalar::from_bytes_mod_order(h_bytes) * g;
let mut alpha_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut alpha_bytes)
.expect("Failed to generate random bytes");
let alpha = Scalar::from_bytes_mod_order(alpha_bytes);
let x = alpha * g;
let mut wa_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut wa_bytes)
.expect("Failed to generate random bytes");
let wrong_alpha = Scalar::from_bytes_mod_order(wa_bytes);
let y = wrong_alpha * h;
let statement = DLEQStatement { g, h, x, y };
let witness = DLEQWitness { alpha };
let (commitment, state) = DLEQProof::prover_commit(&statement, &witness);
let mut ch_bytes = [0u8; 32];
OsRng
.try_fill_bytes(&mut ch_bytes)
.expect("Failed to generate random bytes");
let challenge = ScalarChallenge(Scalar::from_bytes_mod_order(ch_bytes));
let response = DLEQProof::prover_response(&statement, &witness, &state, &challenge)
.expect("Failed to generate response");
let result = DLEQProof::verifier(&statement, &commitment, &challenge, &response);
assert!(result.is_err(), "Invalid DLEQ proof should not verify");
}