Skip to content
Myr

commitment.test.ts

This file

Path

packages/visibility/test/commitment.test.ts

265 lines · 8.1 KiB · TypeScript

Last changed by

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–33why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import type { HeldBackResult } from "@repo/protocol/domain/answer";3import { AreaName, BlobSha, RepoPath, SuiteRoot } from "@repo/protocol/ids";4import { Effect, Option, Schema } from "effect";56import {7 areaRoots,8 heldBackTrailer,9 heldBackValue,10 suiteRoot,11} from "../src/commitment.ts";12import type { SuiteFile } from "../src/commitment.ts";1314const salt = Uint8Array.from({ length: 32 }, (_, index) => index);1516const exportArea = AreaName.make("export");1718const parserArea = AreaName.make("parser");1920const file = (path: string, area: AreaName, digit: string): SuiteFile => ({21 area,22 blob: BlobSha.make(digit.repeat(40)),23 path: RepoPath.make(path),24});2526const a = file("checks/export/a.test.ts", exportArea, "1");2728const b = file("checks/export/b.test.ts", exportArea, "2");2930const c = file("checks/parser/c.test.ts", parserArea, "3");3132const suite = [a, b, c];33

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

line 34why, one step back
34// The construction, written out by hand with WebCrypto:

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 35–265why, one step back
35// leaf = H(salt ‖ 0x00 ‖ path ‖ 0x00 ‖ blob id), node = H(0x01 ‖ left ‖ right),
36// an odd one out promoted; the suite joins each area's root under its name,37// H(0x02 ‖ area ‖ 0x00 ‖ area root), in the same tree, areas sorted.38const utf8 = (text: string) => new TextEncoder().encode(text);3940const sha256 = (...parts: readonly Uint8Array[]) =>41 Effect.promise(() =>42 crypto.subtle.digest(43 "SHA-256",44 Uint8Array.from(parts.flatMap((part) => [...part]))45 )46 ).pipe(Effect.map((digest) => new Uint8Array(digest)));4748const leaf = ({ blob, path }: SuiteFile) =>49 sha256(salt, Uint8Array.of(0), utf8(path), Uint8Array.of(0), utf8(blob));5051const node = (left: Uint8Array, right: Uint8Array) =>52 sha256(Uint8Array.of(1), left, right);5354const named = (area: string, root: Uint8Array) =>55 sha256(Uint8Array.of(2), utf8(area), Uint8Array.of(0), root);5657const hex = (bytes: Uint8Array) =>58 `sha256:${[...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;5960// The same vectors from Python's hashlib, an implementation of its own.61const vectors = {62 exportRoot:63 "sha256:cda4db168b5b2967a1560c909b01dcfc04ae78f7b556d634b32d1a9ebb9de04e",64 oneAreaRoot:65 "sha256:86f9b2ca4a764792d4dfbfbe347813a8c03555b057332256298ff8e6c0ab4695",66 oneAreaSuite:67 "sha256:06c8e5cd215d629e4fb0245f5ac7a4929fd2075261d1ae94d31a6dafafcf17af",68 parserRoot:69 "sha256:c601cb4e3ac341f25fe476b8ff09fc88dbf325ad70f91326a879c5e58ef8ae56",70 suite:71 "sha256:7f06a303c5990bd14625987bf96771c7f89573a135636e32c765447f169f51ba",72};7374describe("a held-back suite's commitment", () => {75 it.effect("matches known-answer vectors for a three-file suite", () =>76 Effect.gen(function* () {77 const exportRoot = yield* node(yield* leaf(a), yield* leaf(b));78 const parserRoot = yield* leaf(c);7980 const byHand = yield* node(81 yield* named("export", exportRoot),82 yield* named("parser", parserRoot)83 );8485 expect(hex(exportRoot)).toBe(vectors.exportRoot);86 expect(hex(parserRoot)).toBe(vectors.parserRoot);87 expect(hex(byHand)).toBe(vectors.suite);8889 expect(yield* suiteRoot(salt, suite)).toBe(vectors.suite);90 expect(yield* areaRoots(salt, suite)).toStrictEqual(91 new Map([92 [exportArea, vectors.exportRoot],93 [parserArea, vectors.parserRoot],94 ])95 );96 })97 );9899 it.effect("promotes the odd leaf in an area of three", () =>100 Effect.gen(function* () {101 const moved = { ...c, area: exportArea };102103 const root = yield* node(104 yield* node(yield* leaf(a), yield* leaf(b)),105 yield* leaf(moved)106 );107108 expect(hex(root)).toBe(vectors.oneAreaRoot);109 expect(hex(yield* named("export", root))).toBe(vectors.oneAreaSuite);110111 expect(yield* areaRoots(salt, [a, b, moved])).toStrictEqual(112 new Map([[exportArea, vectors.oneAreaRoot]])113 );114 expect(yield* suiteRoot(salt, [a, b, moved])).toBe(vectors.oneAreaSuite);115 })116 );117118 it.effect("is the same for the same salt and files, in any order", () =>119 Effect.gen(function* () {120 const root = yield* suiteRoot(salt, suite);121122 for (const order of [123 [a, b, c],124 [c, b, a],125 [b, c, a],126 [c, a, b],127 ]) {128 expect(yield* suiteRoot(salt, order)).toBe(root);129 expect(yield* areaRoots(salt, order)).toStrictEqual(130 yield* areaRoots(salt, suite)131 );132 }133134 expect(Schema.is(SuiteRoot)(root)).toBeTruthy();135 })136 );137138 it.effect(139 "changes when any file's content, path or area changes, or the salt does",140 () =>141 Effect.gen(function* () {142 const root = yield* suiteRoot(salt, suite);143144 const changed = [145 [a, b, { ...c, blob: BlobSha.make("4".repeat(40)) }],146 [a, b, { ...c, path: RepoPath.make("checks/parser/d.test.ts") }],147 [a, b, { ...c, area: AreaName.make("format") }],148 [a, { ...b, area: parserArea }, c],149 [a, b],150 ];151152 for (const files of changed) {153 expect(yield* suiteRoot(salt, files)).not.toBe(root);154 }155156 const salted = salt.toReversed();157158 expect(yield* suiteRoot(salted, suite)).not.toBe(root);159 })160 );161162 it.effect("roots each area in that area's leaves alone", () =>163 Effect.gen(function* () {164 const roots = yield* areaRoots(salt, suite);165 const exportOnly = yield* areaRoots(salt, [b, a]);166167 expect(roots.get(exportArea)).toBe(exportOnly.get(exportArea));168 expect(roots.get(parserArea)).toBe(169 (yield* areaRoots(salt, [c])).get(parserArea)170 );171 })172 );173174 it.effect("roots an empty suite in the hash of nothing", () =>175 Effect.gen(function* () {176 expect(yield* suiteRoot(salt, [])).toBe(177 "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"178 );179 expect(yield* areaRoots(salt, [])).toStrictEqual(new Map());180 })181 );182});183184const root = (digit: string) => SuiteRoot.make(`sha256:${digit.repeat(64)}`);185186const result = (187 area: AreaName,188 passed: number,189 total: number,190 commitment = root("3")191): HeldBackResult => ({ area, passed, suite: commitment, total });192193describe("the Held-back trailer", () => {194 it("names each area's result and its root, and n/a for an area with no checks in this run", () => {195 expect(196 heldBackTrailer({197 areas: [parserArea, exportArea],198 results: [result(exportArea, 7, 7)],199 })200 ).toStrictEqual(201 Option.some(202 `Held-back: export 7/7 @ sha256:${"3".repeat(64)}; parser n/a`203 )204 );205 });206207 it("is the value alone for the trailer machinery, without its key", () => {208 expect(209 heldBackValue({210 areas: [exportArea, parserArea],211 results: [result(exportArea, 7, 7)],212 })213 ).toStrictEqual(214 Option.some(`export 7/7 @ sha256:${"3".repeat(64)}; parser n/a`)215 );216 });217218 it("sorts the areas, whatever order they ran or were declared in", () => {219 const format = AreaName.make("format");220221 const expected = Option.some(222 [223 `Held-back: export 2/3 @ sha256:${"1".repeat(64)}`,224 "format n/a",225 `parser 1/1 @ sha256:${"2".repeat(64)}`,226 ].join("; ")227 );228229 expect(230 heldBackTrailer({231 areas: [parserArea, format, exportArea],232 results: [233 result(parserArea, 1, 1, root("2")),234 result(exportArea, 2, 3, root("1")),235 ],236 })237 ).toStrictEqual(expected);238 expect(239 heldBackTrailer({240 areas: [exportArea, parserArea, format],241 results: [242 result(exportArea, 2, 3, root("1")),243 result(parserArea, 1, 1, root("2")),244 ],245 })246 ).toStrictEqual(expected);247 });248249 it("names an area that ran even when it is not declared", () => {250 expect(251 heldBackTrailer({ areas: [], results: [result(exportArea, 1, 2)] })252 ).toStrictEqual(253 Option.some(`Held-back: export 1/2 @ sha256:${"3".repeat(64)}`)254 );255 });256257 it("is none when no area ran", () => {258 expect(259 heldBackTrailer({ areas: [exportArea, parserArea], results: [] })260 ).toStrictEqual(Option.none());261 expect(heldBackTrailer({ areas: [], results: [] })).toStrictEqual(262 Option.none()263 );264 });265});