Example #1
0
def main():
    data = []
    tree = MerkleTree(data, hashfunc)
    for i in ascii_lowercase:
        tree.append(i)

    beautify(tree)

    print("Original verify_leaf_inclusion")
    original_proof = tree.get_proof('a')
    print(tree.verify_leaf_inclusion('a', original_proof))

    print("Proof to lists/ Lists to proof verify_leaf_inclusion")
    root = tree._root
    proof_hashs, proof_types = proof_to_lists(original_proof)
    remade_proof = lists_to_proof(proof_hashs, proof_types)
    print(
        merklelib.verify_leaf_inclusion('a', remade_proof, hashfunc,
                                        utils.to_hex(root.hash)))

    print("Proof to string test")
    string_proof = proof_to_string(original_proof)
    print(type(string_proof) == type("hello"))

    print("Proof to string/ String to proof verify_leaf_inclusion")
    new_proof = string_to_proof(string_proof)
    print(
        merklelib.verify_leaf_inclusion('a', remade_proof, hashfunc,
                                        utils.to_hex(root.hash)))
Example #2
0
def system_summary():
    """
    Summarises the system this function is run on.
    Includes system, cpu, gpu and module details
    :return: A dictionary of system details
    """
    merkletree = MerkleTree()
    system_info = {}
    uname = platform.uname()
    system_info["system"] = {
        "system": uname.system,
        "release": uname.release,
        "machine": uname.machine,
        "processor": uname.processor,
    }
    cpu_freq = psutil.cpu_freq()
    system_info["cpu"] = {
        "cores_phys": psutil.cpu_count(logical=False),
        "cores_logic": psutil.cpu_count(logical=True),
        "max_frequency": cpu_freq.max,
        "min_frequency": cpu_freq.min,
    }
    sys_mem = psutil.virtual_memory()
    system_info["memory"] = {"total": sys_mem.total}
    gpus = GPUtil.getGPUs()
    system_info["gpu"] = {}
    for gpu in gpus:
        system_info["gpu"][gpu.id] = {
            "name": gpu.name,
            "memory": gpu.memoryTotal
        }
    system_info["modules"] = find_loaded_modules()
    merkletree.append(list(system_info.items()))
    system_info["signature"] = merkletree.merkle_root
    return system_info
Example #3
0
def system_summary():
    """
    Summarises the system this function is run on.
    Includes system, cpu, gpu and module details
    :return: A dictionary of system details
    """
    merkletree = MerkleTree()
    system_info = {}
    uname = platform.uname()
    system_info['system'] = {
        'system': uname.system,
        'release': uname.release,
        'machine': uname.machine,
        'processor': uname.processor
    }
    cpu_freq = psutil.cpu_freq()
    system_info['cpu'] = {
        'cores_phys': psutil.cpu_count(logical=False),
        'cores_logic': psutil.cpu_count(logical=True),
        'max_frequency': cpu_freq.max,
        'min_frequency': cpu_freq.min
    }
    sys_mem = psutil.virtual_memory()
    system_info['memory'] = {'total': sys_mem.total}
    gpus = GPUtil.getGPUs()
    system_info['gpu'] = {}
    for gpu in gpus:
        system_info['gpu'][gpu.id] = {
            'name': gpu.name,
            'memory': gpu.memoryTotal
        }
    system_info['modules'] = find_loaded_modules()
    merkletree.append([system_info[item] for item in system_info])
    system_info['signature'] = merkletree.merkle_root
    return system_info
def recompute_command(command, fout):
    """
    Recomputes a given command, tracing the methods logged on the way.
    These methods are loaded in a MerkleTree and returns its root.
    :param command: The executed python command (string)
    :param fout: The name of the file to store the trace
    :return: A merkleroot of the trace file.
    """
    np.random.seed(42)  # Random number control
    random.seed(42)
    sys.stdout = open(fout, 'w')
    tracer = trace.Trace(trace=1,
                         count=0)
    tracer.run(command)
    sys.stdout.close()
    sys.stdout = sys.__stdout__

    log = open(fout, 'r')
    mtree = MerkleTree(log.readlines())
    mtree.append(system_summary()['signature'])
    return mtree.merkle_root
