Skip to content

Commit

Permalink
update wasm bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Apr 27, 2023
1 parent b1f1ec1 commit 1cc7036
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 522 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ferveo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ serde_with = "2.2.0"
bincode = "1.3.3"
rand = "0.8"
rand_core = "0.6"
hex = "0.4.3"
thiserror = "1.0.40"

[dev-dependencies]
ark-bls12-381 = "0.4.0"
2 changes: 1 addition & 1 deletion ferveo-common/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn from_bytes<T: CanonicalDeserialize>(
Ok(item)
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct PreparedPublicKey<E: Pairing> {
pub encryption_key: E::G2Prepared,
}
Expand Down
74 changes: 0 additions & 74 deletions ferveo-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,7 @@
use std::{cmp::Ordering, fmt::Display, str::FromStr};

use ark_ec::pairing::Pairing;
use serde::{Deserialize, Serialize};
use thiserror::Error;

pub mod keypair;
pub mod serialization;
pub mod utils;

pub use keypair::*;
pub use serialization::*;
pub use utils::*;

#[derive(
Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash,
)]
pub struct EthereumAddress(String);

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum EthereumAddressParseError {
#[error("Invalid Ethereum address length.")]
InvalidLength,

#[error("Invalid hex value in Ethereum address.")]
InvalidHex,
}

impl FromStr for EthereumAddress {
type Err = EthereumAddressParseError;

fn from_str(s: &str) -> Result<EthereumAddress, EthereumAddressParseError> {
if s.len() != 42 {
return Err(EthereumAddressParseError::InvalidLength);
}
hex::decode(&s[2..])
.map_err(|_| EthereumAddressParseError::InvalidHex)?;
Ok(EthereumAddress(s.to_string()))
}
}

impl Display for EthereumAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
/// Represents an external validator
pub struct Validator<E: Pairing> {
/// The established address of the validator
pub address: EthereumAddress,
/// The Public key
pub public_key: PublicKey<E>,
}

impl<E: Pairing> PartialOrd for Validator<E> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.address.partial_cmp(&other.address)
}
}

impl<E: Pairing> Ord for Validator<E> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.address.cmp(&other.address)
}
}

impl<E: Pairing> Validator<E> {
pub fn new(
address: String,
public_key: PublicKey<E>,
) -> Result<Self, EthereumAddressParseError> {
Ok(Self {
address: EthereumAddress::from_str(&address)?,
public_key,
})
}
}
7 changes: 0 additions & 7 deletions ferveo-python/examples/server_api_precomputed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
combine_decryption_shares_precomputed,
decrypt_with_shared_secret,
Keypair,
PublicKey,
Validator,
Transcript,
Dkg,
Ciphertext,
DecryptionSharePrecomputed,
AggregatedTranscript,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
)


Expand Down
7 changes: 0 additions & 7 deletions ferveo-python/examples/server_api_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
combine_decryption_shares_simple,
decrypt_with_shared_secret,
Keypair,
PublicKey,
Validator,
Transcript,
Dkg,
Ciphertext,
DecryptionShareSimple,
AggregatedTranscript,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
)


