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

Fix incorrect signatures in .pyi stubs #131

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion ferveo-python/examples/server_api_precomputed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
decrypt_with_shared_secret,
Keypair,
Validator,
ValidatorMessage,
Dkg,
AggregatedTranscript,
)
Expand Down Expand Up @@ -38,7 +39,7 @@ def gen_eth_addr(i: int) -> str:
validators=validators,
me=sender,
)
messages.append((sender, dkg.generate_transcript()))
messages.append(ValidatorMessage(sender, dkg.generate_transcript()))

# Every validator can aggregate the transcripts
dkg = Dkg(
Expand Down
3 changes: 2 additions & 1 deletion ferveo-python/examples/server_api_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
decrypt_with_shared_secret,
Keypair,
Validator,
ValidatorMessage,
Dkg,
AggregatedTranscript,
)
Expand Down Expand Up @@ -36,7 +37,7 @@ def gen_eth_addr(i: int) -> str:
validators=validators,
me=sender,
)
messages.append((sender, dkg.generate_transcript()))
messages.append(ValidatorMessage(sender, dkg.generate_transcript()))

# Now that every validator holds a dkg instance and a transcript for every other validator,
# every validator can aggregate the transcripts
Expand Down
2 changes: 1 addition & 1 deletion ferveo-python/ferveo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
DecryptionSharePrecomputed,
AggregatedTranscript,
DkgPublicKey,
DkgPublicParameters,
SharedSecret,
ValidatorMessage,
ThresholdEncryptionError,
InvalidShareNumberParameter,
InvalidDkgStateToDeal,
Expand Down
21 changes: 17 additions & 4 deletions ferveo-python/ferveo/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence, Tuple
from typing import Sequence


class Keypair:
Expand Down Expand Up @@ -65,6 +65,19 @@ class DkgPublicKey:
...


class ValidatorMessage:

def __init__(
self,
validator: Validator,
transcript: Transcript,
):
...

validator: Validator
transcript: Transcript


class Dkg:

def __init__(
Expand All @@ -82,7 +95,7 @@ class Dkg:
def generate_transcript(self) -> Transcript:
...

def aggregate_transcripts(self, messages: Sequence[Tuple[Validator, Transcript]]) -> AggregatedTranscript:
def aggregate_transcripts(self, messages: Sequence[ValidatorMessage]) -> AggregatedTranscript:
...


Expand Down Expand Up @@ -115,10 +128,10 @@ class DecryptionSharePrecomputed:

class AggregatedTranscript:

def __init__(self, messages: Sequence[Tuple[Validator, Transcript]]):
def __init__(self, messages: Sequence[ValidatorMessage]):
...

def verify(self, shares_num: int, messages: Sequence[Tuple[Validator, Transcript]]) -> bool:
def verify(self, shares_num: int, messages: Sequence[ValidatorMessage]) -> bool:
...

def create_decryption_share_simple(
Expand Down
4 changes: 2 additions & 2 deletions ferveo-python/test/test_ferveo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
decrypt_with_shared_secret,
Keypair,
Validator,
ValidatorMessage,
Dkg,
AggregatedTranscript,
DkgPublicKey,
Expand Down Expand Up @@ -57,7 +58,7 @@ def scenario_for_variant(variant, shares_num, threshold, shares_to_use):
validators=validators,
me=sender,
)
messages.append((sender, dkg.generate_transcript()))
messages.append(ValidatorMessage(sender, dkg.generate_transcript()))

dkg = Dkg(
tau=tau,
Expand Down Expand Up @@ -144,7 +145,6 @@ def test_precomputed_tdec_doesnt_have_enough_messages():
for threshold in range(1, shares_num):
TEST_CASES_WITH_THRESHOLD_RANGE.append((variant, shares_num, threshold))


# Avoid running this test case as it takes a long time
# @pytest.mark.parametrize("variant, shares_num, threshold", TEST_CASES_WITH_THRESHOLD_RANGE)
# def test_reproduce_nucypher_issue(variant, shares_num, threshold):
Expand Down
53 changes: 37 additions & 16 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,33 @@ impl DkgPublicKey {
}
}

// TODO: Consider using a `pyclass` instead
#[derive(FromPyObject, Clone)]
pub struct ValidatorMessage(Validator, Transcript);
#[pyclass(module = "ferveo")]
#[derive(derive_more::From, derive_more::AsRef, Clone)]
pub struct ValidatorMessage(api::ValidatorMessage);

#[pymethods]
impl ValidatorMessage {
#[new]
pub fn new(validator: &Validator, transcript: &Transcript) -> Self {
Self((validator.0.clone(), transcript.0.clone()))
}

#[getter]
pub fn validator(&self) -> Validator {
Validator(self.0 .0.clone())
}

#[getter]
pub fn transcript(&self) -> Transcript {
Transcript(self.0 .1.clone())
}
}

impl ValidatorMessage {
pub(crate) fn to_inner(&self) -> api::ValidatorMessage {
self.0.clone()
}
}

#[pyclass(module = "ferveo")]
#[derive(derive_more::From, derive_more::AsRef)]
Expand Down Expand Up @@ -415,10 +439,7 @@ impl Dkg {
&mut self,
messages: Vec<ValidatorMessage>,
) -> PyResult<AggregatedTranscript> {
let messages: Vec<_> = messages
.iter()
.map(|m| ((m.0).0.clone(), (m.1).0.clone()))
.collect();
let messages: Vec<_> = messages.iter().map(|m| m.to_inner()).collect();
let aggregated_transcript = self
.0
.aggregate_transcripts(&messages)
Expand Down Expand Up @@ -465,10 +486,8 @@ generate_common_methods!(AggregatedTranscript);
impl AggregatedTranscript {
#[new]
pub fn new(messages: Vec<ValidatorMessage>) -> Self {
let messages: Vec<_> = messages
.into_iter()
.map(|ValidatorMessage(v, t)| (v.0, t.0))
.collect();
let messages: Vec<_> =
messages.into_iter().map(|vm| vm.to_inner()).collect();
Self(api::AggregatedTranscript::new(&messages))
}

Expand All @@ -477,10 +496,8 @@ impl AggregatedTranscript {
shares_num: u32,
messages: Vec<ValidatorMessage>,
) -> PyResult<bool> {
let messages: Vec<_> = messages
.into_iter()
.map(|ValidatorMessage(v, t)| (v.0, t.0))
.collect();
let messages: Vec<_> =
messages.into_iter().map(|vm| vm.to_inner()).collect();
let is_valid = self
.0
.verify(shares_num, &messages)
Expand Down Expand Up @@ -569,6 +586,7 @@ pub fn make_ferveo_py_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<AggregatedTranscript>()?;
m.add_class::<DkgPublicKey>()?;
m.add_class::<SharedSecret>()?;
m.add_class::<ValidatorMessage>()?;

// Exceptions
m.add(
Expand Down Expand Up @@ -668,7 +686,10 @@ mod test_ferveo_python {
&sender,
)
.unwrap();
ValidatorMessage(sender, dkg.generate_transcript().unwrap())
ValidatorMessage::new(
&sender,
&dkg.generate_transcript().unwrap(),
)
})
.collect();
(messages, validators, validator_keypairs)
Expand Down