graphops: refuse JSON numbers that do not round-trip through binary64 (bdp#21)

The operator's ruling of 2026-09-08 (gastownhall/bdp#21) makes RFC 7493
I-JSON's interoperability rule mandatory: equality over JSON numbers stays
exact-decimal, and an authority refuses at admission any number literal
whose exact decimal value does not round-trip through IEEE-754 binary64.

Round-trip, as implemented: parse the literal's exact decimal value to the
nearest binary64 (strconv.ParseFloat), serialize that double shortest
(strconv.FormatFloat with precision -1), place its digits with the same
ECMAScript rule the literal's own digits get, and admit the literal exactly
when the two canonical forms are byte-equal. The canonical output stays the
literal's exact decimal form - no float64 laundering - and on every admitted
value it now equals what RFC 8785 emits for the nearest double.

- CanonicalizeJSON, and so NewProperties, ParseTypeDescriptor and JSONEqual,
  refuse with ErrValidation naming the RFC 6901 pointer of the offending
  member or element, the literal (abbreviated when long) and what a binary64
  reader would serialize it as.
- The ledger framing is hashed by an unexported exact-mode canonicalizer:
  seq and epoch stay exact over the full uint64 range and the frozen hash
  layout is unchanged. DECISION recorded: the framing spells them as JSON
  numbers a binary64 JCS peer would round past 2^53 (B4's existing interop
  caveat); the ledger is internal, so the exemption is stated, not the
  layout re-cut.
- ownsOutgoing max obeys the same law at the Go-constructor door, with the
  same predicate, so every descriptor that exists has a canonical form the
  JSON door re-admits.
- The exponent bound and the fixed-point property are kept; the bound is
  checked first and now decides only for zero and in exact mode.

Tests: every vector from the ruling, nested pointers with RFC 6901 escaping,
the exponent-bound interaction, fixed point after admission, RFC 8785
Appendix B's table and worked example, and a seeded sweep of random doubles
with math/big as the oracle. Coverage stays at 100%.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Donna Box 2026-09-08 11:31:28 -03:00
commit 944b1fc9fd
5 changed files with 712 additions and 96 deletions

View file

@ -1,6 +1,7 @@
package graphops
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
@ -317,12 +318,13 @@ func CompareCodeUnits(a, b string) int {
}
// ---------------------------------------------------------------------------
// JSON canonicalization and equality
// JSON canonicalization, the number admission law, and equality
//
// What is canonicalized: any JSON text (RFC 8259) into ONE byte string, such
// that two texts canonicalize to the same bytes exactly when they are equal
// under RFC 6902 §4.6 — the comparison that decides whether a properties write
// is a no-op. The form is RFC 8785 (JCS) with one stated departure:
// is a no-op. The form is RFC 8785 (JCS), bit-exact over every admitted
// document, with an admission law on numbers where JCS would silently round:
//
// - Whitespace outside strings is removed.
// - Object members are sorted by the UTF-16 code units of their keys
@ -336,21 +338,58 @@ func CompareCodeUnits(a, b string) int {
// - Literals are true, false, null.
// - Numbers are serialized with ECMAScript's Number::toString digit
// placement (RFC 8785 §3.2.2.3): no exponent for 10^-6 ≤ |x| < 10^21,
// shortest digit string, "e+"/"e-" otherwise — APPLIED TO THE EXACT
// DECIMAL VALUE OF THE LITERAL, not to its nearest IEEE-754 double. This
// is the departure. For every literal a double represents exactly under
// shortest round-trip the output is byte-identical to JCS ("1.0" → "1",
// "-0.0" → "0", "1e300" → "1e+300", "0.1" → "0.1"); where JCS would round
// (an integer past 2^53, more than 17 significant digits, an exponent
// past the double range) the value is preserved exactly instead. The plan
// forbids float64 laundering of properties, RFC 6902 equality is numeric
// equality, and the two demands meet only here: 9007199254740993 and
// 9007199254740992 are different values and canonicalize differently.
// shortest digit string, "e+"/"e-" otherwise — applied to the EXACT
// DECIMAL VALUE of the literal, never to a float64 reading of it — and a
// literal is ADMITTED only if that value round-trips through IEEE-754
// binary64 (bdp#21, ruled 2026-09-08; RFC 7493 I-JSON's interoperability
// rule made mandatory). Round-trip, precisely: parse the literal's exact
// decimal value to the nearest binary64 (round-to-nearest-even, which is
// what strconv.ParseFloat computes however the literal is spelled and
// however long it is); serialize that double as the shortest decimal
// that reads back to it (strconv.FormatFloat with precision -1, the
// digits Number::toString prints); the literal is admissible exactly
// when that decimal's value is the literal's own value. So 1, 1.0 and
// 1e0 admit and canonicalize to "1"; -0.0 to "0"; 1e300 and 1E300 to
// "1e+300"; 9007199254740992 (2^53) and 9007199254740994 (a double whose
// shortest spelling is itself) admit as themselves. 9007199254740993
// (2^53+1, halfway between two doubles), a 20-significant-digit decimal,
// 18446744073709551616 (2^64: a double, but one a binary64 reader
// spells 18446744073709552000), 1e400 (past the range) and 1e-400
// (below it, read as 0) are refused with ErrValidation naming the RFC
// 6901 JSON pointer of the offending member or element and what a
// binary64 reader would have made of the value.
//
// DECISION: that departure. Alternatives were bit-exact JCS (rounds numbers
// the storage design exists to keep) and source-literal preservation (the
// metadata plane's rule, under which 1 and 1.0 are different values, which
// contradicts the no-op law). Exact-value JCS satisfies both texts.
// The canonical bytes are the literal's own digits — no float64
// laundering — and on every admitted value they coincide, by
// construction, with what RFC 8785 emits for the nearest double: the
// exact-decimal model and the binary64 model agree wherever a value is
// admitted, and disagree only where the value is refused. RFC 6902
// equality is numeric equality over that admitted domain.
//
// DECISION 3 (P0, 2026-09-07; amended by the bdp#21 ruling, 2026-09-08). The
// P0 form was exact-value JCS with one stated departure: a literal a double
// could not carry (an integer past 2^53, 18 or more significant digits, an
// exponent past the range) was preserved exactly where JCS rounds it. It was
// chosen over bit-exact JCS (which rounds numbers the storage design existed
// to keep) and source-literal preservation (the metadata plane's rule, under
// which 1 and 1.0 are different values, contradicting the no-op law), and it
// left B4's interop caveat: a JCS peer modeling numbers as binary64 could
// not reproduce such a value. The ruling closes the caveat from the other
// side — equality stays exact-decimal, and a value no binary64 peer could
// reproduce is refused at admission instead of stored. Two consequences are
// recorded where they bite: the ledger framing is NOT under the law (see
// "Ledger hashing": its integers are Go-typed, hashed exactly, and the frozen
// layout is unchanged), and a descriptor's ownsOutgoing max obeys the law at
// both of its doors (ParseTypeDescriptor and NewOwnedLinkDecl), so every
// descriptor that exists has a canonical form the law admits.
//
// DECISION: the order of the number refusals. Syntax first; then the spelled
// exponent guard and the normalized exponent bound (maxExponentMagnitude,
// "exponent out of range"); then the binary64 law. 1e1000000000001 is
// therefore refused as out of range and 1e400 as outside binary64 — both
// ErrValidation, each message naming the first law the literal breaks. The
// exponent bound stays (see its comment) although, for every nonzero literal
// in admission mode, the binary64 range is now the tighter one.
//
// Nesting is bounded at maxJSONDepth (the depth encoding/json accepts) as a
// stack-safety measure, not a protocol limit; advertised limits are the
@ -360,9 +399,28 @@ func CompareCodeUnits(a, b string) int {
const maxJSONDepth = 10000
// CanonicalizeJSON returns the canonical bytes of one JSON text, or
// ErrValidation for anything that is not exactly one well-formed JSON value.
// ErrValidation for anything that is not exactly one well-formed JSON value —
// a number the binary64 admission law refuses included, reported with the
// RFC 6901 JSON pointer of the offending member or element ("" is the whole
// document). This is the one door through which user-supplied JSON enters
// the domain: NewProperties, ParseTypeDescriptor and JSONEqual all pass
// through it.
func CanonicalizeJSON(raw []byte) ([]byte, error) {
s := &jsonScanner{in: raw}
return canonicalizeJSON(raw, false)
}
// canonicalizeJSONExact is CanonicalizeJSON without the binary64 admission
// law: every number is kept at its exact decimal value whatever a binary64
// reader would make of it. It exists for the ledger framing alone (see
// "Ledger hashing"), whose integers are Go-typed and hashed exactly under a
// frozen layout, and it is deliberately unexported: nothing user-supplied
// reaches the domain through it.
func canonicalizeJSONExact(raw []byte) ([]byte, error) {
return canonicalizeJSON(raw, true)
}
func canonicalizeJSON(raw []byte, exact bool) ([]byte, error) {
s := &jsonScanner{in: raw, exact: exact}
s.skipSpace()
out, err := s.value(make([]byte, 0, len(raw)))
if err != nil {
@ -377,7 +435,8 @@ func CanonicalizeJSON(raw []byte) ([]byte, error) {
// JSONEqual is RFC 6902 §4.6 equality of two JSON texts: numbers by numeric
// value, strings by code points, arrays element-wise, objects as member sets,
// literals as themselves. Either text failing to parse is an error.
// literals as themselves. Either text failing to parse — or carrying a number
// the admission law refuses — is an error.
func JSONEqual(a, b []byte) (bool, error) {
ca, err := CanonicalizeJSON(a)
if err != nil {
@ -394,10 +453,47 @@ type jsonScanner struct {
in []byte
pos int
depth int
// path is the RFC 6901 location of the value being scanned: one token per
// enclosing object member or array element, pushed before the value and
// popped after it, so a refusal can say where it bit.
path []jsonPathToken
// exact disables the binary64 admission law. Only canonicalizeJSONExact
// sets it.
exact bool
}
// jsonPathToken is one RFC 6901 reference token: a member name or an index.
type jsonPathToken struct {
key string
index int
isKey bool
}
// pointerEscaper is RFC 6901 §3: "~" becomes "~0" and "/" becomes "~1".
var pointerEscaper = strings.NewReplacer("~", "~0", "/", "~1")
// pointer renders the scanner's current location as an RFC 6901 JSON
// pointer: "" for the whole document, "/a/0/b~1c" for member "b/c" of the
// first element of member "a".
func (s *jsonScanner) pointer() string {
var b strings.Builder
for _, t := range s.path {
b.WriteByte('/')
if t.isKey {
b.WriteString(pointerEscaper.Replace(t.key))
} else {
b.WriteString(strconv.Itoa(t.index))
}
}
return b.String()
}
func (s *jsonScanner) errorf(format string, args ...any) error {
return fmt.Errorf("%w: JSON at byte %d: %s", ErrValidation, s.pos, fmt.Sprintf(format, args...))
return s.errorAt(s.pos, format, args...)
}
func (s *jsonScanner) errorAt(pos int, format string, args ...any) error {
return fmt.Errorf("%w: JSON at byte %d: %s", ErrValidation, pos, fmt.Sprintf(format, args...))
}
func (s *jsonScanner) skipSpace() {
@ -507,10 +603,12 @@ func (s *jsonScanner) objectMembers() ([]jsonMember, error) {
}
s.pos++
s.skipSpace()
s.path = append(s.path, jsonPathToken{key: key, isKey: true})
value, err := s.value(nil)
if err != nil {
return nil, err
}
s.path = s.path[:len(s.path)-1]
members = append(members, jsonMember{key: key, value: value})
s.skipSpace()
if s.pos >= len(s.in) {
@ -582,10 +680,12 @@ func (s *jsonScanner) array(out []byte) ([]byte, error) {
if i > 0 {
out = append(out, ',')
}
s.path = append(s.path, jsonPathToken{index: i})
var err error
if out, err = s.value(out); err != nil {
return nil, err
}
s.path = s.path[:len(s.path)-1]
s.skipSpace()
if s.pos >= len(s.in) {
return nil, s.errorf("unterminated array")
@ -747,10 +847,19 @@ func appendCanonicalString(out []byte, str string) []byte {
// representable here, but no consumer could use it, and unbounded
// accumulation would be an integer overflow waiting to happen. Bounding the
// normalized value is what makes canonicalization a fixed point: a literal
// is admitted exactly when its canonical form re-parses, so 10e1000000000000
// passes this bound exactly when its canonical form does, so 10e1000000000000
// (canonically 1e+1000000000001) is refused on the way in rather than stored
// and then unreadable, and 0.1e1000000000001 (canonically 1e+1000000000000)
// is admitted although its spelled exponent exceeds the bound.
// is not refused HERE although its spelled exponent exceeds the bound.
//
// Since the bdp#21 ruling the binary64 range (about 5e-324 to 1.8e308) is the
// tighter bound for every nonzero literal in admission mode — 1e+1000000000000
// is refused by the admission law on its own account — so this bound decides
// there only for zero, which has no magnitude (0e1000000000005 is still "0",
// and 0e99999999999999999999999 is still refused by the spelled-exponent
// guard), and in exact mode, where the ledger framing spells no exponent at
// all. It stays because the overflow guard and the fixed-point argument rest
// on it, not on the admission law.
const maxExponentMagnitude = 1_000_000_000_000
func (s *jsonScanner) digits() ([]byte, error) {
@ -764,8 +873,10 @@ func (s *jsonScanner) digits() ([]byte, error) {
return s.in[start:s.pos], nil
}
// number parses one RFC 8259 number and appends its canonical form.
// number parses one RFC 8259 number, appends its canonical form, and — unless
// the scanner is exact — applies the binary64 admission law to it.
func (s *jsonScanner) number(out []byte) ([]byte, error) {
start := s.pos
neg := false
if s.in[s.pos] == '-' {
neg = true
@ -820,10 +931,22 @@ func (s *jsonScanner) number(out []byte) ([]byte, error) {
exp = -exp
}
}
mark := len(out)
out, ok := appendCanonicalNumber(out, neg, intPart, frac, exp)
if !ok {
return nil, s.errorf("exponent out of range")
}
if s.exact {
return out, nil
}
literal := s.in[start:s.pos]
peer, ok := binary64Peer(literal)
if !ok {
return nil, s.errorAt(start, "number %s at JSON pointer %q is outside the IEEE-754 binary64 range (bdp#21: a number must round-trip through binary64)", abbreviate(literal), s.pointer())
}
if !bytes.Equal(peer, out[mark:]) {
return nil, s.errorAt(start, "number %s at JSON pointer %q does not round-trip through IEEE-754 binary64: a binary64 reader serializes it as %s (bdp#21)", abbreviate(literal), s.pointer(), peer)
}
return out, nil
}
@ -893,6 +1016,62 @@ func appendCanonicalNumber(out []byte, neg bool, intPart, frac []byte, exp int64
return out, true
}
// binary64Peer is what RFC 8785's number model makes of a literal: the
// canonical form of the shortest decimal that reads back to the literal's
// nearest IEEE-754 binary64. The admission law (bdp#21) admits a literal
// exactly when this equals the literal's own canonical form. ok is false
// when the nearest binary64 is infinite — the literal is outside the range;
// a literal below the range is not an error here, its peer is "0".
//
// strconv.ParseFloat is correctly rounded (round-to-nearest-even over the
// exact decimal value, however the literal is spelled and however long it
// is), and strconv.FormatFloat with precision -1 prints the shortest digits
// that round-trip — the digits ECMAScript's Number::toString prints. The 'e'
// form is asked for so that the digits and the exponent are read back
// without guessing at Go's own placement rule; ECMAScript's placement is
// then applied by appendCanonicalNumber, the function that placed the
// literal's digits, so the two sides are compared in one normal form.
func binary64Peer(literal []byte) (peer []byte, ok bool) {
f, err := strconv.ParseFloat(string(literal), 64)
if err != nil {
return nil, false // the scanner admitted the syntax, so only the range can fail
}
es := strconv.FormatFloat(f, 'e', -1, 64) // [-]d[.ddd]e±dd
neg := es[0] == '-'
if neg {
es = es[1:]
}
e := strings.IndexByte(es, 'e')
exp, _ := strconv.ParseInt(es[e+1:], 10, 64) // a sign and digits: cannot fail
intPart, frac := []byte(es[:1]), []byte(nil)
if e > 1 {
frac = []byte(es[2:e]) // past "d."
}
peer, _ = appendCanonicalNumber(nil, neg, intPart, frac, exp) // |exp| ≤ 324: within the bound
return peer, true
}
// admissibleInt reports whether n, spelled as a JSON integer, passes the
// binary64 admission law. An int is below 10^21, so its spelling is its own
// canonical form and the law holds exactly when its binary64 peer is that
// same spelling.
func admissibleInt(n int) bool {
literal := strconv.AppendInt(nil, int64(n), 10)
peer, ok := binary64Peer(literal)
return ok && bytes.Equal(peer, literal)
}
// abbreviate renders a number literal for a diagnostic. A properties document
// may spell a number at any length, and the pointer and byte offset already
// locate it, so a long one is elided in the middle.
func abbreviate(literal []byte) string {
const keep = 24
if len(literal) <= 2*keep+3 {
return string(literal)
}
return fmt.Sprintf("%s...%s (%d bytes)", literal[:keep], literal[len(literal)-keep:], len(literal))
}
// ---------------------------------------------------------------------------
// Scope URL and Type URL
//
@ -1703,8 +1882,12 @@ func claimsScope(scopeURL, reference string) bool {
// "state" string "pruned" | "erased" (optional)
//
// hash = lowercase hex SHA-256 of those bytes; the next event's prev_hash is
// this hash. Integers are exact (see the number rule above), so seq and
// epoch survive the full uint64 range. Every string member is VALID UTF-8:
// this hash. The framing is canonicalized by canonicalizeJSONExact, NOT by
// CanonicalizeJSON: seq and epoch are Go uint64 values, spelled as exact
// JSON integers and hashed as such over the full uint64 range, and they
// are not subject to the binary64 admission law (bdp#21, 2026-09-08),
// which governs user-supplied JSON entering the domain. Every string
// member is VALID UTF-8:
// the hex, path, URL and enum members by their own grammars, and the
// opaque revision by NewRevision's refusal of invalid bytes — encoding/json
// would otherwise launder an invalid byte to U+FFFD, and two events that
@ -1715,6 +1898,17 @@ func claimsScope(scopeURL, reference string) bool {
// and the UTF-8 requirement on every hashed string (P0 council). B2 gives
// the formula and the member list; the framing and the formats are this
// file's choice and are pinned by a golden test.
//
// DECISION (bdp#21 fold, 2026-09-08): the layout is UNCHANGED by the
// admission law; the exemption above is stated rather than the layout
// re-cut. The framing does spell seq and epoch as JSON number literals, so a
// JCS peer that models numbers as binary64 would read 18446744073709551615
// as 18446744073709552000 and could not reproduce the hash of an event whose
// seq or epoch is past 2^53 — the interop caveat B4 already records. The
// ledger is internal (this package alone computes and verifies its hashes),
// the layout is frozen by the P1 migration that stores it, and the ruling's
// law is about values a peer is asked to hold. The golden test hashes epoch
// 2^64-1 and pins that CanonicalizeJSON refuses those same bytes.
// ---------------------------------------------------------------------------
// GenesisHash is the prev_hash of the first event of a Scope's history: no
@ -1759,8 +1953,8 @@ func ledgerEventCanonicalBytes(spec LedgerEventSpec) []byte {
Seq: spec.Seq,
State: spec.State,
}
raw, _ := json.Marshal(w) // strings and integers: cannot fail
canonical, _ := CanonicalizeJSON(raw) // encoder output is one valid value
raw, _ := json.Marshal(w) // strings and integers: cannot fail
canonical, _ := canonicalizeJSONExact(raw) // one valid value, integers exact: see the DECISION above
return canonical
}

View file

@ -92,3 +92,49 @@ func TestCodeUnitKeyOrdersLikeUTF16(t *testing.T) {
t.Fatal("codeUnitKey does not order like UTF-16 code units")
}
}
// binary64Peer is the admission law's other side: what a binary64 reader
// serializes a literal as, in the package's own canonical form; ok is false
// only outside the range. admissibleInt is the same law over a Go int.
func TestBinary64Peer(t *testing.T) {
for _, tc := range []struct {
in, peer string
ok bool
}{
{"-0.0", "0", true}, {"0", "0", true}, {"1.5e-7", "1.5e-7", true}, {"1e21", "1e+21", true},
{"0.1000000000000000055511151231257827021181583404541015625", "0.1", true},
{"9007199254740993", "9007199254740992", true}, {"-9007199254740993", "-9007199254740992", true},
{"1e-400", "0", true}, {"1e400", "", false}, {"-1e400", "", false},
} {
peer, ok := binary64Peer([]byte(tc.in))
if ok != tc.ok || string(peer) != tc.peer {
t.Errorf("binary64Peer(%q) = %q, %v; want %q, %v", tc.in, peer, ok, tc.peer, tc.ok)
}
}
for n, want := range map[int]bool{1: true, 9007199254740992: true, 9007199254740994: true, 9007199254740993: false, 1 << 60: false, math.MaxInt64: false} {
if got := admissibleInt(n); got != want {
t.Errorf("admissibleInt(%d) = %v, want %v", n, got, want)
}
}
}
func TestAbbreviate(t *testing.T) {
short := strings.Repeat("1", 51)
if abbreviate([]byte(short)) != short {
t.Error("a 51-byte literal is shown whole")
}
long := strings.Repeat("1", 25) + "X" + strings.Repeat("2", 26) // 52 bytes
if got := abbreviate([]byte(long)); got != strings.Repeat("1", 24)+"..."+strings.Repeat("2", 24)+" (52 bytes)" {
t.Errorf("abbreviate = %q", got)
}
}
func TestJSONPointerEscaping(t *testing.T) {
s := &jsonScanner{path: []jsonPathToken{{key: "a/b", isKey: true}, {index: 3}, {key: "m~n", isKey: true}, {key: "", isKey: true}}}
if got := s.pointer(); got != "/a~1b/3/m~0n/" {
t.Errorf("pointer = %q", got)
}
if got := (&jsonScanner{}).pointer(); got != "" {
t.Errorf("root pointer = %q", got)
}
}

View file

@ -5,7 +5,11 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math"
"math/big"
"math/rand/v2"
"strconv"
"strings"
"testing"
"time"
@ -212,27 +216,101 @@ func TestCompareCodeUnits(t *testing.T) {
// --- JSON canonicalization ------------------------------------------------
// jcsNumber is RFC 8785 §3.2.2.3 — ECMAScript Number::toString — over a
// binary64, written here independently of the package: Go's shortest
// round-trip digits placed by the ECMAScript rule. The package never formats
// a double into its output, so agreement between the two is the claim the
// bdp#21 ruling makes: on every admitted value the exact-decimal model and
// the binary64 model serialize identically.
func jcsNumber(f float64) string {
if f == 0 {
return "0" // both zeros
}
es := strconv.FormatFloat(f, 'e', -1, 64) // [-]d[.ddd]e±dd
neg := strings.HasPrefix(es, "-")
if neg {
es = es[1:]
}
e := strings.IndexByte(es, 'e')
x, _ := strconv.Atoi(es[e+1:])
digits := strings.Replace(es[:e], ".", "", 1)
k, n := len(digits), x+1 // value = 0.digits × 10^n
var out string
switch {
case k <= n && n <= 21:
out = digits + strings.Repeat("0", n-k)
case 0 < n && n <= 21:
out = digits[:n] + "." + digits[n:]
case -6 < n && n <= 0:
out = "0." + strings.Repeat("0", -n) + digits
case n-1 >= 0:
out = digits[:1] + fractionOf(digits) + "e+" + strconv.Itoa(n-1)
default:
out = digits[:1] + fractionOf(digits) + "e-" + strconv.Itoa(1-n)
}
if neg {
return "-" + out
}
return out
}
func fractionOf(digits string) string {
if len(digits) == 1 {
return ""
}
return "." + digits[1:]
}
// checkAdmittedNumber pins what every admitted literal must satisfy: the
// canonical form is a fixed point, and it is byte-identical to the RFC 8785
// serialization of the literal's nearest binary64.
func checkAdmittedNumber(t *testing.T, in string, canonical []byte) {
t.Helper()
again, err := graphops.CanonicalizeJSON(canonical)
if err != nil || !bytes.Equal(again, canonical) {
t.Errorf("number %q: canonical form %q is not a fixed point: %q %v", in, canonical, again, err)
}
f, err := strconv.ParseFloat(in, 64)
if err != nil {
t.Fatalf("number %q: admitted, but strconv cannot read it: %v", in, err)
}
if jcs := jcsNumber(f); jcs != string(canonical) {
t.Errorf("number %q: canonical %q, but RFC 8785 serializes its nearest binary64 as %q", in, canonical, jcs)
}
}
// The number rule in two halves. The canonical form is the literal's EXACT
// decimal value in RFC 8785 digit placement; and a literal is admitted only
// when that value round-trips through IEEE-754 binary64 (bdp#21, ruled
// 2026-09-08): read to the nearest double, serialized shortest, equal to the
// literal's own value. The refused half names, for each vector, what a
// binary64 reader would have made of it.
func TestCanonicalizeJSONNumbers(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"1.0", "1"}, {"-0.0", "0"}, {"-0", "0"}, {"0.0", "0"}, {"0e10", "0"}, {"0", "0"},
{"1.0", "1"}, {"1", "1"}, {"1e0", "1"}, {"-0.0", "0"}, {"-0", "0"}, {"0.0", "0"}, {"0e10", "0"}, {"0", "0"},
{"1e300", "1e+300"}, {"1E300", "1e+300"},
{"9007199254740993", "9007199254740993"}, // 2^53+1, exact
{"9007199254740992", "9007199254740992"},
{"18446744073709551616", "18446744073709551616"}, // 2^64, exact
{"1e21", "1e+21"}, {"1e20", "100000000000000000000"},
{"123456789012345678901", "123456789012345678901"},
{"1234567890123456789012", "1.234567890123456789012e+21"},
{"9007199254740992", "9007199254740992"}, // 2^53
{"-9007199254740992", "-9007199254740992"},
{"9007199254740994", "9007199254740994"}, // 2^53+2: a double whose shortest spelling is itself
{"18446744073709552000", "18446744073709552000"}, // what a binary64 reader makes of 2^64
{"1e21", "1e+21"}, {"1e20", "100000000000000000000"}, {"1000000000000000000000", "1e+21"},
{"123456789012345680000", "123456789012345680000"}, // 17 significant digits, then zeros
{"1e15", "1000000000000000"}, {"1000000000000000", "1000000000000000"},
{"0.000001", "0.000001"}, {"1e-6", "0.000001"},
{"0.0000001", "1e-7"}, {"1e-7", "1e-7"}, {"-1e-7", "-1e-7"},
{"0.0000001", "1e-7"}, {"1e-7", "1e-7"}, {"-1e-7", "-1e-7"}, {"1.5e-7", "1.5e-7"},
{"123.456e2", "12345.6"}, {"-1.5", "-1.5"}, {"1.50", "1.5"},
{"100", "100"}, {"1E+2", "100"}, {"1e-1", "0.1"}, {"0.1", "0.1"},
{"10.0e-1", "1"}, {"25e-1", "2.5"}, {"123e-2", "1.23"}, {"1e0", "1"},
{"100", "100"}, {"1E+2", "100"}, {"1e-1", "0.1"}, {"0.1", "0.1"}, {"0.10", "0.1"},
{"10.0e-1", "1"}, {"25e-1", "2.5"}, {"123e-2", "1.23"},
{"1.5e1", "15"}, {"9.99e2", "999"}, {"1.23e+3", "1230"}, {"0.5", "0.5"}, {"-0.5", "-0.5"},
{"1000000", "1000000"}, {"5e-324", "5e-324"},
{"1.7976931348623157e308", "1.7976931348623157e+308"},
{"-1e400", "-1e+400"}, // beyond double range: exact, where JCS could not serialize
{"0.1000000000000000055511151231257827021181583404541015625", "0.1000000000000000055511151231257827021181583404541015625"},
{"333333333.33333329", "333333333.33333329"}, // JCS's appendix rounds this to 333333333.3333333
{"1000000", "1000000"}, {"5e-324", "5e-324"}, {"-5e-324", "-5e-324"}, // the smallest subnormal
{"1e-323", "1e-323"}, {"1e308", "1e+308"},
{"1.7976931348623157e308", "1.7976931348623157e+308"}, // the largest double
{"0.30000000000000004", "0.30000000000000004"}, // 0.1+0.2, as a binary64 reader spells it
{"3.0000000000000004", "3.0000000000000004"},
{"333333333.3333333", "333333333.3333333"}, // RFC 8785 Appendix B's rounded value
{"1424953923781206.2", "1424953923781206.2"}, // RFC 8785 Appendix B, "round to even"
{"1.0000000000000001e+23", "1.0000000000000001e+23"}, {"9.999999999999997e+22", "9.999999999999997e+22"},
{"295147905179352830000", "295147905179352830000"}, // RFC 8785 Appendix B's spelling of 2^68
} {
got, err := graphops.CanonicalizeJSON([]byte(tc.in))
if err != nil {
@ -242,58 +320,152 @@ func TestCanonicalizeJSONNumbers(t *testing.T) {
if string(got) != tc.want {
t.Errorf("number %q canonicalized to %q, want %q", tc.in, got, tc.want)
}
checkAdmittedNumber(t, tc.in, got)
}
// Refused: peer is what a binary64 reader serializes the literal as; ""
// marks a literal outside the binary64 range altogether.
for _, tc := range []struct{ in, peer string }{
{"9007199254740993", "9007199254740992"}, // 2^53+1: halfway between two doubles, rounds to even
{"-9007199254740993", "-9007199254740992"},
{"9007199254740995", "9007199254740996"},
{"18446744073709551616", "18446744073709552000"}, // 2^64: exactly a double, but not its shortest spelling
{"18446744073709551615", "18446744073709552000"}, // MaxUint64
{"12345678901234567891", "12345678901234567000"}, // 20 significant digits
{"12345678901234567890", "12345678901234567000"},
{"123456789012345678901", "123456789012345680000"},
{"1234567890123456789012", "1.2345678901234568e+21"},
{"295147905179352825856", "295147905179352830000"}, // 2^68 exactly; a binary64 reader spells it rounded
{"1.00000000000000001", "1"},
{"0.1000000000000000055511151231257827021181583404541015625", "0.1"}, // 0.1's exact binary expansion
{"333333333.33333329", "333333333.3333333"}, // the RFC 8785 worked example's input
{"1424953923781206.25", "1424953923781206.2"},
{"1e-400", "0"}, {"1e-324", "0"}, {"1e-1000000000000", "0"}, // below the range: read as zero
{"2.4703282292062327e-324", "0"}, // just under half the smallest subnormal
{"4.9e-324", "5e-324"}, // rounds up to the smallest subnormal, which spells differently
{"1.7976931348623158e308", "1.7976931348623157e+308"}, // rounds down to the largest double
{"1e400", ""}, {"-1e400", ""}, {"1e309", ""}, {"1.7976931348623159e308", ""}, {"1e1000000000000", ""}, // beyond the range
} {
_, err := graphops.CanonicalizeJSON([]byte(tc.in))
wantValidation(t, err, "number "+tc.in)
want := `at JSON pointer "" is outside the IEEE-754 binary64 range (bdp#21`
if tc.peer != "" {
want = `at JSON pointer "" does not round-trip through IEEE-754 binary64: a binary64 reader serializes it as ` + tc.peer + ` (bdp#21)`
}
if !strings.Contains(err.Error(), want) {
t.Errorf("number %q: %v\n want a diagnostic containing %q", tc.in, err, want)
}
}
// Malformed literals are syntax errors, refused before the law is reached.
for _, in := range []string{
"01", "1.", ".5", "+1", "1e", "1e+", "-", "0x10", "NaN", "Infinity", "1_000", "--1", "1.e5", "-a",
"1e1000000000000000", // exponent out of range
} {
_, err := graphops.CanonicalizeJSON([]byte(in))
wantValidation(t, err, "number "+in)
if strings.Contains(err.Error(), "binary64") {
t.Errorf("number %q: refused by the admission law instead of its syntax: %v", in, err)
}
}
}
// The exponent bound applies to the NORMALIZED value, so canonicalization is
// closed: every admitted literal's canonical form re-parses to itself, and a
// literal whose canonical exponent would exceed the bound is refused on the
// way in — never admitted and then unreadable.
func TestCanonicalizeJSONNumberFixedPointAtTheExponentBounds(t *testing.T) {
// A refusal names where it bit: the RFC 6901 pointer of the member or
// element ("" is the whole document), with "~" and "/" in member names
// escaped, the byte offset of the literal, and the literal itself —
// abbreviated when it is long. Every door user-supplied JSON enters through
// reports the same way.
func TestCanonicalizeJSONBinary64RefusalNamesThePointer(t *testing.T) {
for _, tc := range []struct{ in, pointer string }{
{`9007199254740993`, ``},
{`{"n":9007199254740993}`, `/n`},
{`{"":9007199254740993}`, `/`},
{`{"a":[1,{"b":9007199254740993}]}`, `/a/1/b`},
{`[[[1e400]]]`, `/0/0/0`},
{`[1,[2,[3,4,1e400]]]`, `/1/1/2`},
{`{"a/b":{"m~n":[0,1,1e400]}}`, `/a~1b/m~0n/2`},
{`{"x":[{"y":[1,2,3,4,5,6,7,8,9,10,11,1e-400]}]}`, `/x/0/y/11`},
{`{"ok":[1.0,-0.0,1e300],"bad":{"deep":[{"n":18446744073709551616}]}}`, `/bad/deep/0/n`},
{`{"first":9007199254740993,"second":9007199254740993}`, `/first`}, // the first offender, in source order
} {
_, err := graphops.CanonicalizeJSON([]byte(tc.in))
wantValidation(t, err, tc.in)
if want := fmt.Sprintf("at JSON pointer %q", tc.pointer); !strings.Contains(err.Error(), want) {
t.Errorf("%s: %v\n want %s", tc.in, err, want)
}
}
// The byte offset is the literal's start.
_, err := graphops.CanonicalizeJSON([]byte(`{"n": 9007199254740993}`))
wantValidation(t, err, "offset")
if !strings.Contains(err.Error(), `JSON at byte 7: number 9007199254740993 at JSON pointer "/n"`) {
t.Errorf("byte offset: %v", err)
}
// A long literal is elided in the diagnostic; the pointer locates it.
long := strings.Repeat("9", 60)
_, err = graphops.CanonicalizeJSON([]byte(`{"n":` + long + `}`))
wantValidation(t, err, "long literal")
if !strings.Contains(err.Error(), `number `+long[:24]+`...`+long[:24]+` (60 bytes) at JSON pointer "/n"`) || strings.Contains(err.Error(), long) {
t.Errorf("long literal is not elided: %v", err)
}
// NewProperties and JSONEqual pass the diagnostic through.
_, err = graphops.NewProperties([]byte(`{"n":9007199254740993}`))
wantValidation(t, err, "properties")
if !strings.Contains(err.Error(), `properties: JSON at byte 5: number 9007199254740993 at JSON pointer "/n" does not round-trip`) {
t.Errorf("NewProperties: %v", err)
}
_, err = graphops.JSONEqual([]byte(`[1]`), []byte(`[9007199254740993]`))
wantValidation(t, err, "JSONEqual")
if !strings.Contains(err.Error(), `at JSON pointer "/0"`) {
t.Errorf("JSONEqual: %v", err)
}
// One refusal never poisons the next document: the scanner's location is
// per call.
if got, err := graphops.CanonicalizeJSON([]byte(`{"n":9007199254740992}`)); err != nil || string(got) != `{"n":9007199254740992}` {
t.Errorf("after a refusal: %q %v", got, err)
}
}
// The exponent bound (maxExponentMagnitude) is stated over the NORMALIZED
// value and is checked before the binary64 law: a literal past it is refused
// as "exponent out of range", one within it but past binary64 as outside the
// range. Zero, which has no magnitude, passes both whatever its exponent —
// short of the spelled-exponent guard — and canonicalization stays a fixed
// point on every admitted literal. In exact mode (the ledger framing) the
// bound is the only bound, which the frozen-layout test pins from its side.
func TestCanonicalizeJSONNumberExponentBoundAndTheBinary64Range(t *testing.T) {
const bound = "1000000000000" // maxExponentMagnitude
accept := []struct{ in, want string }{
{"1e" + bound, "1e+" + bound},
{"10e999999999999", "1e+" + bound}, // trailing zero moves the exponent up to the bound
{"1.0e" + bound, "1e+" + bound}, // fractional zero normalizes away
{"0.1e1000000000001", "1e+" + bound}, // the spelled exponent exceeds the bound; the value does not
{"0.001e1000000000003", "1e+" + bound}, // further past the spelled bound, same value
{"1.50e" + bound, "1.5e+" + bound}, // significant digits are kept
{"-1e" + bound, "-1e+" + bound}, // sign is orthogonal
{"1e-" + bound, "1e-" + bound}, // the negative bound
{"0.01e-999999999998", "1e-" + bound}, // fraction moves the exponent down to the bound
{"100e-1000000000002", "1e-" + bound}, // trailing zeros move it back up to the bound
{"1.5e-" + bound, "1.5e-" + bound}, // digits kept at the negative bound
{"0e1000000000005", "0"}, // every zero is "0", whatever its exponent
{"0.000e-1000000000005", "0"}, // and so is every fractional zero
{"123456789e999999999992", "1.23456789e+" + bound},
}
for _, tc := range accept {
once, err := graphops.CanonicalizeJSON([]byte(tc.in))
if err != nil {
t.Errorf("number %q: refused: %v", tc.in, err)
for _, tc := range []struct{ in, want string }{
{"0e" + bound, "0"}, {"0e1000000000005", "0"}, {"0.000e-1000000000005", "0"}, {"-0.0e999999999999", "0"},
{"0e-" + bound, "0"}, {"0.0e0", "0"},
} {
got, err := graphops.CanonicalizeJSON([]byte(tc.in))
if err != nil || string(got) != tc.want {
t.Errorf("number %q: got %q, %v; want %q", tc.in, got, err, tc.want)
continue
}
if string(once) != tc.want {
t.Errorf("number %q canonicalized to %q, want %q", tc.in, once, tc.want)
}
twice, err := graphops.CanonicalizeJSON(once)
if err != nil {
t.Errorf("canonical form %q does not re-parse: %v", once, err)
continue
}
if !bytes.Equal(once, twice) {
t.Errorf("canonical(canonical(%q)) = %q, canonical = %q: not a fixed point", tc.in, twice, once)
checkAdmittedNumber(t, tc.in, got)
}
// Within the bound, beyond binary64: refused by the admission law.
for _, tc := range []struct{ in, reason string }{
{"1e" + bound, "is outside the IEEE-754 binary64 range"},
{"10e999999999999", "is outside the IEEE-754 binary64 range"}, // trailing zero moves the exponent up to the bound
{"0.1e1000000000001", "is outside the IEEE-754 binary64 range"}, // the spelled exponent exceeds the bound; the value does not
{"1.50e" + bound, "is outside the IEEE-754 binary64 range"},
{"-1e" + bound, "is outside the IEEE-754 binary64 range"},
{"123456789e999999999992", "is outside the IEEE-754 binary64 range"},
{"1e-" + bound, "a binary64 reader serializes it as 0"}, // below the range
{"0.01e-999999999998", "a binary64 reader serializes it as 0"},
{"100e-1000000000002", "a binary64 reader serializes it as 0"},
{"1.5e-" + bound, "a binary64 reader serializes it as 0"},
{"1e309", "is outside the IEEE-754 binary64 range"}, // the first power of ten past the largest double
} {
_, err := graphops.CanonicalizeJSON([]byte(tc.in))
wantValidation(t, err, "number "+tc.in)
if !strings.Contains(err.Error(), tc.reason) {
t.Errorf("number %q: %v\n want %q", tc.in, err, tc.reason)
}
}
// Past the bound: refused as out of range, before the law is consulted.
for _, in := range []string{
"10e" + bound, // Codex's counterexample: canonically 1e+1000000000001
"10e" + bound, // canonically 1e+1000000000001
"1e1000000000001", // one past the bound
"100e" + bound, // two past
"1.5e1000000000001", // digits do not rescue it
@ -303,25 +475,125 @@ func TestCanonicalizeJSONNumberFixedPointAtTheExponentBounds(t *testing.T) {
"-10e" + bound, // sign is orthogonal
"1e99999999999999999999999", // absurd spelled exponent: refused before it can overflow
"1e-99999999999999999999999",
"0e99999999999999999999999", // zero too: the guard is on the spelling
} {
_, err := graphops.CanonicalizeJSON([]byte(in))
wantValidation(t, err, "number "+in)
if !strings.Contains(err.Error(), "exponent out of range") || strings.Contains(err.Error(), "binary64") {
t.Errorf("number %q: want the exponent bound's refusal, got %v", in, err)
}
}
// The same law holds inside a document, where a properties write lands.
doc := `{"big":10e999999999999,"small":100e-1000000000002}`
// The same laws hold inside a document, where a properties write lands.
doc := `{"zero":0e1000000000005,"big":1e308,"small":5e-324}`
once, err := graphops.CanonicalizeJSON([]byte(doc))
if err != nil {
t.Fatal(err)
}
if string(once) != `{"big":1e+`+bound+`,"small":1e-`+bound+`}` {
if string(once) != `{"big":1e+308,"small":5e-324,"zero":0}` {
t.Fatalf("document canonicalized to %s", once)
}
twice, err := graphops.CanonicalizeJSON(once)
if err != nil || !bytes.Equal(once, twice) {
t.Fatalf("document canonical form is not a fixed point: %s %v", twice, err)
}
if _, err := graphops.NewProperties([]byte(`{"x":10e` + bound + `}`)); !errors.Is(err, graphops.ErrValidation) {
t.Fatalf("a properties document beyond the bound must be refused at admission: %v", err)
for _, in := range []string{`{"x":10e` + bound + `}`, `{"x":1e` + bound + `}`, `{"x":1e400}`} {
if _, err := graphops.NewProperties([]byte(in)); !errors.Is(err, graphops.ErrValidation) {
t.Fatalf("a properties document %s must be refused at admission: %v", in, err)
}
}
}
// RFC 8785's own vectors. Appendix B's table lists binary64 bit patterns
// with the JSON each serializes to; under the admission law every such
// serialization is admitted, is its own canonical form, and reads back to
// the listed double. The RFC's worked example is refused as authored — its
// input carries 333333333.33333329, which JCS rounds — while the RFC's
// expected output is admitted as a fixed point, and the input with that one
// value already rounded canonicalizes to the RFC's output byte for byte. Then
// a seeded sweep of random doubles: each one's RFC 8785 serialization is
// admitted and canonical, and its EXACT decimal expansion is admitted only
// when it is that serialization's value (math/big is the oracle), in which
// case the two canonicalize identically.
func TestCanonicalizeJSONAgreesWithRFC8785(t *testing.T) {
for _, tc := range []struct {
bits uint64
json string
}{
{0x0000000000000000, "0"},
{0x8000000000000000, "0"}, // minus zero
{0x0000000000000001, "5e-324"},
{0x8000000000000001, "-5e-324"},
{0x7fefffffffffffff, "1.7976931348623157e+308"},
{0xffefffffffffffff, "-1.7976931348623157e+308"},
{0x4340000000000000, "9007199254740992"},
{0xc340000000000000, "-9007199254740992"},
{0x4430000000000000, "295147905179352830000"},
{0x44b52d02c7e14af5, "9.999999999999997e+22"},
{0x44b52d02c7e14af6, "1e+23"},
{0x44b52d02c7e14af7, "1.0000000000000001e+23"},
{0x444b1ae4d6e2ef4e, "999999999999999700000"},
{0x444b1ae4d6e2ef4f, "999999999999999900000"},
{0x444b1ae4d6e2ef50, "1e+21"},
{0x3eb0c6f7a0b5ed8c, "9.999999999999997e-7"},
{0x3eb0c6f7a0b5ed8d, "0.000001"},
{0x41b3de4355555553, "333333333.3333332"},
{0x41b3de4355555554, "333333333.33333325"},
{0x41b3de4355555555, "333333333.3333333"},
{0x41b3de4355555556, "333333333.3333334"},
{0x41b3de4355555557, "333333333.33333343"},
{0xbecbf647612f3696, "-0.0000033333333333333333"},
{0x43143ff3c1cb0959, "1424953923781206.2"}, // round to even
} {
f, err := strconv.ParseFloat(tc.json, 64)
if err != nil || f != math.Float64frombits(tc.bits) {
t.Errorf("premise: %q does not read back as %016x: %v", tc.json, tc.bits, err)
}
got, err := graphops.CanonicalizeJSON([]byte(tc.json))
if err != nil || string(got) != tc.json {
t.Errorf("RFC 8785 vector %q: got %q, %v", tc.json, got, err)
}
}
const rfcInput = `{"numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", "literals": [null, true, false]}`
const rfcOutput = "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"\u20ac$\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}"
_, err := graphops.CanonicalizeJSON([]byte(rfcInput))
wantValidation(t, err, "the RFC's worked example as authored")
if !strings.Contains(err.Error(), `number 333333333.33333329 at JSON pointer "/numbers/0" does not round-trip through IEEE-754 binary64: a binary64 reader serializes it as 333333333.3333333`) {
t.Errorf("worked example: %v", err)
}
got, err := graphops.CanonicalizeJSON([]byte(rfcOutput))
if err != nil || string(got) != rfcOutput {
t.Errorf("the RFC's expected output must be a fixed point: %q %v", got, err)
}
got, err = graphops.CanonicalizeJSON([]byte(strings.Replace(rfcInput, "333333333.33333329", "333333333.3333333", 1)))
if err != nil || string(got) != rfcOutput {
t.Errorf("the worked example with its rounded value: %q %v\nwant %q", got, err, rfcOutput)
}
rng := rand.New(rand.NewPCG(0x8785, 0x21))
for i := 0; i < 2000; i++ {
f := math.Float64frombits(rng.Uint64())
if math.IsNaN(f) || math.IsInf(f, 0) {
continue
}
jcs := jcsNumber(f)
got, err := graphops.CanonicalizeJSON([]byte(jcs))
if err != nil || string(got) != jcs {
t.Fatalf("%016x: RFC 8785 form %q: got %q, %v", math.Float64bits(f), jcs, got, err)
}
exact := strconv.FormatFloat(f, 'e', 1100, 64) // every double's decimal expansion ends within 1100 digits
jcsValue, ok := new(big.Rat).SetString(jcs)
if !ok {
t.Fatalf("%016x: oracle cannot read %q", math.Float64bits(f), jcs)
}
got, err = graphops.CanonicalizeJSON([]byte(exact))
if new(big.Rat).SetFloat64(f).Cmp(jcsValue) == 0 {
if err != nil || string(got) != jcs {
t.Fatalf("%016x: exact expansion %q is the RFC 8785 value and must canonicalize to %q: got %q, %v", math.Float64bits(f), exact, jcs, got, err)
}
} else if !errors.Is(err, graphops.ErrValidation) || !strings.Contains(err.Error(), "a binary64 reader serializes it as "+jcs+" (bdp#21)") {
t.Fatalf("%016x: exact expansion must be refused naming %q: got %q, %v", math.Float64bits(f), jcs, got, err)
}
}
}
@ -383,12 +655,12 @@ func TestCanonicalizeJSONStructure(t *testing.T) {
`{"\u20ac":"Euro Sign","\r":"Carriage Return","\ufb33":"Hebrew Letter Dalet With Dagesh","1":"One","\ud83d\ude02":"Emoji: Face with Tears of Joy","\u0080":"Control","\u00f6":"Latin Small Letter O With Diaeresis"}`,
"{\"\\r\":\"Carriage Return\",\"1\":\"One\",\"\u0080\":\"Control\",\"\u00f6\":\"Latin Small Letter O With Diaeresis\",\"\u20ac\":\"Euro Sign\",\"\U0001F602\":\"Emoji: Face with Tears of Joy\",\"\uFB33\":\"Hebrew Letter Dalet With Dagesh\"}",
},
// RFC 8785 Appendix's example, under the exact-number rule: identical
// to the appendix's output except that 333333333.33333329 keeps its
// digits where JCS rounds it through a double.
// RFC 8785's worked example, with the one value JCS rounds already
// rounded: TestCanonicalizeJSONAgreesWithRFC8785 pins that the
// example as authored is refused at /numbers/0.
{
`{"numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", "literals": [null, true, false]}`,
"{\"literals\":[null,true,false],\"numbers\":[333333333.33333329,1e+30,4.5,0.002,1e-27],\"string\":\"\u20ac$\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}",
`{"numbers": [333333333.3333333, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", "literals": [null, true, false]}`,
"{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"\u20ac$\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}",
},
} {
got, err := graphops.CanonicalizeJSON([]byte(tc.in))
@ -447,7 +719,8 @@ func TestJSONEqual(t *testing.T) {
want bool
}{
{"1", "1.0", true}, {"1", `"1"`, false}, {"1e2", "100", true}, {"0.1", "0.10", true}, {"-0", "0", true},
{"9007199254740993", "9007199254740992", false}, {"1", "1.00000000000000001", false},
{"9007199254740994", "9007199254740992", false}, {"1", "1.0000000000000002", false},
{"9007199254740992", "9.007199254740992e15", true}, {"0.1", "1e-1", true},
{`{"a":1,"b":2}`, `{"b":2,"a":1}`, true}, {`{"a":1}`, `{"a":1,"b":2}`, false},
{"[1,2]", "[2,1]", false}, {"[1,2]", "[1,2]", true}, {"[]", "{}", false},
{"null", "null", true}, {`{"a":null}`, `{}`, false}, {"true", "false", false},
@ -469,6 +742,14 @@ func TestJSONEqual(t *testing.T) {
if _, err := graphops.JSONEqual([]byte("1"), []byte("}")); err == nil {
t.Error("JSONEqual accepted a malformed right operand")
}
// A number the admission law refuses is an error on either side, never a
// verdict: 9007199254740993 is not "unequal to" 9007199254740992, it is
// not a value the domain holds.
for _, tc := range [][2]string{{"9007199254740993", "9007199254740992"}, {"1", "1.00000000000000001"}, {`{"a":1e400}`, `{"a":1e400}`}} {
if _, err := graphops.JSONEqual([]byte(tc[0]), []byte(tc[1])); !errors.Is(err, graphops.ErrValidation) {
t.Errorf("JSONEqual(%s, %s): want ErrValidation, got %v", tc[0], tc[1], err)
}
}
}
// --- Scope URL and Type URL -----------------------------------------------
@ -750,6 +1031,20 @@ func TestLedgerHashLayoutIsFrozen(t *testing.T) {
if tomb.Hash() != sha256Hex([]byte(wantTomb)) {
t.Fatal("tombstone hash is not sha256 of the canonical bytes")
}
// The framing is exact and EXEMPT from the binary64 admission law
// (bdp#21 DECISION in laws.go): epoch 2^64-1 has no binary64 spelling,
// so the public canonicalizer refuses these very bytes, and the ledger
// hashes them unchanged. The same holds for a seq past 2^53.
if _, err := graphops.CanonicalizeJSON([]byte(wantTomb)); !errors.Is(err, graphops.ErrValidation) || !strings.Contains(err.Error(), `number 18446744073709551615 at JSON pointer "/epoch"`) {
t.Fatalf("the ledger framing must be exempt from the admission law CanonicalizeJSON enforces: %v", err)
}
far := mustEvent(t, graphops.LedgerEventSpec{
Seq: 9007199254740993, Kind: graphops.LedgerPromote, OpID: opID,
AuthorityID: authorityID, Epoch: 9007199254740993, At: at, PrevHash: mint.Hash(),
})
if got := string(far.CanonicalBytes()); !strings.Contains(got, `"epoch":9007199254740993,`) || !strings.HasSuffix(got, `"seq":9007199254740993}`) {
t.Fatalf("seq and epoch past 2^53 must be hashed exactly: %s", got)
}
fp := strings.Repeat("ab", 32)
install := mustEvent(t, graphops.LedgerEventSpec{

View file

@ -209,15 +209,18 @@ func (a Attribution) IsZero() bool { return a.principal == "" }
// Properties is the authored JSON OBJECT of a Bead or Link, held as ONE
// canonical byte string: the RFC 8785 form of the document with numbers kept
// exact (see CanonicalizeJSON). Two Properties are the same value exactly when
// exact and admitted only when they round-trip through binary64 (see
// CanonicalizeJSON; bdp#21). Two Properties are the same value exactly when
// their bytes are equal, and that equality is the RFC 6902 §4.6 comparison the
// no-op law is made of — so a write whose result Equal()s the value before it
// mints no revision.
//
// The zero value is the empty object. Properties is never engine JSON: the
// storage leg stores these bytes in a BLOB and serves them back unchanged, so
// what a creator wrote — 9007199254740993, 1e300, a key order — survives
// exactly as canonicalized, never as some engine's float64 reading of it.
// what a creator wrote — 9007199254740992, 1e300, 0.1 — survives exactly as
// canonicalized, never as some engine's float64 reading of it; and a number
// no binary64 reader could hand back unchanged (9007199254740993) is refused
// at the door rather than stored and later rounded by a peer.
type Properties struct{ canonical []byte }
var emptyObject = []byte("{}")
@ -704,8 +707,8 @@ func NewOwnedLinkDecl(linkTypeURL, label string, max int) (OwnedLinkDecl, error)
if err := ValidateTypeURL(linkTypeURL); err != nil {
return OwnedLinkDecl{}, fmt.Errorf("ownsOutgoing key: %w", err)
}
if max < 1 {
return OwnedLinkDecl{}, fmt.Errorf("%w: ownsOutgoing %s: max must be a positive integer", ErrValidation, linkTypeURL)
if err := validateOwnedMax(linkTypeURL, max); err != nil {
return OwnedLinkDecl{}, err
}
if err := validUTF8(label, "ownsOutgoing "+linkTypeURL+": label"); err != nil {
return OwnedLinkDecl{}, err
@ -735,12 +738,33 @@ func NewOwnedLinkDecl(linkTypeURL, label string, max int) (OwnedLinkDecl, error)
// explicit Types included — is the one the bdp spec PR for item 5 would have
// to state; this package takes the per-declaration reading until it does.
func NewWildcardOwnedLinkDecl(max int) (OwnedLinkDecl, error) {
if max < 1 {
return OwnedLinkDecl{}, fmt.Errorf("%w: ownsOutgoing %s: max must be a positive integer", ErrValidation, WildcardOwnedLinkKey)
if err := validateOwnedMax(WildcardOwnedLinkKey, max); err != nil {
return OwnedLinkDecl{}, err
}
return OwnedLinkDecl{typeURL: WildcardOwnedLinkKey, max: max}, nil
}
// validateOwnedMax is the max law both declarations share: positive, and —
// because a descriptor's canonical form spells max as a JSON number — under
// the binary64 admission law (bdp#21), so that every descriptor that exists
// has a canonical form ParseTypeDescriptor re-admits.
//
// DECISION: the law is applied at this door with the SAME predicate the
// canonicalizer uses, not with a simpler 2^53 cap. 9007199254740994 is then
// admitted at both doors and 9007199254740993 refused at both, so the JSON
// door and the Go constructor agree on exactly which descriptors exist; a
// cap would have made the constructor stricter than the wire for no
// consumer's benefit.
func validateOwnedMax(key string, max int) error {
if max < 1 {
return fmt.Errorf("%w: ownsOutgoing %s: max must be a positive integer", ErrValidation, key)
}
if !admissibleInt(max) {
return fmt.Errorf("%w: ownsOutgoing %s: max %d does not round-trip through IEEE-754 binary64 (bdp#21)", ErrValidation, key, max)
}
return nil
}
// Wildcard reports whether d is the wildcard declaration.
func (d OwnedLinkDecl) Wildcard() bool { return d.typeURL == WildcardOwnedLinkKey }
@ -1142,6 +1166,8 @@ func parseOwnedLinkDecl(key string, canonical []byte) (OwnedLinkDecl, error) {
// The canonical form spells every integral number as a plain
// integer (1.0 and 1e0 are "1"), so a canonical value that
// ParseInt refuses is fractional, out of range, or not a number.
// A value the binary64 admission law refuses never reaches here:
// CanonicalizeJSON refused the document, naming this member.
n, err := strconv.ParseInt(string(m.value), 10, 0)
if err != nil {
return fail("max must be an integer within the platform int range, got %s", m.value)

View file

@ -3,6 +3,8 @@ package graphops_test
import (
"encoding/json"
"errors"
"fmt"
"math"
"regexp"
"strings"
"testing"
@ -1250,3 +1252,56 @@ func TestCheckBeadRecordUnderAWildcard(t *testing.T) {
t.Errorf("lone wildcard, nothing present: %v", err)
}
}
// ownsOutgoing max obeys the binary64 admission law at both of its doors
// (bdp#21): the JSON door refuses it inside CanonicalizeJSON, naming the
// member's pointer (the Type URL's slashes escaped per RFC 6901), and the Go
// constructors refuse the same values with the same predicate, so every
// descriptor that exists has a canonical form the JSON door re-admits,
// fingerprint intact.
func TestOwnedLinkDeclMaxObeysTheBinary64Law(t *testing.T) {
const c = "https://work.example/types/c"
for _, max := range []int{9007199254740993, 9007199254740995, 1<<60 + 1, 1 << 60, math.MaxInt64} {
for name, construct := range map[string]func() error{
"explicit": func() error { _, err := graphops.NewOwnedLinkDecl(c, "", max); return err },
"wildcard": func() error { _, err := graphops.NewWildcardOwnedLinkDecl(max); return err },
} {
err := construct()
if !errors.Is(err, graphops.ErrValidation) || !strings.Contains(err.Error(), fmt.Sprintf("max %d does not round-trip through IEEE-754 binary64 (bdp#21)", max)) {
t.Errorf("%s max %d: want the binary64 refusal, got %v", name, max, err)
}
}
doc := fmt.Sprintf(`{"id":"https://work.example/types/x","name":"X","describes":"bead","conformsTo":[],"ownsOutgoing":{"%s":{"max":%d}}}`, c, max)
_, err := graphops.ParseTypeDescriptor([]byte(doc))
if !errors.Is(err, graphops.ErrValidation) || !strings.Contains(err.Error(), `at JSON pointer "/ownsOutgoing/https:~1~1work.example~1types~1c/max"`) {
t.Errorf("descriptor max %d: want the binary64 refusal at the member's pointer, got %v", max, err)
}
}
explicit, err := graphops.NewOwnedLinkDecl(c, "", 9007199254740994)
if err != nil {
t.Fatal(err)
}
wildcard, err := graphops.NewWildcardOwnedLinkDecl(9007199254740992)
if err != nil {
t.Fatal(err)
}
built, err := graphops.NewTypeDescriptor(graphops.TypeDescriptorSpec{ID: "https://work.example/types/x", Name: "X", Describes: graphops.KindBead, OwnsOutgoing: []graphops.OwnedLinkDecl{explicit, wildcard}})
if err != nil {
t.Fatal(err)
}
want := `{"conformsTo":[],"describes":"bead","id":"https://work.example/types/x","name":"X","ownsOutgoing":{"*":{"max":9007199254740992},"https://work.example/types/c":{"max":9007199254740994}}}`
if got := string(built.CanonicalJSON()); got != want {
t.Fatalf("canonical form\n got %s\nwant %s", got, want)
}
parsed, err := graphops.ParseTypeDescriptor(built.CanonicalJSON())
if err != nil || parsed.Fingerprint() != built.Fingerprint() {
t.Fatalf("the canonical form must re-admit with the same fingerprint: %v", err)
}
maxes := map[string]int{}
for _, d := range parsed.OwnsOutgoing() {
maxes[d.TypeURL()] = d.Max()
}
if len(maxes) != 2 || maxes[c] != 9007199254740994 || maxes[graphops.WildcardOwnedLinkKey] != 9007199254740992 {
t.Fatalf("max values after the round trip: %v", maxes)
}
}