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

Builtin operators #197

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions examples/arithmetic.no
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@ fn main(pub public_input: Field, private_input: Field) {
let xx = private_input + public_input;
let yy = private_input * public_input;
assert_eq(xx, yy);

// modulus constant
let mod_res = (xx + yy) % 3;
assert_eq(mod_res, 2);

// modulus var
let mod_var_res = (xx + yy) % (private_input + 1);
assert_eq(mod_var_res, 2);

// divide constant
let div_res = xx / 2;
assert_eq(div_res, 2);

// divide var
let div_var_res = xx / private_input;
assert_eq(div_res, 2);

// left shift
let lf_res = xx << 2;
assert_eq(lf_res, 16);
}
4 changes: 4 additions & 0 deletions examples/comparator.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(pub xx: Field, yy: Field) {
let res = xx < yy;
assert(res);
}
64 changes: 64 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,70 @@ pub trait Backend: Clone {

Ok(res)
}
Value::VarDivVar(dividend, divisor) => {
let dividend = self.compute_var(env, dividend)?.to_biguint();
let divisor = self.compute_var(env, divisor)?.to_biguint();
let res = Self::Field::from(dividend / divisor);

env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::CstDivVar(dividend, divisor) => {
let divisor = self.compute_var(env, divisor)?;
let res = *dividend / divisor;
Ok(res)
}
Value::VarDivCst(dividend, divisor) => {
let dividend = self.compute_var(env, dividend)?;
// convert to bigint
let dividend = dividend.to_biguint();
let divisor = divisor.to_biguint();

let res = Self::Field::from(dividend / divisor);
env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::CstDivCst(dividend, divisor) => {
let res = *dividend / *divisor;
env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::VarModVar(dividend, divisor) => {
let dividend = self.compute_var(env, dividend)?;
let divisor = self.compute_var(env, divisor)?;
// convert to bigint
let dividend = dividend.to_biguint();
let divisor = divisor.to_biguint();
let res = Self::Field::from(dividend % divisor);

env.cached_values.insert(cache_key, res);
Ok(res)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not a single Value::Div(VarOrCst, VarOrCst)?

Value::CstModVar(dividend, divisor) => {
let divisor = self.compute_var(env, divisor)?;
// convert to bigint
let dividend = dividend.to_biguint();
let divisor = divisor.to_biguint();
let res = Self::Field::from(dividend % divisor);
env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::VarModCst(dividend, divisor) => {
let dividend = self.compute_var(env, dividend)?;
// convert to bigint
let dividend = dividend.to_biguint();
let divisor = divisor.to_biguint();
let res = Self::Field::from(dividend % divisor);
env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::CstModCst(dividend, divisor) => {
let dividend = dividend.to_biguint();
let divisor = divisor.to_biguint();
let res = Self::Field::from(dividend % divisor);
env.cached_values.insert(cache_key, res);
Ok(res)
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/circuit_writer/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,16 @@ impl<B: Backend> CircuitWriter<B> {
Op2::Addition => field::add(self, &lhs[0], &rhs[0], expr.span),
Op2::Subtraction => field::sub(self, &lhs[0], &rhs[0], expr.span),
Op2::Multiplication => field::mul(self, &lhs[0], &rhs[0], expr.span),
Op2::Modulus => field::modulus(self, &lhs[0], &rhs[0], expr.span),
Op2::Division => field::div(self, &lhs[0], &rhs[0], expr.span),
Op2::Equality => field::equal(self, &lhs, &rhs, expr.span),
Op2::Inequality => field::not_equal(self, &lhs, &rhs, expr.span),
// todo: refactor the input vars from Var to VarInfo,
// which contain the type to provide the info about the bit length
Op2::LessThan => field::less_than(self, None, &lhs[0], &rhs[0], expr.span),
Op2::LeftShift => field::left_shift(self, &lhs[0], &rhs[0], expr.span),
Op2::BoolAnd => boolean::and(self, &lhs[0], &rhs[0], expr.span),
Op2::BoolOr => boolean::or(self, &lhs[0], &rhs[0], expr.span),
Op2::Division => todo!(),
};

Ok(Some(VarOrRef::Var(res)))
Expand Down
Loading
Loading