cryptography.c (Cryptography)
This file implements core cryptographic utility functions for MicroSui.
It provides encoding and decoding helpers for Sui-compatible key formats, including Bech32 private key representation with the suiprivkey prefix.
These utilities are used to convert between raw cryptographic key material and the standardized formats expected by the Sui ecosystem.
All functions are designed for deterministic behavior and compatibility with resource-constrained and embedded environments.
File Location
Defined in microsui-lib/src/microsui_core/cryptography.c
microsui_encode_bech32_private_key
Description
Encodes a 32-byte Ed25519 seed into a Bech32 string with the Sui prefix suiprivkey1.
The encoded payload includes the 0x00 Ed25519 scheme flag before the seed and a 6-character Bech32 checksum. The caller must provide an output buffer with room for at least PK_BECH32_LEN + 1 characters, including the null terminator.
int microsui_encode_bech32_private_key(char *private_key_bech_out, const uint8_t *seed)
Parameters
| Name | Type | Description |
|---|---|---|
private_key_bech_out | char * | Output buffer for the encoded Bech32 private key; provide at least PK_BECH32_LEN + 1 bytes |
seed | const uint8_t * | Input pointer to the 32-byte Ed25519 seed |
Returns
0on success.-1if encoding fails (e.g. due to bad input, insufficient buffer size or internal conversion failure).
Example usage
Given:
- A 32-byte Ed25519 seed named
seed.
Encode the seed as follows:
char private_key_bech_out[PK_BECH32_LEN + 1];
int status = microsui_encode_bech32_private_key(private_key_bech_out, seed);
if (status == 0)
// Bech32 private key is now stored in private_key_bech_out
microsui_decode_bech32_private_key
Description
Decode a Sui Bech32 private key string into a 32-byte Ed25519 seed.
Validates the Bech32-encoded private key (HRP = suiprivkey, no mixed case), converts the 5-bit words back to 8-bit bytes, and copies the 32-byte seed after the 1-byte scheme flag into the output buffer.
int microsui_decode_bech32_private_key(uint8_t seed_out[32], const char *private_key_bech)
Parameters
| Name | Type | Description |
|---|---|---|
seed_out | uint8_t[32] | Output buffer for the decoded 32-byte Ed25519 seed |
private_key_bech | const char * | Null-terminated Bech32 private key string to decode |
Returns
0on success.-1if the string has an invalid length or format, uses mixed case, has an invalid alphabet/checksum, or cannot be decoded.
Example usage
Given a Bech32 private key string private_key_bech (for example, one beginning with suiprivkey1), decode it as follows:
uint8_t seed_out[32];
int status = microsui_decode_bech32_private_key(seed_out, private_key_bech);
if (status == 0)
// Decoded 32-byte seed is now stored in seed_out