Skip to content

Commit

Permalink
fix: Emit opcodes for sorting variables in order of execution (#1941)
Browse files Browse the repository at this point in the history
fix: emit opcodes for sorting variables in order of execution
  • Loading branch information
TomAFrench authored Jul 17, 2023
1 parent 1056ba1 commit c43efab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,14 @@ impl AcirContext {
let outputs_var = vecmap(&outputs_witness, |witness_index| {
self.add_data(AcirVarData::Witness(*witness_index))
});

// Enforce the outputs to be a permutation of the inputs
self.acir_ir.permutation(&inputs_expr, &output_expr);

// Enforce the outputs to be sorted
for i in 0..(outputs_var.len() - 1) {
self.less_than_constrain(outputs_var[i], outputs_var[i + 1], bit_size, None)?;
}
// Enforce the outputs to be a permutation of the inputs
self.acir_ir.permutation(&inputs_expr, &output_expr);

Ok(outputs_var)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,19 +795,25 @@ impl GeneratedAcir {
/// n.b. A sorting network is a predetermined set of switches,
/// the control bits indicate the configuration of each switch: false for pass-through and true for cross-over
pub(crate) fn permutation(&mut self, in_expr: &[Expression], out_expr: &[Expression]) {
let bits = Vec::new();
let (w, b) = self.permutation_layer(in_expr, &bits, true);
// Constrain the network output to out_expr
for (b, o) in b.iter().zip(out_expr) {
self.push_opcode(AcirOpcode::Arithmetic(b - o));
let mut bits_len = 0;
for i in 0..in_expr.len() {
bits_len += ((i + 1) as f32).log2().ceil() as u32;
}

let bits = vecmap(0..bits_len, |_| self.next_witness_index());
let inputs = in_expr.iter().map(|a| vec![a.clone()]).collect();
self.push_opcode(AcirOpcode::Directive(Directive::PermutationSort {
inputs,
tuple: 1,
bits: w,
bits: bits.clone(),
sort_by: vec![0],
}));
let (_, b) = self.permutation_layer(in_expr, &bits, false);

// Constrain the network output to out_expr
for (b, o) in b.iter().zip(out_expr) {
self.push_opcode(AcirOpcode::Arithmetic(b - o));
}
}
}

Expand Down

0 comments on commit c43efab

Please sign in to comment.