Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat!: add trait for witness & make output of mul expression optional (
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-000 authored Jun 20, 2023
1 parent 7d28f42 commit 5bcdfc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions acir/src/native_types/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ impl Sub<&Expression> for &Expression {
}

impl Mul<&Expression> for &Expression {
type Output = Expression;
fn mul(self, rhs: &Expression) -> Expression {
type Output = Option<Expression>;
fn mul(self, rhs: &Expression) -> Option<Expression> {
if self.is_const() {
return self.q_c * rhs;
return Some(self.q_c * rhs);
} else if rhs.is_const() {
return self * rhs.q_c;
return Some(self * rhs.q_c);
} else if !(self.is_linear() && rhs.is_linear()) {
// `Expression`s can only represent terms which are up to degree 2.
// We then disallow multiplication of `Expression`s which have degree 2 terms.
unreachable!("Can only multiply linear terms");
return None;
}

let mut output = Expression::from_field(self.q_c * rhs.q_c);
Expand Down Expand Up @@ -210,7 +210,7 @@ impl Mul<&Expression> for &Expression {
i2 += 1;
}

output
Some(output)
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ fn mul_smoketest() {
};

assert_eq!(
&a * &b,
(&a * &b).unwrap(),
Expression {
mul_terms: vec![(FieldElement::from(8u128), Witness(2), Witness(4)),],
linear_combinations: vec![
Expand Down
13 changes: 13 additions & 0 deletions acir/src/native_types/witness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::ops::Add;

use acir_field::FieldElement;
use serde::{Deserialize, Serialize};

use super::Expression;

// Witness might be a misnomer. This is an index that represents the position a witness will take
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize,
Expand Down Expand Up @@ -28,3 +33,11 @@ impl From<u32> for Witness {
Self(value)
}
}

impl Add<Witness> for Witness {
type Output = Expression;

fn add(self, rhs: Witness) -> Self::Output {
Expression::from(self).add_mul(FieldElement::one(), &Expression::from(rhs))
}
}

0 comments on commit 5bcdfc6

Please sign in to comment.