def test_traversal(): from bdstraversal import traverse, H, AUTH, keygen_and_setup correct_root = recursive_hash(H) keygen_and_setup() assert compute_root(H, 0, AUTH) == correct_root for s in range(2**H - 1): assert compute_root(H, s + 1, traverse(s)) == correct_root
def test_state_traversal(): from bdstraversal_mt_c_like import BDSState, H correct_root = recursive_hash(H) state = BDSState() state.keygen_and_setup() assert compute_root(H, 0, state.auth) == correct_root for s in range(2**H - 1): auth = state.traverse_and_update(s) assert compute_root(H, s + 1, auth) == correct_root
def test_mt_state_traversal(): from bdstraversal_mt_c_like import MTBDSState, H, D correct_root = recursive_hash(H) states = MTBDSState() states.keygen_and_setup() for s in range(2**(D * H)): authpaths = states.authpaths() for i, path in enumerate(authpaths): idx = (s >> (H * i)) & ((1 << H) - 1) assert compute_root(H, idx, path) == correct_root if s + 1 < 2**(D * H): states.traverse(s)
startidx = s + 1 + 3 * 2**h if startidx < 2**H: TREEHASH[h].__init__(h, startidx) for _ in range((H - K) // 2): l_min = float('inf') h = None for j in range(H - K): if TREEHASH[j].completed: low = float('inf') elif TREEHASH[j].stackusage == 0: low = j else: low = TREEHASH[j].height() if low < l_min: h = j l_min = low if h is not None: TREEHASH[h].update() return AUTH if __name__ == "__main__": correct_root = recursive_hash(H) keygen_and_setup() print('leaf 0: {}'.format(compute_root(H, 0, AUTH) == correct_root)) for s in range(2**H - 1): root = compute_root(H, s + 1, traverse(s)) print('leaf {}: {}'.format(s + 1, root == correct_root))
startidx = s + 1 + 3 * 2**h if startidx < 2 ** H: TREEHASH[h].__init__(h, startidx) for _ in range((H - K) // 2): l_min = float('inf') h = None for j in range(H - K): if TREEHASH[j].completed: low = float('inf') elif TREEHASH[j].stackusage == 0: low = j else: low = TREEHASH[j].height() if low < l_min: h = j l_min = low if h is not None: TREEHASH[h].update() return AUTH if __name__ == "__main__": correct_root = recursive_hash(H) keygen_and_setup() print('leaf 0: {}'.format(compute_root(H, 0, AUTH) == correct_root)) for s in range(2 ** H - 1): root = compute_root(H, s + 1, traverse(s)) print('leaf {}: {}'.format(s + 1, root == correct_root))
"""Returns the auth nodes by updating the most needed stacks first.""" authpath = copy.copy(AUTH) refresh_auth_nodes(s) # build stacks for _ in range(2*H - 1): l_min = float('inf') focus = None for h in range(H): if TREEHASH[h].completed: low = float('inf') elif len(TREEHASH[h].stack) == 0: low = h else: low = TREEHASH[h].low() if low < l_min: focus = h l_min = low if focus is not None: TREEHASH[focus].update() return authpath if __name__ == "__main__": correct_root = recursive_hash(H) keygen_and_setup() for s in range(2 ** H): root = compute_root(H, s, traverse(s)) print('iteration {}: {}'.format(s, root == correct_root))
"""Returns the auth nodes by updating the most needed stacks first.""" authpath = copy.copy(AUTH) refresh_auth_nodes(s) # build stacks for _ in range(2 * H - 1): l_min = float('inf') focus = None for h in range(H): if TREEHASH[h].completed: low = float('inf') elif len(TREEHASH[h].stack) == 0: low = h else: low = TREEHASH[h].low() if low < l_min: focus = h l_min = low if focus is not None: TREEHASH[focus].update() return authpath if __name__ == "__main__": correct_root = recursive_hash(H) keygen_and_setup() for s in range(2**H): root = compute_root(H, s, traverse(s)) print('iteration {}: {}'.format(s, root == correct_root))