Aztec_backend
Add serialiser for Merkle root and membership constraints
@@ -75,7 +75,79 @@ pub fn serialise_circuit(
sha256_constraints.push(sha256_constraint);
},
- OPCODE::AES => panic!("AES has not yet been implemented")
+ OPCODE::MerkleRoot => {
+
+ let mut leaves = Vec::new();
+ for input in gadget_call.inputs.iter() {
+ let witness_index = input.witness.witness_index() as i32;
+
+ // Ignore num_bits, it's only needed for
+ // hash functions like sha2/blake
+
+ leaves.push(witness_index);
+ }
+
+ assert_eq!(gadget_call.outputs.len(), 1);
+
+ let root_index = gadget_call.outputs[0].witness_index() as i32;
+
+ let constraint = MerkleRootConstraint {
+ leaves: leaves,
+ root: root_index,
+ };
+
+ merkle_root_constraints.push(constraint);
+ },
+ OPCODE::MerkleMembership => {
+
+ let mut inputs_iter = gadget_call.inputs.iter().peekable();
+
+ // root
+ let root = {
+ let root_input = inputs_iter.next().expect("missing merkle root");
+ root_input.witness.witness_index() as i32
+ };
+ // leaf
+ let leaf = {
+ let leaf_input = inputs_iter.next().expect("missing leaf to check membership for");
+ leaf_input.witness.witness_index() as i32
+ };
+ // index
+ let index = {
+ let index_input = inputs_iter.next().expect("missing index for leaf");
+ index_input.witness.witness_index() as i32
+ };
+
+ if inputs_iter.peek().is_none() {
+ unreachable!("cannot check membership without a hash path")
+ }
+
+ let mut hash_path = Vec::new();
+ while let (Some(path_left), path_right_option) = (inputs_iter.next(),inputs_iter.next()) {
+ let path_right = path_right_option.expect("iterator contains an odd amount of items");
+
+ let path_left_index = path_left.witness.witness_index() as i32;
+ let path_right_index = path_right.witness.witness_index() as i32;
+
+ hash_path.push((path_left_index, path_right_index));
+ }
+
+ // result
+ let result = gadget_call.outputs[0].witness_index() as i32;
+
+
+ let constraint = MerkleMembershipConstraint {
+ root,
+ leaf,
+ index,
+ result,
+ hash_path,
+ };
+
+ merkle_membership_constraints.push(constraint);
+
+ },
+ OPCODE::AES => panic!("AES has not yet been implemented"),
};
}
gate => panic!("Serialiser does not know how to serialise this gate"),
