Skip to content

Commit 0a17df5

Browse files
authored
Merge pull request #14 from cryptidtech/ml/traits-async
Add Async traits and a WaitQueue
2 parents 47e5230 + 90222fd commit 0a17df5

File tree

5 files changed

+712
-147
lines changed

5 files changed

+712
-147
lines changed

crates/bs-traits/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ readme = "README.md"
88
license = "Functional Source License 1.1"
99

1010
[dependencies]
11+
async-trait = "0.1"
1112
thiserror.workspace = true
1213

14+
[dev-dependencies]
15+
futures = "0.3"
16+
tokio = { version = "1", features = ["full"] }
17+
1318
[lints]
1419
workspace = true

crates/bs-traits/src/async.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// SPDX-License-Identifier: FSL-1.1
2+
3+
use crate::*;
4+
use async_trait::*;
5+
use std::num::NonZeroUsize;
6+
7+
/// Trait for types that can sign data asynchronously
8+
#[async_trait]
9+
pub trait AsyncSigner: Signer {
10+
/// Attempt to sign the data asynchronously
11+
async fn try_sign_async(&self, key: &Self::Key, data: &[u8]) -> Result<Self::Signature, Error>;
12+
13+
/// Sign the data asynchronously
14+
async fn sign_async(&self, key: &Self::Key, data: &[u8]) -> Self::Signature {
15+
self.try_sign_async(key, data)
16+
.await
17+
.expect("signing operation failed")
18+
}
19+
}
20+
21+
/// Trait for types that can verify data asynchronously
22+
#[async_trait]
23+
pub trait AsyncVerifier: Verifier {
24+
/// Verify the data asynchronously
25+
async fn verify_async(
26+
&self,
27+
key: &Self::Key,
28+
data: &[u8],
29+
signature: &Self::Signature,
30+
) -> Result<(), Error>;
31+
}
32+
33+
/// Trait for types that can encrypt data asynchronously
34+
#[async_trait]
35+
pub trait AsyncEncryptor: Encryptor {
36+
/// Attempt to encrypt the data asynchronously
37+
async fn try_encrypt_async(
38+
&self,
39+
key: &Self::Key,
40+
plaintext: &Self::Plaintext,
41+
) -> Result<Self::Ciphertext, Error>;
42+
43+
/// Encrypt the data asynchronously
44+
async fn encrypt_async(
45+
&self,
46+
key: &Self::Key,
47+
plaintext: &Self::Plaintext,
48+
) -> Self::Ciphertext {
49+
self.try_encrypt_async(key, plaintext)
50+
.await
51+
.expect("encryption operation failed")
52+
}
53+
}
54+
55+
/// Trait for types that can decrypt data asynchronously
56+
#[async_trait]
57+
pub trait AsyncDecryptor: Decryptor {
58+
/// Decrypt the data asynchronously
59+
async fn decrypt_async(
60+
&self,
61+
key: &Self::Key,
62+
ciphertext: &Self::Ciphertext,
63+
) -> Result<Self::Plaintext, Error>;
64+
}
65+
66+
/// Trait for types that can split a secret into shares asynchronously
67+
#[async_trait]
68+
pub trait AsyncSecretSplitter: SecretSplitter {
69+
/// Split the secret into shares asynchronously
70+
async fn split_async(
71+
&self,
72+
secret: &Self::Secret,
73+
threshold: NonZeroUsize,
74+
limit: NonZeroUsize,
75+
) -> Result<Self::Output, Error>;
76+
77+
/// Split the secret into shares with the given identifiers asynchronously
78+
async fn split_with_identifiers_async(
79+
&self,
80+
secret: &Self::Secret,
81+
threshold: NonZeroUsize,
82+
identifiers: &[Self::Identifier],
83+
) -> Result<Self::Output, Error>;
84+
}
85+
86+
/// Trait for types that can combine shares into a secret asynchronously
87+
#[async_trait]
88+
pub trait AsyncSecretCombiner: SecretCombiner {
89+
/// Combine the shares into a secret asynchronously
90+
async fn combine_async(
91+
&self,
92+
shares: &[(Self::Identifier, Self::Shares)],
93+
) -> Result<Self::Secret, Error>;
94+
}
95+
96+
/// Trait for types that can get a key asynchronously
97+
#[async_trait]
98+
pub trait AsyncGetKey: GetKey {
99+
/// Get the key asynchronously
100+
async fn get_key_async(
101+
&self,
102+
key_path: &Self::KeyPath,
103+
codec: &Self::Codec,
104+
threshold: usize,
105+
limit: usize,
106+
) -> Result<Self::Key, Error>;
107+
}

crates/bs-traits/src/lib.rs

Lines changed: 11 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,15 @@
11
// SPDX-License-Identifier: FSL-1.1
22

