@Test void testMerkleize() { final List<Bytes32> input = Arrays.asList( Bytes32.fromHexString( "0xA99A76ED7796F7BE22D5B7E85DEEB7C5677E88E511E0B337618F8C4EB61349B4"), Bytes32.fromHexString( "0xBF2D153F649F7B53359FE8B94A38E44C00000000000000000000000000000000")); final Bytes expected = Bytes.fromHexString("0x89E40BFF069E391CA393901DA3287BBE35DC429265927ABE3C3F06BEC8E0B9CD"); assertEquals(expected, HashTreeUtil.merkleize(input)); }
public static Bytes32 merkleize(List<Bytes32> chunks, long limit) { int count = chunks.size(); if (limit == 0) return zerohashes.get(0); int depth = Long.SIZE - Long.numberOfLeadingZeros(max(count - 1, 0)); int max_depth = Long.SIZE - Long.numberOfLeadingZeros(limit - 1); List<Bytes32> tmp = new ArrayList<>(); IntStream.range(0, max_depth + 1).boxed().forEach(i -> tmp.add(null)); IntStream.range(0, count).forEach(i -> merge(chunks.get(i), i, tmp, count, depth)); if ((1 << depth) != count) { merge(zerohashes.get(0), count, tmp, count, depth); } IntStream.range(depth, max_depth) .forEach( j -> tmp.set(j + 1, Hash.sha2_256(Bytes.concatenate(tmp.get(j), zerohashes.get(j))))); return tmp.get(max_depth); }
HashTreeUtil { public static Bytes32 merkleize(List<Bytes32> chunks, long limit) { int count = chunks.size(); if (limit == 0) return zerohashes.get(0); int depth = Long.SIZE - Long.numberOfLeadingZeros(max(count - 1, 0)); int max_depth = Long.SIZE - Long.numberOfLeadingZeros(limit - 1); List<Bytes32> tmp = new ArrayList<>(); IntStream.range(0, max_depth + 1).boxed().forEach(i -> tmp.add(null)); IntStream.range(0, count).forEach(i -> merge(chunks.get(i), i, tmp, count, depth)); if ((1 << depth) != count) { merge(zerohashes.get(0), count, tmp, count, depth); } IntStream.range(depth, max_depth) .forEach( j -> tmp.set(j + 1, Hash.sha2_256(Bytes.concatenate(tmp.get(j), zerohashes.get(j))))); return tmp.get(max_depth); } }
HashTreeUtil { public static Bytes32 merkleize(List<Bytes32> chunks, long limit) { int count = chunks.size(); if (limit == 0) return zerohashes.get(0); int depth = Long.SIZE - Long.numberOfLeadingZeros(max(count - 1, 0)); int max_depth = Long.SIZE - Long.numberOfLeadingZeros(limit - 1); List<Bytes32> tmp = new ArrayList<>(); IntStream.range(0, max_depth + 1).boxed().forEach(i -> tmp.add(null)); IntStream.range(0, count).forEach(i -> merge(chunks.get(i), i, tmp, count, depth)); if ((1 << depth) != count) { merge(zerohashes.get(0), count, tmp, count, depth); } IntStream.range(depth, max_depth) .forEach( j -> tmp.set(j + 1, Hash.sha2_256(Bytes.concatenate(tmp.get(j), zerohashes.get(j))))); return tmp.get(max_depth); }  }
HashTreeUtil { public static Bytes32 merkleize(List<Bytes32> chunks, long limit) { int count = chunks.size(); if (limit == 0) return zerohashes.get(0); int depth = Long.SIZE - Long.numberOfLeadingZeros(max(count - 1, 0)); int max_depth = Long.SIZE - Long.numberOfLeadingZeros(limit - 1); List<Bytes32> tmp = new ArrayList<>(); IntStream.range(0, max_depth + 1).boxed().forEach(i -> tmp.add(null)); IntStream.range(0, count).forEach(i -> merge(chunks.get(i), i, tmp, count, depth)); if ((1 << depth) != count) { merge(zerohashes.get(0), count, tmp, count, depth); } IntStream.range(depth, max_depth) .forEach( j -> tmp.set(j + 1, Hash.sha2_256(Bytes.concatenate(tmp.get(j), zerohashes.get(j))))); return tmp.get(max_depth); }  static Bytes32 hash_tree_root(SSZTypes sszType, Bytes... bytes); @SuppressWarnings({"rawtypes", "unchecked"}) static Bytes32 hash_tree_root(SSZTypes sszType, SSZImmutableCollection bytes); static Bytes32 hash_tree_root_list_ul(SSZList<Bytes> bytes); static Bytes32 hash_tree_root_vector_uint64(SSZVector<UInt64> vector); @SuppressWarnings({"rawtypes", "unchecked"}) static Bytes32 hash_tree_root(SSZTypes sszType, SSZList bytes); static Bytes32 merkleize1(List<Bytes32> sszChunks, long limit); static Bytes32 merkleize(List<Bytes32> chunks, long limit); static Bytes32 merkleize(List<Bytes32> sszChunks); static Bytes32 hash_tree_root_bitlist(Bitlist bitlist); static Bytes32 hash_tree_root_bitvector(Bitvector bitvector); static Bytes32 hash_tree_root_list_bytes(SSZList<Bytes32> bytes); }
HashTreeUtil { public static Bytes32 merkleize(List<Bytes32> chunks, long limit) { int count = chunks.size(); if (limit == 0) return zerohashes.get(0); int depth = Long.SIZE - Long.numberOfLeadingZeros(max(count - 1, 0)); int max_depth = Long.SIZE - Long.numberOfLeadingZeros(limit - 1); List<Bytes32> tmp = new ArrayList<>(); IntStream.range(0, max_depth + 1).boxed().forEach(i -> tmp.add(null)); IntStream.range(0, count).forEach(i -> merge(chunks.get(i), i, tmp, count, depth)); if ((1 << depth) != count) { merge(zerohashes.get(0), count, tmp, count, depth); } IntStream.range(depth, max_depth) .forEach( j -> tmp.set(j + 1, Hash.sha2_256(Bytes.concatenate(tmp.get(j), zerohashes.get(j))))); return tmp.get(max_depth); }  static Bytes32 hash_tree_root(SSZTypes sszType, Bytes... bytes); @SuppressWarnings({"rawtypes", "unchecked"}) static Bytes32 hash_tree_root(SSZTypes sszType, SSZImmutableCollection bytes); static Bytes32 hash_tree_root_list_ul(SSZList<Bytes> bytes); static Bytes32 hash_tree_root_vector_uint64(SSZVector<UInt64> vector); @SuppressWarnings({"rawtypes", "unchecked"}) static Bytes32 hash_tree_root(SSZTypes sszType, SSZList bytes); static Bytes32 merkleize1(List<Bytes32> sszChunks, long limit); static Bytes32 merkleize(List<Bytes32> chunks, long limit); static Bytes32 merkleize(List<Bytes32> sszChunks); static Bytes32 hash_tree_root_bitlist(Bitlist bitlist); static Bytes32 hash_tree_root_bitvector(Bitvector bitvector); static Bytes32 hash_tree_root_list_bytes(SSZList<Bytes32> bytes); static final int BYTES_PER_CHUNK; }