Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Emit opcodes for sorting variables in order of execution #1941

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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