Skip to content

Commit

Permalink
add fixed proof string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Sep 3, 2024
1 parent d598eb2 commit dab4ce4
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 15 deletions.
37 changes: 35 additions & 2 deletions zk-sdk/src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,13 @@ fn delta(bit_lengths: &[usize], y: &Scalar, z: &Scalar) -> Scalar {

#[cfg(test)]
mod tests {
use super::*;
use {
super::*,
crate::{
encryption::pod::pedersen::PodPedersenCommitment, range_proof::pod::PodRangeProofU128,
},
std::str::FromStr,
};

#[test]
fn test_single_rangeproof() {
Expand Down Expand Up @@ -506,5 +512,32 @@ mod tests {
.is_ok());
}

// TODO: write test for serialization/deserialization
#[test]
fn test_range_proof_string() {
let commitment_1_str = "dDaa/MTEDlyI0Nxx+iu1tOteZsTWmPXAfn9QI0W9mSc=";
let pod_commitment_1 = PodPedersenCommitment::from_str(&commitment_1_str).unwrap();
let commitment_1: PedersenCommitment = pod_commitment_1.try_into().unwrap();

let commitment_2_str = "tnRILjKpogi2sXxLgZzMqlqPMLnCJmrSjZ5SPQYhtgg=";
let pod_commitment_2 = PodPedersenCommitment::from_str(&commitment_2_str).unwrap();
let commitment_2: PedersenCommitment = pod_commitment_2.try_into().unwrap();

let commitment_3_str = "ZAC5ZLXotsMOVExtrr56D/EZNeyo9iWepNbeH22EuRo=";
let pod_commitment_3 = PodPedersenCommitment::from_str(&commitment_3_str).unwrap();
let commitment_3: PedersenCommitment = pod_commitment_3.try_into().unwrap();

let proof_str = "AvvBQL63pXMXsmuvuNbs/CqXdzeyrMpEIO2O/cI6/SyqU4N+7HUU3LmXai9st+DxqTnuKsm0SgnADfpLpQCEbDDupMb09NY8oHT8Bx8WQhv9eyoBlrPRd7DVhOUsio02gBshe3p2Wj7+yDCpFaZ7/PMypFBX6+E+EqCiPI6yUk4ztslWY0Ksac41eJgcPzXyIx2kvmSTsVBKLb7U01PWBC+AUyUmK3/IdvmJ4DnlS3xFrdg/mxSsYJFd3OZA3cwDb0jePQf/P43/2VVqPRixMVO7+VGoMKPoRTEEVbClsAlW6stGTFPcrimu3c+geASgvwElkIKNGtYcjoj3SS+/VeqIG9Ei1j+TJtPhOE9SG4KNw9xBGwecpliDbQhKjO950EVcnOts+a525/frZV1jHJmOOrZtKRV4pvk37dtQkx4sv+pxRmfVrjwOcKQeg+BzcuF0vaQbqa4SUbzbO9z3RwIMlYIBaz0bqZgJmtPOFuFmNyCJaeB29vlcEAfYbn5gdlgtWP50tKmhoskndulziKZjz4qHSA9rbG2ZtoMHoCsAobHKu2H9OxcaK4Scj1QGwst+zXBEY8uePNbxvU5DMJLVFORtLUXkVdPCmCSsm1Bz4TRbnls8LOVW6wqTgShQMhjNM3RtwdHXENPn5uDnhyvfduAcL+DtI8AIJyRneROefk7i7gjal8dLdMM/QnXT7ctpMQU6uNlpsNzq65xlOQKXO71vQ3c2mE/DmxVJi6BTS5WCzavvhiqdhQyRL61ESCALQpaP0/d0DLwLikVH3ypuDLEnVXe9Pmkxdd0xCzO6QcfyK50CPnV/dVgHeLg8EVag2O83+/7Ys5oLxrDad9TJTDcrT2xsRqECFnSA+z9uZtDPujhQL0ogS5RH4agnQN4mVGTwOLV8OKpn+AvWq6+j1/9EXFkLPBTU5wT0FQuT2VZ8xp5GeqdI13Zey1uPrxc6CZZ407y9OINED4IdBQ==";
let pod_proof = PodRangeProofU128::from_str(&proof_str).unwrap();
let proof: RangeProof = pod_proof.try_into().unwrap();

let mut transcript_verify = Transcript::new(b"Test");

assert!(proof
.verify(
vec![&commitment_1, &commitment_2, &commitment_3],
vec![64, 32, 32],
&mut transcript_verify,
)
.is_ok());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,22 @@ impl BatchedGroupedCiphertext2HandlesValidityProof {
mod test {
use {
super::*,
crate::encryption::{elgamal::ElGamalKeypair, pedersen::Pedersen},
crate::{
encryption::{
elgamal::ElGamalKeypair,
pedersen::Pedersen,
pod::{
elgamal::{PodDecryptHandle, PodElGamalPubkey},
pedersen::PodPedersenCommitment,
},
},
sigma_proofs::pod::PodBatchedGroupedCiphertext2HandlesValidityProof,
},
std::str::FromStr,
};

#[test]
fn test_batched_grouped_ciphertext_validity_proof() {
fn test_batched_grouped_ciphertext_2_handles_validity_proof() {
let first_keypair = ElGamalKeypair::new_rand();
let first_pubkey = first_keypair.pubkey();

Expand Down Expand Up @@ -173,4 +184,60 @@ mod test {
)
.is_ok());
}

#[test]
fn test_batched_grouped_ciphertext_2_handles_validity_proof_string() {
let first_pubkey_str = "3FQGicS6AgVkRnX5Sau8ybxJDvlehmbdvBUdo+o+oE4=";
let pod_first_pubkey = PodElGamalPubkey::from_str(&first_pubkey_str).unwrap();
let first_pubkey: ElGamalPubkey = pod_first_pubkey.try_into().unwrap();

let second_pubkey_str = "IieU/fJCRksbDNvIJZvg/N/safpnIWAGT/xpUAG7YUg=";
let pod_second_pubkey = PodElGamalPubkey::from_str(&second_pubkey_str).unwrap();
let second_pubkey: ElGamalPubkey = pod_second_pubkey.try_into().unwrap();

let commitment_lo_str = "Lq0z7bx3ccyxIB0rRHoWzcba8W1azvAhMfnJogxcz2I=";
let pod_commitment_lo = PodPedersenCommitment::from_str(&commitment_lo_str).unwrap();
let commitment_lo: PedersenCommitment = pod_commitment_lo.try_into().unwrap();

let commitment_hi_str = "dLPLdQrcl5ZWb0EaJcmebAlJA6RrzKpMSYPDVMJdOm0=";
let pod_commitment_hi = PodPedersenCommitment::from_str(&commitment_hi_str).unwrap();
let commitment_hi: PedersenCommitment = pod_commitment_hi.try_into().unwrap();

let first_handle_lo_str = "GizvHRUmu6CMjhH7qWg5Rqu43V69Nyjq4QsN/yXBHT8=";
let pod_first_handle_lo_str = PodDecryptHandle::from_str(&first_handle_lo_str).unwrap();
let first_handle_lo: DecryptHandle = pod_first_handle_lo_str.try_into().unwrap();

let first_handle_hi_str = "qMuR929bbkKiVJfRvYxnb90rbh2btjNDjaXpeLCvQWk=";
let pod_first_handle_hi_str = PodDecryptHandle::from_str(&first_handle_hi_str).unwrap();
let first_handle_hi: DecryptHandle = pod_first_handle_hi_str.try_into().unwrap();

let second_handle_lo_str = "MmDbMo2l/jAcXUIm09AQZsBXa93lI2BapAiGZ6f9zRs=";
let pod_second_handle_lo_str = PodDecryptHandle::from_str(&second_handle_lo_str).unwrap();
let second_handle_lo: DecryptHandle = pod_second_handle_lo_str.try_into().unwrap();

let second_handle_hi_str = "gKhb0o3d22XcUcQl5hENF4l1SJwg1vpgiw2RDYqXOxY=";
let pod_second_handle_hi_str = PodDecryptHandle::from_str(&second_handle_hi_str).unwrap();
let second_handle_hi: DecryptHandle = pod_second_handle_hi_str.try_into().unwrap();

let proof_str = "2n2mADpkNrop+eHJj1sAryXWcTtC/7QKcxMp7FdHeh8wjGKLAa9kC89QLGrphv7pZdb2J25kKXqhWUzRBsJWU0izi5vxau9XX6cyd72F3Q9hMXBfjk3htOHI0VnGAalZ/3dZ6C7erjGQDoeTVGOd1vewQ+NObAbfZwcry3+VhQNpkhL17E1dUgZZ+mb5K0tXAjWCmVh1OfN9h3sGltTUCg==";
let pod_proof =
PodBatchedGroupedCiphertext2HandlesValidityProof::from_str(&proof_str).unwrap();
let proof: BatchedGroupedCiphertext2HandlesValidityProof = pod_proof.try_into().unwrap();

let mut verifier_transcript = Transcript::new(b"Test");

assert!(proof
.verify(
&first_pubkey,
&second_pubkey,
&commitment_lo,
&commitment_hi,
&first_handle_lo,
&first_handle_hi,
&second_handle_lo,
&second_handle_hi,
&mut verifier_transcript,
)
.is_ok());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,22 @@ impl BatchedGroupedCiphertext3HandlesValidityProof {
mod test {
use {
super::*,
crate::encryption::{elgamal::ElGamalKeypair, pedersen::Pedersen},
crate::{
encryption::{
elgamal::ElGamalKeypair,
pedersen::Pedersen,
pod::{
elgamal::{PodDecryptHandle, PodElGamalPubkey},
pedersen::PodPedersenCommitment,
},
},
sigma_proofs::pod::PodBatchedGroupedCiphertext3HandlesValidityProof,
},
std::str::FromStr,
};

#[test]
fn test_batched_grouped_ciphertext_validity_proof() {
fn test_batched_grouped_ciphertext_3_handles_validity_proof() {
let first_keypair = ElGamalKeypair::new_rand();
let first_pubkey = first_keypair.pubkey();

Expand Down Expand Up @@ -193,4 +204,75 @@ mod test {
)
.is_ok());
}

#[test]
fn test_batched_grouped_ciphertext_3_handles_validity_proof_string() {
let first_pubkey_str = "PFQ4AD4W/Y4BEg3nI/qckFLhnjMQ12xPHyaMg9Bkg3w=";
let pod_first_pubkey = PodElGamalPubkey::from_str(&first_pubkey_str).unwrap();
let first_pubkey: ElGamalPubkey = pod_first_pubkey.try_into().unwrap();

let second_pubkey_str = "2CZ4h5oK7zh4/3P6s/kCQoNlpUPk1IrsrAtTWjCtfFo=";
let pod_second_pubkey = PodElGamalPubkey::from_str(&second_pubkey_str).unwrap();
let second_pubkey: ElGamalPubkey = pod_second_pubkey.try_into().unwrap();

let third_pubkey_str = "yonKhqkoXNvMbN/tU6fjHFhfZuNPpvMj8L55aP2bBG4=";
let pod_third_pubkey = PodElGamalPubkey::from_str(&third_pubkey_str).unwrap();
let third_pubkey: ElGamalPubkey = pod_third_pubkey.try_into().unwrap();

let commitment_lo_str = "atIteiveexponnuF2Z1nbovZYYtcGWjglpEA3caMShM=";
let pod_commitment_lo = PodPedersenCommitment::from_str(&commitment_lo_str).unwrap();
let commitment_lo: PedersenCommitment = pod_commitment_lo.try_into().unwrap();

let commitment_hi_str = "IoZlSj7spae2ogiAUiEuuwAjYA5khgBH8FhaHzkh+lc=";
let pod_commitment_hi = PodPedersenCommitment::from_str(&commitment_hi_str).unwrap();
let commitment_hi: PedersenCommitment = pod_commitment_hi.try_into().unwrap();

let first_handle_lo_str = "6PlKiitdapVZnh7VccQNbskXop9nmITGppLsV42UMkU=";
let pod_first_handle_lo_str = PodDecryptHandle::from_str(&first_handle_lo_str).unwrap();
let first_handle_lo: DecryptHandle = pod_first_handle_lo_str.try_into().unwrap();

let first_handle_hi_str = "vF+oZ3WWnrJyJ95Wl8EW+aVJiFmruiuRw6+TT3QVMBI=";
let pod_first_handle_hi_str = PodDecryptHandle::from_str(&first_handle_hi_str).unwrap();
let first_handle_hi: DecryptHandle = pod_first_handle_hi_str.try_into().unwrap();

let second_handle_lo_str = "rvxzo5ZyrD6YTm7X3GjplgOGJjx6PtoZ+DKbL4LsQWA=";
let pod_second_handle_lo_str = PodDecryptHandle::from_str(&second_handle_lo_str).unwrap();
let second_handle_lo: DecryptHandle = pod_second_handle_lo_str.try_into().unwrap();

let second_handle_hi_str = "0mdZSGiWQhOjqsExqFMD8hfgUlRRRrF/G3CJ7d0LEEk=";
let pod_second_handle_hi_str = PodDecryptHandle::from_str(&second_handle_hi_str).unwrap();
let second_handle_hi: DecryptHandle = pod_second_handle_hi_str.try_into().unwrap();

let third_handle_lo_str = "bpT2LuFektFhI/sacjSsqNtCsO8ac5qn0jWeMeQq4WM=";
let pod_third_handle_lo_str = PodDecryptHandle::from_str(&third_handle_lo_str).unwrap();
let third_handle_lo: DecryptHandle = pod_third_handle_lo_str.try_into().unwrap();

let third_handle_hi_str = "OE8z7Bbv2AHnjxebK6ASJfkJbOlYQdnN6ZPkG2u4SnA=";
let pod_third_handle_hi_str = PodDecryptHandle::from_str(&third_handle_hi_str).unwrap();
let third_handle_hi: DecryptHandle = pod_third_handle_hi_str.try_into().unwrap();

let proof_str = "GkjZ7QKcJq5X/OU8wb26wZ7p2D9thVK+Cb11CzRjWUoihYvGfuCbVG1vr4qtnfx65SS4jVK1H0q/948A9wy8ZPTrOZJA122G4+cpt5mKnSrKq/vbv4ZRha0oR9RGJFZ2SPT3gx2jysKDKRAQgBLOzSGfQg9Hsbz57i55SQfliUF5mByZKuzGKHSIHi81BDqbrFAj6x5bOeMAaLqsCboCA5XGDUZ2HMPUGuAd9F+OaVH+eJZnuoDjwwcBQ2eANgMB";
let pod_proof =
PodBatchedGroupedCiphertext3HandlesValidityProof::from_str(&proof_str).unwrap();
let proof: BatchedGroupedCiphertext3HandlesValidityProof = pod_proof.try_into().unwrap();

let mut verifier_transcript = Transcript::new(b"Test");

assert!(proof
.verify(
&first_pubkey,
&second_pubkey,
&third_pubkey,
&commitment_lo,
&commitment_hi,
&first_handle_lo,
&first_handle_hi,
&second_handle_lo,
&second_handle_hi,
&third_handle_lo,
&third_handle_hi,
&mut verifier_transcript,
)
.is_ok());
}
}
44 changes: 43 additions & 1 deletion zk-sdk/src/sigma_proofs/ciphertext_ciphertext_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,14 @@ impl CiphertextCiphertextEqualityProof {

#[cfg(test)]
mod test {
use super::*;
use {
super::*,
crate::{
encryption::pod::elgamal::{PodElGamalCiphertext, PodElGamalPubkey},
sigma_proofs::pod::PodCiphertextCiphertextEqualityProof,
},
std::str::FromStr,
};

#[test]
fn test_ciphertext_ciphertext_equality_proof_correctness() {
Expand Down Expand Up @@ -338,4 +345,39 @@ mod test {
)
.is_err());
}

#[test]
fn test_ciphertext_ciphertext_equality_proof_string() {
let first_pubkey_str = "VOPKaqo4nsX4XnbgGjCKHkLkR6JG1jX9D5G/e0EuYmM=";
let pod_first_pubkey = PodElGamalPubkey::from_str(&first_pubkey_str).unwrap();
let first_pubkey: ElGamalPubkey = pod_first_pubkey.try_into().unwrap();

let second_pubkey_str = "JnVhtKo9B7g9c8Obo/5/EqvA59i3TvtuOcQWf17T7SU=";
let pod_second_pubkey = PodElGamalPubkey::from_str(&second_pubkey_str).unwrap();
let second_pubkey: ElGamalPubkey = pod_second_pubkey.try_into().unwrap();

let first_ciphertext_str = "oKv6zxN051MXdk2cISD+CUsH2+FINoH1iB4WZyuy6nNkE7Q+eLiY9JB8itJhgKHJEA/1sAzDvpnRlLL06OXvIg==";
let pod_first_ciphertext = PodElGamalCiphertext::from_str(&first_ciphertext_str).unwrap();
let first_ciphertext: ElGamalCiphertext = pod_first_ciphertext.try_into().unwrap();

let second_ciphertext_str = "ooSA2cQDqutgyCBoMiQktM1Cu4NDNEbphF010gjG4iF0iMK1N+u/Qxqk0wwO/+w+5S6RiicwPs4mEKRJpFiHEw==";
let pod_second_ciphertext = PodElGamalCiphertext::from_str(&second_ciphertext_str).unwrap();
let second_ciphertext: ElGamalCiphertext = pod_second_ciphertext.try_into().unwrap();

let proof_str = "MlfRDO4sBPbpciEXci3QfVSLVABAJ0s8wMZ/Uz3AyETmGJ1BUE961fHIiNQXPD0j1uu1Josj//E8loPD1w+4E3bfDBJ3Mp2YqeOv41Bdec02YXlAotTGjq/UfncGdUhyampkuXUmSvnmkf5BIp4nr3X18cR9KHTAzBrKv6erjAxIckyRnACaZGEx+ZboEb3FBEXqTklytT1nrebbwkjvDUWbcpZrE+xxBWYek3qeq1x1debzxVhtS2yx44cvR5UIGLzGYa2ec/xh7wvyNEbnX80rZju2dztr4bN5f2vrTgk=";
let pod_proof = PodCiphertextCiphertextEqualityProof::from_str(&proof_str).unwrap();
let proof: CiphertextCiphertextEqualityProof = pod_proof.try_into().unwrap();

let mut verifier_transcript = Transcript::new(b"Test");

assert!(proof
.verify(
&first_pubkey,
&second_pubkey,
&first_ciphertext,
&second_ciphertext,
&mut verifier_transcript
)
.is_ok());
}
}
38 changes: 37 additions & 1 deletion zk-sdk/src/sigma_proofs/ciphertext_commitment_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,18 @@ impl CiphertextCommitmentEqualityProof {
mod test {
use {
super::*,
crate::encryption::{elgamal::ElGamalSecretKey, pedersen::Pedersen},
crate::{
encryption::{
elgamal::ElGamalSecretKey,
pedersen::Pedersen,
pod::{
elgamal::{PodElGamalCiphertext, PodElGamalPubkey},
pedersen::PodPedersenCommitment,
},
},
sigma_proofs::pod::PodCiphertextCommitmentEqualityProof,
},
std::str::FromStr,
};

#[test]
Expand Down Expand Up @@ -423,4 +434,29 @@ mod test {
)
.is_ok());
}

#[test]
fn test_ciphertext_commitment_equality_proof_string() {
let pubkey_str = "JNa7rRrDm35laU7f8HPds1PmHoZEPSHFK/M+aTtEhAk=";
let pod_pubkey = PodElGamalPubkey::from_str(&pubkey_str).unwrap();
let pubkey: ElGamalPubkey = pod_pubkey.try_into().unwrap();

let ciphertext_str = "RAXnbQ/DPRlYAWmD+iHRNqMDv7oQcPgQ7OejRzj4bxVy2qOJNziqqDOC7VP3iTW1+z/jckW4smA3EUF7i/r8Rw==";
let pod_ciphertext = PodElGamalCiphertext::from_str(&ciphertext_str).unwrap();
let ciphertext: ElGamalCiphertext = pod_ciphertext.try_into().unwrap();

let commitment_str = "ngPTYvbY9P5l6aOfr7bLQiI+0HZsw8GBgiumdW3tNzw=";
let pod_commitment = PodPedersenCommitment::from_str(&commitment_str).unwrap();
let commitment: PedersenCommitment = pod_commitment.try_into().unwrap();

let proof_str = "cCZySLxB2XJdGyDvckVBm2OWiXqf7Jf54IFoDuLJ4G+ySj+lh5DbaDMHDhuozQC9tDWtk2mFITuaXOc5Zw3nZ2oEvVYpqv5hN+k5dx9k8/nZKabUCkZwx310z7x4fE4Np5SY9PYia1hkrq9AWq0b3v97XvW1+XCSSxuflvBk5wsdaQQ+ZgcmPnKWKjHfRwmU2k5iVgYzs2VmvZa5E3OWBoM/M2yFNvukY+FCC2YMnspO0c4lNBr/vDFQuHdW0OgJ";
let pod_proof = PodCiphertextCommitmentEqualityProof::from_str(&proof_str).unwrap();
let proof: CiphertextCommitmentEqualityProof = pod_proof.try_into().unwrap();

let mut verifier_transcript = Transcript::new(b"Test");

assert!(proof
.verify(&pubkey, &ciphertext, &commitment, &mut verifier_transcript)
.is_ok());
}
}
Loading

0 comments on commit dab4ce4

Please sign in to comment.