# `ExCEP.Crypto.SM2`

Pure Elixir implementation of **SM2** digital signatures (GB/T 32918.2-2016) over `sm2p256v1`.

Implements `ExCEP.Crypto.Signer` and produces/verifies standard 64-byte `<<r::256, s::256>>`
signatures required by CSG CEP TS2.2 Section 4.8 and Section 5 (Authentication).

## Key Format
- Private key: 32-byte big-endian binary (`<<d::unsigned-big-256>>`)
- Public key: 64-byte uncompressed raw coordinates (`<<x::256, y::256>>`) or 65-byte SEC1
  uncompressed (`<<0x04, x::256, y::256>>`)

## Signer Options
- `:private_key` - 32-byte private key binary (required for `sign/2`)
- `:public_key` - 64- or 65-byte public key binary (optional for `sign/2`, derived from
  `:private_key` if omitted; required for `verify/3` unless `:peer_public_key` is set)
- `:peer_public_key` - 64- or 65-byte peer public key binary (used by `verify/3` if present)
- `:user_id` - SM2 user ID binary (default `"1234567812345678"` per GB/T 32918.2)

# `generate_keypair`

```elixir
@spec generate_keypair() :: {&lt;&lt;_::512&gt;&gt;, &lt;&lt;_::256&gt;&gt;}
```

Generates a new SM2 keypair `{public_key_64b, private_key_32b}`.

# `normalize_public_key`

```elixir
@spec normalize_public_key(binary() | struct()) ::
  {:ok, &lt;&lt;_::512&gt;&gt;} | {:error, term()}
```

Normalizes and validates an SM2 public key into a 64-byte raw binary `<<X::256, Y::256>>`.

Accepts 64-byte raw, 65-byte uncompressed (`0x04 || X || Y`), 33-byte compressed
(`0x02/0x03 || X`), or `%ExCEP.Crypto.Cert{}`.

# `public_key_from_private`

```elixir
@spec public_key_from_private(&lt;&lt;_::256&gt;&gt;) :: &lt;&lt;_::512&gt;&gt;
```

Derives the 64-byte raw public key from a 32-byte private key (`1 <= d <= n - 2`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
