-
Notifications
You must be signed in to change notification settings - Fork 10
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
Organize helper functions #166
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
63c04c9
Refactor(circom): Move selector, division generic funcs to `/utils`
stefan-nikolov96 9ef61e5
feat(circom): Added Pow template
stefan-nikolov96 a7da21d
fix(circom) Add TODO for DivisionVerification constraint.
stefan-nikolov96 3bb8fd0
feat(circom) Add tests for pow function
stefan-nikolov96 946b7fc
feat(circom) Add LessThanBitsCheck tests
stefan-nikolov96 0b72802
feat(circom) Add tests for LessThanOrEqualBitsCheck circuit.
stefan-nikolov96 821b55a
fix(circom): Move RangeCheck circuit to numerical.circom
stefan-nikolov96 a6a522e
fix(circom): Update RangeCheck to propogate numerical constraints.
stefan-nikolov96 dfb10fe
feat(circom): Add tests for RangeCheck circuit.
stefan-nikolov96 43b2d3c
feat(circom): Add tests to IsEqaulArrays
stefan-nikolov96 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../../../node_modules/circomlib/circuits/comparators.circom"; | ||
include "../../../../node_modules/circomlib/circuits/bitify.circom"; | ||
|
||
template Selector(N) { | ||
signal input in[N]; | ||
signal input index; | ||
signal output out; | ||
|
||
signal sums[N + 1]; | ||
sums[0] <== 0; | ||
|
||
component eqs[N]; | ||
|
||
// For each item, check whether its index equals the input index. | ||
for (var i = 0; i < N; i ++) { | ||
eqs[i] = IsEqual(); | ||
eqs[i].in[0] <== i; | ||
eqs[i].in[1] <== index; | ||
|
||
// eqs[i].out is 1 if the index matches. As such, at most one input to | ||
sums[i + 1] <== sums[i] + eqs[i].out * in[i]; | ||
} | ||
|
||
// Returns 0 + 0 + ... + item | ||
out <== sums[N]; | ||
} | ||
|
||
template IsEqualArrays(N) { | ||
signal input in[2][N]; | ||
signal output out; | ||
|
||
signal isEqual[N]; | ||
var counter = 0; | ||
|
||
for(var i = 0; i < N; i++) { | ||
isEqual[i] <== IsEqual()([in[0][i], in[1][i]]); | ||
|
||
counter += isEqual[i]; | ||
} | ||
|
||
out <== IsEqual()([N, counter]); | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this file and move it to |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../../../node_modules/circomlib/circuits/comparators.circom"; | ||
include "../../../../node_modules/circomlib/circuits/bitify.circom"; | ||
|
||
template LessThanBitsCheck(n) { | ||
signal input in[2]; | ||
signal output out; | ||
|
||
signal bitCheck1[n] <== Num2Bits(n)(in[0]); | ||
|
||
signal bitCheck2[n] <== Num2Bits(n)(in[1]); | ||
|
||
out <== LessThan(n)(in); | ||
} | ||
|
||
template LessThanOrEqualBitsCheck(n) { | ||
signal input in[2]; | ||
signal output out; | ||
|
||
signal bitCheck1[n] <== Num2Bits(n)(in[0]); | ||
|
||
signal bitCheck2[n] <== Num2Bits(n)(in[1]); | ||
|
||
out <== LessEqThan(n)(in); | ||
} |
68 changes: 68 additions & 0 deletions
68
beacon-light-client/circom/circuits/utils/numerical.circom
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../../../node_modules/circomlib/circuits/comparators.circom"; | ||
include "../../../../node_modules/circomlib/circuits/bitify.circom"; | ||
include "arrays.circom"; | ||
|
||
template LessThanBitsCheck(n) { | ||
signal input in[2]; | ||
signal output out; | ||
|
||
signal bitCheck1[n] <== Num2Bits(n)(in[0]); | ||
|
||
signal bitCheck2[n] <== Num2Bits(n)(in[1]); | ||
|
||
out <== LessThan(n)(in); | ||
} | ||
|
||
template LessThanOrEqualBitsCheck(n) { | ||
signal input in[2]; | ||
signal output out; | ||
|
||
signal bitCheck1[n] <== Num2Bits(n)(in[0]); | ||
|
||
signal bitCheck2[n] <== Num2Bits(n)(in[1]); | ||
|
||
out <== LessEqThan(n)(in); | ||
} | ||
|
||
template RangeCheck(n) { | ||
signal input in[3]; | ||
signal output out; | ||
|
||
signal first <== LessThanBitsCheck(n)([in[0], in[1]]); | ||
signal second <== LessThanBitsCheck(n)([in[1], in[2]]); | ||
|
||
out <== first * second; | ||
} | ||
|
||
template DivisionVerification() { | ||
signal input dividend; | ||
signal input divisor; | ||
signal input quotient; | ||
signal input remainder; | ||
|
||
//TODO: Needs additional corebase nstraint | ||
dividend === divisor * quotient + remainder; | ||
} | ||
|
||
template Pow(N){ | ||
signal input base; | ||
signal input power; | ||
signal output out; | ||
|
||
assert(power < N); | ||
|
||
signal intermediary[N]; | ||
for (var i=0; i < N; i++) { | ||
intermediary[i] <== i == 0 ? 1 : (intermediary[i-1] * base); | ||
} | ||
|
||
component selector = Selector(N); | ||
for (var i = 0; i < N; i++) { | ||
selector.in[i] <== intermediary[i]; | ||
} | ||
selector.index <== power; | ||
|
||
out <== selector.out; | ||
} |
37 changes: 3 additions & 34 deletions
37
beacon-light-client/circom/circuits/validator_balances.circom
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
5 changes: 5 additions & 0 deletions
5
beacon-light-client/circom/test/is_equal_arrays/circuit.circom
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../circuits/utils/arrays.circom"; | ||
|
||
component main = IsEqualArrays(3); // N must be equal to length in both arrays in input["in"] |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case01/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": [["1","2","2"],["1","2","2"]]} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case01/output.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"out": "1"} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case02/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": [["100","2","2"],["1","2","101"]]} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case02/output.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"out": "0"} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case03/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": [["10","8","7"],["7","8","10"]]} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/is_equal_arrays/data/case03/output.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"out": "0"} |
5 changes: 5 additions & 0 deletions
5
beacon-light-client/circom/test/less_than_bits_check/circuit.circom
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../circuits/utils/numerical.circom"; | ||
|
||
component main = LessThanBitsCheck(32); |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case01/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["100","50"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case01/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "0" | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case02/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["1","2"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case02/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1" | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case03/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["1","1"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_bits_check/data/case03/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "0" | ||
} |
5 changes: 5 additions & 0 deletions
5
beacon-light-client/circom/test/less_than_eq_bits_check/circuit.circom
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../circuits/utils/numerical.circom"; | ||
|
||
component main = LessThanOrEqualBitsCheck(32); |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case01/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["100","50"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case01/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "0" | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case02/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["1","2"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case02/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1" | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case03/input.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"in": ["1","1"] | ||
} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/less_than_eq_bits_check/data/case03/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../circuits/utils/numerical.circom"; | ||
|
||
component main = Pow(256); |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"base": "10", "power": "3"} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1000" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"base": "2", | ||
"power": "10" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1024" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"base": "99", | ||
"power": "1" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "99" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"base": "1", | ||
"power": "100" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../../circuits/utils/numerical.circom"; | ||
|
||
component main = RangeCheck(32); |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/range_check/data/case01/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": ["1","2","3"]} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/range_check/data/case01/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "1" | ||
} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/range_check/data/case02/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": ["3","2","1"]} |
3 changes: 3 additions & 0 deletions
3
beacon-light-client/circom/test/range_check/data/case02/output.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"out": "0" | ||
} |
1 change: 1 addition & 0 deletions
1
beacon-light-client/circom/test/range_check/data/case03/input.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"in": ["2","2","3"]} |
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.
Move
RangeCheck
to numerical