|
1 | 1 | // SPDX-License-Identifier: FSL-1.1 |
2 | 2 |
|
| 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; |
3 | 8 | 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; |
62 | 11 |
|
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