0

In the process of testing these web assembly bindings in this tutorial application I discovered an issue with the serializers in this crate whenever I tested them in a JS environment.

For context, here is how I discovered the issue:

At first I realized the elgamalEncrypt binding was failing for the wasm tutorial... I fixed the issue by changing

 pub fn elgamalEncrypt(
    pk: wasm_bindgen::Clamped<Vec<u8>>,
    message: wasm_bindgen::Clamped<Vec<u8>>,
    r: wasm_bindgen::Clamped<Vec<u8>>,
) -> Vec<u8> {
    let pk: G1Affine = serde_json::from_slice(&pk[..]).unwrap();
    let message: Vec<Fr> = serde_json::from_slice(&message[..]).unwrap();
    let r: Fr = serde_json::from_slice(&r[..]).unwrap();

    let output = crate::circuit::modules::elgamal::ElGamalGadget::encrypt(pk, message, r);

    serde_json::to_vec(&output).unwrap()
}

to this

/// Encrypt using elgamal in browser. Input message
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn elgamalEncrypt(
    pk: wasm_bindgen::Clamped<Vec<u8>>,
    message: wasm_bindgen::Clamped<Vec<u8>>,
    r: wasm_bindgen::Clamped<Vec<u8>>,
) -> Vec<u8> {
    let pk: [[u64; 4]; 2] = serde_json::from_slice(&pk[..]).unwrap();
    let pk: G1Affine = G1Affine {
        x: Fq::from_raw(pk[0]),
        y: Fq::from_raw(pk[1]),
    };
    let message: Vec<[u64; 4]> = serde_json::from_slice(&message[..]).unwrap();
    let message: Vec<Fr> = message.iter().map(|b| vecu64_to_field(b)).collect();
    let r: [u64; 4] = serde_json::from_slice(&r[..]).unwrap();
    let r: Fr = vecu64_to_field(&r);

    let output = crate::circuit::modules::elgamal::ElGamalGadget::encrypt(pk, message, r);

    serde_json::to_vec(&output).unwrap()
}

And that stopped that binding from failing. But then the elgamalDecrypt wasn't properly decrypting the message anymore in the wasm tutorial application...

Ultimately I ended up "fixing" the issue in this PR here by essentially replicating the functionality of these serializers in the wasm binding.

The reason why we haven't merged these changes yet is because it feels like we're putting a bandaid on a deeper issue. The changes definitely make sense if the serializer isn't being called, but ultimately we need to figure out why the serde_json serializer is not triggering properly.

0 Answers0