Powered by AppSignal & Oban Pro

Contract-major 2 inclusive ranges

docs/notebooks/range-selectors.livemd

Contract-major 2 inclusive ranges

Download this .livemd file, open it in Livebook, and choose Open → Import from file, then Run all. Use Elixir 1.18 or later and OTP 27 or later with access to public Hex. No secrets or external services are needed.

Limit a compute request to one through eight workers. Exercise both inclusive endpoints, out-of-range integers, a mismatched float tag, and invalid selector inputs.

Every refusal uses a newly signed proof matching its request arguments, so the range selector is what fails. The host still owns trusted keys, revocation, replay reservation, and authority. Keys stay in memory; no processes, tables, or files are created.

Open this 1.1.0 release notebook. The launch opens this journey at its immutable release tag.

Setup

Install the exact published package. Fixed evaluation times keep the results reproducible; only safe semantic values are persisted, never keys, signatures, tokens, or nonces.

Mix.install([{:bounded_authority_protocol, "== 1.1.0"}])
:ok

Generate Ed25519 keys

Contract-major 2 uses Ed25519. Compute the holder thumbprint and define the signing operation that will assemble each grant and proof.

alias BoundedAuthorityProtocol.V2
alias BoundedAuthorityProtocol.V1.{Credentials, ExpectedRequest, TrustedIssuer}

{issuer_public, issuer_private} = :crypto.generate_key(:eddsa, :ed25519)
{holder_public, holder_private} = :crypto.generate_key(:eddsa, :ed25519)
{:ok, thumbprint} = BoundedAuthorityProtocol.V1.Jwk.public_key_thumbprint_raw(holder_public, %{})

now = 1_800_000_100

sign = fn input, key ->
  signature = :crypto.sign(:eddsa, :none, input.message, [key, :ed25519])
  {:ok, compact} = V2.assemble_compact(input, signature)
  compact
end

:signer_ready
:signer_ready

Build and sign a bounded grant

Both range selectors must hold. Integer bounds require integer-tagged arguments; numeric equality does not convert a float tag into an integer tag.

grant = %V2.Grant{
  key_id: "issuer-1",
  issuer: "https://issuer.example",
  grant_id: "range-1",
  audiences: ["https://compute.example"],
  issued_at: now,
  not_before: now,
  expires_at: now + 3_600,
  holder_thumbprint: thumbprint,
  operations: [
    %V2.Operation{
      name: "allocate_workers",
      selectors: [
        {:gte, ["workers"], {:integer, 1}},
        {:lte, ["workers"], {:integer, 8}}
      ]
    }
  ]
}

{:ok, input} = V2.grant_signing_input(grant, %{})
compact = sign.(input, issuer_private)
:range_grant_signed
:range_grant_signed

Sign a proof for each request

Keep the grant fixed while signing a proof for each worker value. The expectation uses the same server-derived operation and arguments, isolating the selector result.

check = fn tagged ->
  arguments = {:object, [{"workers", tagged}]}

  proof = %V2.Proof{
    holder_public_key: holder_public,
    proof_id: "range-proof",
    method: "POST",
    target_uri: "https://compute.example/jobs",
    issued_at: now,
    nonce: nil,
    invocation_id: "7d444840-9dc0-11ed-a8fc-0242ac120002",
    operation: "allocate_workers",
    grant_compact: compact,
    cast_arguments: arguments
  }

  {:ok, proof_input} = V2.proof_signing_input(proof, %{})
  credentials = %Credentials{grant: compact, proof: sign.(proof_input, holder_private)}

  expected = %ExpectedRequest{
    trusted_issuer: %TrustedIssuer{key_id: "issuer-1", public_key: issuer_public},
    issuer: "https://issuer.example",
    audience: "https://compute.example",
    method: "POST",
    target_uri: "https://compute.example/jobs",
    invocation_id: "7d444840-9dc0-11ed-a8fc-0242ac120002",
    operation: "allocate_workers",
    cast_arguments: arguments,
    evaluation_time: now,
    clock_skew: 0,
    proof_max_age: 300,
    nonce: :not_required,
    bounds: %{}
  }

  V2.check_envelope(credentials, expected)
end

:request_check_ready
:request_check_ready

Verify the inclusive endpoints

One and eight workers are both inside the grant. Successful facts still carry no execution decision.

{:ok, lower} = check.({:integer, 1})
{:ok, upper} = check.({:integer, 8})

%{lower: lower.authorization, upper: upper.authorization}
%{lower: :not_evaluated, upper: :not_evaluated}

Refuse out-of-range values and the wrong tag

Zero and nine fall outside the bounds. A float-tagged one is also refused, despite having the same numeric value as the lower integer bound.

{:error, :invalid} = check.({:integer, 0})
{:error, :invalid} = check.({:integer, 9})
{:error, :invalid} = check.({:float, 1.0})

[:below_minimum, :above_maximum, :numeric_tag_mismatch]
[:below_minimum, :above_maximum, :numeric_tag_mismatch]

Refuse invalid selectors before signing

Range bounds must be numeric and paths must name a member. These malformed inputs never produce bytes for an issuer to sign.

string_bound = %V2.Operation{
  name: "allocate_workers",
  selectors: [{:lte, ["workers"], {:string, "8"}}]
}

empty_path = %V2.Operation{
  name: "allocate_workers",
  selectors: [{:gte, [], {:integer, 1}}]
}

{:error, :invalid} = V2.grant_signing_input(%{grant | operations: [string_bound]}, %{})
{:error, :invalid} = V2.grant_signing_input(%{grant | operations: [empty_path]}, %{})

[:string_bound, :empty_path]
[:string_bound, :empty_path]

See tagged arguments and selectors for the closed selector algebra and integer/float rules.