Skip to content

Commit

Permalink
Strings instead of bytes in Conditions and Context
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Sep 14, 2022
1 parent 17cd741 commit 82cbb7b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 48 deletions.
20 changes: 12 additions & 8 deletions nucypher-core-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ pub struct Conditions {
#[pymethods]
impl Conditions {
#[new]
pub fn new(conditions: &[u8]) -> Self {
pub fn new(conditions: &str) -> Self {
Self {
backend: nucypher_core::Conditions::new(conditions),
}
}

#[staticmethod]
pub fn from_bytes(data: &[u8]) -> Self {
Self::new(data)
pub fn from_string(conditions: String) -> Self {
Self {
backend: conditions.into(),
}
}

fn __bytes__(&self) -> &[u8] {
fn __str__(&self) -> &str {
self.backend.as_ref()
}
}
Expand All @@ -102,18 +104,20 @@ pub struct Context {
#[pymethods]
impl Context {
#[new]
pub fn new(context: &[u8]) -> Self {
pub fn new(context: &str) -> Self {
Self {
backend: nucypher_core::Context::new(context),
}
}

#[staticmethod]
pub fn from_bytes(data: &[u8]) -> Self {
Self::new(data)
pub fn from_bytes(context: String) -> Self {
Self {
backend: context.into(),
}
}

fn __bytes__(&self) -> &[u8] {
fn __str__(&self) -> &str {
self.backend.as_ref()
}
}
Expand Down
24 changes: 14 additions & 10 deletions nucypher-core-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,19 @@ pub struct Conditions(nucypher_core::Conditions);
#[wasm_bindgen]
impl Conditions {
#[wasm_bindgen(constructor)]
pub fn new(conditions: &[u8]) -> Self {
pub fn new(conditions: &str) -> Self {
Self(nucypher_core::Conditions::new(conditions))
}

#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(data: &[u8]) -> Self {
Self::new(data)
pub fn from_bytes(data: &str) -> Self {
let data_owned: String = data.into();
Self(nucypher_core::Conditions::from(data_owned))
}

#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Box<[u8]> {
#[allow(clippy::inherent_to_string)]
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
self.0.as_ref().into()
}
}
Expand All @@ -98,17 +100,19 @@ pub struct Context(nucypher_core::Context);
#[wasm_bindgen]
impl Context {
#[wasm_bindgen(constructor)]
pub fn new(context: &[u8]) -> Self {
pub fn new(context: &str) -> Self {
Self(nucypher_core::Context::new(context))
}

#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(data: &[u8]) -> Self {
Self::new(data)
pub fn from_bytes(data: &str) -> Self {
let data_owned: String = data.into();
Self(nucypher_core::Context::from(data_owned))
}

#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Box<[u8]> {
#[allow(clippy::inherent_to_string)]
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
self.0.as_ref().into()
}
}
Expand Down
24 changes: 12 additions & 12 deletions nucypher-core-wasm/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ pub fn node_metadata_of_js_value(js_value: JsValue) -> Option<NodeMetadata> {
fn make_message_kit(
sk: &SecretKey,
plaintext: &[u8],
conditions: Option<impl AsRef<[u8]>>,
conditions: Option<impl AsRef<str>>,
) -> MessageKit {
let policy_encrypting_key = sk.public_key();
MessageKit::new(
&policy_encrypting_key,
plaintext,
conditions.map(|bytes| Conditions::new(bytes.as_ref())),
conditions.map(|s| Conditions::new(s.as_ref())),
)
}

Expand Down Expand Up @@ -130,7 +130,7 @@ fn make_metadata_response_payload() -> (MetadataResponsePayload, Vec<NodeMetadat
fn message_kit_decrypts() {
let sk = SecretKey::random();
let plaintext = "Hello, world!".as_bytes();
let conditions = Some(b"{'llamas': 'yes'}");
let conditions = Some("{'llamas': 'yes'}");
let message_kit = make_message_kit(&sk, plaintext, conditions);

let decrypted = message_kit.decrypt(&sk).unwrap().to_vec();
Expand All @@ -146,7 +146,7 @@ fn message_kit_decrypt_reencrypted() {
let delegating_sk = SecretKey::random();
let delegating_pk = delegating_sk.public_key();
let plaintext = b"Hello, world!";
let conditions = Some(&b"{'hello': 'world'}");
let conditions = Some(&"{'hello': 'world'}");
let message_kit = make_message_kit(&delegating_sk, plaintext, conditions);

// Create key fragments for reencryption
Expand Down Expand Up @@ -194,7 +194,7 @@ fn message_kit_to_bytes_from_bytes() {
let sk = SecretKey::random();
let plaintext = b"Hello, world!";

let conditions = Some(&b"{'hello': 'world'}");
let conditions = Some(&"{'hello': 'world'}");
let message_kit = make_message_kit(&sk, plaintext, conditions);

assert_eq!(
Expand Down Expand Up @@ -369,7 +369,7 @@ fn reencryption_request_from_bytes_to_bytes() {
// Make capsules
let publisher_sk = SecretKey::random();
let plaintext = b"Hello, world!";
let conditions = Some(&b"{'hello': 'world'}");
let conditions = Some(&"{'hello': 'world'}");
let message_kit = make_message_kit(&publisher_sk, plaintext, conditions);
let capsules = vec![message_kit.capsule()];

Expand All @@ -381,8 +381,8 @@ fn reencryption_request_from_bytes_to_bytes() {
let signer = Signer::new(&publisher_sk);
let verified_kfrags = make_kfrags(&publisher_sk, &receiving_sk);
let encrypted_kfrag = EncryptedKeyFrag::new(&signer, &receiving_pk, &hrac, &verified_kfrags[0]);
let conditions = Some(Conditions::new(b"{'some': 'condition'}"));
let context = Some(Context::new(b"{'user': 'context'}"));
let conditions = Some(Conditions::new("{'some': 'condition'}"));
let context = Some(Context::new("{'user': 'context'}"));

// Make reencryption request
let reencryption_request = ReencryptionRequestBuilder::new(
Expand Down Expand Up @@ -422,7 +422,7 @@ fn reencryption_response_verify() {
// Make capsules
let policy_encrypting_key = alice_sk.public_key();
let plaintext = b"Hello, world!";
let conditions = Some(b"{'hello': 'world'}");
let conditions = Some("{'hello': 'world'}");

let message_kit = make_message_kit(&alice_sk, plaintext, conditions);
let capsules: Vec<Capsule> = kfrags.iter().map(|_| message_kit.capsule()).collect();
Expand Down Expand Up @@ -488,12 +488,12 @@ fn reencryption_response_verify() {
#[wasm_bindgen_test]
fn retrieval_kit() {
// Make a message kit
let conditions_bytes = b"{'hello': 'world'}";
let conditions = Some(Conditions::new(conditions_bytes));
let conditions_str = "{'hello': 'world'}";
let conditions = Some(Conditions::new(conditions_str));
let message_kit = make_message_kit(
&SecretKey::random(),
b"Hello, world!",
Some(&conditions_bytes),
Some(&conditions_str),
);

let retrieval_kit_from_mk = RetrievalKit::from_message_kit(&message_kit);
Expand Down
27 changes: 13 additions & 14 deletions nucypher-core/src/conditions.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
use alloc::boxed::Box;
use alloc::string::String;

use serde::{Deserialize, Serialize};
use umbral_pre::serde_bytes;

/// Reencryption conditions.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct Conditions(#[serde(with = "serde_bytes::as_base64")] Box<[u8]>);
pub struct Conditions(String);

impl Conditions {
/// Creates a new conditions object.
pub fn new(conditions: &[u8]) -> Self {
pub fn new(conditions: &str) -> Self {
Self(conditions.into())
}
}

impl AsRef<[u8]> for Conditions {
fn as_ref(&self) -> &[u8] {
impl AsRef<str> for Conditions {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl From<Box<[u8]>> for Conditions {
fn from(source: Box<[u8]>) -> Self {
impl From<String> for Conditions {
fn from(source: String) -> Self {
Self(source)
}
}

/// Context for reencryption conditions.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct Context(#[serde(with = "serde_bytes::as_base64")] Box<[u8]>);
pub struct Context(String);

impl Context {
/// Creates a new context object.
pub fn new(context: &[u8]) -> Self {
pub fn new(context: &str) -> Self {
Self(context.into())
}
}

impl AsRef<[u8]> for Context {
fn as_ref(&self) -> &[u8] {
impl AsRef<str> for Context {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl From<Box<[u8]>> for Context {
fn from(source: Box<[u8]>) -> Self {
impl From<String> for Context {
fn from(source: String) -> Self {
Self(source)
}
}
8 changes: 4 additions & 4 deletions nucypher-core/src/reencryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ mod tests {
&encrypted_kfrag,
&some_trinket,
&another_trinket,
Some(&Conditions::new(&[47u8])),
Some(&Context::new(&[51u8])),
Some(&Conditions::new("abcd")),
Some(&Context::new("efgh")),
);
let conditions = request.conditions.unwrap();
assert!(conditions.as_ref()[0].eq(&47u8));
assert_eq!(conditions.as_ref(), "abcd");

let context = request.context.unwrap();
assert!(context.as_ref()[0].eq(&51u8));
assert_eq!(context.as_ref(), "efgh");
}
}

0 comments on commit 82cbb7b

Please sign in to comment.