Skip to content

Commit

Permalink
combined circuits wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Mar 29, 2024
1 parent b679fd3 commit 3fee10b
Show file tree
Hide file tree
Showing 28 changed files with 1,755 additions and 925 deletions.
7 changes: 6 additions & 1 deletion gnark-prover/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ keys = [
"circuits/non-inclusion_26_2.key",
"circuits/non-inclusion_26_3.key",
"circuits/non-inclusion_26_4.key",
"circuits/non-inclusion_26_8.key"
"circuits/non-inclusion_26_8.key",
"circuits/combined_26_1.key",
"circuits/combined_26_2.key",
"circuits/combined_26_3.key",
"circuits/combined_26_4.key",
"circuits/combined_26_8.key",
]
178 changes: 145 additions & 33 deletions gnark-prover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,49 @@ func runCli() {
{
Name: "setup",
Flags: []cli.Flag{
&cli.StringFlag{Name: "circuit", Usage: "Type of circuit (\"inclusion\" / \"non-inclusion\")", Required: true},
&cli.StringFlag{Name: "circuit", Usage: "Type of circuit (\"inclusion\" / \"non-inclusion\" / \"combined\")", Required: true},
&cli.StringFlag{Name: "output", Usage: "Output file", Required: true},
&cli.StringFlag{Name: "output-vkey", Usage: "Output file", Required: true},
&cli.UintFlag{Name: "tree-depth", Usage: "Merkle tree depth", Required: true},
&cli.UintFlag{Name: "utxos", Usage: "Number of Utxos", Required: true},
&cli.UintFlag{Name: "inclusion-tree-depth", Usage: "Merkle tree depth", Required: false},
&cli.UintFlag{Name: "inclusion-utxos", Usage: "Number of Utxos", Required: false},
&cli.UintFlag{Name: "non-inclusion-tree-depth", Usage: "Non-inclusion merkle tree depth", Required: false},
&cli.UintFlag{Name: "non-inclusion-utxos", Usage: "Non-inclusion number of Utxos", Required: false},
},
Action: func(context *cli.Context) error {
circuit := context.String("circuit")
if circuit != "inclusion" && circuit != "non-inclusion" && circuit != "combined" {
return fmt.Errorf("invalid circuit type %s", circuit)
}

path := context.String("output")
path_vkey := context.String("output-vkey")
treeDepth := uint32(context.Uint("tree-depth"))
numberOfUtxos := uint32(context.Uint("utxos"))
inclusionTreeDepth := uint32(context.Uint("inclusion-tree-depth"))
inclusionNumberOfUtxos := uint32(context.Uint("inclusion-utxos"))
nonInclusionTreeDepth := uint32(context.Uint("non-inclusion-tree-depth"))
nonInclusionNumberOfUtxos := uint32(context.Uint("non-inclusion-utxos"))

if (inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0) && circuit == "inclusion" {
return fmt.Errorf("inclusion tree depth and number of utxos must be provided")
}

if (nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0) && circuit == "non-inclusion" {
return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided")
}

if circuit == "combined" {
if inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0 {
return fmt.Errorf("inclusion tree depth and number of utxos must be provided")
}
if nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0 {
return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided")
}
}

logging.Logger().Info().Msg("Running setup")

var system *prover.ProvingSystem
var err error
system, err = prover.SetupCircuit(circuit, treeDepth, numberOfUtxos)
system, err = prover.SetupCircuit(circuit, inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,29 +151,58 @@ func runCli() {
{
Name: "import-setup",
Flags: []cli.Flag{
&cli.StringFlag{Name: "circuit", Usage: "Type of circuit (\"inclusion\" / \"non-inclusion\")", Required: true},
&cli.StringFlag{Name: "circuit", Usage: "Type of circuit (\"inclusion\" / \"non-inclusion\" / \"combined\")", Required: true},
&cli.StringFlag{Name: "output", Usage: "Output file", Required: true},
&cli.StringFlag{Name: "pk", Usage: "Proving key", Required: true},
&cli.StringFlag{Name: "vk", Usage: "Verifying key", Required: true},
&cli.UintFlag{Name: "tree-depth", Usage: "Merkle tree depth", Required: true},
&cli.UintFlag{Name: "utxos", Usage: "Number of utxos", Required: true},
&cli.UintFlag{Name: "inclusion-tree-depth", Usage: "Merkle tree depth", Required: false},
&cli.UintFlag{Name: "inclusion-utxos", Usage: "Number of Utxos", Required: false},
&cli.UintFlag{Name: "non-inclusion-tree-depth", Usage: "Non-inclusion merkle tree depth", Required: false},
&cli.UintFlag{Name: "non-inclusion-utxos", Usage: "Non-inclusion number of Utxos", Required: false},
},
Action: func(context *cli.Context) error {
circuit := context.String("circuit")
if circuit != "inclusion" && circuit != "non-inclusion" && circuit != "combined" {
return fmt.Errorf("invalid circuit type %s", circuit)
}

path := context.String("output")
pk := context.String("pk")
vk := context.String("vk")
treeDepth := uint32(context.Uint("tree-depth"))
utxos := uint32(context.Uint("utxos"))

inclusionTreeDepth := uint32(context.Uint("inclusion-tree-depth"))
inclusionNumberOfUtxos := uint32(context.Uint("inclusion-utxos"))
nonInclusionTreeDepth := uint32(context.Uint("non-inclusion-tree-depth"))
nonInclusionNumberOfUtxos := uint32(context.Uint("non-inclusion-utxos"))

if (inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0) && circuit == "inclusion" {
return fmt.Errorf("inclusion tree depth and number of utxos must be provided")
}

if (nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0) && circuit == "non-inclusion" {
return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided")
}

if circuit == "combined" {
if inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0 {
return fmt.Errorf("inclusion tree depth and number of utxos must be provided")
}
if nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0 {
return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided")
}
}

var system *prover.ProvingSystem
var err error

logging.Logger().Info().Msg("Importing setup")

if circuit == "inclusion" {
system, err = prover.ImportInclusionSetup(treeDepth, utxos, pk, vk)
system, err = prover.ImportInclusionSetup(inclusionTreeDepth, inclusionNumberOfUtxos, pk, vk)
} else if circuit == "non-inclusion" {
system, err = prover.ImportNonInclusionSetup(treeDepth, utxos, pk, vk)
system, err = prover.ImportNonInclusionSetup(nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk)
} else if circuit == "combined " {
system, err = prover.ImportCombinedSetup(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk)
} else {
return fmt.Errorf("invalid circuit type %s", circuit)
}
Expand Down Expand Up @@ -271,6 +326,7 @@ func runCli() {
{
Name: "prove",
Flags: []cli.Flag{
&cli.StringFlag{Name: "circuit", Usage: "Type of circuit (\"inclusion\" / \"non-inclusion\" / \"combined\")", Required: true},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Expand All @@ -280,6 +336,10 @@ func runCli() {
&cli.StringSliceFlag{Name: "keys-file", Aliases: []string{"k"}, Value: cli.NewStringSlice(), Usage: "Proving system file"},
},
Action: func(context *cli.Context) error {
circuit := context.String("circuit")
if circuit != "inclusion" && circuit != "non-inclusion" && circuit != "combined" {
return fmt.Errorf("invalid circuit type %s", circuit)
}

ps, err := LoadKeysFromConfigOrInline(context)
if err != nil {
Expand All @@ -293,28 +353,65 @@ func runCli() {
}

var proof *prover.Proof
var params prover.InclusionParameters
err = json.Unmarshal(bytes, &params)
if err != nil {
return err
}
if circuit == "inclusion" {
var params prover.InclusionParameters
err = json.Unmarshal(bytes, &params)
if err != nil {
return err
}

treeDepth := params.TreeDepth()
if treeDepth != 26 {
return fmt.Errorf("tree depth must be 26, got %d", treeDepth)
}
treeDepth := params.TreeDepth()
utxos := params.NumberOfUTXOs()

for _, provingSystem := range ps {
if provingSystem.InclusionTreeDepth == treeDepth && provingSystem.InclusionNumberOfUtxos == utxos {
proof, err = provingSystem.ProveInclusion(&params)
if err != nil {
return err
}
r, _ := json.Marshal(&proof)
fmt.Println(string(r))
break
}
}
} else if circuit == "non-inclusion" {
var params prover.NonInclusionParameters
err = json.Unmarshal(bytes, &params)
if err != nil {
return err
}

utxos := params.NumberOfUTXOs()
treeDepth := params.TreeDepth()
utxos := params.NumberOfUTXOs()

for _, provingSystem := range ps {
if provingSystem.NonInclusionTreeDepth == treeDepth && provingSystem.NonInclusionNumberOfUtxos == utxos {
proof, err = provingSystem.ProveNonInclusion(&params)
if err != nil {
return err
}
r, _ := json.Marshal(&proof)
fmt.Println(string(r))
break
}
}
} else if circuit == "combined" {
var params prover.CombinedParameters
err = json.Unmarshal(bytes, &params)
if err != nil {
return err
}

for _, provingSystem := range ps {
if provingSystem.TreeDepth == treeDepth && provingSystem.NumberOfUtxos == utxos {
proof, err = provingSystem.ProveInclusion(&params)
if err != nil {
return err
for _, provingSystem := range ps {
if provingSystem.InclusionTreeDepth == params.TreeDepth() && provingSystem.InclusionNumberOfUtxos == params.NumberOfUTXOs() && provingSystem.NonInclusionTreeDepth == params.NonInclusionTreeDepth() && provingSystem.InclusionNumberOfUtxos == params.NonInclusionNumberOfUTXOs() {
proof, err = provingSystem.ProveCombined(&params)
if err != nil {
return err
}
r, _ := json.Marshal(&proof)
fmt.Println(string(r))
break
}
r, _ := json.Marshal(&proof)
fmt.Println(string(r))
break
}
}

Expand Down Expand Up @@ -359,7 +456,12 @@ func runCli() {
if err != nil {
return err
}
logging.Logger().Info().Uint32("treeDepth", ps.TreeDepth).Uint32("utxos", ps.NumberOfUtxos).Msg("Read proving system")
logging.Logger().Info().
Uint32("treeDepth", ps.InclusionTreeDepth).
Uint32("utxos", ps.InclusionNumberOfUtxos).
Uint32("nonInclusionTreeDepth", ps.NonInclusionTreeDepth).
Uint32("nonInclusionUtxos", ps.NonInclusionNumberOfUtxos).
Msg("Read proving system")
logging.Logger().Info().Msg("Reading proof from stdin")
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
Expand Down Expand Up @@ -456,6 +558,11 @@ func LoadKeysFromConfigOrInline(context *cli.Context) ([]*prover.ProvingSystem,
"circuits/non-inclusion_26_3.key",
"circuits/non-inclusion_26_4.key",
"circuits/non-inclusion_26_8.key",
"circuits/combined_26_1.key",
"circuits/combined_26_2.key",
"circuits/combined_26_3.key",
"circuits/combined_26_4.key",
"circuits/combined_26_8.key",
},
}
}
Expand All @@ -469,7 +576,12 @@ func LoadKeysFromConfigOrInline(context *cli.Context) ([]*prover.ProvingSystem,
return nil, err
}
pss[i] = ps
logging.Logger().Info().Uint32("treeDepth", ps.TreeDepth).Uint32("utxos", ps.NumberOfUtxos).Msg("Read proving system")
logging.Logger().Info().
Uint32("treeDepth", ps.InclusionTreeDepth).
Uint32("utxos", ps.InclusionNumberOfUtxos).
Uint32("nonInclusionTreeDepth", ps.NonInclusionTreeDepth).
Uint32("nonInclusionUtxos", ps.NonInclusionNumberOfUtxos).
Msg("Read proving system")
}
return pss, nil
}
Expand Down
31 changes: 31 additions & 0 deletions gnark-prover/merkle-tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package merkle_tree

import (
"fmt"
"light/light-prover/prover"
"math/big"
"os"
"testing"
Expand Down Expand Up @@ -172,3 +173,33 @@ func TestNonInclusionParameters_TestTree(t *testing.T) {
}
}
}

func TestCombined(t *testing.T) {
file, err := os.OpenFile("../test-data/combined_tmp.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
t.Errorf("Error opening file: %v", err)
return
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
t.Errorf("Error closing file: %v", err)
}
}(file)

tree1 := BuildTestTree(3, 1, true)
tree2 := BuildTestNonInclusionTree(3, 1, true, true)

var combinedParams prover.CombinedParameters = prover.CombinedParameters{
InclusionParameters: tree1,
NonInclusionParameters: tree2,
}

json, err := combinedParams.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}

fmt.Println(string(json))
}
8 changes: 5 additions & 3 deletions gnark-prover/prover/circuit_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package prover

import "fmt"

func SetupCircuit(circuit string, treeDepth uint32, numberOfUtxos uint32) (*ProvingSystem, error) {
func SetupCircuit(circuit string, inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32) (*ProvingSystem, error) {
if circuit == "inclusion" {
return SetupInclusion(treeDepth, numberOfUtxos)
return SetupInclusion(inclusionTreeDepth, inclusionNumberOfUtxos)
} else if circuit == "non-inclusion" {
return SetupNonInclusion(treeDepth, numberOfUtxos)
return SetupNonInclusion(nonInclusionTreeDepth, nonInclusionNumberOfUtxos)
} else if circuit == "combined" {
return SetupCombined(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos)
} else {
return nil, fmt.Errorf("invalid circuit: %s", circuit)
}
Expand Down
Loading

0 comments on commit 3fee10b

Please sign in to comment.