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

Recurssive light client commit inputs #227

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e7163d1
add(feat): hash_verifier_pedersen
stefan-nikolov96 Jul 31, 2023
9bcd219
add(test): hash_verifier_pedersen
stefan-nikolov96 Jul 31, 2023
63fed8d
chore(get_light_clinet_recursive_input): fix imports
stefan-nikolov96 Jul 31, 2023
e185499
add(feat): commit public inputs in light_client_recurssive
stefan-nikolov96 Jul 31, 2023
90deca9
chore(build_proof): modify pot28_final location & compile flags
stefan-nikolov96 Jul 31, 2023
c5e3f0b
fix(hash_verifier_pedersen): Remove unused input
stefan-nikolov96 Jul 31, 2023
eb54547
fix(light_client_recursive): update N,K constants propogation
stefan-nikolov96 Jul 31, 2023
828ee19
fix(light_client_recursive): update template interfaces
stefan-nikolov96 Aug 1, 2023
8cb5348
fix(light_client_recursive): hasher uses SyncCommiteeHashTreeRoot
stefan-nikolov96 Aug 1, 2023
f0f8eb3
fix(light_client_recursive): update include
stefan-nikolov96 Aug 1, 2023
3611abd
feat(light_client_recursive): pass network config params to recursive…
stefan-nikolov96 Aug 1, 2023
0825981
fix(light_client_recursive): instantiate SigningRoot sooner
stefan-nikolov96 Aug 1, 2023
1166db3
feat(light_client_recursive): originator and nextHeaderHashNum are pr…
stefan-nikolov96 Aug 1, 2023
0a4e13e
feat(circom): hash_tree_root_poseidon
stefan-nikolov96 Aug 3, 2023
cb27716
WIP Poseidon RLC Hash
stefan-nikolov96 Sep 10, 2023
e77977d
feat(circom): Add hash_verifier_poseidon
stefan-nikolov96 Sep 10, 2023
3e3a204
feat(circom): Added counters and final commitment torecursive_light_c…
stefan-nikolov96 Sep 10, 2023
e21c3d6
test(circom): Add hash_tree_root_poseidon tests
stefan-nikolov96 Sep 11, 2023
31cbf71
WIP yarn format
stefan-nikolov96 Sep 13, 2023
7c48b57
WIP rename historicSyncCommitteeHashTreeRoot
stefan-nikolov96 Sep 13, 2023
4241bb0
WIP formatting
stefan-nikolov96 Sep 13, 2023
bd9e685
WIP counters circuits
stefan-nikolov96 Sep 15, 2023
93e9c0e
WIP correct prevCommitment
stefan-nikolov96 Sep 20, 2023
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
4 changes: 2 additions & 2 deletions beacon-light-client/circom/circuits/compute_domain.circom
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ include "hash_two.circom";

