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

Circuits update circom 2.1.5 #163

Merged
merged 5 commits into from
Jun 21, 2023
Merged
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
5 changes: 2 additions & 3 deletions beacon-light-client/circom/circuits/aggregate_bitmask.circom
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "../../../vendor/circom-pairing/circuits/curve.circom";

template AggregateKeysBitmask(N) {
template AggregateKeysBitmask(N,K) {
var J = 2;
var K = 7;
signal input points[N][J][K];
signal input bitmask[N];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

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

Expand Down
11 changes: 3 additions & 8 deletions beacon-light-client/circom/circuits/compress.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "../../../vendor/circom-pairing/circuits/bigint.circom";

Expand All @@ -10,12 +10,7 @@ template Compress() {
// CURVE.P / 2
var prime[7] = [35888059530597717, 36027359614205881, 18556878317960535, 21977360498475850, 26290126778424359, 29735955799434292, 914940731273212];

component lessThan = BigLessThan(55, 7);

for(var i = 0; i < 7; i++) {
lessThan.b[i] <== point[1][i];
lessThan.a[i] <== prime[i];
}
signal lessThan <== BigLessThan(55, 7)(prime, point[1]);

component num2Bits[7];

Expand All @@ -30,5 +25,5 @@ template Compress() {

bits[0] <== 1;
bits[1] <== 0;
bits[2] <== lessThan.out;
bits[2] <== lessThan;
}
15 changes: 6 additions & 9 deletions beacon-light-client/circom/circuits/compute_domain.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "hash_two.circom";

Expand All @@ -9,25 +9,22 @@ template ComputeDomain() {
signal input GENESIS_VALIDATORS_ROOT[256];
signal input DOMAIN_SYNC_COMMITTEE[32];

component hashTwo = HashTwo();
signal concated_fork_version[256];

for(var i = 0; i < 32; i++) {
hashTwo.in[0][i] <== fork_version[i];
concated_fork_version[i] <== fork_version[i];
}

for(var i = 32; i < 256; i++) {
hashTwo.in[0][i] <== 0;
concated_fork_version[i] <== 0;
}

for(var i = 0; i < 256; i++) {
hashTwo.in[1][i] <== GENESIS_VALIDATORS_ROOT[i];
}
signal hashTwo[256] <== HashTwo()([concated_fork_version,GENESIS_VALIDATORS_ROOT]);

for(var i = 0; i < 32; i++) {
domain[i] <== DOMAIN_SYNC_COMMITTEE[i];
}

for(var i = 32; i < 256; i++) {
domain[i] <== hashTwo.out[i - 32];
domain[i] <== hashTwo[i - 32];
}
}
17 changes: 3 additions & 14 deletions beacon-light-client/circom/circuits/compute_signing_root.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "compute_domain.circom";

Expand All @@ -10,18 +10,7 @@ template ComputeSigningRoot() {

signal output signing_root[256];

component hashTwo = HashTwo();
signal hashTwo[256] <== HashTwo()([headerHash,domain]);

for(var i = 0; i < 256; i++) {
hashTwo.in[0][i] <== headerHash[i];
}

for(var i = 0; i < 256; i++) {
hashTwo.in[1][i] <== domain[i];
}


for(var i = 0; i < 256; i++) {
signing_root[i] <== hashTwo.out[i];
}
signing_root <== hashTwo;
}
9 changes: 2 additions & 7 deletions beacon-light-client/circom/circuits/expand_message.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/sha256/sha256.circom";
include "../../../node_modules/circomlib/circuits/bitify.circom";
Expand Down Expand Up @@ -31,12 +31,7 @@ template ExpandMessage() {
b_0Sha.in[i] <== BIG_SIG_DST[i - 792];
}


var b_0[256];

for(var i = 0; i < 256; i++) {
b_0[i] = b_0Sha.out[i];
}
var b_0[256] = b_0Sha.out;

component prevSha256[8];

Expand Down
24 changes: 24 additions & 0 deletions beacon-light-client/circom/circuits/hash_aggregated_key.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/sha256/sha256.circom";

template hashAggregatedKey(){
signal input aggregatedKey[384];
signal output out[256];

component hash = Sha256(512);

for(var i = 0; i < 384; i++) {
hash.in[i] <== aggregatedKey[i];
}

for(var i = 384; i < 512; i++) {
hash.in[i] <== 0;
}

for(var i = 0; i < 256; i++) {
out[i] <== hash.out[i];
}


}
24 changes: 8 additions & 16 deletions beacon-light-client/circom/circuits/hash_to_field.circom
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "expand_message.circom";
include "../../../vendor/circom-pairing/circuits/bigint.circom";

template HashToField() {
var K = 7;
template HashToField(K) {
signal input in[256];
signal output out[2][2][K];

component expand_message = ExpandMessage();

for(var i = 0; i < 256; i++) {
expand_message.in[i] <== in[i];
}
signal expand_message[2048] <== ExpandMessage()(in);

component bigInts[2][2][10];

for(var i = 0; i < 2; i++) {
for(var j = 0; j < 2; j++) {
bigInts[i][j][9] = Bits2Num(55);
for(var i1=0; i1 < 17; i1++) {
bigInts[i][j][9].in[16 - i1] <== expand_message.out[i * 1024 + j * 512 + i1];
bigInts[i][j][9].in[16 - i1] <== expand_message[i * 1024 + j * 512 + i1];
}

for(var i1 = 17; i1 < 55; i1++) {
Expand All @@ -30,7 +25,7 @@ template HashToField() {
for(var k = 8; k >= 0; k--) {
bigInts[i][j][k] = Bits2Num(55);
for(var i1 = 0; i1 < 55; i1++) {
bigInts[i][j][k].in[54 - i1] <== expand_message.out[i * 1024 + j * 512 + (8-k) * 55 + i1 + 17];
bigInts[i][j][k].in[54 - i1] <== expand_message[i * 1024 + j * 512 + (8-k) * 55 + i1 + 17];
}
}
}
Expand All @@ -52,17 +47,14 @@ template HashToField() {
bigMod[i][j].a[k] <== 0;
}

for(var k = 0; k < 7; k++) {
bigMod[i][j].b[k] <== p[k];
}
bigMod[i][j].b <== p;

}
}

for(var i = 0; i < 2; i++) {
for(var j = 0; j < 2; j++) {
for(var k = 0; k < 7; k++) {
out[i][j][k] <== bigMod[i][j].mod[k];
}
out[i][j] <== bigMod[i][j].mod;
}
}
}
18 changes: 6 additions & 12 deletions beacon-light-client/circom/circuits/hash_tree_root.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/sha256/sha256.circom";
include "hash_two.circom";
Expand All @@ -15,24 +15,18 @@ template HashTreeRoot(N) {
}

for(var i = 0; i < N / 2; i++) {
for(var j = 0; j < 256; j++) {
hashers[i].in[0][j] <== leaves[i * 2][j];
hashers[i].in[1][j] <== leaves[i * 2 + 1][j];
}
hashers[i].in[0] <== leaves[i * 2];
hashers[i].in[1] <== leaves[i * 2 + 1];
}

var k = 0;

for(var i = N / 2; i < N - 1; i++) {
for(var j = 0; j < 256; j++) {
hashers[i].in[0][j] <== hashers[k * 2].out[j];
hashers[i].in[1][j] <== hashers[k * 2 + 1].out[j];
}
hashers[i].in[0] <== hashers[k * 2].out;
hashers[i].in[1] <== hashers[k * 2 + 1].out;

k++;
}

for(var i = 0; i < 256; i++) {
out[i] <== hashers[N - 2].out[i];
}
out <== hashers[N - 2].out;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "hash_two.circom";
include "hash_tree_root.circom";
Expand All @@ -12,35 +12,16 @@ template HashTreeRootBeaconHeader() {

signal output out[256];

component hashTreeRoot = HashTreeRoot(8);
signal zerosArr[3][256];

for(var i = 0; i < 256; i++) {
hashTreeRoot.leaves[0][i] <== slot[i];
}

for(var i = 0; i < 256; i++) {
hashTreeRoot.leaves[1][i] <== proposer_index[i];
}

for(var i = 0; i < 256; i++) {
hashTreeRoot.leaves[2][i] <== parent_root[i];
}

for(var i = 0; i < 256; i++) {
hashTreeRoot.leaves[3][i] <== state_root[i];
}

for(var i = 0; i < 256; i++) {
hashTreeRoot.leaves[4][i] <== body_root[i];
}

for(var i = 5; i < 8; i++) {
for(var i = 0; i < 3; i++) {
for(var j = 0; j < 256; j++) {
hashTreeRoot.leaves[i][j] <== 0;
zerosArr[i][j] <== 0;
}
}

for(var i = 0; i < 256; i++) {
out[i] <== hashTreeRoot.out[i];
}
signal hashTreeRoot[256] <== HashTreeRoot(8)([slot, proposer_index,
parent_root, state_root, body_root, zerosArr[0], zerosArr[1], zerosArr[2]]);

out <== hashTreeRoot;
}
15 changes: 7 additions & 8 deletions beacon-light-client/circom/circuits/hash_two.circom
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

include "hash_two.circom";
include "../../../node_modules/circomlib/circuits/sha256/sha256.circom";

template HashTwo() {
signal input in[2][256];

signal output out[256];

component sha256 = Sha256(512);
signal concateneted[512];

for(var i = 0; i < 256; i++) {
sha256.in[i] <== in[0][i];
concateneted[i] <== in[0][i];
}

for(var i = 256; i < 512; i++) {
sha256.in[i] <== in[1][i - 256];
concateneted[i] <== in[1][i - 256];
}

for(var i = 0; i < 256; i++) {
out[i] <== sha256.out[i];
}
signal sha256[256] <== Sha256(512)(concateneted);

out <== sha256;
}
18 changes: 6 additions & 12 deletions beacon-light-client/circom/circuits/is_first.circom
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

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

template IsFirst() {
signal input firstHash[2];
signal input secondHash[2];

signal output out;

component isEqual1 = IsEqual();
isEqual1.in[0] <== firstHash[0];
isEqual1.in[1] <== secondHash[0];
signal isEqual1 <== IsEqual()([firstHash[0],secondHash[0]]);

component isEqual2 = IsEqual();
isEqual2.in[0] <== firstHash[1];
isEqual2.in[1] <== secondHash[1];
signal isEqual2 <== IsEqual()([firstHash[1],secondHash[1]]);

component and = AND();
signal and <== AND()(isEqual1,isEqual2);

and.a <== isEqual1.out;
and.b <== isEqual2.out;

out <== and.out;
out <== and;
}
11 changes: 3 additions & 8 deletions beacon-light-client/circom/circuits/is_supermajority.circom
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
pragma circom 2.0.3;
pragma circom 2.1.5;

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

template IsSuperMajority(N) {
signal input bitmask[N];

signal output out;

var sum = 0;
component lessThan[N];
//count the number of 1s in the bitmask
for(var i = 0; i < N; i++) {
sum += bitmask[i];
}
// check if 1s are more then 2/3 of the bitmask
component greaterThan = GreaterEqThan(252);
greaterThan.in[0] <== sum * 3;
greaterThan.in[1] <== 2 * N;
signal greaterThan <== GreaterEqThan(252)([sum * 3, 2 * N]);

greaterThan.out === 1;
greaterThan === 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ template IsValidMerkleBranch(N) {

isValidBalanceBranchOut === 1;
}

Loading