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

chore: adds inputs to the circuit and update sort for stepwise acir #1929

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ impl Abi {
self.parameters.iter().map(|x| &x.name).collect()
}

pub fn parameter_witness(&self) -> Vec<Witness> {
self.param_witnesses.values().flatten().copied().collect()
}

pub fn num_parameters(&self) -> usize {
self.parameters.len()
}
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub fn create_circuit(
opcodes,
public_parameters: PublicInputs(public_parameters),
return_values: PublicInputs(return_values.iter().copied().collect()),
inputs: param_witnesses.values().flatten().copied().collect(),
};

let (parameters, return_type) = program.main_function_signature;
Expand Down
5 changes: 3 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ pub fn experimental_create_circuit(
let public_parameters =
PublicInputs(public_abi.param_witnesses.values().flatten().copied().collect());
let return_values = PublicInputs(return_witnesses.into_iter().collect());

let circuit = Circuit { current_witness_index, opcodes, public_parameters, return_values };
let inputs = abi.parameter_witness();
let circuit =
Circuit { current_witness_index, opcodes, public_parameters, return_values, inputs };

Ok((circuit, abi))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,12 @@ 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 @@ -781,19 +781,24 @@ 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