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

Addition of gates, and poseidon bn254 #32

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
b7dd73a
add poseidon2 goldilocks
utkarsh-21st Dec 17, 2023
34afaf3
optimize reduction
utkarsh-21st Jan 9, 2024
736ad0c
add goldilocks algebra extension
utkarsh-21st Dec 18, 2023
444d3ec
fix goldilocks algebra
utkarsh-21st Dec 19, 2023
72d12ad
add arithmetic extension gate
utkarsh-21st Dec 19, 2023
91789c6
fix reduce bits in arithmetic gate
utkarsh-21st Dec 20, 2023
f511e43
add some goldilocks functions
utkarsh-21st Dec 21, 2023
ad75b9f
fix reduce bits in arithmetic extension
utkarsh-21st Dec 25, 2023
17591c6
refactor
utkarsh-21st Jan 9, 2024
c3f3874
optimize algebra; reevaluate bits in a in arithmetic extension gate
utkarsh-21st Jan 9, 2024
752d861
fix reduction bits
utkarsh-21st Jan 9, 2024
3bf5ef0
add coset interpolation gate
utkarsh-21st Dec 21, 2023
cb0386b
reve function export
utkarsh-21st Dec 22, 2023
a9a8bbe
fix reduce bits
utkarsh-21st Dec 26, 2023
9130252
refactor CosetInteroplationGate
utkarsh-21st Dec 26, 2023
77872e3
fix reduction bits in coset interpolation gate
utkarsh-21st Jan 20, 2024
d9855fa
add base_sum gate
utkarsh-21st Dec 24, 2023
fc5c9f0
add exponentiation gate
utkarsh-21st Dec 24, 2023
d1488e6
fix reduction in exponentiation
utkarsh-21st Jan 21, 2024
0a6caa9
add lookup gate
utkarsh-21st Dec 25, 2023
0214f40
add lookup_table gate
utkarsh-21st Dec 25, 2023
b7c2a03
add multiplication extension gate
utkarsh-21st Dec 25, 2023
7c38bc7
fix reduction bits in MulExtension gate
utkarsh-21st Jan 21, 2024
777ed6e
add noop gate
utkarsh-21st Dec 25, 2023
7aa8d7a
add random access gate
utkarsh-21st Dec 26, 2023
90bcf9b
fix reduction bits in ranom access gate
utkarsh-21st Jan 21, 2024
fc1a02a
add reducing gate
utkarsh-21st Dec 26, 2023
280e884
add reducing extension gate
utkarsh-21st Dec 26, 2023
b95016c
add poseidon mds gate
utkarsh-21st Dec 26, 2023
4308005
add u32 comparison gate
utkarsh-21st Jan 5, 2024
517a9ef
add U32AddManyGate
utkarsh-21st Jan 5, 2024
3cacfc0
add U32ArithmeticGate
utkarsh-21st Jan 7, 2024
2ccd438
fix SelectHashOutRecursive and SelectGoldilocksExt2Recursive
utkarsh-21st Jan 8, 2024
0a4a426
script to export solidity verifier
utkarsh-21st Jan 8, 2024
89ef7cb
add poseidon bn254 hash
utkarsh-21st Jan 11, 2024
afee10b
refactor poseidon
utkarsh-21st Jan 11, 2024
5c8d1e1
add poseidon bn254 hasher
utkarsh-21st Jan 11, 2024
c6d431c
fix goldilocks elements to bn254 conversion
utkarsh-21st Jan 12, 2024
32724a7
integrate poseidon bn254 hasher
utkarsh-21st Jan 14, 2024
79a705b
fix PoseidonBn254HashOut ToVec function
utkarsh-21st Jan 16, 2024
0e97f7b
add plonk backend scripts
utkarsh-21st Jan 21, 2024
fa5d188
update fri test
utkarsh-21st Jan 21, 2024
26f700d
some fixes
utkarsh-21st Jan 21, 2024
4110138
derive gnark public inputs from ponky2 public inputs
utkarsh-21st Jan 21, 2024
2c88220
sync
adityashukla700 Jan 21, 2024
84a41bc
move proof testdata
utkarsh-21st Jan 21, 2024
e3e1f43
update number of plonky2 pub inputs
utkarsh-21st Jan 28, 2024
328fd64
add U32Interleave gate
utkarsh-21st Jan 28, 2024
047a364
add UninterleaveToU32 gate
utkarsh-21st Jan 29, 2024
290e9a8
add UninterleaveToB32 gate
utkarsh-21st Jan 29, 2024
12860a8
Merge branch 'plonk_backend' into tendermint
utkarsh-21st Jan 29, 2024
97d8126
add U2Subtraction gate
utkarsh-21st Jan 29, 2024
0f77159
Merge branch 'plonk_backend' into tendermint
utkarsh-21st Jan 29, 2024
3efb14c
update gnark public inputs
utkarsh-21st Feb 8, 2024
cef7903
export build and prove functions in C ABI
utkarsh-21st Feb 12, 2024
8ad79a7
return serialized proof in hex in function GeneratePlonkProof
utkarsh-21st Feb 13, 2024
c3ea36f
export solidity verifier and update .gitignore
utkarsh-21st Feb 13, 2024
218f054
add TestBuildPlonkCircuit
utkarsh-21st Feb 13, 2024
285bbf4
add main.so file
utkarsh-21st Feb 14, 2024
aaa41c1
update public inputs calculation
utkarsh-21st Feb 20, 2024
1b448ae
update .so file
utkarsh-21st Feb 20, 2024
c743d08
return proofGenTime in FFI
utkarsh-21st Feb 22, 2024
2a3914d
return proofGenTme in msg
utkarsh-21st Feb 23, 2024
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
main
data
data
data_write/**/*.sol
data_write/**/*.bin
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
Wraps up plonky2 verifier as a groth16 circuit

