Skip to content

Commit

Permalink
[zk-token-sdk] Fix ElGamal key derivation (solana-labs#28792)
Browse files Browse the repository at this point in the history
* fix ElGamal key derivation

* cargo fmt
  • Loading branch information
samkim-crypto authored and gnapoli23 committed Dec 16, 2022
1 parent 5ef5dac commit 560416f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 7 additions & 8 deletions zk-token-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ impl ElGamal {
#[cfg(not(target_os = "solana"))]
#[allow(non_snake_case)]
fn keygen_with_scalar(s: &Scalar) -> ElGamalKeypair {
assert!(s != &Scalar::zero());

let P = s.invert() * &(*H);
let secret = ElGamalSecretKey(*s);
let public = ElGamalPubkey::new(&secret);

ElGamalKeypair {
public: ElGamalPubkey(P),
secret: ElGamalSecretKey(*s),
}
ElGamalKeypair { public, secret }
}

/// On input an ElGamal public key and an amount to be encrypted, the function returns a
Expand Down Expand Up @@ -267,7 +263,10 @@ impl ElGamalPubkey {
/// Derives the `ElGamalPubkey` that uniquely corresponds to an `ElGamalSecretKey`.
#[allow(non_snake_case)]
pub fn new(secret: &ElGamalSecretKey) -> Self {
ElGamalPubkey(&secret.0 * &(*H))
let s = &secret.0;
assert!(s != &Scalar::zero());

ElGamalPubkey(s.invert() * &(*H))
}

pub fn get_point(&self) -> &RistrettoPoint {
Expand Down
17 changes: 16 additions & 1 deletion zk-token-sdk/src/sigma_proofs/pubkey_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,30 @@ impl PubkeySigmaProof {

#[cfg(test)]
mod test {
use super::*;
use {
super::*,
solana_sdk::{pubkey::Pubkey, signature::Keypair},
};

#[test]
fn test_pubkey_proof_correctness() {
// random ElGamal keypair
let keypair = ElGamalKeypair::new_rand();

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

let proof = PubkeySigmaProof::new(&keypair, &mut prover_transcript);
assert!(proof
.verify(&keypair.public, &mut verifier_transcript)
.is_ok());

// derived ElGamal keypair
let keypair = ElGamalKeypair::new(&Keypair::new(), &Pubkey::default()).unwrap();

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

let proof = PubkeySigmaProof::new(&keypair, &mut prover_transcript);
assert!(proof
.verify(&keypair.public, &mut verifier_transcript)
Expand Down

0 comments on commit 560416f

Please sign in to comment.