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: replace usage of Directive::Quotient with brillig opcode #1766

Merged
merged 16 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -1,4 +1,6 @@
use acvm::acir::brillig_vm::{BinaryFieldOp, Opcode as BrilligOpcode, RegisterIndex, Value};
use acvm::acir::brillig_vm::{
BinaryFieldOp, BinaryIntOp, Opcode as BrilligOpcode, RegisterIndex, Value,
};

/// Generates brillig bytecode which computes the inverse of its input if not null, and zero else.
pub(crate) fn directive_invert() -> Vec<BrilligOpcode> {
Expand Down Expand Up @@ -29,3 +31,55 @@ pub(crate) fn directive_invert() -> Vec<BrilligOpcode> {
BrilligOpcode::Stop,
]
}

/// Generates brillig bytecode which computes the a / b and returns the quotient and remainder.
/// It returns (0,0) if the predicate is null
pub(crate) fn directive_quotient(bit_size: u32) -> Vec<BrilligOpcode> {
// We generate the following code:
// fn quotient(a : Int, b: Int, predicate: bool) -> (Int,Int) {
// if predicate != 0 {
// (a/b, a-a/b*b)
// } else {
// (0,0)
// }
// }

//a is (0) (i.e register index 0)
//b is (1)
//predicate is (2)
vec![
// If the predicate is zero, we jump to the exit segment
BrilligOpcode::JumpIfNot { condition: RegisterIndex::from(2), location: 6 },
//q = a/b is set into register (3)
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::UnsignedDiv,
lhs: RegisterIndex::from(0),
rhs: RegisterIndex::from(1),
destination: RegisterIndex::from(3),
bit_size,
},
//(1)= q*b
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::Mul,
lhs: RegisterIndex::from(3),
rhs: RegisterIndex::from(1),
destination: RegisterIndex::from(1),
bit_size,
},
//(1) = a-q*b
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::Sub,
lhs: RegisterIndex::from(0),
rhs: RegisterIndex::from(1),
destination: RegisterIndex::from(1),
bit_size,
},
//(0) = q
BrilligOpcode::Mov { destination: RegisterIndex::from(0), source: RegisterIndex::from(3) },
BrilligOpcode::Stop,
// Exit segment: we return 0,0
BrilligOpcode::Const { destination: RegisterIndex::from(0), value: Value::from(0_usize) },
BrilligOpcode::Const { destination: RegisterIndex::from(1), value: Value::from(0_usize) },
BrilligOpcode::Stop,
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,10 @@ impl GeneratedAcir {
max_bit_size: u32,
predicate: &Expression,
) -> Result<(Witness, Witness), AcirGenError> {
let q_witness = self.next_witness_index();
let r_witness = self.next_witness_index();

// lhs = rhs * q + r
//
// If predicate is zero, `q_witness` and `r_witness` will be 0
self.push_opcode(AcirOpcode::Directive(Directive::Quotient(QuotientDirective {
a: lhs.clone(),
b: rhs.clone(),
q: q_witness,
r: r_witness,
predicate: Some(predicate.clone()),
})));
let (q_witness, r_witness) = self.brillig_quotient(lhs, rhs, predicate, max_bit_size);

// Constrain r to be 0 <= r < 2^{max_bit_size}
let r_expr = Expression::from(r_witness);
Expand All @@ -357,6 +348,29 @@ impl GeneratedAcir {
Ok((q_witness, r_witness))
}

pub(crate) fn brillig_quotient(
&mut self,
lhs: &Expression,
rhs: &Expression,
predicate: &Expression,
max_bit_size: u32,
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
) -> (Witness, Witness) {
// Create the witness for the result
let q_witness = self.next_witness_index();
let r_witness = self.next_witness_index();

let quotient_code = brillig_directive::directive_quotient(max_bit_size);
let inputs = vec![
BrilligInputs::Single(lhs.clone()),
BrilligInputs::Single(rhs.clone()),
BrilligInputs::Single(predicate.clone()),
];
let outputs = vec![BrilligOutputs::Simple(q_witness), BrilligOutputs::Simple(r_witness)];
self.brillig(quotient_code, inputs, outputs);

(q_witness, r_witness)
}

/// Generate constraints that are satisfied iff
/// lhs < rhs , when offset is 1, or
/// lhs <= rhs, when offset is 0
Expand Down Expand Up @@ -618,9 +632,7 @@ impl GeneratedAcir {
let two_max_bits = two.pow(&FieldElement::from(max_bits as i128));
comparison_evaluation.q_c += two_max_bits;

let q_witness = self.next_witness_index();
let r_witness = self.next_witness_index();

let predicate = predicate.unwrap_or_else(Expression::one);
// Add constraint : 2^{max_bits} + a - b = q * 2^{max_bits} + r
//
// case: a == b
Expand All @@ -641,19 +653,17 @@ impl GeneratedAcir {
// - 2^{max_bits} - k == q * 2^{max_bits} + r
// - This is only the case when q == 0 and r == 2^{max_bits} - k
//
let (q_witness, r_witness) = self.brillig_quotient(
&comparison_evaluation,
&Expression::from_field(two_max_bits),
&predicate,
max_bits + 1,
);
let mut expr = Expression::default();
expr.push_addition_term(two_max_bits, q_witness);
expr.push_addition_term(FieldElement::one(), r_witness);
self.push_opcode(AcirOpcode::Arithmetic(&comparison_evaluation - &expr));

self.push_opcode(AcirOpcode::Directive(Directive::Quotient(QuotientDirective {
a: comparison_evaluation,
b: Expression::from_field(two_max_bits),
q: q_witness,
r: r_witness,
predicate,
})));

// Add constraint to ensure `r` is correctly bounded
// between [0, 2^{max_bits}-1]
self.range_constraint(r_witness, max_bits)?;
Expand Down