# TODOS
- [ ] Implement constraints for rest of the plonky2 gates
- [ ] ArithmeticExtensionGate
- [ ] BaseSumGate
- [ ] CosetInterpolationGate
- [ ] ExponentiationGate
- [ ] LookupGate
- [ ] LookupTableGate
- [ ] MulExtensionGate
- [ ] NoopGate
- [ ] PoseidonMdsGate
- [ ] RandomAccessGate
- [ ] ReducingGate
- [ ] ReducingExtensionGate
- [x] Implement constraints for rest of the plonky2 gates
- [x] ArithmeticExtensionGate
- [x] BaseSumGate
- [x] CosetInterpolationGate
- [x] ExponentiationGate
- [x] LookupGate
- [x] LookupTableGate
- [x] MulExtensionGate
- [x] NoopGate
- [x] PoseidonMdsGate
- [x] RandomAccessGate
- [x] ReducingGate
- [x] ReducingExtensionGate
- [ ] Implement constraints for lookups in vanishing polynomial evaluation
- [ ] Use poseidon over BN254 scalar field rather than goldilocks field; it will reduce constraints vastly. Also implement the corresponding config for plonky2 prover
- [x] Use poseidon over BN254 scalar field rather than goldilocks field; it will reduce constraints vastly. Also implement the corresponding config for plonky2 prover
30 changes: 30 additions & 0 deletions build_plonk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
go build main.go
go install
# plonky2-groth16-verifier buildPlonk --common_data ./data/tendermint/common_data_struct.json
# go run -tags=debug main.go buildPlonk --common_data ./data/tendermint/common_data_struct.json

# plonky2-groth16-verifier provePlonk --plonky2_proof_path ./data/tendermint/proof_with_pis_struct.json --verifier_only_path ./data/tendermint/verifier_only_struct.json --plonky2_public_inputs_path ./data/tendermint/plonky2_pub_inputs_struct.json --gnark_public_inputs_path ./data/tendermint/gnark_pub_inputs_struct.json --proving_key_path ./data/pk.bin --r1cs_path ./data/r1cs.bin

# plonky2-groth16-verifier verifyPlonk --plonkProofPath ./data/proofP --vkey_path ./data/vk.bin --gnark_public_inputs_path ./data/tendermint/gnark_pub_inputs_struct.json

