10 Hash Functions

The hashes/ library computes digests of DUST values.

10.1. Events

The events plugin carries temporary messages between the requests of one open database: publications that reach every matching subscription, and calls that one responder or worker answers. Nothing is stored or replayed. Record facts first when an event must survive, then publish. The requests, the exchange modes and the page formats are in the ABI reference. Without the plugin, a namespace that contains defsubscription or defresponder cannot be saved or loaded: the macros are unknown.

The events functions are host effects, so a patch expression cannot call them, and a function that publishes or sends refuses with #err <denied events> whenever it runs while a write transaction is open: the write can still roll back, and a message cannot. Publish in an on {commit …} body instead, which runs after the write commits. A subject is non-empty UTF-8 of slash-separated segments, and a pattern is a regular expression that must match the whole subject.

(ns app/billing)
(defmutation note [invoice] {patch {_n {kind "notice" invoice invoice}}})
(defsubscription notify-paid "billing/paid"
  (fn [{data <invoice>}] (note invoice)))
(defmutation pay [invoice]
  {patch {?invoice {status "paid"}}
   on {commit (events/publish "billing/paid" {invoice invoice})}})

10.1.1. defsubscription

Macro

(defsubscription name pattern fn) → name

Defines a subscription in a saved namespace. For each publication whose subject pattern matches, the database calls fn with {subject data} before the publish returns, in publication order. fn may write, publish and call; its writes run their own on handlers. A publication that the body makes runs its subscriptions after the current ones, so a chain runs in order without a limit on its length. A failed body never undoes the publication: the publish result lists it under failures. name is also bound to fn.

10.1.2. defresponder

Macro

(defresponder name pattern fn) → name

Defines a responder in a saved namespace. A request call on a subject that pattern matches calls fn with {subject data}, and its value is the reply. A responder answers before any host worker. A save that defines a responder with the pattern of another saved responder, written the same, fails with #err <conflict name>, with a message such as "math/add already answered by app/math/add". Different patterns that match one subject make the call fail with #err <conflict>, which names both responders. name is also bound to fn.

(ns app/math)
(defresponder add "math/add" (fn [{data <a b>}] (+ a b)))
; (events/call "math/add" {a 1 b 2}) is 3.

10.1.3. events/publish

Function

(events/publish subject data) → map

Publishes data on subject and returns {delivered dropped}: the subscriptions it reached, saved ones included, and those whose queue allocation failed. The saved subscriptions run before it returns; a failed body adds an entry {namespace name subject error} to failures.

10.1.4. events/call

Function

(events/call subject data) → value

Sends data as a request on subject and returns the reply. A saved responder that matches answers at once. Otherwise a host worker (events/accept) must answer: the call waits, and the wait counts against the evaluation's CPU budget and ends when the evaluation is cancelled. While it waits, plugin requests run on the database's other workers, so a call that needs a host worker fails with #err <denied events> when the database has one worker. Without a worker, it fails with no_worker.

10.1.5. events/send

Function

(events/send handle data) → map

Sends data to the other side of the exchange behind handle, as the events/send request does.

10.1.6. events/finish

Function

(events/finish handle) → map

Ends this side's stream of the exchange behind handle.

10.1.7. events/cancel

Function

(events/cancel handle) → map

Ends the exchange behind handle; the other side reads cancel.

10.1.8. events/status

Function

(events/status) → map

Returns {subscriptions registrations rpcs queued runs failures}: the open subscriptions, workers and exchanges, the queued notices, and the saved handler runs and failures since the database opened.

10.2. Blackholes

The blackholes plugin erases or replaces selected values in every version of history, current values included. An operator previews the change, then executes it with the preview's digest. Each execute writes an audit. The requests and their results are in the ABI reference. Without the plugin, a namespace that contains defblackhole cannot be saved or loaded: the macro is unknown.

10.2.1. defblackhole

Macro

(defblackhole name doc? [params*] {query patch links?}) → name

Defines a blackhole in a saved namespace. The parameters are locals in the query and in the expressions. query is a query map without as-of: a saved query cannot select an earlier snapshot. A blackhole rewrites every earlier version of a selected field, also when its current value is retracted. A distinct query keeps its own tuples: the variables that the patch reads do not make more rows. patch maps query variables that bind entities to null, which erases every field of the entity, or to a map of fields. A field's null erases each version; an expression computes a replacement for each version. links {incoming remove} removes links to erased entities from other fields.

(defblackhole erase-keys
  "Erase the API keys of one owner."
  [owner]
  {query {find [?e] where [[?e kind "apiKey"] [?e owner ?o] (= ?o owner)]}
   patch {?e {secret null
              token (hashes/digest "sha256" [?seed ?value])
              note "redacted"}}})

