Example #1
0
 def compute_merkle_root(self, modifications: Collection[Tuple[int, int]]):
     """
     Applies the given modifications (a list of (leaf index, value)) to the tree and returns
     the Merkle root.
     """
     default_node = self.default_leaf
     indices = set()
     leaves_offset = 2**self.tree_height
     for index, value in modifications:
         node_index = leaves_offset + index
         self.node_values[node_index] = value
         indices.add(node_index // 2)
     for _ in range(self.tree_height):
         new_indices = set()
         while len(indices) > 0:
             index = indices.pop()
             left = self.node_values.get(2 * index, default_node)
             right = self.node_values.get(2 * index + 1, default_node)
             self.node_values[index] = node_hash = pedersen_hash(
                 left, right)
             self.preimage[node_hash] = (left, right)
             new_indices.add(index // 2)
         default_node = pedersen_hash(default_node, default_node)
         indices = new_indices
     assert indices == {0}
     return self.node_values[1]
Example #2
0
def get_merkle_root(accounts: Dict[int, Balance]) -> int:
    """
    Returns the merkle root given accounts state.

    accounts: the state of the accounts (the merkle tree leaves).
    """
    tree = MerkleTree(tree_height=10, default_leaf=0)
    return tree.compute_merkle_root([
        (i, pedersen_hash(pedersen_hash(a.pub_key, a.balance.a), a.balance.b))
        for i, a in accounts.items()
    ])
Example #3
0
def hash_deed(deed):
  return  pedersen_hash(
            pedersen_hash(
              pedersen_hash(
                pedersen_hash(
                  pedersen_hash(
                    DEED,
                    deed.owner),
                  deed.manager),
                deed.spawner),
              deed.voter),
            deed.transfer)
Example #4
0
def hash_point(point):
  return  pedersen_hash(
            pedersen_hash(
              pedersen_hash(
                pedersen_hash(
                  pedersen_hash(
                    POINT,
                    point.encryption_key),
                  point.authentication_key),
                point.life),
              point.rift),
            point.sponsor)
Example #5
0
def hash_tree(tree):
  if type(tree).__name__ == 'point':
    return hash_point(tree)
  if type(tree).__name__ == 'deed':
    return hash_deed(tree)
  if type(tree).__name__ == 'tree':
    return pedersen_hash(hash_tree(tree.left),hash_tree(tree.rite))
  raise "bad tree"
Example #6
0
def sign_rights_tx(sk,rights,ship,deed):
  message = \
    pedersen_hash(
      pedersen_hash(
        pedersen_hash(
          RIGHTS,
          ship),
        hash_deed(get_value(rights,ship))),
      hash_deed(deed))
  sig = signature.sign(message,sk)
  print(json.dumps({
      "function": RIGHTS,
      "ship": ship,
      "owner": deed.owner,
      "manager": deed.manager,
      "spawner": deed.spawner,
      "voter": deed.voter,
      "transfer": deed.transfer,
      "sig_r": sig[0],
      "sig_s": sig[1]},
    indent=2))
Example #7
0
def sign_points_tx(sk,points,ship,point):
  message = \
    pedersen_hash(
      pedersen_hash(
        pedersen_hash(
          POINTS,
          ship),
        hash_point(get_value(points,ship))),
      hash_point(point))
  sig = signature.sign(message,sk)
  print(json.dumps({
      "function": POINTS,
      "ship": ship,
      "encryption_key": point.encryption_key,
      "authentication_key": point.authentication_key,
      "life": point.life,
      "rift": point.rift,
      "sponsor": point.sponsor,
      "sig_r": sig[0],
      "sig_s": sig[1]},
    indent=2))
Example #8
0
def get_storage_var_address(var_name: str, *args) -> int:
    """
    Returns the storage address of a StarkNet storage variable given its name and arguments.
    """
    res = starknet_keccak(var_name.encode('utf8'))

    for arg in args:
        assert isinstance(
            arg, int), f'Expected arguments to be integers. Found: {arg}.'
        res = pedersen_hash(res, arg)

    return res % ADDR_BOUND
    def __init__(self, tree_height: int, default_leaf: int):
        self.tree_height = tree_height
        self.default_leaf = default_leaf

        # Compute the root of an empty tree.
        empty_tree_root = default_leaf
        for _ in range(tree_height):
            empty_tree_root = pedersen_hash(empty_tree_root, empty_tree_root)

        # A map from node indices to their values.
        self.node_values: Dict[int, int] = {1: empty_tree_root}
        # A map from node hash to its two children.
        self.preimage: Dict[int, Tuple[int, int]] = {}
Example #10
0
def hash_account(pub_key, balances):
    res = int(pub_key, 16)
    for balance in balances:
        res = pedersen_hash(res, balance)
    return res
Example #11
0
def test_compute_hash_chain():
    data = [1, 2, 3]
    res = compute_hash_chain(data)
    assert res == pedersen_hash(1, pedersen_hash(2, 3))
Example #12
0
def main():
    data = [1, 2, 3]
    res = compute_hash_chain(data)
    assert res == pedersen_hash(1, pedersen_hash(2, 3))
    print(f'result of hashchain {data}: {res}')