-
Notifications
You must be signed in to change notification settings - Fork 57
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
katat
wants to merge
15
commits into
main
Choose a base branch
from
experiment-hint-func
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
95627fa
temporary disable const attr check
katat 4dce945
allow proper generic call within forloop
katat 87ba85a
fix: only fold on literal constants for binary ops, otherwise propaga…
katat 371eca3
allow [0][0]
katat 59f98d1
disable unused cell var check
katat 6efdb67
fix: avoid re-instantiate functions/methods
katat bf9efb0
add builtins: left_shift/div/pow/mod/nth_bit/bit_len
katat 75dcbe3
allow - binary op
katat 6f71efc
incorporate native stdlib for testing
katat 28756c9
add int module
katat bd2e612
add comparator stdlib
katat cd58594
native bits stdlib
katat 1e586e9
bigint stdlib
katat f794eeb
fmt
katat 381315a
revert disabling const checking
katat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.