template ComputeDomain() {
signal input fork_version[32];
signal output domain[256];

signal input GENESIS_VALIDATORS_ROOT[256];
signal input DOMAIN_SYNC_COMMITTEE[32];

signal output domain[256];

signal concated_fork_version[256];

for(var i = 0; i < 32; i++) {
Expand Down
29 changes: 29 additions & 0 deletions beacon-light-client/circom/circuits/hash_tree_root_poseidon.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma circom 2.1.5;

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

template HashTreeRootPoseidon(N) {
signal input leaves[N];
signal output out;

component hashers[N - 1];

for(var i = 0; i < N - 1; i++) {
hashers[i] = Poseidon(2);
}

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

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

k++;
}

out <== hashers[N - 2].out;
}
96 changes: 96 additions & 0 deletions beacon-light-client/circom/circuits/hash_verifier_poseidon.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
pragma circom 2.1.5;

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

template VerifierPoseidon(pubInpCount, k) {
signal input originator[2];
signal input nextHeaderHashNum[2];
signal input historicSyncCommitteeHashTreeRoot;
signal input syncCommitteeHistoricParticipationIndex;

Copy link
Collaborator

Choose a reason for hiding this comment

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

the domain should also be part of the commitment

// Verification Key
signal input negalfa1xbeta2[6][2][k]; // e(-alfa1, beta2)
signal input gamma2[2][2][k];
signal input delta2[2][2][k];
signal input IC[pubInpCount+1][2][k];
signal input domain[256];

signal output out;

var negalfa1xbeta2_index = 6 * 2 * k;
var gamma2_index = 2 * 2 * k;
var delta2_index = 2 * 2 * k;
var IC_index = (pubInpCount + 1) * 2 * k;

var cummulative_index = 0;

var commitment_size = 2 + 2 + 1 + negalfa1xbeta2_index + gamma2_index + delta2_index + IC_index;

component commitment = HashTreeRootPoseidon(185);

for (var i = 0; i < 6; i++) {
for (var j = 0; j < 2; j++) {
for (var q = 0; q < k; q++) {
commitment.in[cummulative_index + i*2*k + j*k + q] <== negalfa1xbeta2[i][j][q];
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add an empty line after the bracket


cummulative_index += 6 * 2 * k;

for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
for (var q = 0; q < k; q++) {
commitment.in[cummulative_index + i*2*k + j*k + q] <== gamma2[i][j][q];
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add an empty line after the bracket


cummulative_index += 2 * 2 * k;

for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
for (var q = 0; q < k; q++) {
commitment.in[cummulative_index + i*2*k + j*k + q] <== delta2[i][j][q];
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add empty line


cummulative_index += 2 * 2 * k;

for (var i = 0; i < pubInpCount + 1; i++) {
for (var j = 0; j < 2; j++) {
for (var q = 0; q < k; q++) {
commitment.in[cummulative_index + i*2*k + j*k + q] <== IC[i][j][q];
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add an empty line after the bracket


cummulative_index += (pubInpCount + 1)*2*k;

for (var i = 0; i < 2; i++) {
commitment.in[cummulative_index + i] <== originator[i];
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add an empty line after the bracket


cummulative_index += 2;

for (var i = 0; i < 2; i++) {
commitment.in[cummulative_index + i] <== nextHeaderHashNum[i];
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add an empty line after the bracket


cummulative_index += 2;

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

cummulative_index += 256;

commitment.in[cummulative_index] <== historicSyncCommitteeHashTreeRoot;

cummulative_index += 1;

commitment.in[cummulative_index] <== syncCommitteeHistoricParticipationIndex;

out <== commitment.out;
}
76 changes: 54 additions & 22 deletions beacon-light-client/circom/circuits/light_client_recursive.circom
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should extend the recursive verification code starting at line 214. Specifically the public inputs part starting line 246.
The public input should now be the prevVerifierCommitment, which you should not get as input to the circuit but calculate it using the originator, prevHeaderHashNum as nextHeaderHashNum as this is the commitment to the previous proof. And using the historicParticipationRateHashTreeRoot which should be calculated somehow using the historicParticipation (omitting the new one and somehow remembering the past one)

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pragma circom 2.1.5;

include "hash_tree_root.circom";
include "hash_verifier_poseidon.circom";
include "sync_committee_historic_participation.circom"
include "sync_commitee_hash_tree_root.circom";
include "compress.circom";
include "aggregate_bitmask.circom";
include "is_supermajority.circom";
Expand All @@ -14,17 +16,20 @@ include "../../../vendor/circom-pairing/circuits/bls_signature.circom";
include "../../../vendor/circom-pairing/circuits/bn254/groth16.circom";

template LightClientRecursive(N, K) {
var pubInpCount = 4;
var pubInpCount = 1;
var PERIODS = 1024;

// BN254 facts
var k = 6;

// public inputs
signal input originator[2];
signal input nextHeaderHashNum[2];
signal input syncCommitteeHistoricParticipation[PERIODS];

// private inputs
signal input prevHeaderHashNum[2];
signal input syncCommitteeHistoricParticipationIndex;

// verification key
signal input negalfa1xbeta2[6][2][k]; // e(-alfa1, beta2)
Expand All @@ -43,7 +48,10 @@ template LightClientRecursive(N, K) {
signal input state_root[256];
signal input body_root[256];

// Exposed as public via domain
signal input fork_version[32];
signal input GENESIS_VALIDATORS_ROOT[256];
signal input DOMAIN_SYNC_COMMITTEE[32];

signal input points[N][2][K];
signal input aggregatedKey[384];
Expand All @@ -52,6 +60,8 @@ template LightClientRecursive(N, K) {
signal input bitmask[N];
signal input signature[2][2][K];

signal output out; // Poseidon Hash of inputs & verification key

var prevHeaderHash[256];
var nextHeaderHash[256];

Expand Down Expand Up @@ -89,8 +99,6 @@ template LightClientRecursive(N, K) {
isSuperMajority.bitmask[i] <== bitmask[i];
}

isSuperMajority.out === 1;

component hash_tree_root_beacon = HashTreeRootBeaconHeader();

for(var i = 0; i < 256; i++) {
Expand All @@ -114,32 +122,30 @@ template LightClientRecursive(N, K) {
}

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

component computeDomain = ComputeDomain();

for(var i = 0; i < 32; i++) {
computeDomain.fork_version[i] <== fork_version[i];
}
computeDomain.fork_version <== fork_version;
stefan-nikolov96 marked this conversation as resolved.
Show resolved Hide resolved
computeDomain.GENESIS_VALIDATORS_ROOT <== GENESIS_VALIDATORS_ROOT;
computeDomain.DOMAIN_SYNC_COMMITTEE <== DOMAIN_SYNC_COMMITTEE;

component computeSigningRoot = ComputeSigningRoot();

for(var i = 0; i < 256; i++) {
computeSigningRoot.headerHash[i] <== nextHeaderHash[i];
}
computeSigningRoot.domain <== computeDomain.domain;

for(var i = 0; i < 256; i++) {
computeSigningRoot.domain[i] <== computeDomain.domain[i];
computeSigningRoot.headerHash[i] <== nextHeaderHash[i];
}

component hashToField = HashToField();
component hashToField = HashToField(K);

for(var i = 0; i < 256; i++) {
hashToField.in[i] <== computeSigningRoot.signing_root[i];
}

component hasher = HashTreeRoot(N);
component hasher = SyncCommiteeHashTreeRoot(N);
component compress[N];

for(var i = 0; i < N; i++) {
Expand Down Expand Up @@ -178,9 +184,7 @@ template LightClientRecursive(N, K) {

isValidMerkleBranch.index <== 55;

isValidMerkleBranch.out === 1;

component aggregateKeys = AggregateKeysBitmask(N);
component aggregateKeys = AggregateKeysBitmask(N,K);

for(var i = 0; i < N; i++) {
for(var j = 0; j < 2; j++) {
Expand Down Expand Up @@ -212,7 +216,7 @@ template LightClientRecursive(N, K) {
}

// check recursive snark
component groth16Verifier = verifyProof(pubInpCount);
component groth16Verifier = verifyProof(1);
for (var i = 0;i < 6;i++) {
for (var j = 0;j < 2;j++) {
for (var idx = 0;idx < k;idx++) {
Expand Down Expand Up @@ -243,11 +247,30 @@ template LightClientRecursive(N, K) {
}
}

groth16Verifier.pubInput[0] <== originator[0];
groth16Verifier.pubInput[1] <== originator[1];
groth16Verifier.pubInput[2] <== prevHeaderHashNum[0];
groth16Verifier.pubInput[3] <== prevHeaderHashNum[1];
component prevHistoricParticipationRateHashTreeRoot = HashTreeRootPoseidon(PERIODS) (
syncCommitteeHistoricParticipation
);

signal prevSyncCommitteeHistoricParticipationIndex <== syncCommitteeHistoricParticipationIndex - 1;

signal prevVerifierCommitment <== VerifierPoseidon(pubInpCount, k)(
originator, prevHeaderHashNum, negalfa1xbeta2,
gamma2, delta2, IC,
historicParticipationRateHashTreeRoot, prevSyncCommitteeHistoricParticipationIndex, computeDomain.domain
);

component updateSyncCommitteeHistoricParticipation = UpdateSyncCommitteeHistoricParticipation(512,PERIODS) (
syncCommitteeHistoricParticipation, syncCommitteeHistoricParticipationIndex, bitmask
);

signal curSyncCommitteeHistoricParticipation <== updateSyncCommitteeHistoricParticipation.out;

component curHistoricParticipationRateHashTreeRoot = HashTreeRootPoseidon(PERIODS) (
curSyncCommitteeHistoricParticipation
);

groth16Verifier.pubInput[0] <== prevVerifierCommitment;

component isFirst = IsFirst();

isFirst.firstHash[0] <== originator[0];
Expand All @@ -260,4 +283,13 @@ template LightClientRecursive(N, K) {
firstORcorrect.b <== groth16Verifier.out;

firstORcorrect.out === 1;

component verifierPoseidon = VerifierPoseidon(pubInpCount, k) (
originator, nextHeaderHashNum, negalfa1xbeta2,
gamma2, delta2, IC,
curHistoricParticipationRateHashTreeRoot, syncCommitteeHistoricParticipationIndex, compute_domain.domain
);

prevVerifierCommitment === verifierPoseidon.out;
out <== verifierPoseidon.out;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pragma circom 2.1.5;

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

template UpdateSyncCommitteeHistoricParticipation(N, PERIODS) {
signal input participationRateArray[PERIODS];
signal input currentIndex;
signal input bitmask[N];

signal output out[PERIODS];

var participationRate = 0;
for (var i=0;i<N;i++) {
participationRate += bitmask[i];
}

//Constrain
signal isValidIndex <== LessThan(32)([currentIndex, PERIODS]);
isValidIndex === 1;

component isZero[PERIODS];
for (var i=currentIndex;i<PERIODS;i++) {
isZero[i] = IsZero();
isZero[i].in <== participationRateArray[i];
isZero[i].out === 1;
}

for (var i=0;i<currentIndex;i++) {
isZero[i] = IsZero();
isZero[i].in <== participationRateArray[i];
isZero[i].out === 0;
}

//Calc. new entry
var bitmask_sum = 0;
for (var i=0;i<N;i++) {
bitmask_sum += bitmask[i];
}

// Assign
for (var i=0;i<currentIndex;i++) {
out[i] <== participationRateArray[i];
}

out[currentIndex] <== bitmask_sum;

for (var i=currentIndex + 1;i<PERIODS;i++) {
out[i] <== 0;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

PHASE1=../../../../../../pot28_final.ptau
PHASE1=/storage/pot28_final.ptau
CIRCUIT_NAME=light_client_recursive
BUILD_DIR=../../build/"$CIRCUIT_NAME"

Expand All @@ -21,7 +21,7 @@ echo $PWD
echo "****COMPILING CIRCUIT****"
start=`date +%s`
#circom "$CIRCUIT_NAME".circom --O0 --c --output "$BUILD_DIR"
circom "$CIRCUIT_NAME".circom --O1 --r1cs --sym --c --output "$BUILD_DIR"
circom "$CIRCUIT_NAME".circom --O2 --r1cs --sym --c --output "$BUILD_DIR"
end=`date +%s`
echo "DONE ($((end-start))s)"

Expand Down
Loading
Loading