Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/InclusionCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HashAlgorithm } from '../crypto/hash/HashAlgorithm.js';
import { FinalizedBranch } from '../smt/radix/FinalizedBranch.js';
import { FinalizedLeafBranch } from '../smt/radix/FinalizedLeafBranch.js';
import { SparseMerkleTreeRootNode } from '../smt/radix/SparseMerkleTreeRootNode.js';
import { pathToRegion } from '../smt/SparseMerkleTreePathUtils.js';
import { BitString } from '../util/BitString.js';
import { HexConverter } from '../util/HexConverter.js';
import { dedent } from '../util/StringUtils.js';
Expand Down Expand Up @@ -145,6 +146,7 @@ export class InclusionCertificate {

hash = await new DataHasher(HashAlgorithm.SHA256)
.update(new Uint8Array([0x01, depth]))
.update(pathToRegion(keyPath, depth))
.update(left)
.update(right)
.digest();
Expand Down
1 change: 0 additions & 1 deletion src/predicate/builtin/BuiltInPredicateType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
export enum BuiltInPredicateType {
Signature = 0x01,
Burn = 0x02,
UnicityId = 0x100,
}
85 changes: 0 additions & 85 deletions src/predicate/builtin/UnicityIdPredicate.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/predicate/builtin/UnicityIdPredicateUnlockScript.ts

This file was deleted.

76 changes: 0 additions & 76 deletions src/predicate/builtin/verification/UnicityIdPredicateVerifier.ts

This file was deleted.

27 changes: 24 additions & 3 deletions src/smt/SparseMerkleTreePathUtils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
type CommonPath = { length: bigint; path: bigint };
type CommonPath = { length: number; path: bigint };

export function calculateCommonPath(path1: bigint, path2: bigint): CommonPath {
let path = 1n;
let mask = 1n;
let length = 0n;
let length = 0;

while ((path1 & mask) === (path2 & mask) && path < path1 && path < path2) {
mask <<= 1n;
length += 1n;
length += 1;
path = mask | ((mask - 1n) & path1);
}

return { length, path };
}

/**
* Region committed by an interior node: the `depth`-bit common prefix of all leaves in the node's
* sub-tree. The `i`th lowest bit of path will be the `i mod 8`th lowest bit in the `i div 8`th
* byte of the returned 32-byte array (so the packing is little-endian); the remaining bits of the array
* are set to zero.
*/
export function pathToRegion(path: bigint, depth: number): Uint8Array {
const region = new Uint8Array(32);
const fullBytes = Math.floor(depth / 8);
const remainderBits = depth % 8;

let bits = path;
for (let j = 0; j < fullBytes; j++, bits >>= 8n) {
region[j] = Number(bits & 0xffn);
}
if (remainderBits > 0) {
region[fullBytes] = Number(bits & 0xffn) & ((1 << remainderBits) - 1);
}
return region;
}
Comment thread
martti007 marked this conversation as resolved.

export function getBitAtDepth(data: Uint8Array, depth: number): number {
depth = Number(depth);
const byteIndex = Math.floor(depth / 8);
Expand Down
2 changes: 2 additions & 0 deletions src/smt/radix/FinalizedNodeBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DataHash } from '../../crypto/hash/DataHash.js';
import { IDataHasher } from '../../crypto/hash/IDataHasher.js';
import { IDataHasherFactory } from '../../crypto/hash/IDataHasherFactory.js';
import { dedent } from '../../util/StringUtils.js';
import { pathToRegion } from '../SparseMerkleTreePathUtils.js';

/**
* Finalized interior node in a radix sparse Merkle tree.
Expand Down Expand Up @@ -32,6 +33,7 @@ export class FinalizedNodeBranch {
const hash = await factory
.create()
.update(new Uint8Array([0x01, node.depth]))
.update(pathToRegion(node.path, node.depth))
.update(left.hash.data)
.update(right.hash.data)
.digest();
Expand Down
Loading
Loading