Skip to content

Commit

Permalink
export py module making utility
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 2, 2023
1 parent 10cc1df commit 3b02634
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 80 deletions.
3 changes: 1 addition & 2 deletions ferveo-python/ferveo/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Keypair:
...

@staticmethod
def secure_randomness_size(data: bytes) -> int:
def secure_randomness_size() -> int:
...

@staticmethod
Expand Down Expand Up @@ -174,7 +174,6 @@ def encrypt(message: bytes, add: bytes, dkg_public_key: DkgPublicKey) -> Ciphert

def combine_decryption_shares_simple(
decryption_shares: Sequence[DecryptionShareSimple],
dkg_public_params: DkgPublicParameters,
) -> bytes:
...

Expand Down
79 changes: 1 addition & 78 deletions ferveo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,5 @@ use pyo3::prelude::*;

#[pymodule]
fn ferveo_py(py: Python, m: &PyModule) -> PyResult<()> {
// Functions
m.add_function(wrap_pyfunction!(encrypt, m)?)?;
m.add_function(wrap_pyfunction!(combine_decryption_shares_simple, m)?)?;
m.add_function(wrap_pyfunction!(
combine_decryption_shares_precomputed,
m
)?)?;
m.add_function(wrap_pyfunction!(decrypt_with_shared_secret, m)?)?;

// Classes
m.add_class::<Keypair>()?;
m.add_class::<FerveoPublicKey>()?;
m.add_class::<Validator>()?;
m.add_class::<Transcript>()?;
m.add_class::<Dkg>()?;
m.add_class::<Ciphertext>()?;
m.add_class::<DecryptionShareSimple>()?;
m.add_class::<DecryptionSharePrecomputed>()?;
m.add_class::<AggregatedTranscript>()?;
m.add_class::<DkgPublicKey>()?;
m.add_class::<DkgPublicParameters>()?;
m.add_class::<SharedSecret>()?;

// Exceptions
m.add(
"ThresholdEncryptionError",
py.get_type::<ThresholdEncryptionError>(),
)?;
m.add(
"InvalidShareNumberParameter",
py.get_type::<InvalidShareNumberParameter>(),
)?;
m.add(
"InvalidDkgStateToDeal",
py.get_type::<InvalidDkgStateToDeal>(),
)?;
m.add(
"InvalidDkgStateToAggregate",
py.get_type::<InvalidDkgStateToAggregate>(),
)?;
m.add(
"InvalidDkgStateToVerify",
py.get_type::<InvalidDkgStateToVerify>(),
)?;
m.add(
"InvalidDkgStateToIngest",
py.get_type::<InvalidDkgStateToIngest>(),
)?;
m.add(
"DealerNotInValidatorSet",
py.get_type::<DealerNotInValidatorSet>(),
)?;
m.add("UnknownDealer", py.get_type::<UnknownDealer>())?;
m.add("DuplicateDealer", py.get_type::<DuplicateDealer>())?;
m.add(
"InvalidPvssTranscript",
py.get_type::<InvalidPvssTranscript>(),
)?;
m.add(
"InsufficientTranscriptsForAggregate",
py.get_type::<InsufficientTranscriptsForAggregate>(),
)?;
m.add("InvalidDkgPublicKey", py.get_type::<InvalidDkgPublicKey>())?;
m.add(
"InsufficientValidators",
py.get_type::<InsufficientValidators>(),
)?;
m.add(
"InvalidTranscriptAggregate",
py.get_type::<InvalidTranscriptAggregate>(),
)?;
m.add("ValidatorsNotSorted", py.get_type::<ValidatorsNotSorted>())?;
m.add(
"ValidatorPublicKeyMismatch",
py.get_type::<ValidatorPublicKeyMismatch>(),
)?;
m.add("SerializationError", py.get_type::<SerializationError>())?;
Ok(())
make_ferveo_py_module(py, m)
}
102 changes: 102 additions & 0 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,108 @@ impl AggregatedTranscript {
}
}

// Since adding functions in pyo3 requires a two-step process
// (`#[pyfunction]` + `wrap_pyfunction!`), and `wrap_pyfunction`
// needs `#[pyfunction]` in the same module, we need these trampolines
// to build modules externally.

pub fn register_decrypt_with_shared_secret(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(decrypt_with_shared_secret, m)?)
}

pub fn register_combine_decryption_shares_precomputed(
m: &PyModule,
) -> PyResult<()> {
m.add_function(wrap_pyfunction!(combine_decryption_shares_precomputed, m)?)
}

pub fn register_combine_decryption_shares_simple(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(combine_decryption_shares_simple, m)?)
}

pub fn register_encrypt(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(encrypt, m)?)
}

pub fn make_ferveo_py_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
// Functions
register_encrypt(m)?;
register_combine_decryption_shares_simple(m)?;
register_combine_decryption_shares_precomputed(m)?;
register_decrypt_with_shared_secret(m)?;

// Classes
m.add_class::<Keypair>()?;
m.add_class::<FerveoPublicKey>()?;
m.add_class::<Validator>()?;
m.add_class::<Transcript>()?;
m.add_class::<Dkg>()?;
m.add_class::<Ciphertext>()?;
m.add_class::<DecryptionShareSimple>()?;
m.add_class::<DecryptionSharePrecomputed>()?;
m.add_class::<AggregatedTranscript>()?;
m.add_class::<DkgPublicKey>()?;
m.add_class::<DkgPublicParameters>()?;
m.add_class::<SharedSecret>()?;

// Exceptions
m.add(
"ThresholdEncryptionError",
py.get_type::<ThresholdEncryptionError>(),
)?;
m.add(
"InvalidShareNumberParameter",
py.get_type::<InvalidShareNumberParameter>(),
)?;
m.add(
"InvalidDkgStateToDeal",
py.get_type::<InvalidDkgStateToDeal>(),
)?;
m.add(
"InvalidDkgStateToAggregate",
py.get_type::<InvalidDkgStateToAggregate>(),
)?;
m.add(
"InvalidDkgStateToVerify",
py.get_type::<InvalidDkgStateToVerify>(),
)?;
m.add(
"InvalidDkgStateToIngest",
py.get_type::<InvalidDkgStateToIngest>(),
)?;
m.add(
"DealerNotInValidatorSet",
py.get_type::<DealerNotInValidatorSet>(),
)?;
m.add("UnknownDealer", py.get_type::<UnknownDealer>())?;
m.add("DuplicateDealer", py.get_type::<DuplicateDealer>())?;
m.add(
"InvalidPvssTranscript",
py.get_type::<InvalidPvssTranscript>(),
)?;
m.add(
"InsufficientTranscriptsForAggregate",
py.get_type::<InsufficientTranscriptsForAggregate>(),
)?;
m.add("InvalidDkgPublicKey", py.get_type::<InvalidDkgPublicKey>())?;
m.add(
"InsufficientValidators",
py.get_type::<InsufficientValidators>(),
)?;
m.add(
"InvalidTranscriptAggregate",
py.get_type::<InvalidTranscriptAggregate>(),
)?;
m.add("ValidatorsNotSorted", py.get_type::<ValidatorsNotSorted>())?;
m.add(
"ValidatorPublicKeyMismatch",
py.get_type::<ValidatorPublicKeyMismatch>(),
)?;
m.add("SerializationError", py.get_type::<SerializationError>())?;

Ok(())
}

// TODO: Consider adding remaining ferveo/api.rs tests here
#[cfg(test)]
mod test_ferveo_python {
Expand Down

0 comments on commit 3b02634

Please sign in to comment.