Skip to content

Commit

Permalink
feat(galois): more tests and optimized cofactor clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Jan 11, 2024
1 parent eb58876 commit a009e22
Show file tree
Hide file tree
Showing 19 changed files with 516 additions and 548 deletions.
52 changes: 26 additions & 26 deletions evm/contracts/clients/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,65 +47,65 @@ contract Verifier is IZKVerifierV2 {

// Groth16 alpha point in G1
uint256 constant ALPHA_X =
4545497642472568342690310908405747139240036052727362141781732401147427278332;
19681918582342826141927615585844819827950494091197079841581098590160509489088;
uint256 constant ALPHA_Y =
21657279939237288836606942458710668831714385968774740438494149333436465843139;
18976290249472753264792873488771466812990993964894861063003977176791880491271;

// Groth16 beta point in G2 in powers of i
uint256 constant BETA_NEG_X_0 =
13700154589878825236434612482502516805936642904411498786936854624077294311682;
17542740552152507448113209307107151415915067720344615090625491194497459342657;
uint256 constant BETA_NEG_X_1 =
7497643146587701237207141457042187540104153076302400103164161194096334760677;
7391419840357209888406550113304609596117324320456425532340186750677647200951;
uint256 constant BETA_NEG_Y_0 =
12061640834087756293066688745856043387269947926655334556036641601689450213892;
19792144694189938307894275195643141100067567927017820917795773938883367365412;
uint256 constant BETA_NEG_Y_1 =
21393283571400157993861538207686485746031497872415111298623015303717243066096;
17211163405892785765064384698545439693511041026525583483293852246253094999960;

// Groth16 gamma point in G2 in powers of i
uint256 constant GAMMA_NEG_X_0 =
20107534645331006032402749367045367765170696291609897560802407293332329737698;
18606218405301761142065379515313210013062685838824185304765852768028043703753;
uint256 constant GAMMA_NEG_X_1 =
6135886662735635672007238208825068442340242201492563368708252608220727995665;
14540190418613230568675456016157166803361906410442369269514923787931816842661;
uint256 constant GAMMA_NEG_Y_0 =
21304025422358833441536274892652379608221995762381190373024977840945920102186;
8951288781929330311740771353182492599878185290969923949343519917796557268219;
uint256 constant GAMMA_NEG_Y_1 =
4753268754089710768567929407828664634165004523165700048765343123172353157016;
8812741715039891617796654796990655544152364726104502685064434338923152748332;

// Groth16 delta point in G2 in powers of i
uint256 constant DELTA_NEG_X_0 =
7466991077765871589299219136524534381311757366195842209075383099119159267653;
4060446808760699692477462845230990229944734548192291022910719993807902355759;
uint256 constant DELTA_NEG_X_1 =
3993057849766236546786517975621342624904647686274232418256214891442175004595;
17803970575871171031178686612122420011629668206026599803865929512658387807614;
uint256 constant DELTA_NEG_Y_0 =
4828611495163838268492412020246640239075991252847184258785137740314670688312;
17124643930680839105590130418783735638684807807530957846654257326870483890070;
uint256 constant DELTA_NEG_Y_1 =
7912615802333993426639034373142603363982203530625133012030570299570446825498;
7629814864078422326695261874116750521593226108645261491403040533513921542483;

// Constant and public input points
uint256 constant CONSTANT_X =
12730996440491230341898748596203954698739716661771354666644872057102948394726;
18676861125246766292059080199576268981667767278300819763274799276376054409743;
uint256 constant CONSTANT_Y =
18188119481706424113895919492547204030227563509791341513627568384483237465563;
5269797328666185490526867808814966151140271775451395274640052553630677159076;
uint256 constant PUB_0_X =
8627654005047498327557788753897980447566216968617518507065934795873759856303;
3010349418202885908760025883515590778403141726894708222433169071368055690912;
uint256 constant PUB_0_Y =
7258461021217822820323520100501249447378191264854934186351306877513723742793;
20724571387755619214201948546999886629454427058875835531981815961969686023639;
uint256 constant PUB_1_X =
10867392565326439682947570558412590838055450106691458097719409041212951853401;
1718980496599153571806495443921791801530740535933073284474040850386158191735;
uint256 constant PUB_1_Y =
3124325152732842906431467328196929469314595151752342394843391644384931489602;
3288376032837046783397899352143814445169932711782482341330476711768756263890;
uint256 constant PUB_2_X =
6627862564104432829412837659942319893523740327889349003623985834967392523238;
9266521894078168597926726825960443668976816125222306871429246198851182099011;
uint256 constant PUB_2_Y =
11980409132042083280769458186828234442115366931894286356450034429211995205398;
9416966066664703605394453818829209487654794520205974695819389893969431707374;
uint256 constant PUB_3_X =
8352580944529539453233628007042528490297057973561012318225452772905637057834;
13194582768609510874189454527180276310818912484460263820189470814556014162264;
uint256 constant PUB_3_Y =
16521805616951802411915576898364661283847250025318378340431083135006258712933;
15983647339013447433771242507224193645257463334651420839328305715367829062538;
uint256 constant PUB_4_X =
12071952363228031783312741175393664539881674330807724365734090335572247236031;
13160686484300787492313686811371534896624215839999346591796239441200125629208;
uint256 constant PUB_4_Y =
15697249904809157640137081638559691717147113859496833342722786814178099529209;
11709584278193617231017776985640196897412209200566866495381859539145549732339;

/// Compute the public input linear combination.
/// @notice Reverts with PublicInputNotInField if the input is not in the field.
Expand Down
8 changes: 4 additions & 4 deletions evm/tests/src/Verifier.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ contract VerifierTests {

function testVerifier_ok() public {
new Verifier().verifyZKP(
0x219BEFB142BED271ACC41A52BA3F412BD1418AED36474A76AEFBFAD12CC6B592,
0x219BEFB142BED271ACC41A52BA3F412BD1418AED36474A76AEFBFAD12CC6B592,
hex"650802113E0200000000000022480A207022627E60ED78120D2FE8DC7ACDB58A2321B0304F8912D2DFB86CE038E23CA812240801122041B8793236EE0980E2EAF1A2FAD268C4A3D8979A0C432F06E284EEC5E74DD69C320E756E696F6E2D6465766E65742D31",
hex"170904B6B6F8E61E6E2D357436C7C3671E0ADE5ECF97E7A7A644F4B9F4DCC865138AAB8106303EFD304171C3B1E5181E4772AE47A050E19D9074BFD59072EE971FAC59DE603A8E7A677C4B55BD96FB62566CABD242D787687A86820E7ECD270407748CFCC9A3C646DD773D06AF98AB4ACBDF0FD76069212ABFF40C73C1FE0017073337A045A075449995D9B64417D017B670950E6C4F0BE8D8DA1AAFC41C3F4F0DCC256FD3B003EC5FC4A1A8BF7638FE2F703322DD418CC05E2EABC2A5801EA70CBC6020CA6DE288936436764093B0B18F815382DCDD75CEDC5B9D2B366478681C3B99E0A7F21D90D856C8AA59693A800A144D730737C3FD3E21B5DEBD1FC2B228516CBE9584853821FAE64F1C903936C07392529A5FB430007B3EB94C7EEB2F26A8A6B5E8474E4C283A2FD7A9DAD63A78230696F6D882D654725E28198653902B1CECF46DC9C200AEB9DB432A8ACA40EECD8CBA27C32975F1B338B7EFFC2CBA"
0x0472116C575F0FECF44ED4F91C34E9E7B67CE8C911FC67F304C2B804330B61F0,
0x0472116C575F0FECF44ED4F91C34E9E7B67CE8C911FC67F304C2B804330B61F0,
hex"650802113E0200000000000022480A20DFAD1A5E2BB2B94BD7ED5F4F85199E0DDD95FB4687CFBF19B36865845BD16E20122408011220E32B1FA520CE4F9D0C1A2C80D51FB1F09B9C241101BE70D5CE0DC0F11B009863320E756E696F6E2D6465766E65742D31",
hex"19EB187C4EA70DA41CE1C2E9C81D7F4ACC9B8CD85A98AD59A64BD9D820A1D4E41DA150358EC5AFCBA7407D66AFABD59B1B92095D6A797572FAE6FE9C9A500E7C284C0AF8310181C072C1002961BE007DFC68DFDBD9CCF930410153829DA0E00D20ECDCE1DF5674D69A70BFB0891CE61888AADC6B362E27C6E4C9D1905ED327402EEE2EB635235068CBCBAB909D9A14A8992E74DE17D69269FC60CC448A832F8E1BCA6C3426A79029508AEE21DBB718F016FEC33A0A500EA14A05E3E19548634F04011075515FF876980559B120DC247A190BF699C2BA19ECDA92220AADAF3F5A1D502B8552BCC4BBDBA3C53EA39FAA5D6A7D23E06AE8F52AEECCED971D7D353613F7C750995DDD6BCCBB3E3185CF5B53EADAA6FA412665A0910FF709E49CFDFB0530B479EE5F0774588EE7397F7E050A9F4C87DB7989E2557E76353BF98CF7B922EB5DBE6D66AEA64C33AF7D6469BF3EFD5ACA10936CED35DAE692D489AAF8C2"
);
}
}
29 changes: 19 additions & 10 deletions galoisd/cmd/galoisd/cmd/example_prove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package cmd

import (
"context"
"encoding/hex"
"crypto/rand"
"fmt"
provergrpc "galois/grpc/api/v1"
"math/big"
"math/rand"
"strconv"

"cosmossdk.io/math"
Expand Down Expand Up @@ -37,18 +36,24 @@ func ExampleProveCmd() *cobra.Command {
if err != nil {
return &types.SimpleValidator{}, err
}
power, err := rand.Int(rand.Reader, big.NewInt(9223372036854775807/8))
if err != nil {
return &types.SimpleValidator{}, err
}
return &types.SimpleValidator{
PubKey: &protoPK,
VotingPower: sdk.TokensToConsensusPower(math.NewInt(rand.Int63n(9223372036854775807/8)), sdk.DefaultPowerReduction),
VotingPower: sdk.TokensToConsensusPower(math.NewInt(power.Int64()), sdk.DefaultPowerReduction),
}, nil
}

blockHash, err := hex.DecodeString("7022627E60ED78120D2FE8DC7ACDB58A2321B0304F8912D2DFB86CE038E23CA8")
blockHash := make([]byte, 32)
_, err = rand.Read(blockHash)
if err != nil {
return err
}

partSetHeaderHash, err := hex.DecodeString("41B8793236EE0980E2EAF1A2FAD268C4A3D8979A0C432F06E284EEC5E74DD69C")
partSetHeaderHash := make([]byte, 32)
_, err = rand.Read(partSetHeaderHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,11 +98,15 @@ func ExampleProveCmd() *cobra.Command {
if votingPower >= int(totalPower)/3*2 {
break
}
index := rand.Int31n(int32(nbOfValidators))
if bitmap.Bit(int(index)) == 0 {
votingPower += int(validators[index].VotingPower)
bitmap.SetBit(&bitmap, int(index), 1)
sig, err := privKeys[index].Sign(signedBytes)
index, err := rand.Int(rand.Reader, big.NewInt(int64(nbOfValidators)))
if err != nil {
return err
}
i := index.Int64()
if bitmap.Bit(int(i)) == 0 {
votingPower += int(validators[i].VotingPower)
bitmap.SetBit(&bitmap, int(i), 1)
sig, err := privKeys[i].Sign(signedBytes)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions galoisd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ require (
)

replace (
github.com/cometbft/cometbft => github.com/unionlabs/cometbls v0.0.0-20240108104619-5ed53d0e59b9
github.com/cometbft/cometbft => github.com/unionlabs/cometbls v0.0.0-20240111183309-e159e964fa07
github.com/consensys/gnark => github.com/consensys/gnark v0.9.1-0.20231013131835-4ebcccd9c0a8
// Fork of gnark crypto until https://github.com/ConsenSys/gnark-crypto/pull/314 is merged
github.com/consensys/gnark-crypto => github.com/unionlabs/gnark-crypto v0.0.0-20231016072529-15c0507b6578
github.com/cosmos/cosmos-sdk => github.com/unionlabs/cosmos-sdk v0.0.0-20240108104904-d7de88f54965
github.com/cosmos/cosmos-sdk => github.com/unionlabs/cosmos-sdk v0.0.0-20240111183419-b58f90f260ce
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.11
)
8 changes: 4 additions & 4 deletions galoisd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/unionlabs/cometbls v0.0.0-20240108104619-5ed53d0e59b9 h1:geQswI0cfu5sD3wprwn8SkIfnNkAmRoDyuEt7JU7p/4=
github.com/unionlabs/cometbls v0.0.0-20240108104619-5ed53d0e59b9/go.mod h1:3H1gcLEVOQZbPwdH8gyv4UzwHtEawNgcnytglkCQVOQ=
github.com/unionlabs/cosmos-sdk v0.0.0-20240108104904-d7de88f54965 h1:pwK3MTnhxialeoEMpU632IjzY8gu53dFoDHya6sY3A8=
github.com/unionlabs/cosmos-sdk v0.0.0-20240108104904-d7de88f54965/go.mod h1:9QHZ3nex7R4cnZuGd/jmzk5nzsQ+7zxblS5MwshytQw=
github.com/unionlabs/cometbls v0.0.0-20240111183309-e159e964fa07 h1:y2q9sWscXNgo0w/iDdnRID3Fd2s4Mbdcn4poDwwSfpI=
github.com/unionlabs/cometbls v0.0.0-20240111183309-e159e964fa07/go.mod h1:3H1gcLEVOQZbPwdH8gyv4UzwHtEawNgcnytglkCQVOQ=
github.com/unionlabs/cosmos-sdk v0.0.0-20240111183419-b58f90f260ce h1:XsA/6QgQixsGW6CBOFYaK6D/viGcRBXF4THJhhBP2LM=
github.com/unionlabs/cosmos-sdk v0.0.0-20240111183419-b58f90f260ce/go.mod h1:l6W0x49uOfoq2JVMJGE6OqmYtBcUssRCNXlPXEzp3cY=
github.com/unionlabs/gnark-crypto v0.0.0-20231016072529-15c0507b6578 h1:Owzup0XvshGNHgS1s0xUI/mZM+fPvTT7dLg7P0cT2vQ=
github.com/unionlabs/gnark-crypto v0.0.0-20231016072529-15c0507b6578/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
Expand Down
15 changes: 15 additions & 0 deletions galoisd/grpc/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,21 @@ func loadOrCreate(r1csPath string, pkPath string, vkPath string) (cs_bn254.R1CS,
return cs, pk, vk, err
}

log.Printf("VK Alpha X: %v", vk.G1.Alpha.X.String())
log.Printf("VK Alpha Y: %v", vk.G1.Alpha.Y.String())
log.Printf("VK Beta X0: %v", vk.G2.Beta.X.A0.String())
log.Printf("VK Beta X1: %v", vk.G2.Beta.X.A1.String())
log.Printf("VK Beta Y0: %v", vk.G2.Beta.Y.A0.String())
log.Printf("VK Beta Y1: %v", vk.G2.Beta.Y.A1.String())
log.Printf("VK Gamma X0: %v", vk.G2.Gamma.X.A0.String())
log.Printf("VK Gamma X1: %v", vk.G2.Gamma.X.A1.String())
log.Printf("VK Gamma Y0: %v", vk.G2.Gamma.Y.A0.String())
log.Printf("VK Gamma Y1: %v", vk.G2.Gamma.Y.A1.String())
log.Printf("VK Delta X0: %v", vk.G2.Delta.X.A0.String())
log.Printf("VK Delta X1: %v", vk.G2.Delta.X.A1.String())
log.Printf("VK Delta Y0: %v", vk.G2.Delta.Y.A0.String())
log.Printf("VK Delta Y1: %v", vk.G2.Delta.Y.A1.String())

return cs, pk, vk, nil
}
}
Expand Down
Loading

0 comments on commit a009e22

Please sign in to comment.