Macaw.Base.Encoding (macaw_base v0.1.1)

Copy Markdown View Source

The canonical encoding of a term and its digest (spec/01 §5): a value's identity is the term itself, never a representation of it, so a value has one digest however it is written or read back.

The encoding is a version byte (version/0), then the term, each part tagged by its type and self-delimiting:

TermEncoding
an atom (nil, true and false too)a, its name's byte size, its name in UTF-8
a binaryb, its byte size, its bytes
a bitstring of another sizes, its bit size, its bits padded with zeros to a byte
an integeri, the byte size of its decimal spelling, that spelling
a floatf, its IEEE 754 64-bit form
a listl, its length, each element
a tuplet, its size, each element
a mapm, its size, each key's encoding then its value's, in the bytewise order of the keys' encodings

Sizes are 64-bit, and every number is big-endian. So key order never matters, and different terms have different encodings: 1 and 1.0, 0.0 and -0.0 (as under ===), :a and "a", {:a} and [:a] all differ. A struct is its canonical term (Macaw.Base.Digest), which unless its type says otherwise is its map, __struct__ and all, so a DateTime carries its zone and precision, and a struct differs from its bare map.

The encoding is part of the family's contract: changing it changes every digest, so it changes only with a new major version, and its version byte is what such a change would move. It was Macaw.Layers.Document's, and is unchanged byte for byte.

Summary

Functions

The canonical digest of a value: 64 lowercase hex characters, the SHA-256 of version/0 followed by the value's encoding (encode/1).

digest/1's digest, for a value the caller built and knows is data. Raises ArgumentError naming every term that is not, where digest/1 answers them.

A term's canonical encoding, without the version byte: so a type whose canonical term is large can hash its parts as it goes, with the version byte first, and get digest/1's digest without a second encoder.

encode/1's encoding, for a term the caller built and knows is data. Raises ArgumentError naming every term that is not, where encode/1 answers them.

The canonical encoding's version byte, which digest/1 hashes ahead of encode/1's bytes: what a change to the encoding would move.

Functions

digest(value)

@spec digest(term()) :: {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}

The canonical digest of a value: 64 lowercase hex characters, the SHA-256 of version/0 followed by the value's encoding (encode/1).

{:ok, digest}, or a term that is not data (a function, a PID, a reference, a port or an improper list) as :not_data at its path, every one. digest!/1 raises there instead.

iex> Macaw.Base.Encoding.digest(%{b: 1, a: [true, nil]}) ==
...>   Macaw.Base.Encoding.digest(%{a: [true, nil], b: 1})
true

iex> Macaw.Base.Encoding.digest(%{v: 1}) == Macaw.Base.Encoding.digest(%{v: 1.0})
false

iex> Macaw.Base.Encoding.digest(%{rows: [1, [2 | 3]]})
{:error, [{[:rows, 1], :not_data}]}

digest!(value)

@spec digest!(term()) :: String.t()

digest/1's digest, for a value the caller built and knows is data. Raises ArgumentError naming every term that is not, where digest/1 answers them.

encode(term)

@spec encode(term()) :: {:ok, iodata()} | {:error, [Macaw.Base.Fault.t(), ...]}

A term's canonical encoding, without the version byte: so a type whose canonical term is large can hash its parts as it goes, with the version byte first, and get digest/1's digest without a second encoder.

iex> alias Macaw.Base.Encoding
iex> value = %{b: 1, a: [true, nil]}
iex> {:ok, bytes} = Encoding.encode(value)
iex> {:ok, :crypto.hash(:sha256, [Encoding.version() | bytes])
...>  |> Base.encode16(case: :lower)} == Encoding.digest(value)
true

{:ok, iodata}, or every term that is not data as :not_data at its path, as digest/1 answers them. encode!/1 raises there instead.

encode!(term)

@spec encode!(term()) :: iodata()

encode/1's encoding, for a term the caller built and knows is data. Raises ArgumentError naming every term that is not, where encode/1 answers them.

version()

@spec version() :: 1

The canonical encoding's version byte, which digest/1 hashes ahead of encode/1's bytes: what a change to the encoding would move.