コード例 #1
0
        public async Task VerifyInclusion_Generated()
        {
            var leafHashes = new List <byte[]>();
            var tree       = CreateMerkleTree();

            for (ulong i = 0; i < 128; i++)
            {
                var leaf = new byte[32];
                Array.Fill(leaf, (byte)i);
                await tree.AppendAsync(leaf);

                leafHashes.Add(_hasher.HashLeaf(leaf));
            }

            var verifier = new MerkleTreeVerifier(_hasher, new TestLogger <MerkleTreeVerifier>());

            for (ulong i = 1; i < 16; i++)
            {
                var rootHash = await tree.GetRootHashAsync(i);

                var treeHead = new SignedTreeHead(rootHash, new byte[0], i, "");
                for (ulong j = 0; j < i; j++)
                {
                    var auditPath = await tree.GetInclusionProofAsync(j, i);

                    var result = verifier.VerifyInclusion(leafHashes[(int)j], j, auditPath.ToArray(), treeHead);

                    Assert.Equal(IntegrityStatus.Success, result.Status);
                }
            }
        }
コード例 #2
0
        public async Task VerifyInclusion_Precomputed(ulong leaf, ulong treeSizeSnapshot, ulong pathLength, string[] path)
        {
            var tree = await CreateMerkleTreeAsync(TestVector);

            var auditPath = await tree.GetInclusionProofAsync(leaf, treeSizeSnapshot);

            Assert.Equal(auditPath.Count, (int)pathLength);
            Assert.Equal(auditPath.Select(p => p.ByteToHex()).ToArray(), path);

            var leafData = TestVector[(int)leaf];
            var leafHash = _hasher.HashLeaf(leafData.HexToByteArray());
            var hash     = await tree.GetRootHashAsync(treeSizeSnapshot);

            var treeHead = new SignedTreeHead(hash, new byte[0], treeSizeSnapshot, "");

            var verifier = new MerkleTreeVerifier(_hasher, new TestLogger <MerkleTreeVerifier>());

            if (treeSizeSnapshot > 0)
            {
                verifier.VerifyInclusion(leafHash, leaf, auditPath.ToArray(), treeHead);
            }
        }