Esempio n. 1
0
    def verifyProof(self, proof: Proof) -> Record:

        if not Proof.isValid(proof):
            raise ProofVerificationException(
                'Input Proof is not valid for a verification process.')
        leaves = proof.leaves
        hashes = proof.nodes
        depth_bytes = bytes.fromhex(proof.depth)
        depth = [
            int.from_bytes(depth_bytes[i * 2:i * 2 + 2], 'big')
            for i in range(0,
                           len(depth_bytes) // 2)
        ]
        bitmap = [b for b in bytes.fromhex(proof.bitmap)]

        it_leaves = 0
        it_hashes = 0
        stack = []

        while it_hashes < len(hashes) or it_leaves < len(leaves):
            act_depth = depth[it_hashes + it_leaves]

            if (bitmap[int((it_hashes + it_leaves) / 8)] &
                (1 << (7 - ((it_hashes + it_leaves) % 8)))) > 0:
                act_hash = hashes[it_hashes]
                it_hashes += 1
            else:
                act_hash = leaves[it_leaves]
                it_leaves += 1

            while len(stack) > 0 and stack[len(stack) - 1][1] == act_depth:
                last_hash = stack.pop()
                act_hash = (Record.fromHex(last_hash[0] + act_hash).getHash())
                act_depth -= 1

            stack.append((act_hash, act_depth))
        return Record.fromHash(stack[0][0])
Esempio n. 2
0
 def test_from_hex_contents_not_string_error(self):
     with self.assertRaises(TypeError):
         Record.fromHex(23)
Esempio n. 3
0
 def test_from_hex_contents_not_hex_error(self):
     s = '101010101010101010101010101010101010101010101010101010101010101g'
     with self.assertRaises(ValueError):
         Record.fromHex(s)
Esempio n. 4
0
 def test_from_hex(self):
     s = '10101010101010101010101010101010101010101010101010101010101010101111111111111111111111111111111111111111111111111111111111111111'
     p = 'e016214a5c4abb88b8b614a916b1a6f075dfcf6fbc16c1e9d6e8ebcec81994a5'
     self.assertEqual(
         Record.fromHex(s)._Record__hash, p, "Records do not match.")