# API Reference (`Ridge-Lab/moonmls`)

The authoritative machine-checked MoonBit public interfaces are tracked in `pkg.generated.mbti` across every package (`moon info --target all` + `git diff --exit-code -- '*.mbti'` in CI). This guide summarizes the primary high-level API.

## Root Package (`Ridge-Lab/moonmls`)

### KeyPackage Management

- `generate_key_package(suite, identity, seed, not_before?, not_after?, extensions?) -> KeyPackageBundle raise MlsError`
  - Generates a signed RFC 9420 `KeyPackage` with distinct `init_key` and `leaf_node.encryption_key` plus `BasicCredential(identity)`.
- `verify_key_package(suite, kp) -> Unit raise MlsError`
  - Enforces RFC 9420 §10.1: `protocol_version == mls10`, matching `cipher_suite`, `init_key != leaf_node.encryption_key`, `LeafNodeSource::KeyPackage`, valid LeafNode signature, and valid KeyPackage signature.
- `key_package_ref(suite, kp) -> Bytes raise MlsError`
- `proposal_ref(suite, auth_content) -> Bytes raise MlsError`
- `KeyPackageBundle::encode_message(self) -> Bytes raise MlsError`

### Group Creation & Joining

- `Group::create(suite, group_id, identity, seed, extensions?) -> Group raise MlsError`
  - Initializes an Epoch 0 single-member group with fresh leaf keys, initial `GroupContext`, `EpochSecrets`, and `SecretTree`.
- `Group::join_from_welcome(welcome_msg, bundle, external_tree?, external_psks?) -> Group raise MlsError`
  - Decrypts `EncryptedGroupSecrets` matching `key_package_ref`, recovers `joiner_secret` and `path_secret`, decrypts and verifies `GroupInfo` (signature, `ratchet_tree` hash, parent hashes, leaf signatures, `confirmation_tag`), resolves PSKs, and initializes the member's `Group` state.
- `Group::join_external(group_info, identity, leaf_seed, commit_leaf_seed, hpke_seed, external_tree?) -> (Group, MLSMessage) raise MlsError`
  - Implements RFC 9420 §12.4.3.2 External Commit: derives `kem_output` and `init_secret` via HPKE against the `external_pub` extension in `GroupInfo`, constructs a `NewMemberCommit` carrying an `ExternalInit` proposal and fresh `UpdatePath`, and returns both the joined `Group` and the broadcast `MLSMessage` commit.

### Proposals & Commits

- `Group::propose(self, proposal, reuse_guard?, encrypted?) -> (MLSMessage, Bytes) raise MlsError`
- `Group::propose_update(self, new_leaf_seed, reuse_guard?, encrypted?) -> (MLSMessage, Bytes) raise MlsError`
- `Group::stage_proposal(self, proposal_msg) -> Bytes raise MlsError`
- `Group::commit(self, inline_proposals?, include_staged?, leaf_seed, hpke_seed, reuse_guard?, encrypted?, include_ratchet_tree?, force_path?) -> CommitResult raise MlsError`
  - Validates and applies proposals in RFC 9420 §12.4.2 order (`Update` -> `Remove` -> `Add` -> `PSK` -> `GroupContextExtensions`), creates an `UpdatePath` when required (or when `force_path=true`), advances the Key Schedule, produces `CommitResult { commit_message, welcome, group_info }`, and updates local state.
- `Group::process_commit(self, commit_msg) -> Unit raise MlsError`
  - Handles member commits (`PublicMessage` and `PrivateMessage`) as well as `NewMemberCommit` (`ExternalInit`). Validates every signature, parent hash, and `confirmation_tag` on candidate state before committing atomically.

### End-to-End Encrypted Messaging & Exporters

- `Group::encrypt_application(self, plaintext, authenticated_data?, reuse_guard?, padding_len?) -> MLSMessage raise MlsError`
- `Group::decrypt_application(self, msg) -> DecryptedApplication raise MlsError`
  - Returns `DecryptedApplication { sender_leaf, generation, authenticated_data, plaintext }`. Rejects `PublicMessage` application payloads, tampered ciphertext/AAD/sender_data, non-zero padding, wrong epoch/group_id, and replayed generations.
- `Group::export_secret(self, label, context, length) -> Bytes raise MlsError` (RFC 9420 §8.5 `MLS-Exporter`)
- `Group::register_external_psk(self, psk_id, psk_value) -> Unit`

### State Inspection

- `Group::epoch(self) -> UInt64`
- `Group::group_id(self) -> Bytes`
- `Group::suite(self) -> CipherSuite`
- `Group::leaf_index(self) -> UInt32`
- `Group::member_count(self) -> Int32`
- `Group::members(self) -> Array[(UInt32, Bytes)]`
- `Group::tree_hash(self) -> Bytes`
- `Group::tree_snapshot(self) -> Array[TreeNode?]`
- `Group::export_ratchet_tree(self) -> Bytes raise MlsError`
- `Group::epoch_authenticator(self) -> Bytes`
- `Group::confirmed_transcript_hash(self) -> Bytes`
- `Group::interim_transcript_hash(self) -> Bytes`

## JS / Browser Foreign Library (`Ridge-Lab/moonmls/bridge`)

Built via `moon build --target js --release` into `_build/js/release/build/bridge/bridge.js`:

- Interactive session API: `session_create`, `gen_key_package_json`, `session_commit_add`, `session_commit_update`, `session_commit_remove`, `session_join_welcome`, `session_join_external`, `session_process_commit`, `session_encrypt`, `session_decrypt`, `session_export_secret`, `session_inspect`, `inspect_mls_message`.
- IETF vector verifiers: `verify_tree_math`, `verify_deserialization`, `verify_crypto_basics`, `verify_secret_tree`, `verify_key_schedule`, `verify_psk_secret`, `verify_transcript_hashes`, `verify_message_protection`, `verify_welcome`, `verify_tree_operations`, `verify_tree_validation`, `verify_treekem`, `verify_messages`, `verify_passive_client`.
