33import org .unicitylabs .sdk .crypto .hash .DataHash ;
44import org .unicitylabs .sdk .crypto .hash .DataHasher ;
55import org .unicitylabs .sdk .crypto .hash .HashAlgorithm ;
6- import org .unicitylabs .sdk .smt .radix .FinalizedBranch ;
7- import org .unicitylabs .sdk .smt .radix .FinalizedLeafBranch ;
86import org .unicitylabs .sdk .smt .SparseMerkleTreePathUtils ;
9- import org .unicitylabs .sdk .smt .radix .FinalizedNodeBranch ;
10- import org .unicitylabs .sdk .util .BitString ;
7+ import org .unicitylabs .sdk .smt .radix .SparseMerkleTreeRootNode ;
118import org .unicitylabs .sdk .util .HexConverter ;
129import org .unicitylabs .sdk .util .LongConverter ;
1310
14- import java .math .BigInteger ;
1511import java .util .ArrayList ;
1612import java .util .Arrays ;
1713import java .util .List ;
@@ -30,35 +26,19 @@ private InclusionCertificate(byte[] bitmap, List<DataHash> siblings) {
3026 this .siblings = siblings ;
3127 }
3228
33- public static InclusionCertificate create (FinalizedNodeBranch root , byte [] key ) {
34- FinalizedBranch node = root ;
29+ public static InclusionCertificate create (SparseMerkleTreeRootNode root , byte [] key ) {
30+ Objects . requireNonNull ( root , "root cannot be null" ) ;
3531
3632 ArrayList <DataHash > siblings = new ArrayList <>();
3733 byte [] bitmap = new byte [InclusionCertificate .BITMAP_SIZE ];
38- BigInteger keyPath = BitString .fromBytesReversedLSB (key ).toBigInteger ();
3934
40- while (node != null ) {
41- if (node instanceof FinalizedLeafBranch ) {
42- FinalizedLeafBranch leaf = (FinalizedLeafBranch ) node ;
43- if (!Arrays .equals (leaf .getKey (), key )) {
44- throw new RuntimeException (String .format ("Leaf not found for key: %s" , HexConverter .encode (key )));
45- }
46-
47- return new InclusionCertificate (bitmap , siblings );
48- }
49-
50- FinalizedNodeBranch nodeBranch = (FinalizedNodeBranch ) node ;
51- boolean isRight = keyPath .testBit (nodeBranch .getDepth ());
52- FinalizedBranch sibling = isRight ? nodeBranch .getLeft () : nodeBranch .getRight ();
53- if (sibling != null ) {
54- bitmap [nodeBranch .getDepth () / 8 ] |= (byte ) (1 << nodeBranch .getDepth () % 8 );
55- siblings .add (sibling .getHash ());
56- }
57-
58- node = isRight ? nodeBranch .getRight () : nodeBranch .getLeft ();
35+ for (SparseMerkleTreeRootNode .Sibling sibling : root .getPath (key )) {
36+ int depth = sibling .getDepth ();
37+ bitmap [depth >> 3 ] |= (byte ) (0x80 >> (depth & 7 ));
38+ siblings .add (sibling .getHash ());
5939 }
6040
61- throw new RuntimeException ( "Could not construct inclusion certificate: Invalid path" );
41+ return new InclusionCertificate ( bitmap , siblings );
6242 }
6343
6444 public static InclusionCertificate decode (byte [] bytes ) {
@@ -114,20 +94,17 @@ public boolean verify(StateId leafKey, DataHash leafValue, DataHash expectedRoot
11494 .update (value )
11595 .digest ();
11696
117- BigInteger keyPath = BitString .fromBytesReversedLSB (key ).toBigInteger ();
118- BigInteger bitmapPath = BitString .fromBytesReversedLSB (this .bitmap ).toBigInteger ();
119-
12097 int position = this .siblings .size ();
12198 for (int depth = InclusionCertificate .MAX_DEPTH ; depth >= 0 ; depth --) {
122- if (! bitmapPath . testBit ( depth )) continue ;
99+ if (SparseMerkleTreePathUtils . getBitAtDepth ( this . bitmap , depth ) == 0 ) continue ;
123100
124101 position -= 1 ;
125102 if (position < 0 ) return false ;
126103
127104 DataHash sibling = this .siblings .get (position );
128105
129106 byte [] left , right ;
130- if (keyPath . testBit ( depth )) {
107+ if (SparseMerkleTreePathUtils . getBitAtDepth ( key , depth ) == 1 ) {
131108 left = sibling .getData ();
132109 right = hash .getData ();
133110 } else {
@@ -138,7 +115,7 @@ public boolean verify(StateId leafKey, DataHash leafValue, DataHash expectedRoot
138115 hash = new DataHasher (HashAlgorithm .SHA256 )
139116 .update (new byte []{0x01 })
140117 .update (LongConverter .encode (depth ))
141- .update (SparseMerkleTreePathUtils .pathToRegion ( keyPath , depth ))
118+ .update (SparseMerkleTreePathUtils .regionFromKey ( key , depth ))
142119 .update (left )
143120 .update (right )
144121 .digest ();
0 commit comments