merkle.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.verifyMerkleInclusion = verifyMerkleInclusion;
  4. /*
  5. Copyright 2023 The Sigstore Authors.
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. const core_1 = require("@sigstore/core");
  17. const error_1 = require("../error");
  18. const RFC6962_LEAF_HASH_PREFIX = Buffer.from([0x00]);
  19. const RFC6962_NODE_HASH_PREFIX = Buffer.from([0x01]);
  20. function verifyMerkleInclusion(entry) {
  21. const inclusionProof = entry.inclusionProof;
  22. const logIndex = BigInt(inclusionProof.logIndex);
  23. const treeSize = BigInt(inclusionProof.treeSize);
  24. if (logIndex < 0n || logIndex >= treeSize) {
  25. throw new error_1.VerificationError({
  26. code: 'TLOG_INCLUSION_PROOF_ERROR',
  27. message: `invalid index: ${logIndex}`,
  28. });
  29. }
  30. // Figure out which subset of hashes corresponds to the inner and border
  31. // nodes
  32. const { inner, border } = decompInclProof(logIndex, treeSize);
  33. if (inclusionProof.hashes.length !== inner + border) {
  34. throw new error_1.VerificationError({
  35. code: 'TLOG_INCLUSION_PROOF_ERROR',
  36. message: 'invalid hash count',
  37. });
  38. }
  39. const innerHashes = inclusionProof.hashes.slice(0, inner);
  40. const borderHashes = inclusionProof.hashes.slice(inner);
  41. // The entry's hash is the leaf hash
  42. const leafHash = hashLeaf(entry.canonicalizedBody);
  43. // Chain the hashes belonging to the inner and border portions
  44. const calculatedHash = chainBorderRight(chainInner(leafHash, innerHashes, logIndex), borderHashes);
  45. // Calculated hash should match the root hash in the inclusion proof
  46. if (!core_1.crypto.bufferEqual(calculatedHash, inclusionProof.rootHash)) {
  47. throw new error_1.VerificationError({
  48. code: 'TLOG_INCLUSION_PROOF_ERROR',
  49. message: 'calculated root hash does not match inclusion proof',
  50. });
  51. }
  52. }
  53. // Breaks down inclusion proof for a leaf at the specified index in a tree of
  54. // the specified size. The split point is where paths to the index leaf and
  55. // the (size - 1) leaf diverge. Returns lengths of the bottom and upper proof
  56. // parts.
  57. function decompInclProof(index, size) {
  58. const inner = innerProofSize(index, size);
  59. const border = onesCount(index >> BigInt(inner));
  60. return { inner, border };
  61. }
  62. // Computes a subtree hash for a node on or below the tree's right border.
  63. // Assumes the provided proof hashes are ordered from lower to higher levels
  64. // and seed is the initial hash of the node specified by the index.
  65. function chainInner(seed, hashes, index) {
  66. return hashes.reduce((acc, h, i) => {
  67. if ((index >> BigInt(i)) & BigInt(1)) {
  68. return hashChildren(h, acc);
  69. }
  70. else {
  71. return hashChildren(acc, h);
  72. }
  73. }, seed);
  74. }
  75. // Computes a subtree hash for nodes along the tree's right border.
  76. function chainBorderRight(seed, hashes) {
  77. return hashes.reduce((acc, h) => hashChildren(h, acc), seed);
  78. }
  79. function innerProofSize(index, size) {
  80. return bitLength(index ^ (size - BigInt(1)));
  81. }
  82. // Counts the number of ones in the binary representation of the given number.
  83. // https://en.wikipedia.org/wiki/Hamming_weight
  84. function onesCount(num) {
  85. return num.toString(2).split('1').length - 1;
  86. }
  87. // Returns the number of bits necessary to represent an integer in binary.
  88. function bitLength(n) {
  89. if (n === 0n) {
  90. return 0;
  91. }
  92. return n.toString(2).length;
  93. }
  94. // Hashing logic according to RFC6962.
  95. // https://datatracker.ietf.org/doc/html/rfc6962#section-2
  96. function hashChildren(left, right) {
  97. return core_1.crypto.digest('sha256', RFC6962_NODE_HASH_PREFIX, left, right);
  98. }
  99. function hashLeaf(leaf) {
  100. return core_1.crypto.digest('sha256', RFC6962_LEAF_HASH_PREFIX, leaf);
  101. }