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

stdlib: comparator & bigint #180

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
95 changes: 57 additions & 38 deletions examples/fixture/asm/kimchi/generic_builtin_bits.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
@ public inputs: 1

DoubleGeneric<1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<2,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,-1,0,-1>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1>
DoubleGeneric<4,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,-1>
DoubleGeneric<1,1>
Expand All @@ -25,47 +40,51 @@ DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,0,0,0,-1>
DoubleGeneric<1,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<2,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<4,0,-1>
DoubleGeneric<1,1>
DoubleGeneric<1,0,-1,0,1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,-1>
DoubleGeneric<1>
DoubleGeneric<1,0,0,0,-1>
DoubleGeneric<1,0,-1>
DoubleGeneric<2,0,-1>
DoubleGeneric<1,1,-1>
DoubleGeneric<4,0,-1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,-1>
(0,0) -> (15,0) -> (28,1) -> (36,1)
(1,0) -> (2,0) -> (4,0) -> (16,0) -> (23,0)
(1,2) -> (2,1)
(2,2) -> (3,0)
(4,2) -> (9,0)
(5,0) -> (6,0) -> (8,0) -> (19,0) -> (24,0)
(5,2) -> (6,1)
(6,2) -> (7,0)
(8,2) -> (9,1)
(9,2) -> (14,0)
(10,0) -> (11,0) -> (13,0) -> (20,0) -> (26,0)
(10,2) -> (11,1)
(11,2) -> (12,0)
DoubleGeneric<1,0,0,0,-2>
(0,0) -> (30,1) -> (49,1) -> (50,0)
(1,0) -> (2,0) -> (7,0) -> (8,0) -> (31,0) -> (38,0) -> (39,0)
(1,2) -> (4,0) -> (5,0)
(2,1) -> (3,0)
(4,2) -> (5,1)
(5,2) -> (6,0)
(7,2) -> (19,0)
(8,1) -> (9,0)
(10,0) -> (11,0) -> (16,0) -> (17,0) -> (34,0) -> (41,0) -> (42,0)
(10,2) -> (13,0) -> (14,0)
(11,1) -> (12,0)
(13,2) -> (14,1)
(14,2) -> (15,1)
(16,1) -> (17,0)
(17,2) -> (18,0)
(20,1) -> (21,0)
(21,2) -> (22,0)
(23,2) -> (25,0)
(24,2) -> (25,1)
(25,2) -> (27,0)
(26,2) -> (27,1)
(27,2) -> (28,0)
(29,0) -> (31,0) -> (34,0)
(30,0) -> (32,0)
(31,2) -> (33,0)
(32,2) -> (33,1)
(33,2) -> (35,0)
(34,2) -> (35,1)
(35,2) -> (36,0)
(14,2) -> (15,0)
(16,2) -> (19,1)
(17,1) -> (18,0)
(19,2) -> (29,0)
(20,0) -> (21,0) -> (26,0) -> (27,0) -> (35,0) -> (45,0) -> (46,0)
(20,2) -> (23,0) -> (24,0)
(21,1) -> (22,0)
(23,2) -> (24,1)
(24,2) -> (25,0)
(26,2) -> (29,1)
(27,1) -> (28,0)
(29,2) -> (30,0)
(31,1) -> (32,0)
(32,2) -> (33,0)
(35,1) -> (36,0)
(36,2) -> (37,0)
(38,2) -> (44,0)
(39,1) -> (40,0)
(41,2) -> (44,1)
(42,1) -> (43,0)
(44,2) -> (48,0)
(45,2) -> (48,1)
(46,1) -> (47,0)
(48,2) -> (49,0)
7 changes: 2 additions & 5 deletions examples/fixture/asm/r1cs/generic_builtin_bits.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ v_5 == (v_4) * (v_4 + -1)
0 == (v_5) * (1)
v_7 == (v_6) * (v_6 + -1)
0 == (v_7) * (1)
v_2 + 2 * v_4 + 4 * v_6 == (v_1) * (1)
v_1 == (v_2 + 2 * v_4 + 4 * v_6) * (1)
1 == (-1 * v_2 + 1) * (1)
1 == (v_4) * (1)
1 == (-1 * v_6 + 1) * (1)
v_1 == (v_2 + 2 * v_4 + 4 * v_6) * (1)
0 == (v_8) * (1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the asm diffs originated from the fact that the builtin version stored constant as cell vars.

1 == (v_9) * (1)
0 == (v_10) * (1)
v_1 == (v_8 + 2 * v_9 + 4 * v_10) * (1)
2 == (v_1) * (1)
3 changes: 2 additions & 1 deletion examples/generic_array_access.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fn last(arr: [Field; LEN]) -> Field {
return arr[LEN - 1];
let last = LEN - 1;
return arr[last];
}

fn main(pub xx: Field) {
Expand Down
103 changes: 96 additions & 7 deletions examples/generic_builtin_bits.no
Original file line number Diff line number Diff line change
@@ -1,22 +1,111 @@
// circom versions:
// template Num2Bits(n) {
// signal input in;
// signal output out[n];
// var lc1=0;

// var e2=1;
// for (var i = 0; i<n; i++) {
// out[i] <-- (in >> i) & 1;
// out[i] * (out[i] -1 ) === 0;
// lc1 += out[i] * e2;
// e2 = e2+e2;
// }

// lc1 === in;
// }

// template Bits2Num(n) {
// signal input in[n];
// signal output out;
// var lc1=0;

// var e2 = 1;
// for (var i = 0; i<n; i++) {
// lc1 += in[i] * e2;
// e2 = e2 + e2;
// }

// lc1 ==> out;
// }

use std::bits;

// 010 = xx, where xx = 2
// obviously writing this in native is much simpler than the builtin version
fn to_bits(const LEN: Field, value: Field) -> [Bool; LEN] {
let mut bits = [false; LEN];
let mut lc1 = 0;
let mut e2 = 1;

let one = 1;
let zero = 0;

for index in 0..LEN {
// maybe add a unconstrained / unsafe attribute before bits::nth_bit, such that:
// bits[index] = unsafe bits::nth_bit(value, index);
// here we need to ensure the related variables are constrained:
// 1. value: constrained to be equal with the sum of bits, which involves the index as well
// 2. index: a cell index in bits
// 3. bits: the output bits
// beyond the notation purpose, what security measures can we take to help guide this unsafe operation?
// one idea is to rely on this unsafe attribute to check if it is non-deterministic when constraining the bits[index]
// eg.
// - bits::nth_bit(value, index) is non-deterministic
// - a metadata can be added to the var of the bits as non-deterministic
// - when CS trying to constrain the non-deterministic var,
// it will raise an error if the var is not marked unsafe via the attribute unsafe
// thus, it seems we also need to add the attribute to the builtin function signature
// eg. `unsafe nth_bit(val: Field, const nth: Field) -> Bool`
// while the unsafe attribute in `bits[index] = unsafe bits::nth_bit(value, index);`
// is for the users to acknowledge they are responsible for having additional constraints.
// This approach makes it explicit whether an expression is non-deterministic at the first place.
// On the other hand, circom lang determines whether it is non-deterministic by folding the arithmetic operation.

bits[index] = bits::nth_bit(value, index);
// nth_bit is a hint function, and it doesn't constraint the value of the bits as boolean,
// although its return type is boolean.
// can we make the arithmetic operation compatible with boolean?
// or just make a stdlib to convert boolean to Field while adding the constraint?
let bit_num = if bits[index] {one} else {zero};
assert_eq(bit_num * (bit_num - 1), 0);

lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
assert_eq(lc1, value);
return bits;
}

fn from_bits(bits: [Bool; LEN]) -> Field {
let mut lc1 = 0;
let mut e2 = 1;
let zero = 0;

for index in 0..LEN {
lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
return lc1;
}

fn main(pub xx: Field) {
// var
let bits = bits::to_bits(3, xx);
// calculate on a cell var
let bits = to_bits(3, xx);
assert(!bits[0]);
assert(bits[1]);
assert(!bits[2]);

let val = bits::from_bits(bits);
let val = from_bits(bits);
assert_eq(val, xx);

// constant
let cst_bits = bits::to_bits(3, 2);
// calculate on a constant
let cst_bits = to_bits(3, 2);
assert(!cst_bits[0]);
assert(cst_bits[1]);
assert(!cst_bits[2]);

let cst = bits::from_bits(cst_bits);
let cst = from_bits(cst_bits);
assert_eq(cst, xx);
}

// ^ the asm diffs originated from the fact that the builtin version stored constant as cell vars.
76 changes: 76 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,82 @@ pub trait Backend: Clone {
Self::Field::zero()
};

env.cached_values.insert(cache_key, res);
Ok(res)
}
Value::LeftShift(var, shift) => {
let var = self.compute_var(env, var)?;
let shifted = var.to_biguint() << *shift;
let res = Self::Field::from(shifted);
Ok(res)
}
Value::VarDivVar(dividend, divisor) => {
let dividend = self.compute_var(env, dividend)?;
let divisor = self.compute_var(env, divisor)?;
let res = 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 = dividend / divisor;
let res = Self::Field::from(res);
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 = dividend % divisor;

let res = Self::Field::from(res);
env.cached_values.insert(cache_key, res);
Ok(res)
}
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 = dividend % divisor;
let res = Self::Field::from(res);
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 = dividend % divisor;
let res = Self::Field::from(res);
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 = dividend % divisor;
let res = Self::Field::from(res);
env.cached_values.insert(cache_key, res);
Ok(res)
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/backends/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ where
);
return Err(err);
} else {
panic!("there's a bug in the circuit_writer, some cellvar does not end up being a cellvar in the circuit!");
// todo: lift this restriction for now
// this is due to the intermediate cell vars introduced by the hint builtins.
// in order to get computed in symbolic value, it needs to introduce intermediate cell vars and pass them around even it is hint builtins.
// panic!("there's a bug in the circuit_writer, some cellvar does not end up being a cellvar in the circuit!");
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub enum ErrorKind {
#[error("invalid array size, expected [_; x] with x in [0,2^32]")]
InvalidArraySize,

#[error("only allow a single generic parameter for the size of an array argument")]
#[error("Invalid expression in symbolic size")]
InvalidSymbolicSize,

#[error("invalid generic parameter, expected single uppercase letter, such as N, M, etc.")]
Expand All @@ -148,6 +148,9 @@ pub enum ErrorKind {
#[error("calling generic functions in for loop is not allowed")]
GenericInForLoop,

#[error("variable `{0}` is forbidden to be accessed in the for loop, likely due to a generic function call")]
VarAccessForbiddenInForLoop(String),

#[error("the value passed could not be converted to a field element")]
InvalidField(String),

Expand Down Expand Up @@ -349,4 +352,7 @@ pub enum ErrorKind {

#[error("invalid range, the end value can't be smaller than the start value")]
InvalidRange,

#[error("division by zero")]
DivisionByZero,
}
Loading
Loading