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 #165

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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";
include "./utils/bits.circom";

template IsValidMerkleBranchOut(N) {
signal input branch[N][256];
Expand Down
54 changes: 54 additions & 0 deletions beacon-light-client/circom/circuits/utils/arrays.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
pragma circom 2.1.5;

include "../../../../node_modules/circomlib/circuits/comparators.circom";
include "../../../../node_modules/circomlib/circuits/bitify.circom";

template RangeCheck(n) {
signal input in[3];
signal output out;

signal first <== LessThanBitsCheck(64)([in[0], in[1]]);
signal second <== LessThanBitsCheck(64)([in[1], in[2]]);

out <== first * second;
}

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]);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/comparators.circom";
Expand All @@ -23,30 +24,4 @@ template LessThanOrEqualBitsCheck(n) {
signal bitCheck2[n] <== Num2Bits(n)(in[1]);

out <== LessEqThan(n)(in);
}

template RangeCheck(n) {
signal input in[3];
signal output out;

signal first <== LessThanBitsCheck(64)([in[0], in[1]]);
signal second <== LessThanBitsCheck(64)([in[1], in[2]]);

out <== first * second;
}

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]);
}
}
Copy link
Contributor

@EmilIvanichkovv EmilIvanichkovv Jun 22, 2023

Choose a reason for hiding this comment

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

Add one more line at the end of the file. You can turn on Insert Final Newline VSCode setting.

10 changes: 10 additions & 0 deletions beacon-light-client/circom/circuits/utils/numerical.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma circom 2.1.5;

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

dividend === divisor * quotient + remainder;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto

36 changes: 3 additions & 33 deletions beacon-light-client/circom/circuits/validator_balances.circom
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,8 @@ 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/numerical.circom";

template CalculateBalancesSum(N) {
signal input balances[(N \ 4) + 1][256];
Expand Down Expand Up @@ -118,6 +87,7 @@ template ValidatorBalances(N) {

signal currentEpoch <-- slot \ 32;


signal epochHighestSlot <== currentEpoch * 32;

signal slotLessThan <== LessThanBitsCheck(64)([slot, epochHighestSlot]);
Expand Down