ShieldedKeyDeriver

@ThreadSafe
object ShieldedKeyDeriver

JNI bridge to Midnight's Rust cryptography for deriving shielded keys.

Architecture:

Kotlin (Android) → JNI → Rust FFI → Midnight Ledger (Rust)

Why JNI? Midnight's shielded key derivation uses:

  • Blake2b hashing (90% of operations)

  • JubJub elliptic curve (10% of operations - ZKP-friendly)

Reimplementing JubJub curve in Kotlin is risky for a wallet. Instead, we use Midnight's battle-tested Rust implementation via JNI (98% confidence vs 85% for pure Kotlin).

Native Library: This class loads libkuira_crypto_ffi.so (Android) which is compiled from:

  • Location: kuira-crypto-ffi/

  • Dependencies: midnight-zswap, midnight-serialize (version-abstract — see Cargo.toml)

Version Compatibility: The Rust FFI is the version abstraction boundary — Kotlin depends on stable function signatures, not Midnight version internals. See MIDNIGHT_GUIDELINES.md for version details.

Thread Safety: This object is thread-safe. The underlying Rust functions are pure and stateless.

Memory Safety:

  • The JNI bridge allocates C strings for results

  • Native memory is freed automatically after copying to Kotlin strings

  • The seed ByteArray is NOT cleared by this function - caller must wipe it

Usage:

val seed = deriveSeedFromBIP32()  // 32 bytes at m/44'/2400'/0'/3/0
try {
val keys = ShieldedKeyDeriver.deriveKeys(seed)
println("Coin PK: ${keys.coinPublicKey}")
println("Enc PK: ${keys.encryptionPublicKey}")
} finally {
MemoryUtils.wipe(seed) // CRITICAL: Always wipe seed
}

Error Handling:

  • Returns null if FFI call fails (invalid seed, native library error, etc.)

  • Logs error details to stderr in the native library

  • On Android, check Logcat for native error messages

References:

  • Rust FFI: kuira-crypto-ffi/src/lib.rs

  • Algorithm: docs/SHIELDED_ADDRESS_ALGORITHM.md

  • POC Results: docs/SHIELDED_JNI_POC_RESULTS.md

Functions

Link copied to clipboard

Derives shielded public keys from a 32-byte seed using Midnight's algorithm.

Link copied to clipboard

Gets the error message if native library failed to load.

Link copied to clipboard

Checks if the native library is loaded and ready to use.