sodot_mpc/
lib.rs

1#![deny(missing_docs)]
2#![doc(html_logo_url = "https://docs.sodot.dev/img/Sodot_shape_only.svg")]
3#![doc(html_favicon_url = "https://docs.sodot.dev/img/favicon.ico")]
4//! # Sodot MPC
5//!
6//! The Sodot MPC provides a secure and efficient implementation of threshold cryptography
7//! using multi-party computation (MPC). This SDK enables distributed key generation and signing
8//! for various cryptographic schemes without requiring a trusted dealer.
9
10//!
11//! ## Key Features
12//!
13//! - **Threshold Key Generation**: Generate cryptographic keys across multiple parties
14//! - **Distributed Signing**: Sign messages collaboratively without exposing private keys
15//! - **Key Refresh**: Update secret shares while maintaining the same public key
16//! - **Multiple Schemes**: Support for ECDSA, Ed25519, BIP340, and SR25519
17//! - **Secure MPC**: All operations use secure multi-party computation protocols
18//!
19//! ## Basic Usage
20//!
21//! ```rust
22//! use sodot_mpc::{KeygenId, KeygenPrivateKey, RoomUUID};
23//!
24//! // Create types for MPC operations
25//! let keygen_id = KeygenId::new("2WpjFqPh8TqQKqZKZXgW8a".to_string());
26//! println!("Party ID: {:?}", keygen_id);
27//!
28//! // Create a private key from bytes
29//! let key_bytes = [1u8; 32]; // Example 32-byte array
30//! let private_key = KeygenPrivateKey::new(key_bytes);
31//! println!("Private key created: {:?}", private_key);
32//!
33//! // Create a room UUID
34//! let room_uuid = RoomUUID::new("550e8400-e29b-41d4-a716-446655440000".to_string());
35//! println!("Room UUID: {:?}", room_uuid);
36//! ```
37//!
38//! ## Error Handling
39//!
40//! All operations return [`Result`] types with [`SodotError`] providing detailed error information
41//! for all threshold cryptography operations.
42
43mod error;
44
45mod ffi;
46mod schemes;
47#[cfg(test)]
48#[allow(missing_docs)]
49#[cfg(not(miri))] // These tests use the Executor and cannot be currently run under miri
50mod tests;
51
52#[cfg(test)]
53pub(crate) const TEST_HOST_URL: &str = "us1-test.sodot.dev";
54
55pub use error::SodotError;
56pub use schemes::*;
57
58/// A specialized [`Result`] type for Sodot SDK operations that defaults to [`SodotError`] for the error type.
59pub type Result<T, E = SodotError> = std::result::Result<T, E>;
60
61/// A base58 string representing the ID of a party in the key generation protocol.
62///
63/// The `KeygenId` uniquely identifies each participant in the multi-party computation
64/// protocols for key generation, signing, and other operations. This ID is generated
65/// during the initialization phase and must be shared with all other parties.
66///
67/// # Examples
68///
69/// ```rust
70/// use sodot_mpc::KeygenId;
71///
72/// // Create a KeygenId from a base58 string
73/// let id = KeygenId::new("2WpjFqPh8TqQKqZKZXgW8a".to_string());
74/// println!("Keygen ID: {:?}", id);
75///
76/// // Access the inner string
77/// assert_eq!(id.as_str(), "2WpjFqPh8TqQKqZKZXgW8a");
78/// ```
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct KeygenId(String);
81
82/// A 32-byte private key used for initializing the key generation protocol.
83///
84/// The `KeygenPrivateKey` represents the private key material that corresponds to a
85/// [`KeygenId`]. This key is used to authenticate and secure the participant's
86/// involvement in the multi-party computation protocols.
87///
88/// # Security Notes
89///
90/// - This key should be kept secure and never shared with other parties
91/// - The key is exactly 32 bytes in length
92/// - It's generated automatically during the initialization phase
93///
94/// # Examples
95///
96/// ```rust
97/// use sodot_mpc::KeygenPrivateKey;
98///
99/// // Create a KeygenPrivateKey from a 32-byte array
100/// let key_bytes = [1u8; 32]; // Example 32-byte array
101/// let private_key = KeygenPrivateKey::new(key_bytes);
102/// println!("Created private key: {:?}", private_key);
103/// ```
104#[derive(Debug, Clone)]
105pub struct KeygenPrivateKey([u8; 32]);
106
107/// A hex string representing a room UUID for MPC operations.
108///
109/// A `RoomUUID` is created for each multi-party computation operation (such as key generation,
110/// signing, refresh, etc.) and acts as a session identifier to synchronize all involved parties.
111/// All parties participating in the same operation must use the same room UUID.
112///
113/// # Usage
114///
115/// Rooms are created by one party (typically the coordinator) and the UUID is shared with
116/// all other participants. The room coordinates the communication and synchronization
117/// between parties during the MPC protocol execution.
118///
119/// # Examples
120///
121/// ```rust
122/// use sodot_mpc::RoomUUID;
123///
124/// // Create a RoomUUID from a string
125/// let room_id = "00000002fddbfdea06319915f5862909".to_string();
126/// let room_uuid = RoomUUID::new(room_id);
127/// println!("Created room: {:?}", room_uuid);
128///
129/// // In tests, you can access the inner value
130/// #[cfg(test)]
131/// {
132///     assert_eq!(room_uuid.as_str(), "00000002fddbfdea06319915f5862909");
133/// }
134/// ```
135#[derive(Debug, Clone)]
136pub struct RoomUUID(String);
137
138impl KeygenId {
139    /// Creates a new `KeygenId` from a string.
140    pub fn new(id: String) -> Self {
141        debug_assert!(!id.is_empty());
142        KeygenId(id)
143    }
144
145    /// Returns the keygen ID as a string slice.
146    pub fn as_str(&self) -> &str {
147        &self.0
148    }
149}
150
151impl KeygenPrivateKey {
152    /// Creates a new `KeygenPrivateKey` from a 32-byte array.
153    pub fn new(bytes: [u8; 32]) -> Self {
154        KeygenPrivateKey(bytes)
155    }
156    /// Returns a reference to the underlying 32-byte array.
157    pub fn as_bytes(&self) -> &[u8; 32] {
158        &self.0
159    }
160    /// Consumes the `KeygenPrivateKey` and returns the underlying 32-byte array.
161    pub fn into_bytes(self) -> [u8; 32] {
162        self.0
163    }
164}
165
166impl RoomUUID {
167    /// Creates a new `RoomUUID` from a string.
168    ///
169    /// # Arguments
170    ///
171    /// * `uuid` - A string representing the room UUID
172    ///
173    /// # Returns
174    ///
175    /// A new `RoomUUID` instance.
176    pub fn new(uuid: String) -> Self {
177        RoomUUID(uuid)
178    }
179
180    /// Returns the room UUID as a string slice.
181    pub fn as_str(&self) -> &str {
182        &self.0
183    }
184}