Skip to content

Conversation

@samkim-crypto
Copy link
Contributor

@samkim-crypto samkim-crypto commented Dec 29, 2025

Problem

Currently, it is not possible to generate ciphertext validity proofs and equality proofs purely using JS/TS because there is no way to generate an ElGamal ciphertext using a pre-specified Pedersen opening. This function exists in the zk-sdk, but there is no wrapper function inside the zk-sdk-wasm-js crate.

Summary of Changes

Added the encrypt_with function to the zk-sdk-wasm-js crate.

The reason why I missed this originally was that some of the unit tests are using functions from the zk-sdk rather than using functions from the zk-sdk-wasm-js. So I also updated some unit tests to use functions directly from zk-sdk-wasm-js.

@samkim-crypto samkim-crypto marked this pull request as ready for review January 5, 2026 08:57
@samkim-crypto samkim-crypto requested a review from grod220 January 5, 2026 08:57
Comment on lines 88 to 91
// Check if the commitment matches what the zk-sdk would create.
let expected_inner = pedersen::Pedersen::with(amount, &opening.inner);
assert_eq!(commitment.inner, expected_inner);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a relevant assertion anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I tried to remove instances of functions from the zk-sdk and I thought this was a pretty minor assertion, so I removed it. But I don't think it hurts to keep it, so added it back in!

Comment on lines +43 to +56
#[wasm_bindgen(js_name = "encryptWith")]
pub fn encrypt_with(
first_pubkey: &ElGamalPubkey,
second_pubkey: &ElGamalPubkey,
amount: u64,
opening: &PedersenOpening,
) -> Self {
let inner = grouped_elgamal::GroupedElGamal::encrypt_with(
[&first_pubkey.inner, &second_pubkey.inner],
amount,
&opening.inner,
);
Self { inner }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get a test somewhere for this? Maybe something like:

#[wasm_bindgen_test]
fn test_encrypt_with_decrypts_correctly() {
    let keypair = ElGamalKeypair::new_rand();
    let amount = 42u64;
    let opening = PedersenOpening::new_rand();
    
    let ciphertext = keypair.pubkey().encrypt_with(amount, &opening);
    let decrypted = keypair.decrypt_u32(&ciphertext);
    
    assert_eq!(decrypted, Some(42));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good idea. Added!

@samkim-crypto samkim-crypto merged commit 29f959c into solana-program:main Jan 6, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants