{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://thehiveryiq.com/.well-known/schemas/entropy-custody-v1.json",
  "title": "Entropy Custody Receipt, v1",
  "description": "Canonical schema for the Hive entropy custody receipt, canonical type entropy.custody. The requesting party derives the sampling keystream and commits to its secret before decoding begins, so the serving process consumes a stream it did not choose and cannot extend. The record carries two measured quantities: the count of keystream positions the sampler consumed, and the interval membership residual at each emitted position. Where a keystream position was absent or already marked consumed the decoder halted and no token was emitted at that position. Every mass, keystream value, cumulative lower bound and residual is carried as an exact decimal integer string at a fixed denominator of 2^64, so the membership arithmetic recomputes exactly in integer arithmetic and no floating point value enters the verification path.",
  "type": "object",
  "required": [
    "receipt_id",
    "payload_sha256",
    "sig_b64u",
    "key_id",
    "algorithm",
    "ts",
    "signed_body"
  ],
  "additionalProperties": false,
  "properties": {
    "receipt_id": {
      "type": "string",
      "pattern": "^r_entropy[._]custody_[0-9]{10,}_[0-9a-f]{12}$"
    },
    "payload_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "sig_b64u": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{86}$"
    },
    "key_id": {
      "type": "string",
      "pattern": "^did:(hive|web|key):[A-Za-z0-9._:%-]+$"
    },
    "algorithm": {
      "type": "string",
      "const": "Ed25519"
    },
    "ts": {
      "type": "integer",
      "minimum": 1000000000
    },
    "signed_body": {
      "type": "object",
      "required": [
        "receipt_type",
        "schema",
        "custody_id",
        "salt",
        "keystream",
        "positions",
        "draws",
        "halt",
        "verdict",
        "measured_at",
        "boundary"
      ],
      "additionalProperties": false,
      "properties": {
        "receipt_type": {
          "type": "string",
          "const": "entropy.custody"
        },
        "schema": {
          "type": "string",
          "const": "r1.0.0"
        },
        "custody_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "salt": {
          "type": "object",
          "required": [
            "commitment_sha256"
          ],
          "additionalProperties": false,
          "properties": {
            "commitment_sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            }
          }
        },
        "keystream": {
          "type": "object",
          "required": [
            "domain_label",
            "secret_commitment_sha256",
            "committed_at",
            "custody_mode",
            "external_source_ref",
            "request_id_commitment",
            "declared_position_count",
            "draws_per_position"
          ],
          "additionalProperties": false,
          "properties": {
            "domain_label": {
              "type": "string",
              "const": "entropy-custody-stream"
            },
            "secret_commitment_sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "committed_at": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}([.][0-9]{1,9})?Z$"
            },
            "custody_mode": {
              "type": "string",
              "enum": [
                "deferred_disclosure",
                "external_derivation"
              ]
            },
            "external_source_ref": {
              "type": [
                "string",
                "null"
              ],
              "minLength": 1,
              "maxLength": 256
            },
            "request_id_commitment": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "declared_position_count": {
              "type": "integer",
              "minimum": 1
            },
            "draws_per_position": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "positions": {
          "type": "array",
          "minItems": 0,
          "items": {
            "type": "object",
            "required": [
              "t",
              "u_value",
              "emitted_token_id",
              "emitted_rank",
              "candidate_count",
              "candidate_digest_sha256",
              "emitted_mass",
              "cumulative_lower_bound",
              "residual_mass",
              "listed_mass_sum",
              "interval_membership_residual",
              "membership",
              "consumption_marked"
            ],
            "additionalProperties": false,
            "properties": {
              "t": {
                "type": "integer",
                "minimum": 0
              },
              "u_value": {
                "type": "string",
                "pattern": "^(0|[1-9][0-9]{0,19})$"
              },
              "emitted_token_id": {
                "type": "integer",
                "minimum": 0
              },
              "emitted_rank": {
                "type": "integer",
                "minimum": 0
              },
              "candidate_count": {
                "type": "integer",
                "minimum": 1
              },
              "candidate_digest_sha256": {
                "type": "string",
                "pattern": "^[0-9a-f]{64}$"
              },
              "emitted_mass": {
                "type": "string",
                "pattern": "^[1-9][0-9]{0,19}$"
              },
              "cumulative_lower_bound": {
                "type": "string",
                "pattern": "^(0|[1-9][0-9]{0,19})$"
              },
              "residual_mass": {
                "type": "string",
                "pattern": "^(0|[1-9][0-9]{0,19})$"
              },
              "listed_mass_sum": {
                "type": "string",
                "pattern": "^[1-9][0-9]{0,19}$"
              },
              "interval_membership_residual": {
                "type": "string",
                "pattern": "^(0|-?[1-9][0-9]{0,19})$"
              },
              "membership": {
                "type": "string",
                "enum": [
                  "inside",
                  "outside"
                ]
              },
              "consumption_marked": {
                "type": "boolean",
                "const": true
              }
            }
          }
        },
        "draws": {
          "type": "object",
          "required": [
            "emitted_token_count",
            "consumed_position_count",
            "counter_final_value",
            "reuse_attempt_count",
            "outside_interval_count"
          ],
          "additionalProperties": false,
          "properties": {
            "emitted_token_count": {
              "type": "integer",
              "minimum": 0
            },
            "consumed_position_count": {
              "type": "integer",
              "minimum": 0
            },
            "counter_final_value": {
              "type": "integer",
              "minimum": 0
            },
            "reuse_attempt_count": {
              "type": "integer",
              "minimum": 0
            },
            "outside_interval_count": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "halt": {
          "type": "object",
          "required": [
            "occurred",
            "reason",
            "position_index",
            "tokens_emitted_before_halt"
          ],
          "additionalProperties": false,
          "properties": {
            "occurred": {
              "type": "boolean"
            },
            "reason": {
              "type": [
                "string",
                "null"
              ],
              "enum": [
                "position_absent",
                "position_already_consumed",
                null
              ]
            },
            "position_index": {
              "type": [
                "integer",
                "null"
              ],
              "minimum": 0
            },
            "tokens_emitted_before_halt": {
              "type": [
                "integer",
                "null"
              ],
              "minimum": 0
            }
          }
        },
        "verdict": {
          "type": "string",
          "enum": [
            "custody_conformant",
            "halted_absent_position",
            "halted_consumed_position",
            "interval_violation",
            "draw_count_mismatch"
          ]
        },
        "measured_at": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}([.][0-9]{1,9})?Z$"
        },
        "boundary": {
          "type": "string",
          "const": "Entropy Custody record. Attests that every stochastic token in the recorded sequence was selected by locating a keystream value derived from a secret committed by the requesting party before decoding began within the cumulative interval structure of a distribution the serving process reported for that position, that the count of keystream positions consumed by the sampler was recomputed from the sampler counter and the emitted length, that the interval membership residual at each emitted position was recomputed from the reported cumulative lower bound and the consumed keystream value, and that where a keystream position was absent or already marked consumed the decoder halted without emitting a further token. It does not attest that the reported distribution is the true distribution of any model, does not attest the identity of the model served, does not attest that the output is correct or useful, and does not attest that the serving process ran any particular model rather than a different model capable of reporting a self consistent distribution."
        }
      }
    }
  }
}
