MetaMUI Crypto Primitives - API Reference
Overview
This document provides comprehensive API documentation for the MetaMUI crypto primitives, including exact function signatures, struct names, and error types for all modules.
Rust Crypto Libraries Structure
The metamui-crypto-rust now use a flat structure with standalone modules:
metamui-hash/- Hash algorithms (SHA256, SHA512, BLAKE2b, BLAKE2s)metamui-ed25519/- Ed25519 digital signaturesmetamui-sr25519/- Sr25519 (Schnorrkel) signaturesmetamui-mnemonic/- BIP39 mnemonic phrase handlingmetamui-chacha20/- ChaCha20-Poly1305 encryptionmetamui-falcon512/- Falcon-512 post-quantum signatures
Each module is independent with its own error types and API.
MetaMUI Hash Module (metamui-hash)
Main Types
HashAlgorithmenum:Sha256,Sha512,Blake2b512,Blake2s256UnifiedHasherstruct implementingHashertrait
Key Functions
hash(algorithm: HashAlgorithm, input: Vec<u8>) -> Vec<u8>hash_hex(algorithm: HashAlgorithm, input: Vec<u8>) -> String
Legacy Functions
hash_sha256(input: Vec<u8>) -> Vec<u8>hash_sha512(input: Vec<u8>) -> Vec<u8>hash_blake2b512(input: Vec<u8>) -> Vec<u8>hash_blake2s256(input: Vec<u8>) -> Vec<u8>hash_string_sha256(input: String) -> Stringhash_string_sha512(input: String) -> Stringhash_string_blake2b512(input: String) -> Stringhash_string_blake2s256(input: String) -> String
MetaMUI Ed25519 Module (metamui-ed25519)
Main Types
Ed25519Signerstruct implementingSignertraitEd25519KeyPair { public_key: String, private_key: String }
Key Functions
generate_keypair() -> Ed25519KeyPairkeypair_from_seed(seed: Vec<u8>) -> Result<Ed25519KeyPair, CryptoError>sign(message: Vec<u8>, private_key: String) -> Result<String, CryptoError>verify(signature: String, message: Vec<u8>, public_key: String) -> Result<bool, CryptoError>sign_string(message: String, private_key: String) -> Result<String, CryptoError>verify_string(signature: String, message: String, public_key: String) -> Result<bool, CryptoError>
MetaMUI Sr25519 Module (metamui-sr25519)
Main Types
Sr25519Signerstruct implementingSignertraitSr25519KeyPair { public_key: String, seed: String }
Key Functions
generate_keypair() -> Sr25519KeyPairkeypair_from_seed(seed: Vec<u8>) -> Result<Sr25519KeyPair, CryptoError>sign(message: Vec<u8>, seed: String) -> Result<String, CryptoError>verify(signature: String, message: Vec<u8>, public_key: String) -> Result<bool, CryptoError>sign_string(message: String, seed: String) -> Result<String, CryptoError>verify_string(signature: String, message: String, public_key: String) -> Result<bool, CryptoError>
Derivation Functions (Substrate paths, as sp_core::sr25519::Pair::derive computes them)
derive_keypair_from_path(seed: &[u8], path: &str) -> Result<KeyPair, Sr25519Error>—private_keyis the 32-byte mini-secret when the last junction is hard, the 64-byte expanded secret when it is softderive_public_from_path(seed: &[u8], path: &str) -> Result<Vec<u8>, Sr25519Error>derive_public_key_from_path(public_key: &[u8], path: &str) -> Result<Vec<u8>, Sr25519Error>— soft junctions from a public key alone; a hard junction is an errorderive_from_path(seed_hex: String, path: String) -> Result<KeyPair, Sr25519Error>— the hex-string formderive_from_mnemonic(mnemonic: String, path: String)— not implemented here: usemetamui_mnemonic::derive_sr25519_keypair_with_pathgenerate_dev_keypair(name: String) -> Result<KeyPair, Sr25519Error>—//namefrom the development phrase ("Alice"→d43593c7…)keypair_from_private_key(private_key: Vec<u8>)— accepts a 32-byte mini-secret or a 64-byte expanded secret
MetaMUI Mnemonic Module (metamui-mnemonic)
Main Types
SigningAlgorithmenum:Ed25519,Sr25519DerivedKeyPair { public_key: String, algorithm: SigningAlgorithm }MnemonicConfig { word_count: u8 }- Note: Only English language support is available
Key Functions
generate_mnemonic(word_count: u8) -> Result<String, CryptoError>validate_mnemonic(mnemonic: String) -> boolmnemonic_to_seed(mnemonic: String) -> Result<Vec<u8>, CryptoError>mnemonic_to_seed_with_passphrase(mnemonic: String, passphrase: String) -> Result<Vec<u8>, CryptoError>derive_keypair(algorithm: SigningAlgorithm, mnemonic: String) -> Result<DerivedKeyPair, CryptoError>derive_ed25519_keypair(mnemonic: String) -> Result<DerivedKeyPair, CryptoError>derive_sr25519_keypair(mnemonic: String) -> Result<DerivedKeyPair, CryptoError>— the mini-secret is substrate-bip39’s (PBKDF2-HMAC-SHA512 over the BIP-39 entropy), assp_core::sr25519::Pair::from_phrasecomputes itmini_secret_from_phrase(phrase: &str, password: &str) -> Result<[u8; 32], MnemonicError>—passwordis Substrate’s///passwordcomponentderive_sr25519_keypair_with_path(phrase: &str, password: &str, path: &str) -> Result<(Vec<u8>, Vec<u8>), MnemonicError>— a Substrate URI in parts;//Alicefrom the dev phrase is Alicederive_key_from_path(phrase: String, path: &str, "sr25519")— the public key at a junction path (Ed25519 accepts no path)
MetaMUI ChaCha20 Module (metamui-chacha20)
Main Types
ChaCha20struct for encryption/decryption operationsEncryptionResult { ciphertext: Vec<u8>, nonce: [u8; 12] }
Key Functions
generate_key() -> [u8; 32]encrypt(plaintext: &[u8], key: &[u8; 32]) -> Result<EncryptionResult, ChaCha20Error>decrypt(ciphertext: &[u8], nonce: &[u8; 12], key: &[u8; 32]) -> Result<Vec<u8>, ChaCha20Error>encrypt_with_nonce(plaintext: &[u8], key: &[u8; 32], nonce: &[u8; 12]) -> Result<Vec<u8>, ChaCha20Error>
Legacy SubCrypto-RS (backup/subcrypto-rs/)
Note: This implementation has been moved to backup/subcrypto-rs/ and is no longer actively developed.
For new projects, use the individual metamui-* modules in rust-crypto-libraries/ instead.
Migration Guide from Legacy Implementation
If migrating from the legacy subcrypto-rs implementation:
- Replace
subcrypto_rsusage with individualmetamui_*modules - Use
metamui_hashfor all hashing operations (SHA256, SHA512, BLAKE2b, BLAKE2s) - Use
metamui_ed25519for Ed25519 digital signatures - Use
metamui_sr25519for Sr25519 (Schnorrkel) signatures - Use
metamui_mnemonicfor BIP39 mnemonic generation and derivation - Use
metamui_chacha20for ChaCha20-Poly1305 encryption operations
Key Differences
- Modular: Each metamui-* module is independent with no cross-dependencies
- Smaller binaries: Use only the crypto functions you need
- Better error handling: Each module has its own error types
- Improved performance: Optimized for specific use cases
Platform-Specific APIs
Kotlin/Android APIs
Hash Functions
class MetaMUIBlake2b256 {
fun hash(input: ByteArray): ByteArray
fun hashToHex(input: ByteArray): String
fun hashString(input: String): String
}
class MetaMUISha256 {
fun hash(input: ByteArray): ByteArray
fun hashToHex(input: ByteArray): String
fun hashString(input: String): String
}
Digital Signatures
class MetaMUIEd25519 {
fun generateKeyPair(): KeyPair
fun sign(message: ByteArray, privateKey: ByteArray): ByteArray
fun verify(signature: ByteArray, message: ByteArray, publicKey: ByteArray): Boolean
fun signString(message: String, privateKeyHex: String): String
fun verifyString(signature: String, message: String, publicKeyHex: String): Boolean
}
Encryption
class MetaMUIChaCha20 {
data class EncryptionResult(val ciphertext: ByteArray, val nonce: ByteArray)
fun generateKey(): ByteArray
fun encrypt(plaintext: ByteArray, key: ByteArray): EncryptionResult
fun decrypt(ciphertext: ByteArray, nonce: ByteArray, key: ByteArray): ByteArray
}
Swift/iOS APIs
Hash Functions
public class Blake2b256 {
public static func hash(_ data: Data) -> Data
public static func hashToHex(_ data: Data) -> String
public static func hashString(_ input: String) -> String
}
public class SHA256 {
public static func hash(_ data: Data) -> Data
public static func hashToHex(_ data: Data) -> String
public static func hashString(_ input: String) -> String
}
Digital Signatures
public class Ed25519 {
public struct KeyPair {
public let publicKey: Data
public let privateKey: Data
}
public static func generateKeyPair() -> KeyPair
public static func sign(_ message: Data, privateKey: Data) -> Data
public static func verify(_ signature: Data, message: Data, publicKey: Data) -> Bool
}
Python APIs
Hash Functions
class Blake2b256:
@staticmethod
def hash(data: bytes) -> bytes:
@staticmethod
def hash_to_hex(data: bytes) -> str:
@staticmethod
def hash_string(input_str: str) -> str:
class SHA256:
@staticmethod
def hash(data: bytes) -> bytes:
@staticmethod
def hash_to_hex(data: bytes) -> str:
@staticmethod
def hash_string(input_str: str) -> str:
Digital Signatures
class Ed25519:
@dataclass
class KeyPair:
public_key: bytes
private_key: bytes
@staticmethod
def generate_keypair() -> KeyPair:
@staticmethod
def sign(message: bytes, private_key: bytes) -> bytes:
@staticmethod
def verify(signature: bytes, message: bytes, public_key: bytes) -> bool:
TypeScript/JavaScript APIs
Hash Functions
export class Blake2b256 {
static hash(data: Uint8Array): Uint8Array;
static hashToHex(data: Uint8Array): string;
static hashString(input: string): string;
}
export class SHA256 {
static hash(data: Uint8Array): Uint8Array;
static hashToHex(data: Uint8Array): string;
static hashString(input: string): string;
}
Digital Signatures
export interface KeyPair {
publicKey: Uint8Array;
privateKey: Uint8Array;
}
export class Ed25519 {
static generateKeyPair(): KeyPair;
static sign(message: Uint8Array, privateKey: Uint8Array): Uint8Array;
static verify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean;
}
MetaMUI Falcon-512 Module (metamui-falcon512)
Rust API
// Main Types
pub struct PublicKey { /* ... */ }
pub struct PrivateKey { /* ... */ }
pub struct KeyPair { pub public_key: PublicKey, pub private_key: PrivateKey }
pub struct Signature { /* ... */ }
// Key Functions
pub fn generate_keypair<R: Rng>(rng: &mut R) -> Result<KeyPair, Falcon512Error>
pub fn sign<R: Rng>(message: &[u8], private_key: &PrivateKey, rng: &mut R) -> Result<Signature, Falcon512Error>
pub fn verify(message: &[u8], signature: &Signature, public_key: &PublicKey) -> bool
pub fn serialize_public_key(public_key: &PublicKey) -> Result<Vec<u8>, Falcon512Error>
pub fn deserialize_public_key(bytes: &[u8]) -> Result<PublicKey, Falcon512Error>
TypeScript API
export interface Falcon512KeyPair {
publicKey: PublicKey;
privateKey: PrivateKey;
}
export class Falcon512 {
static generateKeyPair(): KeyPair;
static sign(message: Uint8Array, privateKey: PrivateKey): Signature;
static verify(message: Uint8Array, signature: Signature, publicKey: PublicKey): boolean;
static serializePublicKey(publicKey: PublicKey): Uint8Array;
static deserializePublicKey(bytes: Uint8Array): PublicKey;
}
Python API
@dataclass
class KeyPair:
public_key: PublicKey
private_key: PrivateKey
def generate_keypair() -> KeyPair:
def sign(message: bytes, private_key: PrivateKey) -> Signature:
def verify(message: bytes, signature: Signature, public_key: PublicKey) -> bool:
def serialize_public_key(public_key: PublicKey) -> bytes:
def deserialize_public_key(data: bytes) -> PublicKey:
Kotlin API
data class Falcon512KeyPair(val publicKey: PublicKey, val privateKey: PrivateKey)
object Falcon512 {
fun generateKeyPair(): KeyPair
fun sign(message: ByteArray, privateKey: PrivateKey): Signature
fun verify(message: ByteArray, signature: Signature, publicKey: PublicKey): Boolean
fun serializePublicKey(publicKey: PublicKey): ByteArray
fun deserializePublicKey(bytes: ByteArray): PublicKey
}
Swift API
public struct Falcon512KeyPair {
public let publicKey: Falcon512PublicKey
public let privateKey: Falcon512PrivateKey
}
public struct Falcon512 {
public static func generateKeyPair() throws -> Falcon512KeyPair
public static func sign(message: Data, privateKey: Falcon512PrivateKey) throws -> Falcon512Signature
public static func verify(message: Data, signature: Falcon512Signature, publicKey: Falcon512PublicKey) -> Bool
public static func serializePublicKey(_ publicKey: Falcon512PublicKey) -> Data
public static func deserializePublicKey(_ data: Data) throws -> Falcon512PublicKey
}
Error Handling
All APIs use consistent error handling patterns:
- Rust:
Result<T, CryptoError>types - Kotlin: Exception throwing with try/catch or
Result<T>wrapper classes - Swift: Error throwing with
throwskeyword orResult<T, Error>types - Python: Exception raising with custom exception classes
- TypeScript: Error throwing with custom error classes
Module Independence
The new flat structure provides:
- Independent modules: Each metamui-* module is standalone
- No cross-dependencies: Modules don’t depend on each other
- Selective compilation: Use only the modules you need
- Smaller binaries: Reduced final binary size
- Clear separation: Each module has focused responsibility
Test Status Summary
✅ Working Implementations
- Rust: All standalone modules in rust-crypto-libraries/ are fully functional
- Kotlin: Platform-specific implementations available for all modules
- Swift: Ready for testing with standard implementations
- Python: Ready for testing with standard implementations
- TypeScript: Ready for testing with standard implementations
- Legacy: backup/subcrypto-rs/ remains available for compatibility
The modular API provides:
- Independent modules for specific cryptographic operations
- Platform-specific wrappers for native language integration
- Consistent error handling across all modules
- Memory-safe implementations using Rust
- Cross-platform compatibility testing