Skip to main content

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_sui_privkey

Description

Encodes a 32-byte Ed25519 private key into a Bech32 string with the Sui prefix suiprivkey1.
The output includes the 0x00 scheme flag byte and a valid 6-character Bech32 checksum, producing a fully compatible Sui private key string.

int microsui_encode_sui_privkey(const uint8_t *privkey_bytes, char *privkey_bech_output)

Parameters

NameTypeDescription
privkey_bytesconst uint8_t *Input bytes array of the Private/Secret Key in bytes format
privkey_bech_outputchar *Output buffer for the encoded Private/Secret Key in Bech32 format

Returns

  • 0 on success.
  • -1 if encoding fails (e.g. due to bad input, insufficient buffer size or internal conversion failure).

Example usage

Given:

  • A defined privkey_bytes: Private/Secret Key in Bytes format.

You can decode the key as follows:

char privkey_bech_output[71]; // We initialize it empty

int status = microsui_encode_sui_privkey(privkey_bytes, privkey_bech_output);

if (status == 0)
// Decoded key is now stored in privkey_bech_output

microsui_decode_sui_privkey

Description

Decode a Sui Bech32 private key (secret key) string into 32 raw bytes.
Validates the Bech32-encoded private key (HRP = "suiprivkey", no mixed case), converts the 5-bit words back to 8-bit bytes, and extracts the 32-byte secret (sk) after the 1-byte scheme flag.

int microsui_decode_sui_privkey(const char *privkey_bech, uint8_t privkey_bytes_output[32])

Parameters

NameTypeDescription
privkey_bechconst char *Input string of the Private/Secret Key in Bech32 format
privkey_bytes_outputuint8_t[32]Output buffer for the decoded Private/Secret Key in bytes format

Returns

  • 0 on success.
  • -1 on fail.

Example usage

Given:

  • A defined privkey_bech: Private/Secret Key in Bech32 format. E.g.: suiprivkey1...

You can decode the key as follows:

uint8_t private_key[97];  // We initialize it empty

int status = microsui_decode_sui_privkey(privkey_bech, private_key);

if (status == 0)
// Decoded key is now stored in private_key