Snarkey.Proof (snarkey v0.1.0)

Copy Markdown View Source

Schnorr proof generation and verification.

Implements the core Schnorr identification protocol:

  • generate_keypair/1: pick random secret x, compute public y = g^x mod p
  • generate_commitment/1: pick random k, compute r = g^k mod p
  • respond/4: compute s = k + c*x mod q
  • verify_interactive/7: check g^s ≡ r * y^c (mod p)
  • verify_non_interactive/9: timestamp check + Fiat-Shamir heuristic + verify

Summary

Types

Schnorr group parameters.

Functions

Convenience that generates a full proof in one call.

Convenience that generates a full proof from an existing secret key.

Generates a commitment for the proof.

Generates a Schnorr keypair.

SHA-256 hash of data reduced to a scalar in [1, q-1].

Computes the Schnorr response s = k + c*x mod q.

Verifies an interactive Schnorr proof.

Verifies a non-interactive (Fiat-Shamir) Schnorr proof.

Types

params()

@type params() :: %{p: integer(), g: integer(), q: integer()}

Schnorr group parameters.

Functions

compute_proof(map, nonce)

Convenience that generates a full proof in one call.

Generates a keypair, creates a commitment, hashes the transcript g ‖ y ‖ r ‖ nonce, and produces s.

Returns %{public_y: y, r: r, s: s}.

compute_proof(map, secret_x, public_y, nonce)

Convenience that generates a full proof from an existing secret key.

Unlike compute_proof/2, this uses a caller-supplied secret_x and public_y.

generate_commitment(p, g, q)

Generates a commitment for the proof.

Picks a random k and returns {r, k} where r = g^k mod p.

generate_keypair(map)

Generates a Schnorr keypair.

Returns {secret_x, public_y} where public_y = g^secret_x mod p.

hash_to_scalar(data, q)

SHA-256 hash of data reduced to a scalar in [1, q-1].

Delegates to Snarkey.Crypto.hash_to_scalar/2.

respond(k, c, x, q)

Computes the Schnorr response s = k + c*x mod q.

verify_interactive(p, g, q, y, r, c, s)

Verifies an interactive Schnorr proof.

Checks g^s ≡ r * y^c (mod p). Returns :ok or {:error, :invalid_proof}.

Rejects proofs where r == 0, s == 0, or c == 0, or where any value is outside the expected range.

verify_non_interactive(params, y, r, s, user_id, timestamp, opts \\ [])

Verifies a non-interactive (Fiat-Shamir) Schnorr proof.

Checks that the timestamp is within max_drift seconds of the current system time, recomputes the Fiat-Shamir challenge c from the transcript g ‖ y ‖ r ‖ user_id ‖ timestamp, and delegates to verify_interactive/7.

Options:

  • :max_drift — maximum allowed timestamp skew in seconds (default 5)