Example #5
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument(
    '-s', '--size',
    help='Initial number of leaves',
    dest='size',
    default=2 ** 8
  )

  parser.add_argument(
    '-a', '--additional',
    help='Number of leaves that we need to append',
    dest='additional',
    default=2 ** 1
  )

  parser.add_argument(
    '-p', '--print',
    help='''
     Beautify the whole tree.
     Recommended to use when the size of the tree is less than 10.
     ''',
    action='store_true',
    dest='printable'
  )

  args = parser.parse_args()
  start, end = int(args.size), int(args.additional)
  count = start + end

  start_t = datetime.now()

  tree = MerkleTree(value for value in range(start))

  print(f'Building: {_get_seconds(start_t)} seconds.')
  start_t = datetime.now()

  # appending
  for v in range(start, start + end):
    tree.append(v)

  print(f'Appending: {_get_seconds(start_t)} seconds.')
  print(f'Tree size: {getsize(tree)}')
  print(f'Number of leaves: {len(tree)}')

  if args.printable:
    beautify(tree)

  start_t = datetime.now()

  total, start_t = 0.0, datetime.now()
  max_t = min_t = None
  for leaf in range(count):
    cycle_t = datetime.now()
    proof = tree.get_proof(leaf)
    if not tree.verify_leaf_inclusion(leaf, proof):
      exit(f'Failed audit proof: {leaf}')
    seconds = _get_seconds(cycle_t)
    if max_t is None:
      max_t = min_t = seconds
    else:
      max_t = max(max_t, seconds)
      min_t = min(min_t, seconds)
    total += seconds

  print(f'Audit proof verification times:')
  _print_times(
    average=(total / float(count)),
    total=_get_seconds(start_t),
    max_t=max_t,
    min_t=min_t
  )

  total, start_t = 0.0, datetime.now()
  max_t = min_t = None
  for limit in range(1, count):
    cycle_t = datetime.now()
    test = MerkleTree([value for value in range(limit)])
    if tree < test:
      exit(f'Failed consistency proof: {limit}')
    seconds = _get_seconds(cycle_t)
    if max_t is None:
      max_t = min_t = seconds
    else:
      max_t = max(max_t, seconds)
      min_t = min(min_t, seconds)
    total += seconds

  print(f'Consitency proof verification times ({count} trees):')
  _print_times(
    average=(total / float(count)),
    total=_get_seconds(start_t),
    max_t=max_t,
    min_t=min_t
  )
def hashfunc(value):
  new_value = value
  return new_value

# --------------- LEAF APPEND / TREE GENERATE TEST -------------------- #
print("\n--------------- LEAF APPEND / TREE GENERATE TEST --------------------")
# This test simulates accumulating nonces one at a time
# Instantiate empty list
data = []

# Instantiate empty Merkle tree for appending
asciiTree = MerkleTree(data, hashfunc)

# Add all ASCII characters to Merkle tree
for i in ascii_lowercase:
  asciiTree.append(i.encode('utf-8'))

# Print MerkleTree
print("\nPrinting Merkle Tree")
beautify(asciiTree)
print("\nPrinting root")
print(asciiTree.merkle_root)
print("\nPrinting leaves")
print(asciiTree.leaves)

# Generate an audit proof the letter a
print("\nGenerating audit proof for a")
proof = asciiTree.get_proof('a')
print("\nProof: ")
print(proof)
    async def get(self):
        """This method handles the GET requests for Tenant verifiers to talk with Provider verifiers 
        """
        rest_params = common.get_restful_params(self.request.uri)

        global tree

        if rest_params is None:
            common.echo_json_response(
                self, 405, "Not Implemented: Use /agents/interface")
            return

        if "agents" in rest_params:
            agent_id = rest_params["agents"]

            if agent_id is not None:
                agent = self.db.get_agent(agent_id)
                if agent != None:
                    response = cloud_verifier_common.process_get_status(agent)
                    common.echo_json_response(self, 200, "Success", response)
                else:
                    #logger.info('GET returning 404 response. agent id: ' + agent_id + ' not found.')
                    common.echo_json_response(self, 404, "agent id not found")
            else:
                # return the available keys in the DB
                json_response = self.db.get_agent_ids()
                common.echo_json_response(self, 200, "Success",
                                          {'uuids': json_response})
                logger.info('GET returning 200 response for agent_id list')
        elif "verifier" in rest_params:

            partial_req = "1"  # don't need a pub key
            agent = self.db.get_agent_ids()
            new_agent = self.db.get_agent(agent[0])

            try:
                await nonce_col.put(rest_params["nonce"])
            except Exception as e:
                print('error: ', e)

            new_agent['quote_col'].append(rest_params["nonce"])

            self.db.update_all_agents('quote_col', new_agent['quote_col'])

            #Simulate busy TPM with async sleep function to allow testing of quote batching functionality
            await asyncio.sleep(10)

            try:
                agent = self.db.get_agent_ids()
                new_agent2 = self.db.get_agent(agent[0])

                tree = MerkleTree([], hashfunc)
                logger.info("Concurrent Nonces: " + str(nonce_col.qsize()))
                for nonce in range(nonce_col.qsize()):
                    temp = nonce_col.get()
                    t = await temp
                    logger.debug(t)
                    tree.append(t)
                    await nonce_col.put(t)
                logger.info("Making Merkle Tree in Provider Verifier")
                logger.info(beautify(tree))
                nonce_proof = tree.get_proof(rest_params['nonce'])

            except Exception as e:
                print('error:  ', e)

            rest_params['nonce'] = tree.merkle_root
            url = "http://%s:%d/quotes/integrity?nonce=%s&mask=%s&vmask=%s&partial=%s" % (
                new_agent['ip'], new_agent['port'], rest_params["nonce"],
                rest_params["mask"], rest_params['vmask'], partial_req)

            res = tornado_requests.request("GET", url, context=None)
            response = await res

            json_response = json.loads(response.body)

            json_response_result = json_response["results"]

            json_response_result['nonce_proof'] = proof_to_string(nonce_proof)

            json_response_result['merkle_head'] = tree.merkle_root

            logger.debug("To string of Nonce Proof",
                         json_response_result['merkle_head'])

            common.echo_json_response(self, 200, "Success",
                                      json_response_result)
        else:
            common.echo_json_response(self, 400, "uri not supported")
            logger.warning('GET returning 400 response. uri not supported: ' +
                           self.request.path)