# plonky2-groth16-verifier exportSolPlonk --vkey_path ./data/vk.bin

# osmosis [without recursion]
# *** results [before adding sha]***
# [plonk backend]
# number of constraints: 20669098
# proving time: 156349.328648
# gas usage: 307932

# osmosis [without recursion]
# *** results [with sha]***
# number of constraints: 26690559
# proving time: 167878.632074
# gas usage:


# aggregated [osmosis]
# [plonk backend]
# number of constraints: 18294271
# proving time: 145921.137651
# gas usage: 307932
2 changes: 2 additions & 0 deletions build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ plonky2-groth16-verifier build --common_data ./data/goldilocks/common_data.json
plonky2-groth16-verifier prove --plonky2_proof_path ./data/goldilocks/proof_with_pis.json --verifier_only_path ./data/goldilocks/verifier_only.json --public_inputs_path ./data/goldilocks/pub_inputs.json --proving_key_path ./data/pk.bin --r1cs_path ./data/r1cs.bin --vk_path ./data/vk.bin

plonky2-groth16-verifier verify --groth16_proof_path ./data/g16p --vkey_path ./data/vk.bin --pub_inputs_path ./data/goldilocks/pub_inputs.json

plonky2-groth16-verifier exportSol --vkey_path ./data/vk.bin
82 changes: 73 additions & 9 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@ package cmd

import (
"fmt"
"math"
"math/big"
"os"

"github.com/Electron-Labs/plonky2-groth16-verifier/verifier"
"github.com/consensys/gnark-crypto/ecc"
kzg_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/kzg"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/spf13/cobra"
)

var common_data_path string

// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
// buildGroth16Cmd represents the buildGroth16
var buildGroth16Cmd = &cobra.Command{
Use: "buildGroth16",
Short: "Build gnark groth16 circuit",
Long: `Builds gnark groth16 circuit corresponding to provided common_data and plonky2 config.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("build called:\n common data: %s\n ", common_data_path)

common_data, err := read_common_data_from_file(common_data_path)
common_data, err := ReadCommonDataFromFile(common_data_path)
if err != nil {
fmt.Println("Failed to read common data file:", err)
os.Exit(1)
}
circuitConstraints := getCircuitConstants(common_data)
circuitConstants := GetCircuitConstants(common_data)

var myCircuit verifier.Runner

// Arrays are resized according to circuitConstants before compiling
myCircuit.Make(circuitConstraints, common_data)
myCircuit.Make(circuitConstants, common_data)

r1cs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &myCircuit)
pk, vk, _ := groth16.Setup(r1cs)
Expand Down Expand Up @@ -63,8 +68,67 @@ var buildCmd = &cobra.Command{
},
}

// buildPlonkCmd represents the buildPlonk
var buildPlonkCmd = &cobra.Command{
Use: "buildPlonk",
Short: "Build gnark plonk circuit",
Long: `Builds gnark plonk circuit corresponding to provided common_data and plonky2 config.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("build called:\n common data: %s\n ", common_data_path)

common_data, err := ReadCommonDataFromFile(common_data_path)
if err != nil {
fmt.Println("Failed to read common data file:", err)
os.Exit(1)
}
circuitConstants := GetCircuitConstants(common_data)

var myCircuit verifier.Runner

// Arrays are resized according to circuitConstants before compiling
myCircuit.Make(circuitConstants, common_data)

ccs, _ := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &myCircuit)
// r1cs := ccs.(*cs.SparseR1CS)
// scs := r1cs.(*cs.SparseR1CS)
// srs, err := test.NewKZGSRS(scs)
srs, err := kzg_bn254.NewSRS(uint64(math.Pow(2, 28)), big.NewInt(-1))
if err != nil {
panic(err)
}
// pk, vk, _ := plonk.Setup(r1cs, srs)
pk, vk, _ := plonk.Setup(ccs, srs)

