Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@

class SparseMerkleSumTreeTest {

@Test
void shouldAgreeWithSpecExamples() throws Exception {
var treeLeftOnly = new SparseMerkleSumTree(HashAlgorithm.SHA256);
treeLeftOnly.addLeaf(new BigInteger("100", 2), new LeafValue("a".getBytes(), BigInteger.valueOf(1)));
var rootLeftOnly = treeLeftOnly.calculateRoot();
Assertions.assertEquals(BigInteger.valueOf(1), rootLeftOnly.getValue());
Assertions.assertEquals('"' + "0000" + "34e0cf342d70c0d10e3ba481f72db532ecfd723afa3c25812a4bef61b5198d0b" + '"', rootLeftOnly.getRootHash().toJson());

var treeRightOnly = new SparseMerkleSumTree(HashAlgorithm.SHA256);
treeRightOnly.addLeaf(new BigInteger("111", 2), new LeafValue("b".getBytes(), BigInteger.valueOf(2)));
var rootRightOnly = treeRightOnly.calculateRoot();
Assertions.assertEquals(BigInteger.valueOf(2), rootRightOnly.getValue());
Assertions.assertEquals('"' + "0000" + "da47d1cda8dab5159b2bed1ea27c3d24ed990989fac3c62ace05273fea51f958" + '"', rootRightOnly.getRootHash().toJson());

var treeFourLeaves = new SparseMerkleSumTree(HashAlgorithm.SHA256);
treeFourLeaves.addLeaf(new BigInteger("1000", 2), new LeafValue("a".getBytes(), BigInteger.valueOf(1)));
treeFourLeaves.addLeaf(new BigInteger("1100", 2), new LeafValue("b".getBytes(), BigInteger.valueOf(2)));
treeFourLeaves.addLeaf(new BigInteger("1011", 2), new LeafValue("c".getBytes(), BigInteger.valueOf(3)));
treeFourLeaves.addLeaf(new BigInteger("1111", 2), new LeafValue("d".getBytes(), BigInteger.valueOf(4)));
var rootFourLeaves = treeFourLeaves.calculateRoot();
Assertions.assertEquals(BigInteger.valueOf(10), rootFourLeaves.getValue());
Assertions.assertEquals('"' + "0000" + "adfefa7c86b18d1216eece9fe0ce82ca58fd8cf482305c3c4e1a0a1361dc9d15" + '"', rootFourLeaves.getRootHash().toJson());
}

@Test
void shouldBuildTreeWithNumericValues() throws Exception {
var leaves = Map.of(
Expand Down
Loading