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

feat: remove redundant predicate from brillig quotients #2784

Merged
merged 1 commit into from
Sep 26, 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 @@ -39,40 +39,31 @@
}

/// Generates brillig bytecode which computes `a / b` and returns the quotient and remainder.
/// It returns `(0,0)` if the predicate is null.
///
///
/// This is equivalent to the Noir (psuedo)code

Check warning on line 43 in compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_directive.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (psuedo)
///
/// ```ignore
/// fn quotient<T>(a: T, b: T, predicate: bool) -> (T,T) {
/// if predicate != 0 {
/// (a/b, a-a/b*b)
/// } else {
/// (0,0)
/// }
/// fn quotient<T>(a: T, b: T) -> (T,T) {
/// (a/b, a-a/b*b)
/// }
/// ```
pub(crate) fn directive_quotient(bit_size: u32) -> GeneratedBrillig {
// `a` is (0) (i.e register index 0)
// `b` is (1)
// `predicate` is (2)
GeneratedBrillig {
byte_code: 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)
//q = a/b is set into register (2)
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::UnsignedDiv,
lhs: RegisterIndex::from(0),
rhs: RegisterIndex::from(1),
destination: RegisterIndex::from(3),
destination: RegisterIndex::from(2),
bit_size,
},
//(1)= q*b
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::Mul,
lhs: RegisterIndex::from(3),
lhs: RegisterIndex::from(2),
rhs: RegisterIndex::from(1),
destination: RegisterIndex::from(1),
bit_size,
Expand All @@ -88,17 +79,7 @@
//(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),
source: RegisterIndex::from(2),
},
BrilligOpcode::Stop,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@
}

/// Signed division lhs / rhs
/// We derive the signed division from the unsigned euclidian division.

Check warning on line 361 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (euclidian)
/// note that this is not euclidian division!

Check warning on line 362 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (euclidian)
// if x is a signed integer, then sign(x)x >= 0
// so if a and b are signed integers, we can do the unsigned division:
// sign(a)a = q1*sign(b)b + r1
Expand Down Expand Up @@ -544,11 +544,7 @@
let r_witness = self.next_witness_index();

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

Expand Down Expand Up @@ -818,7 +814,7 @@
let two_max_bits: FieldElement = two.pow(&FieldElement::from(max_bits as i128));
let comparison_evaluation = (a - b) + two_max_bits;

// Euclidian division by 2^{max_bits} : 2^{max_bits} + a - b = q * 2^{max_bits} + r

Check warning on line 817 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Euclidian)
//
// 2^{max_bits} is of max_bits+1 bit size
// If a>b, then a-b is less than 2^{max_bits} - 1, so 2^{max_bits} + a - b is less than 2^{max_bits} + 2^{max_bits} - 1 = 2^{max_bits+1} - 1
Expand Down
Loading