Skip to content

test: property-based tests for the off-chain/circuit parity checks - #299

Closed
nicolasLuduena wants to merge 2 commits into
test/vitest-workspace-globfrom
test/298-property-tests
Closed

nicolasLuduena wants to merge 2 commits into
test/vitest-workspace-globfrom
test/298-property-tests

Conversation

@nicolasLuduena

Copy link
Copy Markdown
Member

Closes #298.

Stacked on #281 (stack: #291 → #292 → #293 → #294 → #285 → #283 → #282 → #281 → this). Test-only: no production code, wire layout or schema changes.

Summary

fast-check generates the inputs for each check, and a failure shrinks to a minimal counterexample. fast-check is a dev dependency of core, contracts and cardano only.

# Property Where Oracle
1 claimToMessage(claim) equals message(toContractClaim(claim)) contracts/test/parity.test.ts MockAttestation (V=3, W=0) and MockAttestationBytes (V=3, W=1)
2 subjectHashOf, holderCommitmentOf and cardanoAddressHashOf match their circuits subject-parity, holder-parity, cardano/.../address-parity the compiled pure circuits
3 two different claims never share a message core/test/claim.test.ts any shape
4 the policy hash is the same exactly when two values mean the same data core/test/claim-context.test.ts a model of the canonical form
5 every claim claimSchema accepts encodes, and each field fits its Compact type core/test/claim.test.ts any shape
packages/core/test/arbitraries.ts    # anyClaim({ v, w }), anySlots, anyBytes32, anyUint
  ├── core/test/claim.test.ts         # properties 3, 5
  └── contracts/test/*parity.test.ts  # properties 1, 2

Generated claims follow the present mask semantics: an absent slot carries zero, and a measured slot may still hold 0.

Decisions the issue left open

  • Address length: the check across lengths stays off-chain, with no new mock specializations. The circuit property covers every 103-byte address. Off-chain, appending bech32 characters to an address, or zero-padding it to a longer Bytes<L>, always changes the digest.
  • Reproducibility: fast-check uses a random seed and prints the seed and path on failure. Pass both to fc.assert to replay a failure. AGENTS.md now says so.
  • Canonical JSON: a bigint and its decimal string, bytes and their hex string, and -0 and 0 share one serialization. That is the documented encoding, so property 4 treats those pairs as the same data. Two policies of one kind could share a hash only if a field's schema accepted both a bigint and a string. I did not file a bug for this.

Removed, because a property now covers them: the 11-claim parity list and its toContractClaim loop, the fixed subject and holder samples, the single-address witness case, the window-swap and flip-every-byte circuit tests, the bytes round-trip and B = 0 parity cases, and the key-order and array-order policy examples.

Kept, because they pin a specific vector: the B = 0 vault id, the known-answer subject, holder and bakedHash digests, the absent-half and zero-padding cases in bytes.test.ts, and the wire-position pins in attestation.test.ts and claim.test.ts.

Evidence

No verification skill in this repo, so the test runs below are the evidence.

  • Before: 776 passed, 4 skipped. The hand-picked samples checked one claim list, one address and one byte-flip loop.
    After: 776 passed, 4 skipped (the opt-in live checks), at the pushed head 9127a17. npm run types and npm run check pass. The properties run 100 cases each (20 for the byte sweep) and add about 0.3 s in total.

  • Breaking claimToMessage's field order on purpose fails property 1 (not committed):

    claimToMessage: windowEnd, windowStart   (swapped)
    × matches the numeric-only mock buildMessage (V = 3, K = 20)
      Property failed after 1 tests  { seed: -442721489, path: "0:0:…" }
      Counterexample: [{ version: 0n, appId: 0…0, …, windowStart: 0n, windowEnd: 1n, …,
                         present: [true,false,true], values: [0n,0n,0n], byteValues: [] }]
      Shrunk 186 time(s)
    
  • Mutation runs (each break applied to packages/core/src, then restored from git):

    Break Caught by Runs failed
    windowStart written twice, windowEnd lost 3 5/5
    last byte of each 16-byte half zeroed 3 (byte sweep) 5/5
    byte-half count dropped from the message 3 (same-length layouts) 10/10
    object keys left unsorted 4 10/10
    bigint written unquoted 4 10/10
    null dropped from arrays 4 10/10
    string arrays sorted 4 20/20

Merge Danger

Door: two-way

Blast Radius: tests

Only test files, the three dev dependencies and one AGENTS.md paragraph change. Seeds are random, so a property can fail on a seed that has never run before. When that happens, the printed seed and path replay it.

Generate claims, subjects, holder secrets, addresses and canonical values
with fast-check, and check encoder and hash parity against the compiled
mock circuits, message injectivity, canonical JSON, and the schema to
encoder round trip. Remove the hand-picked cases the properties cover.
Change one claim field at a time in most injectivity pairs and change
every byte of every 32-byte field, so a field or byte the encoder drops
fails the property. Pair policy values with one-step edits and reversed
arrays, so lost array order or dropped nulls fail the canonical JSON
property. Name the shared generators for what they produce and share the
header field table.
@nicolasLuduena
nicolasLuduena marked this pull request as draft November 25, 2026 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant