def verify_opening(self, i): # returns True for pass, False for fail node_number = self.prover.merkle_tree_size - i self.prover.p.pebble_value.seek(i * self.prover.p.hash_length) hash_of_node = self.prover.p.pebble_value.read( self.prover.p.hash_length) row = self.prover.merkle_tree_rows while (row > 1): if node_number % 2 == 0: # Node has a sibling to the right self.prover.p.pebble_value.seek( (self.prover.merkle_tree_size - (node_number - 1)) * self.prover.p.hash_length) sibling = self.prover.p.pebble_value.read( self.prover.p.hash_length) node_number = node_number / 2 - 1 hash_of_node = utils.secure_hash(hash_of_node + sibling) else: # Node has a sibling to the left. self.prover.p.pebble_value.seek( (self.prover.merkle_tree_size - (node_number + 1)) * self.prover.p.hash_length) sibling = self.prover.p.pebble_value.read( self.prover.p.hash_length) node_number = node_number / 2 hash_of_node = utils.secure_hash(sibling + hash_of_node) row = row - 1 if hash_of_node == self.prover.merkle_root: return True else: return False
def add_pebble(self, v): if v is None: return if not self.is_pebbled(v): if self.is_source(v): self.pebble_value[str(v)] = utils.secure_hash(str(v)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: prehash = "" error = 0 for i in range(7): if self.B[str(v)][i] != None: if self.pebble_value[str(self.B[str(v)][i])] == None: print "Error: Attempted to pebble node " + str( v) + "without pebbling parent " + str(i) + "." error = 1 else: prehash = prehash + str(self.pebble_value[str( self.B[str(v)][i])]) if error == 0: self.pebble_value[str(v)] = utils.secure_hash(str(v)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: print "Error: Attempted to pebble node " + str( v) + " but it has already been pebbled"
def add_pebble(self, v): if v is None: return if not self.is_pebbled(v): if self.is_source(v): self.pebble_value[str(v)] = utils.secure_hash(str(v)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: prehash = "" error = 0 for i in range(7): if self.B[str(v)][i] != None: if self.pebble_value[str(self.B[str(v)][i])] == None: print "Error: Attempted to pebble node " + str(v) + "without pebbling parent " + str(i) + "." error = 1 else: prehash = prehash + str(self.pebble_value[str(self.B[str(v)][i])]) if error == 0: self.pebble_value[str(v)] = utils.secure_hash(str(v)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: print "Error: Attempted to pebble node " + str(v) + " but it has already been pebbled"
def merkle_merkle_roots(self): power_of_two = 1 #intitially set to the smallest power of two greater than or equal to k. while (power_of_two < self.k): power_of_two *= 2 power_of_two /= 2 for i in range(power_of_two): if (2*i + 2 <= self.k): self.merkle_tree.seek(2*i*self.hash_length) prehash = self.merkle_tree.read(2*self.hash_length) self.merkle_tree.seek(i*self.hash_length) self.merkle_tree.write(utils.secure_hash(prehash)) elif (2*i + 1 == self.k): self.merkle_tree.seek(2*i*self.hash_length) prehash = self.merkle_tree.read(self.hash_length) + '\00' * self.hash_length self.merkle_tree.seek(i*self.hash_length) self.merkle_tree.write(utils.secure_hash(prehash)) else: prehash = '\00' * self.hash_length self.merkle_tree.seek(i*self.hash_length) self.merkle_tree.write(utils.secure_hash(prehash)) power_of_two /= 2 while (power_of_two >= 1): for i in range(power_of_two): self.merkle_tree.seek(2*i*self.hash_length) prehash = self.merkle_tree.read(2*self.hash_length) self.merkle_tree.seek(i*self.hash_length) self.merkle_tree.write(utils.secure_hash(prehash)) power_of_two /= 2 self.merkle_tree.seek(0) return self.merkle_tree.read(self.hash_length)
def merkle_pebble_value(self, fill): power_of_two = 1 # first we set it to smallest power of two greater than or equal to n while (power_of_two < self.n): power_of_two *= 2 for i in range(power_of_two): if (2*i + 2 <= self.n): self.pebble_value.seek(2*i*self.hash_length + self.hash_length * self.opp(fill)*self.n) prehash = self.pebble_value.read(2 * self.hash_length) self.pebble_value.seek(i*self.hash_length + self.hash_length * fill * self.n) self.pebble_value.write(utils.secure_hash(prehash)) elif (2*i + 1 == self.n): self.pebble_value.seek(2*i*self.hash_length + self.hash_length*self.opp(fill)*self.n) prehash = self.pebble_value.read(self.hash_length) + '\00' * self.hash_length self.pebble_value.seek(i*self.hash_length + self.hash_length * fill * self.n) self.pebble_value.write(utils.secure_hash(prehash)) else: prehash = '\00' * 2 * self.hash_length self.pebble_value.seek(i*self.hash_length + self.hash_length * fill * self.n) self.pebble_value.write(utils.secure_hash(prehash)) power_of_two /= 2 while (power_of_two >= 1): for i in range(power_of_two): self.pebble_value.seek(2*i*self.hash_length + self.hash_length * fill * self.n) prehash = self.pebble_value.read(2 * self.hash_length) self.pebble_value.seek(i*self.hash_length + self.hash_length * fill * self.n) self.pebble_value.write(utils.secure_hash(prehash)) power_of_two /= 2 self.pebble_value.seek(self.n * fill * self.hash_length) return self.pebble_value.read(self.hash_length)
def __init__(self, leaf_values, parent=None, prehashed=False): self.size = len(leaf_values) self.parent = parent self.leaf_values = leaf_values self.leaves = None self.on_right = None if self.size == 1: if prehashed: self.value = leaf_values[0] else: self.value = utils.secure_hash(leaf_values[0]) self.left = None self.right = None self.leaves = [self] if self.size > 1: self.left = MerkleNode(leaf_values[:self.size / 2], self, prehashed) self.right = MerkleNode(leaf_values[self.size / 2:], self, prehashed) self.left.sibling = self.right self.right.sibling = self.left self.left.on_right = False self.right.on_right = True self.value = utils.secure_hash(self.left.value + self.right.value) self.leaves = self.left.leaves + self.right.leaves # note leaves is an array
def add_pebble(self, v): if self.debug: if v is "z" * self.all_graphs_increment: return if not self.is_pebbled(v): if self.is_source(v): self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(str(v))) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: self.all_graphs.seek(7 * v * self.all_graphs_increment) parents = [] prehash = "" error = 0 for i in range(7): parents.append( all_graphs.read(self.all_graphs_increment)) if parents[i] != "z" * self.all_graphs_increment: self.pebble_value.seek(self.hash_length * parents[i]) parent_hash = self.pebble_value.read(hash_length) if parent_hash == "z" * self.hash_length: print "Error: Attempted to pebble node " + str( v) + "without pebbling parent " + str( i) + "." error = 1 else: prehash = prehash + parent_hash if error == 0: self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(str(v))) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: print "Error: Attempted to pebble node " + str( v) + " but it has already been pebbled" else: # This is not for testing, but to run linear_pebble_graph_trivial as fast as possible. if v < 64 * 2**self.graph_num: self.pebble_value.write( utils.secure_hash(utils.prehash_associated_with_source(v))) else: self.all_graphs.seek(self.all_graphs_start + self.all_graphs_increment * 7 * v) prehash = "" for i in range(7): parent = self.all_graphs.read(self.all_graphs_increment) if parent != "z" * self.all_graphs_increment: self.pebble_value.seek(self.hash_length * int(parent)) prehash += self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(prehash))
def verify_opening(self, opening): # returns True for pass, False for fail cur = opening.leaf_value if self.debug: print "V: Working my way up the Merkle tree..." for x in opening.sibling_path: # recall that x is a two-element array of the form [sibling_value, sibling_path] value = x[0] key = x[1] if key[-1] == 'R': cur = utils.secure_hash(cur + value) else: cur = utils.secure_hash(value + cur) if self.debug: print "V: Calculated root value is " + cur return cur == self.rroot
def verify_opening(self, opening): # returns True for pass, False for fail cur = opening.leaf_value if self.debug: print "V: Working my way up the Merkle tree..." for x in opening.sibling_path: # recall that x is a two-element array of the form [sibling_value, sibling_path] value = x[0] key = x[1] if key[-1] == 'R': cur = utils.secure_hash(cur+value) else: cur = utils.secure_hash(value+cur) if self.debug: print "V: Calculated root value is "+cur return cur == self.rroot
def MT(leaf_range, shelf, key, prehashed=False): # recursively generates merkle tree size = leaf_range[1]-leaf_range[0] # number of leaves under (and possibly including) the current node value = None if size == 1: # if it is a leaf if prehashed: value = shelf["leaf"+str(leaf_range[0])] else: value = utils.secure_hash(shelf["leaf"+str(leaf_range[0])]) shelf[key] = [value, leaf_range, True] return [key] else: left_range = [leaf_range[0], leaf_range[0]+size/2] right_range = [leaf_range[0]+size/2, leaf_range[1]] left_keys = MT(left_range, shelf, key+'L', prehashed) right_keys = MT(right_range, shelf, key+'R', prehashed) value = utils.secure_hash(shelf[key+'L'][0]+shelf[key+'R'][0]) shelf[key] = [value, leaf_range, False] return left_keys + right_keys
def MT(leaf_range, shelf, key, prehashed=False): # recursively generates merkle tree size = leaf_range[1]-leaf_range[0] # number of leaves under (and possibly including) the current node value = None if size == 1: # if it is a leaf if prehashed: value = shelf["leaf_values"][leaf_range[0]] else: value = utils.secure_hash(shelf["leaf_values"][leaf_range[0]]) shelf[key] = [value, leaf_range, True] return [key] else: left_range = [leaf_range[0], leaf_range[0]+size/2] right_range = [leaf_range[0]+size/2, leaf_range[1]] left_keys = MT(left_range, shelf, key+'L', prehashed) right_keys = MT(right_range, shelf, key+'R', prehashed) value = utils.secure_hash(shelf[key+'L'][0]+shelf[key+'R'][0]) shelf[key] = [value, leaf_range, False] return left_keys + right_keys
def open(self, i): leaf_value = utils.secure_hash(self.mt["leaf_values"][i]) if self.debug: print "P: Opening that vertex, which has value "+leaf_value sibling_path = trees.mtopen(i, self.mt) if self.debug: print "P: The sibling path is: " print sibling_path return Opening(leaf_value, sibling_path)
def open(self, i): leaf_value = utils.secure_hash(self.mt["leaf_values"][i]) if self.debug: print "P: Opening that vertex, which has value " + leaf_value sibling_path = trees.mtopen(i, self.mt) if self.debug: print "P: The sibling path is: " print sibling_path return Opening(leaf_value, sibling_path)
def pebble_row(self, fill): self.parents.seek(0) for i in range(self.n): prehash = '' for i in range(16): self.pebble_value.seek(fill*self.n*self.hash_length + int(self.parents.read(self.parents_increment))) prehash += self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.n * self.opp(fill) * self.hash_length + i * self.hash_length) self.pebble_value.write(utils.secure_hash(prehash))
def create_merkle_tree(self): # Creates merkle tree vertex = 2**(self.merkle_tree_rows - 1) - 2 while vertex >= 0: self.p.pebble_value.seek((self.merkle_tree_size - (2*vertex + 2)) * self.p.hash_length) prehash = self.p.pebble_value.read(2 * self.p.hash_length) # reads both hash values at once! self.p.pebble_value.seek((self.merkle_tree_size - vertex) * self.p.hash_length) self.p.pebble_value.write(utils.secure_hash(prehash)) vertex -= 1 # Finds merkle root self.p.pebble_value.seek(self.merkle_tree_size * self.p.hash_length) self.merkle_root = self.p.pebble_value.read(self.p.hash_length)
def __init__(self, leaf_values, parent=None, prehashed=False): self.size = len(leaf_values) self.parent = parent self.leaf_values = leaf_values self.leaves = None self.on_right = None if self.size == 1: if prehashed: self.value = leaf_values[0] else: self.value = utils.secure_hash(leaf_values[0]) self.left = None self.right = None self.leaves = [self] if self.size > 1: self.left = MerkleNode(leaf_values[:self.size/2], self, prehashed) self.right = MerkleNode(leaf_values[self.size/2:], self, prehashed) self.left.sibling = self.right self.right.sibling = self.left self.left.on_right = False self.right.on_right = True self.value = utils.secure_hash(self.left.value+self.right.value) self.leaves = self.left.leaves + self.right.leaves # note leaves is an array
def add_pebble(self, v): if v is None: return if not self.is_pebbled(v): if self.is_source(v): self.pebble_value[str(v)] = utils.secure_hash(str(v)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) elif self.is_pebbled( self.B[str(v)][0]) and (self.B[str(v)][1] is None): self.pebble_value[str(v)] = utils.secure_hash( str(self.pebble_value[str(self.B[str(v)][0])])) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) elif self.is_pebbled(self.B[str(v)][0]) and self.is_pebbled( self.B[str(v)][1]): self.pebble_value[str(v)] = utils.secure_hash( str(self.pebble_value[str(self.B[str(v)][0])]) + str(self.pebble_value[str(self.B[str(v)][1])])) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: print "Error: attempted to pebble node " + str( v) + " without pebbling both parents" else: print "Attempted to pebble node " + str( v) + " but it has already been pebbled"
def verify_opening(self, i): # returns True for pass, False for fail node_number = self.prover.merkle_tree_size - i self.prover.p.pebble_value.seek(i * self.prover.p.hash_length) hash_of_node = self.prover.p.pebble_value.read(self.prover.p.hash_length) row = self.prover.merkle_tree_rows while (row > 1): if node_number % 2 == 0: # Node has a sibling to the right self.prover.p.pebble_value.seek((self.prover.merkle_tree_size - (node_number - 1)) * self.prover.p.hash_length) sibling = self.prover.p.pebble_value.read(self.prover.p.hash_length) node_number = node_number/2 - 1 hash_of_node = utils.secure_hash(hash_of_node + sibling) else: # Node has a sibling to the left. self.prover.p.pebble_value.seek((self.prover.merkle_tree_size - (node_number + 1)) * self.prover.p.hash_length) sibling = self.prover.p.pebble_value.read(self.prover.p.hash_length) node_number = node_number/2 hash_of_node = utils.secure_hash(sibling + hash_of_node) row = row - 1 if hash_of_node == self.prover.merkle_root: return True else: return False
def create_merkle_tree(self): # Creates merkle tree vertex = 2**(self.merkle_tree_rows - 1) - 2 while vertex >= 0: self.p.pebble_value.seek((self.merkle_tree_size - (2 * vertex + 2)) * self.p.hash_length) prehash = self.p.pebble_value.read( 2 * self.p.hash_length) # reads both hash values at once! self.p.pebble_value.seek( (self.merkle_tree_size - vertex) * self.p.hash_length) self.p.pebble_value.write(utils.secure_hash(prehash)) vertex -= 1 # Finds merkle root self.p.pebble_value.seek(self.merkle_tree_size * self.p.hash_length) self.merkle_root = self.p.pebble_value.read(self.p.hash_length)
def add_pebble(self, v): if self.debug: if v is 'z' * self.all_graphs_increment: return parents = [] all_graphs.seek(self.all_graphs_start + 2 * self.all_graphs_increment * v) parents.append(all_graphs.read(all_graphs_increment)) parents.append(all_graphs.read(all_graphs_increment)) if not self.is_pebbled(v): if self.is_source(v): self.pebble_value.seek(v * self.hash_length) self.pebble_value.write(utils.secure_hash(utils.prehash_associated_with_source(v))) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) elif self.is_pebbled(int(parents[0])) and (parents[1]) is 'z' * self.all_graphs_increment: self.pebble_value.seek(self.hash_length * int(parents[0])) pre_hash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(pre_hash)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) elif self.is_pebbled(int(parents[0])) and self.is_pebbled(int(parents[1])): self.pebble_value.seek(self.hash_length * int(parents[0])) first_prehash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * int(parents[1])) second_prehash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(first_prehash + second_prehash)) self.num_pebbles += 1 if self.num_pebbles > self.max_pebbles: self.max_pebbles = self.num_pebbles if (self.debug): print "Pebble added to node " + str(v) else: print "Error: attempted to pebble node " + str(v) + " without pebbling both parents" else: print "Attempted to pebble node " + str(v) + " but it has already been pebbled" else: # This is meant to run faster and to execute trivial_pebble_graph(). It won't detect errors in the code. if v < 2**self.graph_num: # is a source self.pebble_value.write(utils.secure_hash(utils.prehash_associated_with_source(v))) # There is no seek before. return # There is no code for setting self.max_pebbles, because this is not for testing code. parents = [self.all_graphs.read(self.all_graphs_increment), self.all_graphs.read(self.all_graphs_increment)] if parents[1] == 'z' * self.all_graphs_increment: # has only one parent self.pebble_value.seek(self.hash_length * int(parents[0])) prehash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(prehash)) else: # has two parents self.pebble_value.seek(self.hash_length * int(parents[0])) prehash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * int(parents[1])) second_prehash = self.pebble_value.read(self.hash_length) self.pebble_value.seek(self.hash_length * v) self.pebble_value.write(utils.secure_hash(prehash + second_prehash))