An expression can read ?value (the version's value), ?entity, ?field, ?transaction, ?seed and the variables of its row. ?seed is 64 hexadecimal digits, new for each preview and each execute, and never stored. An expression that reads a variable that the query does not bind, a field in stardust/, and a member other than query, patch and links refuse the namespace save. When a row does not bind a variable that an expression reads, the preview or the execute fails with unbound_variable and changes nothing. Only an optional group that does not match leaves a variable unbound. A null that the row binds, such as the default of lag, is a value.

A blackhole rewrites earlier versions in place: an as-of read of an earlier transaction sees the changed value. A session that opened with as-of before the execute reads one snapshot, so it reads the old values until it closes. A replaced version stays a version, so a second execute of the same definition rewrites it again.

10.2.2. blackholes/preview

Function

(blackholes/preview invocation) → map

Plans a blackhole and changes nothing. invocation is the map {namespace name args}; args is optional. The result has digest, targets, matched, replacements, removals, confirm, affected and samples.

10.2.3. blackholes/execute

Function

(blackholes/execute approval) → map

Applies a previewed blackhole. approval is the preview's invocation map with digest, confirm, actor and reason added. digest is the preview's digest and confirm is its confirmation text. A commit after the preview gives an error with the conflict code. Execute is refused inside a mutation or a reactor.

(let [invocation {namespace "secrets/keys" name "erase-keys" args ["ada"]}
      plan (blackholes/preview invocation)]
  (blackholes/execute {namespace "secrets/keys" name "erase-keys" args ["ada"]
                       digest (get plan "digest") confirm (get plan "confirm")
                       actor "ops" reason "Erasure request 2291"}))

10.2.4. blackholes/audit

Function

(blackholes/audit options?) → list

Returns the audits in transaction order. options is a map with optional after (a transaction) and limit (a positive integer). Without limit, the list has every audit after after. Each audit is also the value of a stardust/blackhole fact that queries can read.

10.2.5. blackholes/status

Function

(blackholes/status) → map

Returns {enabled audits recovering backups}.

10.3. Hash values

#hash holds a digest together with the algorithm that produced it. It uses RON's #prefix value notation, and its payload is itself a tagged element: the tag names the algorithm and its value is the digest in lowercase hexadecimal.

#hash #sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
{"#hash": {"#sha256": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}}

Import validates the payload: the algorithm tag must name one hashes/digest defines, and the digest must be lowercase hexadecimal of that algorithm's length. An invalid payload produces the parse error invalid tag at the tag.

DUST
#hash #sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
→
RON
#hash #sha256 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
=
DUST Result
#hash #sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
DUST
#hash #md5 "900150983cd24fb0d6963f7d28e17f72"
=
DUST Result
#err <invalid tag>
DUST
#hash #sha256 "ba7816bf"
=
DUST Result
#err <invalid tag>
DUST
#hash #xxh3-64 "78AF5F94892F3950"
=
DUST Result
#err <invalid tag>

Two #hash values are equal when both the algorithm and the digest are equal, so digests of the same input under different algorithms are unequal rather than a type mismatch. A #hash value never equals a string.

10.4. hashes/digest

Function

(hashes/digest algorithm value) → hash

Returns a #hash of value under algorithm, a name from this table:

Algorithm Digest Kind
sha256 32 bytes SHA-2, cryptographic
sha512 64 bytes SHA-2, cryptographic
sha3-256 32 bytes SHA-3, cryptographic
sha3-512 64 bytes SHA-3, cryptographic
xxh3-64 8 bytes XXH3 64-bit, non-cryptographic
xxh3-128 16 bytes XXH3 128-bit, non-cryptographic

Every DUST value can be hashed. The digest covers the value's canonical DUST form: the bytes the DUST formatter writes for it, encoded as UTF-8, without the single terminal file newline. All other newlines, including those inside string contents, are kept. Arguments evaluate before hashing, and a quoted form hashes its resulting data. The canonical form follows the formatter exactly:

  • A top-level list of two or more forms omits its brackets, so (hashes/digest "sha256" [1 2]) digests the bytes of 1 and 2 on separate lines; a one-element list keeps its brackets, and an empty list is empty.
  • Maps keep their braces and sort their keys, so maps with the same members hash identically regardless of member order.
  • Integer 1 and float 1.0 have different forms and digests.
  • #utc and #dur payloads use their canonical spelling, so every spelling of the same instant or duration hashes identically.
  • Comments are excluded: documenting a form never changes its digest.
  • A function hashes as its canonical form: a closure as its fn source, and a library function as its quoted name, such as "strings/case". Values captured by a closure are not part of its form.
  • Unicode text is not normalized.

A form whose known tag has an invalid payload, such as #link true, is rejected when it is read, so it cannot reach hashes/digest.

The string "abc" has canonical form "abc", including the two quote characters. Its digests therefore cover five bytes. The empty string has canonical form "":

Call Result
(hashes/digest "sha256" "abc") #hash #sha256 "6cc43f858fbb763301637b5af970e2a46b46f461f27e5a0f41e009c59b827b25"
(hashes/digest "sha3-256" "abc") #hash #sha3-256 "460c09e673f81da9d6604954002d01498dd56bf136c5813e5a5ae2d4c3f2873b"
(hashes/digest "xxh3-64" "abc") #hash #xxh3-64 "236ac2698e6b6688"

The XXH3 variants are for keys, bucketing, deduplication, and change detection, never for integrity against an adversary. xxh3-128 makes accidental collisions negligible where 64 bits are too few, such as content addressing. An unknown algorithm produces #err <algorithm hash unknown>.

DUST
(hashes/digest "sha256" null)
(hashes/digest "sha256" "abc")
(hashes/digest "sha256" "")
(hashes/digest "sha256" 1)
(hashes/digest "sha256" 1.0)
(hashes/digest "sha256" ["é" "猫"])
(hashes/digest "sha256" `name)
(hashes/digest "sha256" ``1)
(hashes/digest "sha256" #link 1)
(hashes/digest "sha256" #utc "1970-01-01T00:00:00Z")
(hashes/digest "sha256" #dur "PT1S")
(hashes/digest "sha256" [])
(hashes/digest "sha256" {a 1 b 2})
(hashes/digest "sha256" #utc "1970-01-01T00:00:00.0000001Z")
(hashes/digest "sha256" #dur "PT0.0000001S")
=
DUST Result
#hash #sha256 "74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b"
#hash #sha256 "6cc43f858fbb763301637b5af970e2a46b46f461f27e5a0f41e009c59b827b25"
#hash #sha256 "12ae32cb1ec02d01eda3581b127c1fee3b0dc53572ed6baf239721a03d82e126"
#hash #sha256 "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"
#hash #sha256 "d0ff5974b6aa52cf562bea5921840c032a860a91a3512f7fe8f768f6bbe005f6"
#hash #sha256 "8abc6af09be09c055f677c0b9be90c456d450c3afa7fa83fdcf3e0f0326bdd18"
#hash #sha256 "82a3537ff0dbce7eec35d69edc3a189ee6f17d82f353a553f9aa96cb0be3ce89"
#hash #sha256 "d135669e0056d96659a6969caa40dad4576a10121eaad744521e9a09cdca6a83"
#hash #sha256 "e64ae69a6b69823b59a98efc4eee9036bfd5dd2b4c171bd6b040bb649cd6d5e3"
#hash #sha256 "c9adbba78bb05b960634a1b7347af953d205ac92212de174e0e3ea8eaacffb0b"
#hash #sha256 "5c341cf9f25684580f1ac2fa23afce874bd5134de4bd473176c46ade2e371ab0"
#hash #sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
#hash #sha256 "53a6fbfea11bafdfe07457e9d925dedda5ca93811d422d4af322379990194d45"
#hash #sha256 "fac9f162216b9ab80bb07b09c082e2e31eb0d56672ba2227536803ab8e0f10e6"
#hash #sha256 "9254d233474e59e0a36a9b6b822a667209b52b755152dbacd409b40ad32b81d0"
DUST
(hashes/digest "sha512" "abc")
(hashes/digest "sha3-256" "abc")
(hashes/digest "sha3-512" "abc")
(hashes/digest "xxh3-64" "abc")
(hashes/digest "xxh3-128" "abc")
=
DUST Result
#hash #sha512 "60b7e079eb5dbdb674156850d75d3eae2dc16e08a70aeeb9a658fa5a7546fbc8c5f14328cf5d5321451fec7deba5258a56d81c1ac70b064facba8127da5c18e8"
#hash #sha3-256 "460c09e673f81da9d6604954002d01498dd56bf136c5813e5a5ae2d4c3f2873b"
#hash #sha3-512 "47070e8f45799540c361c6d92c2df5b2a54f25ff2a19bc8d2da1ef70ddcff117137baf4e206e56528e9eca14aea6a3ea24e4dfa942447d4a92dce09078f93128"
#hash #xxh3-64 "236ac2698e6b6688"
#hash #xxh3-128 "590cff53cfd1122698f832b385c169a4"
DUST
(= (hashes/digest "sha256" {a 1 b 2}) (hashes/digest "sha256" {a 1 b 2}))
(comparisons/not= (hashes/digest "sha256" 1) (hashes/digest "sha256" 1.0))
(comparisons/not= (hashes/digest "sha256" "é") (hashes/digest "sha256" "é"))
=
DUST Result
true
true
true
DUST
(comparisons/not= (hashes/digest "sha256" "abc") (hashes/digest "sha3-256" "abc"))
(comparisons/not= (hashes/digest "xxh3-64" "abc") (hashes/digest "xxh3-128" "abc"))
(= (hashes/digest "xxh3-64" "abc") #hash #xxh3-64 "236ac2698e6b6688")
=
DUST Result
true
true
true
DUST
(hashes/digest "md5" "abc")
=
DUST Result
#err <algorithm hash unknown>
DUST
(hashes/digest 256 "abc")
=
DUST Result
#err <mismatch type>
DUST
(hashes/digest "sha256" (fn [] 1))
=
DUST Result
#hash #sha256 "06d0dff99f70155bfd64b168de5c36cede11e9c14ddf87358cbb89ea9f08ce69"
DUST
(hashes/digest "sha256" [(fn [] 1)])
=
DUST Result
#hash #sha256 "b6ca49a321d2250ae8533a020a3fbf07d9942b3af2e67728b342ed4b73f86d0e"
DUST
(hashes/digest)
=
DUST Result
#err <arity mismatch>