Expand Down
29 changes: 14 additions & 15 deletions ferveo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ use pyo3::{
use rand::thread_rng;

fn from_py_bytes<T: FromBytes>(bytes: &[u8]) -> PyResult<T> {
T::from_bytes(bytes).map_err(map_py_error)
T::from_bytes(bytes).map_err(map_py_err)
}

fn to_py_bytes<T: ToBytes>(t: T) -> PyResult<PyObject> {
let bytes = t.to_bytes().map_err(map_py_error)?;
let bytes = t.to_bytes().map_err(map_py_err)?;
Ok(Python::with_gil(|py| -> PyObject {
PyBytes::new(py, &bytes).into()
}))
}

fn map_py_error<T: fmt::Display>(err: T) -> PyErr {
fn map_py_err<T: fmt::Display>(err: T) -> PyErr {
PyValueError::new_err(format!("{}", err))
}

Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn encrypt(
let rng = &mut thread_rng();
let ciphertext =
ferveo::api::encrypt(message, aad, &dkg_public_key.0 .0, rng)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(Ciphertext(ciphertext))
}

Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn decrypt_with_shared_secret(
&shared_secret.0 .0,
&dkg_params.0.g1_inv,
)
.map_err(map_py_error)
.map_err(map_py_err)
}

#[pyclass(module = "ferveo")]
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Keypair {
#[staticmethod]
pub fn from_secure_randomness(bytes: &[u8]) -> PyResult<Self> {
let keypair = ferveo::api::Keypair::<E>::from_secure_randomness(bytes)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(Self(keypair))
}

Expand Down Expand Up @@ -205,7 +205,7 @@ impl PublicKey {
}

fn __hash__(&self) -> PyResult<isize> {
let bytes = self.0.to_bytes().map_err(map_py_error)?;
let bytes = self.0.to_bytes().map_err(map_py_err)?;
hash("PublicKey", &bytes)
}
}
Expand All @@ -219,7 +219,7 @@ impl Validator {
#[new]
pub fn new(address: String, public_key: &PublicKey) -> PyResult<Self> {
let validator = ferveo::api::Validator::new(address, public_key.0)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(Self(validator))
}

Expand Down Expand Up @@ -292,7 +292,7 @@ impl Dkg {
&validators,
&me.0,
)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(Self(dkg))
}

Expand All @@ -303,8 +303,7 @@ impl Dkg {

pub fn generate_transcript(&self) -> PyResult<Transcript> {
let rng = &mut thread_rng();
let transcript =
self.0.generate_transcript(rng).map_err(map_py_error)?;
let transcript = self.0.generate_transcript(rng).map_err(map_py_err)?;
Ok(Transcript(transcript))
}

Expand All @@ -319,7 +318,7 @@ impl Dkg {
let aggregated_transcript = self
.0
.aggregate_transcripts(&messages)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(AggregatedTranscript(aggregated_transcript))
}

Expand Down Expand Up @@ -402,7 +401,7 @@ impl AggregatedTranscript {
.map(|ValidatorMessage(v, t)| (v.0, t.0))
.collect();
let is_valid =
self.0.verify(shares_num, &messages).map_err(map_py_error)?;
self.0.verify(shares_num, &messages).map_err(map_py_err)?;
Ok(is_valid)
}

Expand All @@ -421,7 +420,7 @@ impl AggregatedTranscript {
aad,
&validator_keypair.0,
)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(DecryptionSharePrecomputed(decryption_share))
}

Expand All @@ -440,7 +439,7 @@ impl AggregatedTranscript {
aad,
&validator_keypair.0,
)
.map_err(map_py_error)?;
.map_err(map_py_err)?;
Ok(DecryptionShareSimple(decryption_share))
}

Expand Down
8 changes: 0 additions & 8 deletions ferveo-python/test/test_ferveo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
combine_decryption_shares_precomputed,
decrypt_with_shared_secret,
Keypair,
PublicKey,
Validator,
Transcript,
Dkg,
Ciphertext,
DecryptionShareSimple,
DecryptionSharePrecomputed,
AggregatedTranscript,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
)


Expand Down
5 changes: 0 additions & 5 deletions ferveo-python/test/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from ferveo_py import (
Keypair,
PublicKey,
Validator,
Transcript,
Dkg,
AggregatedTranscript,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
)


Expand Down
10 changes: 8 additions & 2 deletions ferveo-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ wee_alloc = { version = "0.4.5" }
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
serde_with = "2.0.1"
serde-wasm-bindgen = "0.5.0"
ark-serialize = { version = "0.4", features = ["derive"] }
js-sys = "0.3.61"
getrandom = { version = "0.2", features = ["js"] }
serde-wasm-bindgen = "0.5.0"
itertools = "0.10.1"

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
console_error_panic_hook = "0.1.7"
rand_core = "0.6"
itertools = "0.10.1"

[package.metadata.cargo-machete]
ignore = ["getrandom"]
ignored = ["getrandom"]

# TODO: Temporarily disabled.
[package.metadata.wasm-pack.profile.release]
wasm-opt = false
Loading

0 comments on commit 1cc7036

Please sign in to comment.