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

Organize helper functions #166

Merged
merged 10 commits into from
Jun 27, 2023
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "hash_two.circom";
include "../../../node_modules/circomlib/circuits/comparators.circom";
include "utils.circom";
include "utils/arrays.circom";

template IsValidMerkleBranchOut(N) {
signal input branch[N][256];
Expand Down Expand Up @@ -33,6 +33,5 @@ template IsValidMerkleBranchOut(N) {
}
}


out <== IsEqualArrays(256)([root, hashers[N - 1].out]);
}
52 changes: 0 additions & 52 deletions beacon-light-client/circom/circuits/utils.circom

This file was deleted.

44 changes: 44 additions & 0 deletions beacon-light-client/circom/circuits/utils/arrays.circom
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move RangeCheck to numerical

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]);
}
26 changes: 26 additions & 0 deletions beacon-light-client/circom/circuits/utils/bits.circom
Copy link
Collaborator

Choose a reason for hiding this comment

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

Delete this file and move it to numerical.circom

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 beacon-light-client/circom/circuits/utils/numerical.circom
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 beacon-light-client/circom/circuits/validator_balances.circom
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/bitify.circom";
include "utils.circom";
include "hash_tree_root.circom";
include "hash_two.circom";
include "validator_hash_tree_root.circom";
include "is_valid_merkle_branch_out.circom";
include "is_valid_merkle_branch.circom";
include "ssz_num.circom";

template DivisionVerification() {
signal input dividend;
signal input divisor;
signal input quotient;
signal input remainder;

dividend === divisor * quotient + remainder;
}

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];
}
include "./utils/arrays.circom";
include "./utils/bits.circom";
include "./utils/numerical.circom";

template CalculateBalancesSum(N) {
signal input balances[(N \ 4) + 1][256];
Expand Down
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"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": [["1","2","2"],["1","2","2"]]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"out": "1"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": [["100","2","2"],["1","2","101"]]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"out": "0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": [["10","8","7"],["7","8","10"]]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"out": "0"}
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["100","50"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["1","2"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["1","1"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "0"
}
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["100","50"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["1","2"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": ["1","1"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1"
}
5 changes: 5 additions & 0 deletions beacon-light-client/circom/test/pow/circuit.circom
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);
1 change: 1 addition & 0 deletions beacon-light-client/circom/test/pow/data/case01/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"base": "10", "power": "3"}
3 changes: 3 additions & 0 deletions beacon-light-client/circom/test/pow/data/case01/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1000"
}
4 changes: 4 additions & 0 deletions beacon-light-client/circom/test/pow/data/case02/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"base": "2",
"power": "10"
}
3 changes: 3 additions & 0 deletions beacon-light-client/circom/test/pow/data/case02/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1024"
}
4 changes: 4 additions & 0 deletions beacon-light-client/circom/test/pow/data/case03/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"base": "99",
"power": "1"
}
3 changes: 3 additions & 0 deletions beacon-light-client/circom/test/pow/data/case03/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "99"
}
4 changes: 4 additions & 0 deletions beacon-light-client/circom/test/pow/data/case04/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"base": "1",
"power": "100"
}
3 changes: 3 additions & 0 deletions beacon-light-client/circom/test/pow/data/case04/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1"
}
5 changes: 5 additions & 0 deletions beacon-light-client/circom/test/range_check/circuit.circom
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": ["1","2","3"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": ["3","2","1"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"out": "0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"in": ["2","2","3"]}
Loading