did-btcr2-js

ADR 166: BTCR2MerkleTree Refuses a Nonce That Is Not 256 Bits, and the SMT Proof Verifier Accepts a Nonce of Any Length

Context

The tree accepted a nonce of any length

BTCR2MerkleTree.addEntries checks the updateId of each entry. It refuses an updateId that is not 32 bytes with a RangeError. addEntries did not check the nonce. SmtApi.build decodes the nonce of each entry and gives it to addEntries, also with no check. ADR 120 says: “A nonce has any length.”

Thus a caller could build a tree with a 0-byte or a 1-byte nonce and publish its root in a Beacon Signal. verifyProof returns true for the proof of the DID with that nonce.

A short nonce does not hide the update

With a nonce, the leaf value of an index is hash(hash(nonce) + updateId) for an update, or hash(hash(nonce)) for no update. The SMT appendix gives the purpose of the nonce: “With a nonce, other parties (aggregators, other DID controllers, verifiers) cannot know if there is an update, or its contents. Only the DID controller can tell them.”

Other parties can see a leaf value. The Aggregate Beacons example, “Respond to Update Opportunities”, says that the Aggregation Participant sends hash(hash(nonce) + updateId) or hash(hash(nonce)) to the Aggregation Service. A party with a leaf value can compare it with hash(hash(n)) for each guess n. For a 0-byte nonce, one guess is sufficient. For a 1-byte nonce, 256 guesses are sufficient. A match shows that the signal announces no update for the DID.

The Beacon Signal stays on Bitcoin, so the DID controller cannot replace the nonce later.

The specification gives the nonce 256 bits

Data Structures, “SMT Proof”, says: “nonce: OPTIONAL 256-bit nonce, one for each index in each [Beacon Signal].”

The SMT appendix says: “The nonce is an OPTIONAL 256-bit value that is unique for each DID and each signal.” It also says: “The inner hash hash(nonce) is 32 bytes for a nonce of any length. Thus [SMT Proof Verification] accepts a nonce of any length.”

No BCP 14 keyword sets the length of the nonce. The data structure gives 256 bits. The rule “any length” applies only to SMT Proof Verification.

Decision

1. addEntries refuses a nonce that is not 32 bytes

BTCR2MerkleTree.addEntries refuses a nonce that is not 32 bytes with a RangeError. The refusal also applies to a nonce that is longer than 32 bytes. The message is `Invalid nonce: expected 32 bytes, got ${n}`. The message prints only a number (ADR 156). The check runs before the updateId check. An entry without a nonce (no-nonce mode) does not change.

SmtApi.build calls addEntries, so it gives the same refusal. A nonce of '' decodes to 0 bytes, so SmtApi.build refuses it. A nonce that is not base64url without padding gives the Error of the decoder, as before.

2. Each producer in this repository uses the check

Each producer in this repository builds its tree with addEntries:

3. The verifier does not change

leafValue, verifyProof, verifySerializedProof, and deserializeProof accept a nonce of any length. verifyProof returns true for a valid proof from another implementation with another nonce length.

4. serializeProof gets no check

serializeProof writes the fields that the caller gives. BTCR2MerkleTree.proof gives it the nonce of an entry that addEntries checked. Thus a check in serializeProof adds nothing for the tree. A test uses leafValue, zeroHashRoot, generateZeroHashProof, and serializeProof to make a proof with a 16-byte nonce for the verifier.

5. The docs give the two rules

The JSDoc of TreeEntry.nonce, SmtEntry.nonce, SMTProof, and SMTProof.nonce says that a nonce is 256 bits. The JSDoc of TreeEntry.nonce, SMTProof, and SMTProof.nonce also says that the verifier accepts a nonce of any length. The smt README gives both rules.

6. Each judgment call conforms to the specification texts

Judgment call Text Result
addEntries refuses a nonce that is not 32 bytes (decision 1). Data Structures, “SMT Proof”: “nonce: OPTIONAL 256-bit nonce, one for each index in each [Beacon Signal].” SMT appendix: “The nonce is an OPTIONAL 256-bit value that is unique for each DID and each signal.” Conforms. The data structure gives the length.
addEntries also refuses a nonce that is longer than 32 bytes (decision 1). The same two texts. Conforms. A longer nonce is not 256 bits.
The refusal is a RangeError (decision 1). The specification does not define the errors of a library that builds a tree. Conforms. addEntries refuses an updateId that is not 32 bytes with the same error class.
The verifier accepts a nonce of any length (decision 3). SMT appendix: “The inner hash hash(nonce) is 32 bytes for a nonce of any length. Thus [SMT Proof Verification] accepts a nonce of any length.” Conforms.
serializeProof and leafValue get no check (decisions 3 and 4). SMT Proof Verification uses the leaf value. The specification does not define the functions of a library. Conforms. leafValue is a step of the verifier.

Alternatives

Refuse only a nonce that is shorter than 32 bytes. Rejected. The data structure gives 256 bits, not a minimum length. A longer nonce adds no privacy to a 256-bit random nonce.

Check the nonce in SmtApi.build only. Rejected. BTCR2MerkleTree is a public export of the smt package. A direct caller of addEntries then gets no check. One check in addEntries covers each producer.

Check the nonce in leafValue. Rejected. The verifier calls leafValue, and SMT Proof Verification accepts a nonce of any length.

Use validateHash for the check. Rejected. Its message says “hash”, and a nonce is not a hash.

Consequences

These residuals stay:

Superseded.

Unchanged.

Implementation

References