f_r1cs, err := os.Create("data/r1cs.bin")
if err != nil {
fmt.Println("Failed to create r1cs file:", err)
os.Exit(1)
}
// r1cs.WriteTo(f_r1cs)
ccs.WriteTo(f_r1cs)

f_pd, _ := os.Create("data/pk.bin")
if err != nil {
fmt.Println("Failed to create pk file:", err)
os.Exit(1)
}
pk.WriteTo(f_pd)

f_vk, err := os.Create("data/vk.bin")
if err != nil {
fmt.Println("Failed to create vk file:", err)
os.Exit(1)
}
vk.WriteTo(f_vk)
},
}

func init() {
buildCmd.Flags().StringVarP(&common_data_path, "common_data", "d", "", "JSON File path to common data of plonky2 circuit")
_ = buildCmd.MarkFlagRequired("common_data")
rootCmd.AddCommand(buildCmd)
buildGroth16Cmd.Flags().StringVarP(&common_data_path, "common_data", "d", "", "JSON File path to common data of plonky2 circuit")
_ = buildGroth16Cmd.MarkFlagRequired("common_data")
rootCmd.AddCommand(buildGroth16Cmd)

buildPlonkCmd.Flags().StringVarP(&common_data_path, "common_data", "d", "", "JSON File path to common data of plonky2 circuit")
_ = buildPlonkCmd.MarkFlagRequired("common_data")
rootCmd.AddCommand(buildPlonkCmd)
}
85 changes: 85 additions & 0 deletions cmd/export_solidity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"os"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/plonk"
"github.com/spf13/cobra"
)

// exportSolGroth16Cmd represents the exportSolGroth16 command
var exportSolGroth16Cmd = &cobra.Command{
Use: "exportSolGroth16",
Short: "Exports Solidity contract for groth16 backend",
Long: `Exports VerifyingKey as a Solidity contract for groth16 backend`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("export_solidity called:\n vkey: %s\n ", vkey_path)

vk := groth16.NewVerifyingKey(ecc.BN254)
vkFile, err := os.Open(vkey_path)
if err != nil {
fmt.Println("vkFile open wrong: ", err)
os.Exit(1)
}
vk.ReadFrom(vkFile)

f_sol, err := os.Create("data/Verifier.sol")
if err != nil {
fmt.Println("Failed to create Verifier.sol file:", err)
os.Exit(1)
}
err = vk.ExportSolidity(f_sol)
if err != nil {
fmt.Println("Failed to export Solidity Verifier")
os.Exit(1)
} else {
fmt.Println("Exported succesfully")
}
},
}

var exportSolPlonkCmd = &cobra.Command{
Use: "exportSolPlonk",
Short: "Exports Solidity contract for groth16 backend",
Long: `Exports VerifyingKey as a Solidity contract for plonk backend`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("export_solidity called:\n vkey: %s\n ", vkey_path)

vk := plonk.NewVerifyingKey(ecc.BN254)
vkFile, err := os.Open(vkey_path)
if err != nil {
fmt.Println("vkFile open wrong: ", err)
os.Exit(1)
}
vk.ReadFrom(vkFile)

f_sol, err := os.Create("data/Verifier.sol")
if err != nil {
fmt.Println("Failed to create Verifier.sol file:", err)
os.Exit(1)
}
err = vk.ExportSolidity(f_sol)
if err != nil {
fmt.Println("Failed to export Solidity Verifier")
os.Exit(1)
} else {
fmt.Println("Exported succesfully")
}
},
}

func init() {
exportSolGroth16Cmd.Flags().StringVarP(&vkey_path, "vkey_path", "v", "", "path to the vk.bin file")
_ = exportSolGroth16Cmd.MarkFlagRequired("vkey")
rootCmd.AddCommand(exportSolGroth16Cmd)

exportSolPlonkCmd.Flags().StringVarP(&vkey_path, "vkey_path", "v", "", "path to the vk.bin file")
_ = exportSolPlonkCmd.MarkFlagRequired("vkey")
rootCmd.AddCommand(exportSolPlonkCmd)
}
Loading