3+
/// `bs-traits` is a crate that provides traits for asynchronous and synchronous operations.
4+
///
5+
/// It also provides a `WaitQueue` type that can be used to implement synchronous and asynchronous operations
6+
/// without having to use tokio::block_in_place or similar.
7+
mod r#async;
38
mod error;
4-
pub use error::Error;
5-
6-
use core::num::NonZeroUsize;
7-
8-
/// Trait for types that can sign data
9-
pub trait Signer {
10-
/// The type of key used to sign
11-
type Key;
12-
/// The type of signature
13-
type Signature;
14-
15-
/// Attempt to sign the data
16-
fn try_sign(&self, key: &Self::Key, data: &[u8]) -> Result<Self::Signature, Error>;
17-
18-
/// Sign the data and return the signature
19-
fn sign(&self, key: &Self::Key, data: &[u8]) -> Self::Signature {
20-
self.try_sign(key, data).expect("signing operation failed")
21-
}
22-
}
23-
24-
/// Trait for types that can verify signatures
25-
pub trait Verifier {
26-
/// The type of key used to verify
27-
type Key;
28-
/// The type of signature
29-
type Signature;
30-
31-
/// Verify that the provided signature for the given data is authentic
32-
fn verify(
33-
&self,
34-
key: &Self::Key,
35-
data: &[u8],
36-
signature: &Self::Signature,
37-
) -> Result<(), Error>;
38-
}
39-
40-
/// Trait for types that can encrypt data
41-
pub trait Encryptor {
42-
/// The type of key used to encrypt
43-
type Key;
44-
/// The type of ciphertext
45-
type Ciphertext;
46-
/// The type of plaintext, might include the nonce, and additional authenticated data
47-
type Plaintext;
48-
49-
/// Attempt to encrypt the plaintext
50-
fn try_encrypt(
51-
&self,
52-
key: &Self::Key,
53-
plaintext: &Self::Plaintext,
54-
) -> Result<Self::Ciphertext, Error>;
55-
56-
/// Encrypt the plaintext
57-
fn encrypt(&self, key: &Self::Key, plaintext: &Self::Plaintext) -> Self::Ciphertext {
58-
self.try_encrypt(key, plaintext)
59-
.expect("encryption operation failed")
60-
}
61-
}
9+
mod sync;
10+
mod wait_queue;
6211

63-
/// Trait for types that can decrypt data
64-
pub trait Decryptor {
65-
/// The type of key used to decrypt
66-
type Key;
67-
/// The type of ciphertext
68-
type Ciphertext;
69-
/// The type of plaintext
70-
type Plaintext;
71-
72-
/// Attempt to decrypt the ciphertext
73-
fn decrypt(
74-
&self,
75-
key: &Self::Key,
76-
ciphertext: &Self::Ciphertext,
77-
) -> Result<Self::Plaintext, Error>;
78-
}
79-
80-
/// Trait for types that can split a secret into shares
81-
pub trait SecretSplitter {
82-
/// The type of secret to split
83-
type Secret;
84-
/// The type of identifier for the shares
85-
type Identifier;
86-
/// The output from splitting the secret.
87-
/// Might include the threshold and limit used to split the secret,
88-
/// the shares, and the verifiers, identifiers,
89-
/// or any other information needed to reconstruct the secret
90-
/// and verify the shares.
91-
type Output;
92-
93-
/// Split the secret into shares.
94-
///
95-
/// Conditions for `split` to succeed:
96-
/// - Threshold must be less than or equal to limit.
97-
/// - Threshold must be greater than or equal to 2.
98-
fn split(
99-
&self,
100-
secret: &Self::Secret,
101-
threshold: NonZeroUsize,
102-
limit: NonZeroUsize,
103-
) -> Result<Self::Output, Error>;
104-
105-
/// Split the secret into shares with the given identifiers.
106-
/// The number of shares will be equal to the number of identifiers i.e. the `limit`.
107-
///
108-
/// Conditions for `split_with_identifiers` to succeed:
109-
/// - Threshold must be less than or equal to the number of identifiers.
110-
/// - Threshold must be greater than or equal to 2.
111-
/// - Identifiers must be unique.
112-
/// - Identifiers must not be empty.
113-
fn split_with_identifiers(
114-
&self,
115-
secret: &Self::Secret,
116-
threshold: NonZeroUsize,
117-
identifiers: &[Self::Identifier],
118-
) -> Result<Self::Output, Error>;
119-
}
120-
121-
/// Trait for types that can combine shares into a secret
122-
pub trait SecretCombiner {
123-
/// The type of secret to combine
124-
type Secret;
125-
/// The type of identifier for the shares
126-
type Identifier;
127-
/// The type of shares to combine
128-
type Shares;
129-
130-
/// Combine the shares into a secret
131-
fn combine(&self, shares: &[(Self::Identifier, Self::Shares)]) -> Result<Self::Secret, Error>;
132-
}
133-
134-
/// Trait for types that can retrieve a key
135-
pub trait GetKey {
136-
/// The type of key
137-
type Key;
138-
/// The type of key path
139-
type KeyPath;
140-
/// The type of codec
141-
type Codec;
142-
143-
/// Get the key
144-
fn get_key(
145-
&self,
146-
key_path: &Self::KeyPath,
147-
codec: &Self::Codec,
148-
threshold: usize,
149-
limit: usize,
150-
) -> Result<Self::Key, Error>;
151-
}
12+
pub use error::Error;
13+
pub use r#async::*;
14+
pub use sync::*;
15+
pub use wait_queue::*;

0 commit comments

Comments
 (0)