Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM bindings #10

Merged
merged 17 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions tpke-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ mod utils;

extern crate group_threshold_cryptography as tpke;

use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger256, ToBytes};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use ark_ff::{FromBytes, ToBytes};
use utils::set_panic_hook;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -296,7 +292,7 @@ pub fn encrypt(
}

#[wasm_bindgen]
pub fn decrypt(ciphertext: Ciphertext, private_key: PrivateKey) -> Vec<u8> {
pub fn decrypt(ciphertext: Vec<u8>, privkey: Vec<u8>) -> Vec<u8> {
set_panic_hook();

tpke::checked_decrypt(
Expand Down
85 changes: 85 additions & 0 deletions tpke-wasm/src/serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! This adds a few utility functions for serializing and deserializing
//! [arkworks](http://arkworks.rs/) types that implement [CanonicalSerialize] and [CanonicalDeserialize].
//! Adapted from [o1-labs/proof-systems](https://raw.githubusercontent.com/o1-labs/proof-systems/31c76ceae3122f0ce09cded8260960ed5cbbe3d8/utils/src/serialization.rs).

use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use serde_with::Bytes;

//
// Serialization with serde
//

pub mod ser {
//! You can use this module for serialization and deserializing arkworks types with [serde].
//! Simply use the following attribute on your field:
//! `#[serde(with = "serialization::ser") attribute"]`

use super::*;
use serde_with::{DeserializeAs, SerializeAs};

/// You can use this to serialize an arkworks type with serde and the "serialize_with" attribute.
/// See <https://serde.rs/field-attrs.html>
pub fn serialize<S>(
val: impl CanonicalSerialize,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut bytes = vec![];
val.serialize(&mut bytes)
.map_err(serde::ser::Error::custom)?;

Bytes::serialize_as(&bytes, serializer)
}

/// You can use this to deserialize an arkworks type with serde and the "deserialize_with" attribute.
/// See <https://serde.rs/field-attrs.html>
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: CanonicalDeserialize,
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = Bytes::deserialize_as(deserializer)?;
T::deserialize(&mut &bytes[..]).map_err(serde::de::Error::custom)
}
}

//
// Serialization with [serde_with]
//

/// You can use [SerdeAs] with [serde_with] in order to serialize and deserialize types that implement [CanonicalSerialize] and [CanonicalDeserialize],
/// or containers of types that implement these traits (Vec, arrays, etc.)
/// Simply add annotations like `#[serde_as(as = "serialization::SerdeAs")]`
/// See <https://docs.rs/serde_with/1.10.0/serde_with/guide/serde_as/index.html#switching-from-serdes-with-to-serde_as>
pub struct SerdeAs;

impl<T> serde_with::SerializeAs<T> for SerdeAs
where
T: CanonicalSerialize,
{
fn serialize_as<S>(val: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut bytes = vec![];
val.serialize(&mut bytes)
.map_err(serde::ser::Error::custom)?;

Bytes::serialize_as(&bytes, serializer)
}
}

impl<'de, T> serde_with::DeserializeAs<'de, T> for SerdeAs
where
T: CanonicalDeserialize,
{
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = Bytes::deserialize_as(deserializer)?;
T::deserialize(&mut &bytes[..]).map_err(serde::de::Error::custom)
}
}
6 changes: 4 additions & 2 deletions tpke-wasm/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ pub fn participant_payload_serialization() {

#[test]
#[wasm_bindgen_test]
fn symmetric_encryption() {
fn encrypts_and_decrypts() {
let threshold = 3;
let shares_num = 5;
let num_entities = 5;
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();

let setup = Setup::new(threshold, shares_num, num_entities);
let setup_result = setup(threshold, shares_num, num_entities);
let public_key = setup_result.public_key().to_vec();
let private_key = setup_result.private_key().to_vec();

let ciphertext = encrypt(message.clone(), aad, setup.public_key);
let plaintext = decrypt(ciphertext, setup.private_key);
Expand Down
2 changes: 1 addition & 1 deletion tpke/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ miracl_core = "=2.3.0"
ark-ff = "0.3"
ark-ec = "0.3"
ark-poly = "0.3"
ark-serialize = { version = "0.3", features = ["derive"] }
ark-serialize = "0.3"
ark-std = "0.3"
ark-bls12-381 = "0.3"
itertools="0.10"
Expand Down
3 changes: 1 addition & 2 deletions tpke/src/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl<E: PairingEngine> Ciphertext<E> {
let mut bytes = Vec::new();
self.commitment.write(&mut bytes).unwrap();
self.auth_tag.write(&mut bytes).unwrap();
bytes.extend_from_slice(&self.ciphertext);
Copy link
Member

Choose a reason for hiding this comment

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

oops! it seems you removed here the ciphertext from the serialized output!

Copy link
Author

Choose a reason for hiding this comment

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

I find it very worrisome that this wasn't caught by tests.

bytes
}

Expand All @@ -62,8 +61,8 @@ impl<E: PairingEngine> Ciphertext<E> {

Self {
commitment,
auth_tag,
ciphertext,
auth_tag,
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions tpke/src/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

use crate::*;
use ark_ec::ProjectiveCurve;
use ark_serialize::CanonicalDeserialize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone)]
pub struct DecryptionShare<E: PairingEngine> {
pub decrypter_index: usize,
pub decryptor_index: usize,
pub decryption_share: E::G1Affine,
}

Expand Down Expand Up @@ -66,7 +64,7 @@ impl<E: PairingEngine> PrivateDecryptionContext<E> {
let blinding_keys = shares[0]
.iter()
.map(|d| {
self.public_decryption_contexts[d.decrypter_index]
self.public_decryption_contexts[d.decryptor_index]
.blinded_key_shares
.blinding_key_prepared
.clone()
Expand Down