# `PhoenixCap.Challenge`
[🔗](https://github.com/elchemista/phoenix_cap/blob/v0.1.0/lib/phoenix_cap/challenge.ex#L1)

Cap-compatible proof-of-work challenge generation and redemption.

This module implements the compact challenge protocol used by the Cap widget:
the server stores `{c, s, d}` plus an opaque challenge token, while the widget
derives the actual `{salt, target}` pairs locally from that token.

# `challenge`

```elixir
@type challenge() :: %{c: pos_integer(), s: pos_integer(), d: pos_integer()}
```

Compact Cap challenge parameters sent to the widget.

# `redeem_params`

```elixir
@type redeem_params() :: %{required(binary()) =&gt; term()}
```

Redeem request body decoded from JSON.

# `response`

```elixir
@type response() :: %{challenge: challenge(), token: binary(), expires: pos_integer()}
```

Challenge response shape expected by the Cap widget.

# `stored_challenge`

```elixir
@type stored_challenge() :: %{challenge: challenge(), expires: pos_integer()}
```

Stored challenge data with an absolute Unix millisecond expiry.

# `create`

```elixir
@spec create() :: response()
```

Creates and stores a single-use Cap challenge.

The returned JSON-compatible map matches the upstream `@cap.js/server`
`createChallenge` response: `%{challenge: %{c, s, d}, token, expires}`.

# `prng`

```elixir
@spec prng(binary(), non_neg_integer()) :: binary()
```

Generates deterministic hex output compatible with Cap's widget PRNG.

# `redeem`

```elixir
@spec redeem(Plug.Conn.t(), redeem_params()) :: {:ok, map()} | {:error, binary()}
```

Redeems a solved challenge and issues a one-time verification token.

The challenge is consumed exactly once, matching Cap's server behavior: once a
syntactically valid redeem attempt reaches stored challenge lookup, retrying
the same challenge token fails.

# `verify_challenge_pair`

```elixir
@spec verify_challenge_pair(binary(), binary(), integer()) :: boolean()
```

Verifies one `{salt, target}` challenge pair against a nonce.

This is useful for parity tests against Cap's WASM solver vectors.

# `verify_solutions`

```elixir
@spec verify_solutions(binary(), challenge(), [integer()]) ::
  :ok | {:error, :invalid_solution}
```

Verifies all nonce solutions for a compact Cap challenge.

---

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