def test_view_label_retrieval(state): reader_params = LocalParams.generate() _, chain, tree = commit_claims( state, [("marios", "test1"), ("bogdan", "test2")], [(reader_params.dh.pk, ["marios", "bogdan"])]) own_view = View(chain) assert own_view["marios"] == b"test1" assert own_view["bogdan"] == b"test2" with reader_params.as_default(): view = View(chain) assert view["marios"] == b"test1" assert view["bogdan"] == b"test2"
def test_view_missing_and_non_existent_label(state): reader_params = LocalParams.generate() _, chain, tree = commit_claims(state, [("marios", "test1"), ("bogdan", "test2")], [(reader_params.dh.pk, ["marios"])]) with reader_params.as_default(): view = View(chain) with pytest.raises(KeyError): view["george"] with pytest.raises(KeyError): view["bogdan"]
def test_evidence(state): reader_params = LocalParams.generate() reader_pk = reader_params.dh.pk state.grant_access(reader_pk, ["marios"]) nonce, chain, tree = commit_claims(state, [("marios", "test")]) evidence = state.compute_evidence_keys(reader_pk, "marios") assert len(evidence) > 0 evidence_store = ObjectStore({k: tree.store[k] for k in evidence}) verification_tree = Tree(object_store=evidence_store, root_hash=tree.root()) with reader_params.as_default(): view = View(source_chain=chain, source_tree=verification_tree) assert view["marios"] == b"test"
def test_e2e_timings(): friends_graph, all_data = generate_test_data() (labels, heads, pubkeys, privkeys) = all_data with LocalParams.generate().as_default() as params: nonce = os.urandom(PublicParams.get_default().nonce_size) # Encode claims t0 = time.time() c0 = 0 enc_claims = [] vrfs = [] for claim_label, claim_body in zip(labels, heads): c0 += 1 c0 += 1 enc = encode_claim(nonce, claim_label, claim_body) vrf_value, lookup_key, encrypted_claim = enc enc_claims += [(lookup_key, encrypted_claim)] vrfs += [vrf_value] t1 = time.time() print("\n\t\tTiming per encoded claim: %1.1f ms" % ((t1 - t0) / c0 * 1000)) # Encode capabilities t0 = time.time() c0 = 0 capabilities = [] cap_index = {} for friend in friends_graph: friend_dh_pk = pubkeys[friend] for fof in friends_graph[friend]: c0 += 1 claim_label = labels[fof] vrf_value = vrfs[fof] cap_lookup_key, encrypted_cap = encode_capability( friend_dh_pk, nonce, claim_label, vrf_value) capabilities += [(cap_lookup_key, encrypted_cap)] cap_index[(friend, fof)] = (cap_lookup_key, encrypted_cap) t1 = time.time() print("\t\tTiming per encoded capab: %1.1f ms" % ((t1 - t0) / c0 * 1000)) data = encode([enc_claims, capabilities]) print("\t\tData length: %1.1f kb" % (len(data) / 1024.0)) # Build our non-equivocable tree t0 = time.time() tree = Tree() for lookup_key, enc_item in enc_claims + capabilities: tree.add(key=lookup_key, item=enc_item) _, evidence = tree.evidence(key=lookup_key) assert tree.is_in(enc_item, key=lookup_key) enc_item_hash = evidence[-1].item tree.store[enc_item_hash] = enc_item t1 = time.time() print("\t\tTiming for building non-equiv. tree: %1.1f ms" % ((t1 - t0) * 1000)) # Build a chain and a block t0 = time.time() c0 = 200 for _ in range(c0): chain = Chain(tree.store) payload = Payload.build(tree, nonce).export() def sign_block(block): sig = sign(block.hash()) block.aux = pet2ascii(sig) chain.multi_add([payload], pre_commit_fn=sign_block) # Pack block block = chain.store[chain.head] packed_block = packb( ("S", block.index, block.fingers, block.items, block.aux)) t1 = time.time() print("\t\tTiming for building a block: %1.1f ms" % ((t1 - t0) / c0 * 1000)) print("\t\tPacked block size: %d bytes" % (len(packed_block))) t0 = time.time() c0 = 0 # Pick a random reader for reader in friends_graph: reader_params = LocalParams( dh=Keypair(sk=privkeys[reader], pk=pubkeys[reader])) for reader_friend in friends_graph[reader]: claim_label = labels[reader_friend] with reader_params.as_default(): view = View(chain) c0 += 1 head = view[labels[reader_friend]] assert head == heads[reader_friend] t1 = time.time() print("\t\tTiming for retrieving a claim by label: %1.1f ms" % ((t1 - t0) / c0 * 1000)) # Pick a target proof to produce f1 = random.choice(list(friends_graph.keys())) f2 = random.choice(list(friends_graph[f1])) (cap_lookup_key, encrypted_cap) = cap_index[(f1, f2)] (claim_lookup_key, encrypted_claim) = enc_claims[f2] root, e1 = tree.evidence(key=cap_lookup_key) _, e2 = tree.evidence(key=claim_lookup_key) evidence_store = {e.identity(): e for e in e1 + e2} t2 = Tree(evidence_store, root) assert t2.is_in(key=cap_lookup_key, item=encrypted_cap) assert t2.is_in(key=claim_lookup_key, item=encrypted_claim) # Serialize evidence: evidence = [] for e in e1 + e2: if isinstance(e, Leaf): evidence += [(e.key, e.item, tree.store[e.item])] elif isinstance(e, Branch): evidence += [(e.pivot, e.left_branch, e.right_branch)] else: pass import zlib bin_evidence = encode(evidence) bin_evidence_compressed = zlib.compress(bin_evidence, 9) print("\t\tSize for one proof: %s bytes (compressed %s bytes)" % (len(bin_evidence), len(bin_evidence_compressed))) print("\t\tPayload:") pprint(payload)