Skip to content

Commit

Permalink
Code cleanup based on RFCs from #52.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed May 20, 2023
1 parent 124a8a2 commit 9cb7921
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 56 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `EncryptedThresholdDecryptionRequest`/`EncryptedThresholdDecryptionResponse` types and bindings. ([#52])`


### Changed

- Bumped MSRV to 1.65. ([#52])


[#52]: https://github.com/nucypher/nucypher-core/pull/52


Expand Down
2 changes: 1 addition & 1 deletion nucypher-core-python/nucypher_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
MetadataResponse,
MetadataResponsePayload,
ThresholdDecryptionRequest,
E2EEThresholdDecryptionRequest,
E2EThresholdDecryptionRequest,
ThresholdDecryptionResponse,
EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse,
Expand Down
34 changes: 12 additions & 22 deletions nucypher-core-python/nucypher_core/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,15 @@ class ThresholdDecryptionRequest:
def __init__(self, ritual_id: int, variant: int, ciphertext: bytes, conditions: Optional[Conditions], context: Optional[Context]):
...

def id(self) -> int:
...
ritual_id: int

def conditions(self) -> Optional[Conditions]:
...
conditions: Optional[Conditions]

def context(self) -> Optional[Context]:
...
context: Optional[Context]

def variant(self) -> int:
...
variant: int

def ciphertext(self) -> bytes:
...
ciphertext: bytes

def encrypt(self, request_encrypting_key: PublicKey, response_encrypting_key: PublicKey) -> EncryptedThresholdDecryptionRequest:
...
Expand All @@ -434,30 +429,27 @@ class ThresholdDecryptionRequest:
...


class E2EEThresholdDecryptionRequest:
class E2EThresholdDecryptionRequest:

def decryption_request(self) -> ThresholdDecryptionRequest:
...
decryption_request: ThresholdDecryptionRequest

def response_encrypting_key(self) -> PublicKey:
...
response_encrypting_key: PublicKey

@staticmethod
def from_bytes(data: bytes) -> E2EEThresholdDecryptionRequest:
def from_bytes(data: bytes) -> E2EThresholdDecryptionRequest:
...

def __bytes__(self) -> bytes:
...


class EncryptedThresholdDecryptionRequest:
def id(self) -> int:
...
ritual_id: int

def decrypt(
self,
sk: SecretKey
) -> E2EEThresholdDecryptionRequest:
) -> E2EThresholdDecryptionRequest:
...

@staticmethod
Expand All @@ -473,8 +465,7 @@ class ThresholdDecryptionResponse:
def __init__(self, decryption_share: bytes):
...

def decryption_share(self) -> bytes:
...
decryption_share: bytes

def encrypt(self, encrypting_key: PublicKey) -> EncryptedThresholdDecryptionResponse:
...
Expand All @@ -487,7 +478,6 @@ class ThresholdDecryptionResponse:
...



class EncryptedThresholdDecryptionResponse:

def decrypt(
Expand Down
24 changes: 12 additions & 12 deletions nucypher-core-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ pub struct ThresholdDecryptionRequest {
impl ThresholdDecryptionRequest {
#[new]
pub fn new(
id: u16,
ritual_id: u16,
variant: u8,
ciphertext: &[u8], // TODO use ferveo Ciphertext type
conditions: Option<&Conditions>,
Expand All @@ -663,7 +663,7 @@ impl ThresholdDecryptionRequest {

Ok(Self {
backend: nucypher_core::ThresholdDecryptionRequest::new(
id,
ritual_id,
ciphertext,
conditions
.map(|conditions| conditions.backend.clone())
Expand All @@ -675,7 +675,7 @@ impl ThresholdDecryptionRequest {
}

#[getter]
pub fn id(&self) -> u16 {
pub fn ritual_id(&self) -> u16 {
self.backend.ritual_id
}

Expand Down Expand Up @@ -734,16 +734,16 @@ impl ThresholdDecryptionRequest {
}

//
// E2EEThresholdDecryptionRequest
// E2EThresholdDecryptionRequest
//
#[pyclass(module = "nucypher_core")]
#[derive(derive_more::From, derive_more::AsRef)]
pub struct E2EEThresholdDecryptionRequest {
backend: nucypher_core::E2EEThresholdDecryptionRequest,
pub struct E2EThresholdDecryptionRequest {
backend: nucypher_core::E2EThresholdDecryptionRequest,
}

#[pymethods]
impl E2EEThresholdDecryptionRequest {
impl E2EThresholdDecryptionRequest {
#[getter]
pub fn decryption_request(&self) -> ThresholdDecryptionRequest {
self.backend.decryption_request.clone().into()
Expand All @@ -756,7 +756,7 @@ impl E2EEThresholdDecryptionRequest {

#[staticmethod]
pub fn from_bytes(data: &[u8]) -> PyResult<Self> {
from_bytes::<_, nucypher_core::E2EEThresholdDecryptionRequest>(data)
from_bytes::<_, nucypher_core::E2EThresholdDecryptionRequest>(data)
}

fn __bytes__(&self) -> PyObject {
Expand All @@ -777,14 +777,14 @@ pub struct EncryptedThresholdDecryptionRequest {
#[pymethods]
impl EncryptedThresholdDecryptionRequest {
#[getter]
pub fn id(&self) -> u16 {
pub fn ritual_id(&self) -> u16 {
self.backend.ritual_id
}

pub fn decrypt(&self, sk: &SecretKey) -> PyResult<E2EEThresholdDecryptionRequest> {
pub fn decrypt(&self, sk: &SecretKey) -> PyResult<E2EThresholdDecryptionRequest> {
self.backend
.decrypt(sk.as_ref())
.map(E2EEThresholdDecryptionRequest::from)
.map(E2EThresholdDecryptionRequest::from)
.map_err(|err| PyValueError::new_err(format!("{}", err)))
}

Expand Down Expand Up @@ -1333,7 +1333,7 @@ fn _nucypher_core(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<MetadataResponsePayload>()?;
m.add_class::<MetadataResponse>()?;
m.add_class::<ThresholdDecryptionRequest>()?;
m.add_class::<E2EEThresholdDecryptionRequest>()?;
m.add_class::<E2EThresholdDecryptionRequest>()?;
m.add_class::<ThresholdDecryptionResponse>()?;
m.add_class::<EncryptedThresholdDecryptionRequest>()?;
m.add_class::<EncryptedThresholdDecryptionResponse>()?;
Expand Down
24 changes: 12 additions & 12 deletions nucypher-core-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ impl ThresholdDecryptionRequest {
)))
}

#[wasm_bindgen(getter)]
pub fn id(&self) -> u16 {
#[wasm_bindgen(getter, js_name = ritualId)]
pub fn ritual_id(&self) -> u16 {
self.0.ritual_id
}

Expand Down Expand Up @@ -613,26 +613,26 @@ impl ThresholdDecryptionRequest {
}

//
// E2EEThresholdDecryptionRequest
// E2EThresholdDecryptionRequest
//

#[wasm_bindgen]
#[derive(PartialEq, Debug, derive_more::From, derive_more::AsRef)]
pub struct E2EEThresholdDecryptionRequest(nucypher_core::E2EEThresholdDecryptionRequest);
pub struct E2EThresholdDecryptionRequest(nucypher_core::E2EThresholdDecryptionRequest);

#[wasm_bindgen]
impl E2EEThresholdDecryptionRequest {
impl E2EThresholdDecryptionRequest {
#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(data: &[u8]) -> Result<E2EEThresholdDecryptionRequest, Error> {
from_bytes::<_, nucypher_core::E2EEThresholdDecryptionRequest>(data)
pub fn from_bytes(data: &[u8]) -> Result<E2EThresholdDecryptionRequest, Error> {
from_bytes::<_, nucypher_core::E2EThresholdDecryptionRequest>(data)
}

#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Box<[u8]> {
to_bytes(self)
}

#[wasm_bindgen(getter, js_name=decryption_request)]
#[wasm_bindgen(getter, js_name=decryptionRequest)]
pub fn decryption_request(&self) -> ThresholdDecryptionRequest {
ThresholdDecryptionRequest::from(self.0.decryption_request.clone())
}
Expand All @@ -653,16 +653,16 @@ pub struct EncryptedThresholdDecryptionRequest(nucypher_core::EncryptedThreshold

#[wasm_bindgen]
impl EncryptedThresholdDecryptionRequest {
#[wasm_bindgen(getter)]
pub fn id(&self) -> u16 {
#[wasm_bindgen(getter, js_name = ritualId)]
pub fn ritual_id(&self) -> u16 {
self.0.ritual_id
}

pub fn decrypt(&self, sk: &SecretKey) -> Result<E2EEThresholdDecryptionRequest, Error> {
pub fn decrypt(&self, sk: &SecretKey) -> Result<E2EThresholdDecryptionRequest, Error> {
self.0
.decrypt(sk.as_ref())
.map_err(map_js_err)
.map(E2EEThresholdDecryptionRequest)
.map(E2EThresholdDecryptionRequest)
}

#[wasm_bindgen(js_name = fromBytes)]
Expand Down
2 changes: 1 addition & 1 deletion nucypher-core-wasm/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ fn threshold_decryption_request() {
EncryptedThresholdDecryptionRequest::from_bytes(&encrypted_request_bytes).unwrap();

assert_eq!(encrypted_request_from_bytes, encrypted_request);
assert_eq!(encrypted_request_from_bytes.id(), ritual_id);
assert_eq!(encrypted_request_from_bytes.ritual_id(), ritual_id);

let e2e_request = encrypted_request_from_bytes
.decrypt(&request_secret)
Expand Down
14 changes: 7 additions & 7 deletions nucypher-core/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ impl<'a> ProtocolObject<'a> for ThresholdDecryptionRequest {}

/// A request for an Ursula to derive a decryption share that specifies the key to encrypt Ursula's response.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct E2EEThresholdDecryptionRequest {
pub struct E2EThresholdDecryptionRequest {
/// The decryption request.
pub decryption_request: ThresholdDecryptionRequest,
/// The key to encrypt the corresponding decryption response.
pub response_encrypting_key: PublicKey,
}

impl E2EEThresholdDecryptionRequest {
impl E2EThresholdDecryptionRequest {
/// Create E2E decryption request.
pub fn new(
decryption_request: &ThresholdDecryptionRequest,
Expand All @@ -114,7 +114,7 @@ impl E2EEThresholdDecryptionRequest {
}
}

impl<'a> ProtocolObjectInner<'a> for E2EEThresholdDecryptionRequest {
impl<'a> ProtocolObjectInner<'a> for E2EThresholdDecryptionRequest {
fn version() -> (u16, u16) {
(1, 0)
}
Expand All @@ -136,7 +136,7 @@ impl<'a> ProtocolObjectInner<'a> for E2EEThresholdDecryptionRequest {
}
}

impl<'a> ProtocolObject<'a> for E2EEThresholdDecryptionRequest {}
impl<'a> ProtocolObject<'a> for E2EThresholdDecryptionRequest {}

/// An encrypted request for an Ursula to derive a decryption share.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand All @@ -156,7 +156,7 @@ impl EncryptedThresholdDecryptionRequest {
response_encrypting_key: &PublicKey,
) -> Self {
let e2e_decryption_request =
E2EEThresholdDecryptionRequest::new(request, response_encrypting_key);
E2EThresholdDecryptionRequest::new(request, response_encrypting_key);
// TODO: using Umbral for encryption to avoid introducing more crypto primitives.
let (capsule, ciphertext) =
encrypt(request_encrypting_key, &e2e_decryption_request.to_bytes())
Expand All @@ -173,11 +173,11 @@ impl EncryptedThresholdDecryptionRequest {
pub fn decrypt(
&self,
sk: &SecretKey,
) -> Result<E2EEThresholdDecryptionRequest, DecryptionError> {
) -> Result<E2EThresholdDecryptionRequest, DecryptionError> {
let decryption_request_bytes = decrypt_original(sk, &self.capsule, &self.ciphertext)
.map_err(DecryptionError::DecryptionFailed)?;
let decryption_request =
E2EEThresholdDecryptionRequest::from_bytes(&decryption_request_bytes)
E2EThresholdDecryptionRequest::from_bytes(&decryption_request_bytes)
.map_err(DecryptionError::DeserializationFailed)?;
Ok(decryption_request)
}
Expand Down
2 changes: 1 addition & 1 deletion nucypher-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct VerificationError;
pub use address::Address;
pub use conditions::{Conditions, Context};
pub use dkg::{
E2EEThresholdDecryptionRequest, EncryptedThresholdDecryptionRequest,
E2EThresholdDecryptionRequest, EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse, FerveoVariant, ThresholdDecryptionRequest,
ThresholdDecryptionResponse,
};
Expand Down

0 comments on commit 9cb7921

Please sign in to comment.