From 8802aae42130ec860d38f3af0203e4fcdbebd742 Mon Sep 17 00:00:00 2001 From: ananas-block Date: Sat, 22 Jun 2024 01:45:11 +0100 Subject: [PATCH] rename utxo to compressed account --- light-prover/integration_test.go | 8 +- light-prover/main.go | 92 +++++++++---------- light-prover/merkle-tree/test_tree.go | 16 ++-- light-prover/merkle-tree/tree_test.go | 58 ++++++------ light-prover/prover/circuit_builder.go | 9 +- light-prover/prover/circuit_utils.go | 16 ++-- light-prover/prover/combined_circuit.go | 10 +- .../prover/combined_proving_system.go | 92 +++++++++---------- light-prover/prover/combined_test.go | 90 +++++++++--------- light-prover/prover/extractor.go | 16 ++-- light-prover/prover/inclusion_circuit.go | 18 ++-- .../prover/inclusion_proving_system.go | 58 ++++++------ light-prover/prover/inclusion_test.go | 55 +++++------ light-prover/prover/marshal.go | 8 +- light-prover/prover/marshal_inclusion.go | 4 +- light-prover/prover/marshal_non_inclusion.go | 4 +- light-prover/prover/non_inclusion_circuit.go | 28 +++--- .../prover/non_inclusion_proving_system.go | 72 +++++++-------- light-prover/prover/non_inclusion_test.go | 79 ++++++++-------- light-prover/server/server.go | 23 ++--- 20 files changed, 379 insertions(+), 377 deletions(-) diff --git a/light-prover/integration_test.go b/light-prover/integration_test.go index 652ef34107..178e2ea3e5 100644 --- a/light-prover/integration_test.go +++ b/light-prover/integration_test.go @@ -33,8 +33,8 @@ func StartServer() { var pss = make([]*prover.ProvingSystem, len(keys)) for i, key := range keys { - // Another way to instantiate the circuit: prover.SetupInclusion(Depth, NumberOfUtxos) - // But we need to know the tree depth and the number of UTXOs + // Another way to instantiate the circuit: prover.SetupInclusion(Depth, NumberOfCompressedAccounts) + // But we need to know the tree depth and the number of compressed accounts ps, err := prover.ReadSystemFromFile(key) if err != nil { panic(err) @@ -92,8 +92,8 @@ func TestInclusionHappyPath26_1(t *testing.T) { } func TestInclusionHappyPath26_12348(t *testing.T) { - for _, utxos := range []int{1, 2, 3, 4, 8} { - tree := merkletree.BuildTestTree(26, utxos, false) + for _, compressedAccounts := range []int{1, 2, 3, 4, 8} { + tree := merkletree.BuildTestTree(26, compressedAccounts, false) jsonBytes, _ := tree.MarshalJSON() jsonString := string(jsonBytes) diff --git a/light-prover/main.go b/light-prover/main.go index 0b9a65c3da..2acc195dd9 100644 --- a/light-prover/main.go +++ b/light-prover/main.go @@ -36,9 +36,9 @@ func runCli() { &cli.StringFlag{Name: "output", Usage: "Output file", Required: true}, &cli.StringFlag{Name: "output-vkey", Usage: "Output file", 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: "inclusion-compressedAccounts", Usage: "Number of compressed accounts", 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}, + &cli.UintFlag{Name: "non-inclusion-compressedAccounts", Usage: "Non-inclusion number of compressed accounts", Required: false}, }, Action: func(context *cli.Context) error { circuit := prover.CircuitType(context.String("circuit")) @@ -49,24 +49,24 @@ func runCli() { path := context.String("output") pathVkey := context.String("output-vkey") inclusionTreeDepth := uint32(context.Uint("inclusion-tree-depth")) - inclusionNumberOfUtxos := uint32(context.Uint("inclusion-utxos")) + inclusionNumberOfCompressedAccounts := uint32(context.Uint("inclusion-compressedAccounts")) nonInclusionTreeDepth := uint32(context.Uint("non-inclusion-tree-depth")) - nonInclusionNumberOfUtxos := uint32(context.Uint("non-inclusion-utxos")) + nonInclusionNumberOfCompressedAccounts := uint32(context.Uint("non-inclusion-compressedAccounts")) - if (inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0) && circuit == prover.Inclusion { - return fmt.Errorf("inclusion tree depth and number of utxos must be provided") + if (inclusionTreeDepth == 0 || inclusionNumberOfCompressedAccounts == 0) && circuit == prover.Inclusion { + return fmt.Errorf("inclusion tree depth and number of compressed accounts must be provided") } - if (nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0) && circuit == prover.NonInclusion { - return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided") + if (nonInclusionTreeDepth == 0 || nonInclusionNumberOfCompressedAccounts == 0) && circuit == prover.NonInclusion { + return fmt.Errorf("non-inclusion tree depth and number of compressed accounts must be provided") } if circuit == prover.Combined { - if inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0 { - return fmt.Errorf("inclusion tree depth and number of utxos must be provided") + if inclusionTreeDepth == 0 || inclusionNumberOfCompressedAccounts == 0 { + return fmt.Errorf("inclusion tree depth and number of compressed accounts must be provided") } - if nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0 { - return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided") + if nonInclusionTreeDepth == 0 || nonInclusionNumberOfCompressedAccounts == 0 { + return fmt.Errorf("non-inclusion tree depth and number of compressed accounts must be provided") } } @@ -74,7 +74,7 @@ func runCli() { var system *prover.ProvingSystem var err error - system, err = prover.SetupCircuit(circuit, inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos) + system, err = prover.SetupCircuit(circuit, inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) if err != nil { return err } @@ -113,18 +113,18 @@ func runCli() { Flags: []cli.Flag{ &cli.StringFlag{Name: "output", 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: "compressedAccounts", Usage: "Number of compressed accounts", Required: true}, }, Action: func(context *cli.Context) error { path := context.String("output") treeDepth := uint32(context.Uint("tree-depth")) - utxos := uint32(context.Uint("utxos")) + compressedAccounts := uint32(context.Uint("compressedAccounts")) logging.Logger().Info().Msg("Building R1CS") var cs constraint.ConstraintSystem var err error - cs, err = prover.R1CSInclusion(treeDepth, utxos) + cs, err = prover.R1CSInclusion(treeDepth, compressedAccounts) if err != nil { return err @@ -155,9 +155,9 @@ func runCli() { &cli.StringFlag{Name: "pk", Usage: "Proving key", Required: true}, &cli.StringFlag{Name: "vk", Usage: "Verifying key", 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: "inclusion-compressedAccounts", Usage: "Number of compressed accounts", 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}, + &cli.UintFlag{Name: "non-inclusion-compressedAccounts", Usage: "Non-inclusion number of compressed accounts", Required: false}, }, Action: func(context *cli.Context) error { circuit := context.String("circuit") @@ -170,24 +170,24 @@ func runCli() { vk := context.String("vk") inclusionTreeDepth := uint32(context.Uint("inclusion-tree-depth")) - inclusionNumberOfUtxos := uint32(context.Uint("inclusion-utxos")) + inclusionNumberOfCompressedAccounts := uint32(context.Uint("inclusion-compressedAccounts")) nonInclusionTreeDepth := uint32(context.Uint("non-inclusion-tree-depth")) - nonInclusionNumberOfUtxos := uint32(context.Uint("non-inclusion-utxos")) + nonInclusionNumberOfCompressedAccounts := uint32(context.Uint("non-inclusion-compressedAccounts")) - if (inclusionTreeDepth == 0 || inclusionNumberOfUtxos == 0) && circuit == "inclusion" { - return fmt.Errorf("inclusion tree depth and number of utxos must be provided") + if (inclusionTreeDepth == 0 || inclusionNumberOfCompressedAccounts == 0) && circuit == "inclusion" { + return fmt.Errorf("inclusion tree depth and number of compressed accounts 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 (nonInclusionTreeDepth == 0 || nonInclusionNumberOfCompressedAccounts == 0) && circuit == "non-inclusion" { + return fmt.Errorf("non-inclusion tree depth and number of compressed accounts 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 inclusionTreeDepth == 0 || inclusionNumberOfCompressedAccounts == 0 { + return fmt.Errorf("inclusion tree depth and number of compressed accounts must be provided") } - if nonInclusionTreeDepth == 0 || nonInclusionNumberOfUtxos == 0 { - return fmt.Errorf("non-inclusion tree depth and number of utxos must be provided") + if nonInclusionTreeDepth == 0 || nonInclusionNumberOfCompressedAccounts == 0 { + return fmt.Errorf("non-inclusion tree depth and number of compressed accounts must be provided") } } @@ -197,11 +197,11 @@ func runCli() { logging.Logger().Info().Msg("Importing setup") if circuit == "inclusion" { - system, err = prover.ImportInclusionSetup(inclusionTreeDepth, inclusionNumberOfUtxos, pk, vk) + system, err = prover.ImportInclusionSetup(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, pk, vk) } else if circuit == "non-inclusion" { - system, err = prover.ImportNonInclusionSetup(nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk) + system, err = prover.ImportNonInclusionSetup(nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts, pk, vk) } else if circuit == "combined " { - system, err = prover.ImportCombinedSetup(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk) + system, err = prover.ImportCombinedSetup(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts, pk, vk) } else { return fmt.Errorf("invalid circuit type %s", circuit) } @@ -261,17 +261,17 @@ func runCli() { Name: "gen-test-params", Flags: []cli.Flag{ &cli.IntFlag{Name: "tree-depth", Usage: "depth of the mock tree", DefaultText: "26", Value: 26}, - &cli.IntFlag{Name: "utxos", Usage: "Number of utxos", DefaultText: "1", Value: 1}, + &cli.IntFlag{Name: "compressedAccounts", Usage: "Number of compressed accounts", DefaultText: "1", Value: 1}, }, Action: func(context *cli.Context) error { treeDepth := context.Int("tree-depth") - utxos := context.Int("utxos") + compressedAccounts := context.Int("compressedAccounts") logging.Logger().Info().Msg("Generating test params for the inclusion circuit") var r []byte var err error - params := merkletree.BuildTestTree(treeDepth, utxos, false) + params := merkletree.BuildTestTree(treeDepth, compressedAccounts, false) r, err = json.Marshal(¶ms) @@ -354,9 +354,9 @@ func runCli() { } treeDepth := params.TreeDepth() - utxos := params.NumberOfUTXOs() + compressedAccounts := params.NumberOfCompressedAccounts() for _, provingSystem := range ps { - if provingSystem.InclusionTreeDepth == treeDepth && provingSystem.InclusionNumberOfUtxos == utxos { + if provingSystem.InclusionTreeDepth == treeDepth && provingSystem.InclusionNumberOfCompressedAccounts == compressedAccounts { proof, err = provingSystem.ProveInclusion(¶ms) if err != nil { return err @@ -374,10 +374,10 @@ func runCli() { } treeDepth := params.TreeDepth() - utxos := params.NumberOfUTXOs() + compressedAccounts := params.NumberOfCompressedAccounts() for _, provingSystem := range ps { - if provingSystem.NonInclusionTreeDepth == treeDepth && provingSystem.NonInclusionNumberOfUtxos == utxos { + if provingSystem.NonInclusionTreeDepth == treeDepth && provingSystem.NonInclusionNumberOfCompressedAccounts == compressedAccounts { proof, err = provingSystem.ProveNonInclusion(¶ms) if err != nil { return err @@ -395,7 +395,7 @@ func runCli() { } for _, provingSystem := range ps { - if provingSystem.InclusionTreeDepth == params.TreeDepth() && provingSystem.InclusionNumberOfUtxos == params.NumberOfUTXOs() && provingSystem.NonInclusionTreeDepth == params.NonInclusionTreeDepth() && provingSystem.InclusionNumberOfUtxos == params.NonInclusionNumberOfUTXOs() { + if provingSystem.InclusionTreeDepth == params.TreeDepth() && provingSystem.InclusionNumberOfCompressedAccounts == params.NumberOfCompressedAccounts() && provingSystem.NonInclusionTreeDepth == params.NonInclusionTreeDepth() && provingSystem.InclusionNumberOfCompressedAccounts == params.NonInclusionNumberOfCompressedAccounts() { proof, err = provingSystem.ProveCombined(¶ms) if err != nil { return err @@ -450,9 +450,9 @@ func runCli() { } logging.Logger().Info(). Uint32("treeDepth", ps.InclusionTreeDepth). - Uint32("utxos", ps.InclusionNumberOfUtxos). + Uint32("compressedAccounts", ps.InclusionNumberOfCompressedAccounts). Uint32("nonInclusionTreeDepth", ps.NonInclusionTreeDepth). - Uint32("nonInclusionUtxos", ps.NonInclusionNumberOfUtxos). + Uint32("nonInclusionCompressedAccounts", ps.NonInclusionNumberOfCompressedAccounts). Msg("Read proving system") logging.Logger().Info().Msg("Reading proof from stdin") proofBytes, err := io.ReadAll(os.Stdin) @@ -481,14 +481,14 @@ func runCli() { Flags: []cli.Flag{ &cli.StringFlag{Name: "output", 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: "compressedAccounts", Usage: "Number of compressed accounts", Required: true}, }, Action: func(context *cli.Context) error { path := context.String("output") treeDepth := uint32(context.Uint("tree-depth")) - utxos := uint32(context.Uint("utxos")) + compressedAccounts := uint32(context.Uint("compressedAccounts")) logging.Logger().Info().Msg("Extracting gnark circuit to Lean") - circuitString, err := prover.ExtractLean(treeDepth, utxos) + circuitString, err := prover.ExtractLean(treeDepth, compressedAccounts) if err != nil { return err } @@ -531,9 +531,9 @@ func LoadKeys(context *cli.Context) ([]*prover.ProvingSystem, error) { pss[i] = ps logging.Logger().Info(). Uint32("treeDepth", ps.InclusionTreeDepth). - Uint32("utxos", ps.InclusionNumberOfUtxos). + Uint32("compressedAccounts", ps.InclusionNumberOfCompressedAccounts). Uint32("nonInclusionTreeDepth", ps.NonInclusionTreeDepth). - Uint32("nonInclusionUtxos", ps.NonInclusionNumberOfUtxos). + Uint32("nonInclusionCompressedAccounts", ps.NonInclusionNumberOfCompressedAccounts). Msg("Read proving system") } return pss, nil diff --git a/light-prover/merkle-tree/test_tree.go b/light-prover/merkle-tree/test_tree.go index 79801fe9e5..adeef132f7 100644 --- a/light-prover/merkle-tree/test_tree.go +++ b/light-prover/merkle-tree/test_tree.go @@ -138,7 +138,7 @@ func NewTree(depth int) PoseidonTree { return PoseidonTree{root: &PoseidonEmptyNode{dep: depth, emptyTreeValues: initHashes}} } -func BuildTestTree(depth int, numberOfUtxos int, random bool) prover.InclusionParameters { +func BuildTestTree(depth int, numberOfCompressedAccounts int, random bool) prover.InclusionParameters { tree := NewTree(depth) var leaf *big.Int var pathIndex int @@ -150,9 +150,9 @@ func BuildTestTree(depth int, numberOfUtxos int, random bool) prover.InclusionPa pathIndex = 0 } - var inputs = make([]prover.InclusionInputs, numberOfUtxos) + var inputs = make([]prover.InclusionInputs, numberOfCompressedAccounts) - for i := 0; i < numberOfUtxos; i++ { + for i := 0; i < numberOfCompressedAccounts; i++ { inputs[i].Leaf = *leaf inputs[i].PathIndex = uint32(pathIndex) inputs[i].PathElements = tree.Update(pathIndex, *leaf) @@ -168,16 +168,16 @@ func rangeIn(low, hi int) int { return low + rand.Intn(hi-low) } -func BuildValidTestNonInclusionTree(depth int, numberOfUtxos int, random bool) prover.NonInclusionParameters { - return BuildTestNonInclusionTree(depth, numberOfUtxos, random, true, false) +func BuildValidTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random bool) prover.NonInclusionParameters { + return BuildTestNonInclusionTree(depth, numberOfCompressedAccounts, random, true, false) } -func BuildTestNonInclusionTree(depth int, numberOfUtxos int, random bool, valid bool, lowValue bool) prover.NonInclusionParameters { +func BuildTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random bool, valid bool, lowValue bool) prover.NonInclusionParameters { tree := NewTree(depth) - var inputs = make([]prover.NonInclusionInputs, numberOfUtxos) + var inputs = make([]prover.NonInclusionInputs, numberOfCompressedAccounts) - for i := 0; i < numberOfUtxos; i++ { + for i := 0; i < numberOfCompressedAccounts; i++ { var value = big.NewInt(0) var leafLower = big.NewInt(0) var leafUpper = big.NewInt(2) diff --git a/light-prover/merkle-tree/tree_test.go b/light-prover/merkle-tree/tree_test.go index 7ced5f5c81..acb60fd509 100644 --- a/light-prover/merkle-tree/tree_test.go +++ b/light-prover/merkle-tree/tree_test.go @@ -22,11 +22,11 @@ func TestInclusionParameters_TestTree(t *testing.T) { }(file) var testTreeDepth = []int{26} - var testUtxoCount = []int{1, 2, 3, 4} + var testCompressedAccountCount = []int{1, 2, 3, 4} for i := 0; i < len(testTreeDepth); i++ { - for j := 0; j < len(testUtxoCount); j++ { - trees := MakeTestIncludedTrees(testTreeDepth[i], testUtxoCount[j]) + for j := 0; j < len(testCompressedAccountCount); j++ { + trees := MakeTestIncludedTrees(testTreeDepth[i], testCompressedAccountCount[j]) for _, tree := range trees { var json, err = tree.Tree.MarshalJSON() if err != nil { @@ -58,11 +58,11 @@ func TestNonInclusionParameters_TestTree(t *testing.T) { }(file) var testTreeDepth = []int{26} - var testUtxoCount = []int{1, 2, 3, 4} + var testCompressedAccountCount = []int{1, 2, 3, 4} for i := 0; i < len(testTreeDepth); i++ { - for j := 0; j < len(testUtxoCount); j++ { - trees := MakeTestNonInclusionTrees(testTreeDepth[i], testUtxoCount[j]) + for j := 0; j < len(testCompressedAccountCount); j++ { + trees := MakeTestNonInclusionTrees(testTreeDepth[i], testCompressedAccountCount[j]) for _, tree := range trees { var json, err = tree.Tree.MarshalJSON() if err != nil { @@ -93,12 +93,12 @@ func TestCombined(t *testing.T) { }(file) var testTreeDepth = []int{26} - var testUtxoCount = []int{1, 2, 3, 4} + var testCompressedAccountCount = []int{1, 2, 3, 4} for i := 0; i < len(testTreeDepth); i++ { - for j := 0; j < len(testUtxoCount); j++ { - trees1 := MakeTestIncludedTrees(testTreeDepth[i], testUtxoCount[j]) - trees2 := MakeTestNonInclusionTrees(testTreeDepth[i], testUtxoCount[j]) + for j := 0; j < len(testCompressedAccountCount); j++ { + trees1 := MakeTestIncludedTrees(testTreeDepth[i], testCompressedAccountCount[j]) + trees2 := MakeTestNonInclusionTrees(testTreeDepth[i], testCompressedAccountCount[j]) for k, tree1 := range trees1 { for l, tree2 := range trees2 { var combinedParams = prover.CombinedParameters{ @@ -141,7 +141,7 @@ type InclusionTreeValidPair struct { // `MakeTestIncludedTrees` // // ```go -// func MakeTestIncludedTrees(depth int, numberOfUtxos int) []InclusionTreeValidPair +// func MakeTestIncludedTrees(depth int, numberOfCompressedAccounts int) []InclusionTreeValidPair // ``` // // # Description @@ -153,7 +153,7 @@ type InclusionTreeValidPair struct { // Parameters: // // - `depth (int)`: Defines the depth of each included tree. -// - `numberOfUtxos (int)`: Number of unspent transaction outputs (UTXOs) to include in each tree. +// - `numberOfCompressedAccounts (int)`: Number of unspent transaction outputs (CompressedAccounts) to include in each tree. // // Returns: // - `[]InclusionTreeValidPair`: An array of `InclusionTreeValidPair` instances, each containing @@ -178,29 +178,29 @@ type InclusionTreeValidPair struct { // } // // ``` -func MakeTestIncludedTrees(depth int, numberOfUtxos int) []InclusionTreeValidPair { +func MakeTestIncludedTrees(depth int, numberOfCompressedAccounts int) []InclusionTreeValidPair { var trees []InclusionTreeValidPair - validTree := BuildTestTree(depth, numberOfUtxos, false) + validTree := BuildTestTree(depth, numberOfCompressedAccounts, false) validPair := InclusionTreeValidPair{Tree: validTree, Valid: true} - invalidRootTree := BuildTestTree(depth, numberOfUtxos, true) + invalidRootTree := BuildTestTree(depth, numberOfCompressedAccounts, true) invalidRootTree.Inputs[0].Root = *big.NewInt(999) invalidRootPair := InclusionTreeValidPair{Tree: invalidRootTree, Valid: false} - invalidLeafTree := BuildTestTree(depth, numberOfUtxos, true) + invalidLeafTree := BuildTestTree(depth, numberOfCompressedAccounts, true) invalidLeafTree.Inputs[0].Leaf = *big.NewInt(999) invalidLeafPair := InclusionTreeValidPair{Tree: invalidLeafTree, Valid: false} - invalidInPathIndicesTreeAddOne := BuildTestTree(depth, numberOfUtxos, true) + invalidInPathIndicesTreeAddOne := BuildTestTree(depth, numberOfCompressedAccounts, true) invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex = invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex + 1 invalidInPathIndicesPairAddOne := InclusionTreeValidPair{Tree: invalidInPathIndicesTreeAddOne, Valid: false} - invalidInPathIndicesTreeSubOne := BuildTestTree(depth, numberOfUtxos, true) + invalidInPathIndicesTreeSubOne := BuildTestTree(depth, numberOfCompressedAccounts, true) invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex = invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex - 1 invalidInPathIndicesPairSubOne := InclusionTreeValidPair{Tree: invalidInPathIndicesTreeSubOne, Valid: false} - invalidInPathElementsTree := BuildTestTree(depth, numberOfUtxos, true) + invalidInPathElementsTree := BuildTestTree(depth, numberOfCompressedAccounts, true) invalidInPathElementsTree.Inputs[0].PathElements[0] = *big.NewInt(999) invalidInPathElementsPair := InclusionTreeValidPair{Tree: invalidInPathElementsTree, Valid: false} @@ -223,7 +223,7 @@ type NonInclusionTreeValidPair struct { // `MakeTestNonInclusionTrees` // // ```go -// func MakeTestNonInclusionTrees(depth int, numberOfUtxos int) []NonInclusionTreeValidPair +// func MakeTestNonInclusionTrees(depth int, numberOfCompressedAccounts int) []NonInclusionTreeValidPair // ``` // // # Description @@ -233,7 +233,7 @@ type NonInclusionTreeValidPair struct { // # Parameters // // - `depth (int)`: Defines the depth of each included tree. -// - `numberOfUtxos (int)`: Number of unspent transaction outputs (UTXOs) to include in each tree. +// - `numberOfCompressedAccounts (int)`: Number of unspent transaction outputs (CompressedAccounts) to include in each tree. // // # Returns // @@ -263,31 +263,31 @@ type NonInclusionTreeValidPair struct { // } // // ``` -func MakeTestNonInclusionTrees(depth int, numberOfUtxos int) []NonInclusionTreeValidPair { +func MakeTestNonInclusionTrees(depth int, numberOfCompressedAccounts int) []NonInclusionTreeValidPair { var trees []NonInclusionTreeValidPair - validTree := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true) + validTree := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true) validPair := NonInclusionTreeValidPair{Tree: validTree, Valid: true} - invalidRootTree := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true) + invalidRootTree := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true) invalidRootTree.Inputs[0].Root = *big.NewInt(999) invalidRootPair := NonInclusionTreeValidPair{Tree: invalidRootTree, Valid: false} - invalidLowValueTree := BuildTestNonInclusionTree(depth, numberOfUtxos, true, false, true) + invalidLowValueTree := BuildTestNonInclusionTree(depth, numberOfCompressedAccounts, true, false, true) invalidLowValuePair := NonInclusionTreeValidPair{Tree: invalidLowValueTree, Valid: false} - invalidHighValueTree := BuildTestNonInclusionTree(depth, numberOfUtxos, true, false, false) + invalidHighValueTree := BuildTestNonInclusionTree(depth, numberOfCompressedAccounts, true, false, false) invalidHighValuePair := NonInclusionTreeValidPair{Tree: invalidHighValueTree, Valid: false} - invalidInPathIndicesTreeAddOne := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true) + invalidInPathIndicesTreeAddOne := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true) invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex += 1 invalidInPathIndicesPairAddOne := NonInclusionTreeValidPair{Tree: invalidInPathIndicesTreeAddOne, Valid: false} - invalidInPathIndicesTreeSubOne := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true) + invalidInPathIndicesTreeSubOne := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true) invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex -= 1 invalidInPathIndicesPairSubOne := NonInclusionTreeValidPair{Tree: invalidInPathIndicesTreeSubOne, Valid: false} - invalidInPathElementsTree := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true) + invalidInPathElementsTree := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true) invalidInPathElementsTree.Inputs[0].PathElements[0] = *big.NewInt(999) invalidInPathElementsPair := NonInclusionTreeValidPair{Tree: invalidInPathElementsTree, Valid: false} diff --git a/light-prover/prover/circuit_builder.go b/light-prover/prover/circuit_builder.go index 3771bd5827..65a0c80e25 100644 --- a/light-prover/prover/circuit_builder.go +++ b/light-prover/prover/circuit_builder.go @@ -9,19 +9,18 @@ type CircuitType string const ( InputCompressedAccounts = "input-compressed-accounts" - NewAddresses = "new-addresses" Combined CircuitType = "combined" Inclusion CircuitType = "inclusion" NonInclusion CircuitType = "non-inclusion" ) -func SetupCircuit(circuit CircuitType, inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32) (*ProvingSystem, error) { +func SetupCircuit(circuit CircuitType, inclusionTreeDepth uint32, inclusionNumberOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfCompressedAccounts uint32) (*ProvingSystem, error) { if circuit == Inclusion { - return SetupInclusion(inclusionTreeDepth, inclusionNumberOfUtxos) + return SetupInclusion(inclusionTreeDepth, inclusionNumberOfCompressedAccounts) } else if circuit == NonInclusion { - return SetupNonInclusion(nonInclusionTreeDepth, nonInclusionNumberOfUtxos) + return SetupNonInclusion(nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) } else if circuit == Combined { - return SetupCombined(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos) + return SetupCombined(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) } else { return nil, fmt.Errorf("invalid circuit: %s", circuit) } diff --git a/light-prover/prover/circuit_utils.go b/light-prover/prover/circuit_utils.go index 19d8c121b1..df4dc3f206 100644 --- a/light-prover/prover/circuit_utils.go +++ b/light-prover/prover/circuit_utils.go @@ -21,9 +21,9 @@ type Proof struct { type ProvingSystem struct { InclusionTreeDepth uint32 - InclusionNumberOfUtxos uint32 + InclusionNumberOfCompressedAccounts uint32 NonInclusionTreeDepth uint32 - NonInclusionNumberOfUtxos uint32 + NonInclusionNumberOfCompressedAccounts uint32 ProvingKey groth16.ProvingKey VerifyingKey groth16.VerifyingKey ConstraintSystem constraint.ConstraintSystem @@ -50,13 +50,13 @@ type InclusionProof struct { InPathIndices []frontend.Variable InPathElements [][]frontend.Variable - NumberOfUtxos uint32 + NumberOfCompressedAccounts uint32 Depth uint32 } func (gadget InclusionProof) DefineGadget(api frontend.API) interface{} { - currentHash := make([]frontend.Variable, gadget.NumberOfUtxos) - for proofIndex := 0; proofIndex < int(gadget.NumberOfUtxos); proofIndex++ { + currentHash := make([]frontend.Variable, gadget.NumberOfCompressedAccounts) + for proofIndex := 0; proofIndex < int(gadget.NumberOfCompressedAccounts); proofIndex++ { hash := MerkleRootGadget{ Hash: gadget.Leaves[proofIndex], Index: gadget.InPathIndices[proofIndex], @@ -79,13 +79,13 @@ type NonInclusionProof struct { InPathIndices []frontend.Variable InPathElements [][]frontend.Variable - NumberOfUtxos uint32 + NumberOfCompressedAccounts uint32 Depth uint32 } func (gadget NonInclusionProof) DefineGadget(api frontend.API) interface{} { - currentHash := make([]frontend.Variable, gadget.NumberOfUtxos) - for proofIndex := 0; proofIndex < int(gadget.NumberOfUtxos); proofIndex++ { + currentHash := make([]frontend.Variable, gadget.NumberOfCompressedAccounts) + for proofIndex := 0; proofIndex < int(gadget.NumberOfCompressedAccounts); proofIndex++ { leaf := LeafHashGadget{ LeafLowerRangeValue: gadget.LeafLowerRangeValues[proofIndex], LeafIndex: gadget.LeafIndices[proofIndex], diff --git a/light-prover/prover/combined_circuit.go b/light-prover/prover/combined_circuit.go index 21d6518bec..3a1bb00fc1 100644 --- a/light-prover/prover/combined_circuit.go +++ b/light-prover/prover/combined_circuit.go @@ -16,7 +16,7 @@ func (circuit *CombinedCircuit) Define(api frontend.API) error { Leaves: circuit.Inclusion.Leaves, InPathElements: circuit.Inclusion.InPathElements, InPathIndices: circuit.Inclusion.InPathIndices, - NumberOfUtxos: circuit.Inclusion.NumberOfUtxos, + NumberOfCompressedAccounts: circuit.Inclusion.NumberOfCompressedAccounts, Depth: circuit.Inclusion.Depth, }) @@ -28,14 +28,14 @@ func (circuit *CombinedCircuit) Define(api frontend.API) error { LeafIndices: circuit.NonInclusion.LeafIndices, InPathIndices: circuit.NonInclusion.InPathIndices, InPathElements: circuit.NonInclusion.InPathElements, - NumberOfUtxos: circuit.NonInclusion.NumberOfUtxos, + NumberOfCompressedAccounts: circuit.NonInclusion.NumberOfCompressedAccounts, Depth: circuit.NonInclusion.Depth, }) return nil } -func ImportCombinedSetup(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32, pkPath string, vkPath string) (*ProvingSystem, error) { - ccs, err := R1CSCombined(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos) +func ImportCombinedSetup(inclusionTreeDepth uint32, inclusionNumberOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfCompressedAccounts uint32, pkPath string, vkPath string) (*ProvingSystem, error) { + ccs, err := R1CSCombined(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) if err != nil { return nil, err } @@ -52,5 +52,5 @@ func ImportCombinedSetup(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint3 return nil, err } - return &ProvingSystem{inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk, ccs}, nil + return &ProvingSystem{inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts, pk, vk, ccs}, nil } diff --git a/light-prover/prover/combined_proving_system.go b/light-prover/prover/combined_proving_system.go index df73e773f6..a7b1c2b64e 100644 --- a/light-prover/prover/combined_proving_system.go +++ b/light-prover/prover/combined_proving_system.go @@ -16,85 +16,85 @@ type CombinedParameters struct { NonInclusionParameters NonInclusionParameters } -func (p *CombinedParameters) NumberOfUTXOs() uint32 { - return p.InclusionParameters.NumberOfUTXOs() +func (p *CombinedParameters) NumberOfCompressedAccounts() uint32 { + return p.InclusionParameters.NumberOfCompressedAccounts() } func (p *CombinedParameters) TreeDepth() uint32 { return p.InclusionParameters.TreeDepth() } -func (p *CombinedParameters) NonInclusionNumberOfUTXOs() uint32 { - return p.NonInclusionParameters.NumberOfUTXOs() +func (p *CombinedParameters) NonInclusionNumberOfCompressedAccounts() uint32 { + return p.NonInclusionParameters.NumberOfCompressedAccounts() } func (p *CombinedParameters) NonInclusionTreeDepth() uint32 { return p.NonInclusionParameters.TreeDepth() } -func (p *CombinedParameters) ValidateShape(inclusionTreeDepth uint32, inclusionNumOfUTXOs uint32, nonInclusionTreeDepth uint32, nonInclusionNumOfUTXOs uint32) error { - if err := p.InclusionParameters.ValidateShape(inclusionTreeDepth, inclusionNumOfUTXOs); err != nil { +func (p *CombinedParameters) ValidateShape(inclusionTreeDepth uint32, inclusionNumOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumOfCompressedAccounts uint32) error { + if err := p.InclusionParameters.ValidateShape(inclusionTreeDepth, inclusionNumOfCompressedAccounts); err != nil { return err } - if err := p.NonInclusionParameters.ValidateShape(nonInclusionTreeDepth, nonInclusionNumOfUTXOs); err != nil { + if err := p.NonInclusionParameters.ValidateShape(nonInclusionTreeDepth, nonInclusionNumOfCompressedAccounts); err != nil { return err } return nil } -func R1CSCombined(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32) (constraint.ConstraintSystem, error) { - circuit := InitializeCombinedCircuit(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos) +func R1CSCombined(inclusionTreeDepth uint32, inclusionNumberOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfCompressedAccounts uint32) (constraint.ConstraintSystem, error) { + circuit := InitializeCombinedCircuit(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) return frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit) } -func InitializeCombinedCircuit(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32) CombinedCircuit { - inclusionRoots := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionLeaves := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionInPathIndices := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionInPathElements := make([][]frontend.Variable, inclusionNumberOfUtxos) - for i := 0; i < int(inclusionNumberOfUtxos); i++ { +func InitializeCombinedCircuit(inclusionTreeDepth uint32, inclusionNumberOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfCompressedAccounts uint32) CombinedCircuit { + inclusionRoots := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionLeaves := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionInPathIndices := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionInPathElements := make([][]frontend.Variable, inclusionNumberOfCompressedAccounts) + for i := 0; i < int(inclusionNumberOfCompressedAccounts); i++ { inclusionInPathElements[i] = make([]frontend.Variable, inclusionTreeDepth) } - nonInclusionRoots := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafLowerRangeValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafHigherRangeValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafIndices := make([]frontend.Variable, nonInclusionNumberOfUtxos) + nonInclusionRoots := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafLowerRangeValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafHigherRangeValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafIndices := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) - nonInclusionInPathIndices := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionInPathElements := make([][]frontend.Variable, nonInclusionNumberOfUtxos) + nonInclusionInPathIndices := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionInPathElements := make([][]frontend.Variable, nonInclusionNumberOfCompressedAccounts) - for i := 0; i < int(nonInclusionNumberOfUtxos); i++ { + for i := 0; i < int(nonInclusionNumberOfCompressedAccounts); i++ { nonInclusionInPathElements[i] = make([]frontend.Variable, nonInclusionTreeDepth) } circuit := CombinedCircuit{ Inclusion: InclusionCircuit{ - Roots: inclusionRoots, - Leaves: inclusionLeaves, - InPathIndices: inclusionInPathIndices, - InPathElements: inclusionInPathElements, - NumberOfUtxos: inclusionNumberOfUtxos, - Depth: inclusionTreeDepth, + Roots: inclusionRoots, + Leaves: inclusionLeaves, + InPathIndices: inclusionInPathIndices, + InPathElements: inclusionInPathElements, + NumberOfCompressedAccounts: inclusionNumberOfCompressedAccounts, + Depth: inclusionTreeDepth, }, NonInclusion: NonInclusionCircuit{ - Roots: nonInclusionRoots, - Values: nonInclusionValues, - LeafLowerRangeValues: nonInclusionLeafLowerRangeValues, - LeafHigherRangeValues: nonInclusionLeafHigherRangeValues, - LeafIndices: nonInclusionLeafIndices, - InPathIndices: nonInclusionInPathIndices, - InPathElements: nonInclusionInPathElements, - NumberOfUtxos: nonInclusionNumberOfUtxos, - Depth: nonInclusionTreeDepth, + Roots: nonInclusionRoots, + Values: nonInclusionValues, + LeafLowerRangeValues: nonInclusionLeafLowerRangeValues, + LeafHigherRangeValues: nonInclusionLeafHigherRangeValues, + LeafIndices: nonInclusionLeafIndices, + InPathIndices: nonInclusionInPathIndices, + InPathElements: nonInclusionInPathElements, + NumberOfCompressedAccounts: nonInclusionNumberOfCompressedAccounts, + Depth: nonInclusionTreeDepth, }, } return circuit } -func SetupCombined(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfUtxos uint32) (*ProvingSystem, error) { - ccs, err := R1CSCombined(inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos) +func SetupCombined(inclusionTreeDepth uint32, inclusionNumberOfCompressedAccounts uint32, nonInclusionTreeDepth uint32, nonInclusionNumberOfCompressedAccounts uint32) (*ProvingSystem, error) { + ccs, err := R1CSCombined(inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts) if err != nil { return nil, err } @@ -102,17 +102,17 @@ func SetupCombined(inclusionTreeDepth uint32, inclusionNumberOfUtxos uint32, non if err != nil { return nil, err } - return &ProvingSystem{inclusionTreeDepth, inclusionNumberOfUtxos, nonInclusionTreeDepth, nonInclusionNumberOfUtxos, pk, vk, ccs}, nil + return &ProvingSystem{inclusionTreeDepth, inclusionNumberOfCompressedAccounts, nonInclusionTreeDepth, nonInclusionNumberOfCompressedAccounts, pk, vk, ccs}, nil } func (ps *ProvingSystem) ProveCombined(params *CombinedParameters) (*Proof, error) { - if err := params.ValidateShape(ps.InclusionTreeDepth, ps.InclusionNumberOfUtxos, ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfUtxos); err != nil { + if err := params.ValidateShape(ps.InclusionTreeDepth, ps.InclusionNumberOfCompressedAccounts, ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfCompressedAccounts); err != nil { return nil, err } - circuit := InitializeCombinedCircuit(ps.InclusionTreeDepth, ps.InclusionNumberOfUtxos, ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfUtxos) + circuit := InitializeCombinedCircuit(ps.InclusionTreeDepth, ps.InclusionNumberOfCompressedAccounts, ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfCompressedAccounts) - for i := 0; i < int(ps.InclusionNumberOfUtxos); i++ { + for i := 0; i < int(ps.InclusionNumberOfCompressedAccounts); i++ { circuit.Inclusion.Roots[i] = params.InclusionParameters.Inputs[i].Root circuit.Inclusion.Leaves[i] = params.InclusionParameters.Inputs[i].Leaf circuit.Inclusion.InPathIndices[i] = params.InclusionParameters.Inputs[i].PathIndex @@ -122,7 +122,7 @@ func (ps *ProvingSystem) ProveCombined(params *CombinedParameters) (*Proof, erro } } - for i := 0; i < int(ps.NonInclusionNumberOfUtxos); i++ { + for i := 0; i < int(ps.NonInclusionNumberOfCompressedAccounts); i++ { circuit.NonInclusion.Roots[i] = params.NonInclusionParameters.Inputs[i].Root circuit.NonInclusion.Values[i] = params.NonInclusionParameters.Inputs[i].Value circuit.NonInclusion.LeafLowerRangeValues[i] = params.NonInclusionParameters.Inputs[i].LeafLowerRangeValue @@ -140,7 +140,7 @@ func (ps *ProvingSystem) ProveCombined(params *CombinedParameters) (*Proof, erro return nil, err } - logging.Logger().Info().Msg("Proof combined" + strconv.Itoa(int(ps.InclusionTreeDepth)) + " " + strconv.Itoa(int(ps.InclusionNumberOfUtxos)) + " " + strconv.Itoa(int(ps.NonInclusionTreeDepth)) + " " + strconv.Itoa(int(ps.NonInclusionNumberOfUtxos))) + logging.Logger().Info().Msg("Proof combined" + strconv.Itoa(int(ps.InclusionTreeDepth)) + " " + strconv.Itoa(int(ps.InclusionNumberOfCompressedAccounts)) + " " + strconv.Itoa(int(ps.NonInclusionTreeDepth)) + " " + strconv.Itoa(int(ps.NonInclusionNumberOfCompressedAccounts))) proof, err := groth16.Prove(ps.ConstraintSystem, ps.ProvingKey, witness) if err != nil { logging.Logger().Error().Msg("combined prove error: " + err.Error()) diff --git a/light-prover/prover/combined_test.go b/light-prover/prover/combined_test.go index 2ea8d1984d..adfc34af38 100644 --- a/light-prover/prover/combined_test.go +++ b/light-prover/prover/combined_test.go @@ -43,14 +43,14 @@ func TestCombined(t *testing.T) { assert.NotEqual(len(params.InclusionParameters.Inputs), 0) assert.NotEqual(len(params.NonInclusionParameters.Inputs), 0) - var inclusionNumberOfUtxos = params.InclusionParameters.NumberOfUTXOs() + var inclusionNumberOfCompressedAccounts = params.InclusionParameters.NumberOfCompressedAccounts() var inclusionTreeDepth = params.InclusionParameters.TreeDepth() - inclusionRoots := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionLeaves := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionInPathIndices := make([]frontend.Variable, inclusionNumberOfUtxos) - inclusionInPathElements := make([][]frontend.Variable, inclusionNumberOfUtxos) - for i := 0; i < int(inclusionNumberOfUtxos); i++ { + inclusionRoots := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionLeaves := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionInPathIndices := make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + inclusionInPathElements := make([][]frontend.Variable, inclusionNumberOfCompressedAccounts) + for i := 0; i < int(inclusionNumberOfCompressedAccounts); i++ { inclusionInPathElements[i] = make([]frontend.Variable, inclusionTreeDepth) } @@ -63,17 +63,17 @@ func TestCombined(t *testing.T) { } } - var nonInclusionNumberOfUtxos = params.NonInclusionParameters.NumberOfUTXOs() + var nonInclusionNumberOfCompressedAccounts = params.NonInclusionParameters.NumberOfCompressedAccounts() var nonInclusionTreeDepth = params.NonInclusionParameters.TreeDepth() - nonInclusionRoots := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafLowerRangeValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafHigherRangeValues := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionLeafIndices := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionInPathIndices := make([]frontend.Variable, nonInclusionNumberOfUtxos) - nonInclusionInPathElements := make([][]frontend.Variable, nonInclusionNumberOfUtxos) - for i := 0; i < int(nonInclusionNumberOfUtxos); i++ { + nonInclusionRoots := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafLowerRangeValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafHigherRangeValues := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionLeafIndices := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionInPathIndices := make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + nonInclusionInPathElements := make([][]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + for i := 0; i < int(nonInclusionNumberOfCompressedAccounts); i++ { nonInclusionInPathElements[i] = make([]frontend.Variable, nonInclusionTreeDepth) } @@ -93,50 +93,50 @@ func TestCombined(t *testing.T) { circuit.Inclusion = InclusionCircuit{} circuit.NonInclusion = NonInclusionCircuit{} - circuit.Inclusion.Roots = make([]frontend.Variable, inclusionNumberOfUtxos) - circuit.Inclusion.Leaves = make([]frontend.Variable, inclusionNumberOfUtxos) - circuit.Inclusion.InPathIndices = make([]frontend.Variable, inclusionNumberOfUtxos) - circuit.Inclusion.InPathElements = make([][]frontend.Variable, inclusionNumberOfUtxos) - for i := 0; i < int(inclusionNumberOfUtxos); i++ { + circuit.Inclusion.Roots = make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + circuit.Inclusion.Leaves = make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + circuit.Inclusion.InPathIndices = make([]frontend.Variable, inclusionNumberOfCompressedAccounts) + circuit.Inclusion.InPathElements = make([][]frontend.Variable, inclusionNumberOfCompressedAccounts) + for i := 0; i < int(inclusionNumberOfCompressedAccounts); i++ { circuit.Inclusion.InPathElements[i] = make([]frontend.Variable, inclusionTreeDepth) } - circuit.Inclusion.NumberOfUtxos = inclusionNumberOfUtxos + circuit.Inclusion.NumberOfCompressedAccounts = inclusionNumberOfCompressedAccounts circuit.Inclusion.Depth = inclusionTreeDepth - circuit.NonInclusion.Roots = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.Values = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.LeafLowerRangeValues = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.LeafHigherRangeValues = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.LeafIndices = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.InPathIndices = make([]frontend.Variable, nonInclusionNumberOfUtxos) - circuit.NonInclusion.InPathElements = make([][]frontend.Variable, nonInclusionNumberOfUtxos) - for i := 0; i < int(nonInclusionNumberOfUtxos); i++ { + circuit.NonInclusion.Roots = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.Values = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.LeafLowerRangeValues = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.LeafHigherRangeValues = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.LeafIndices = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.InPathIndices = make([]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + circuit.NonInclusion.InPathElements = make([][]frontend.Variable, nonInclusionNumberOfCompressedAccounts) + for i := 0; i < int(nonInclusionNumberOfCompressedAccounts); i++ { circuit.NonInclusion.InPathElements[i] = make([]frontend.Variable, nonInclusionTreeDepth) } - circuit.NonInclusion.NumberOfUtxos = nonInclusionNumberOfUtxos + circuit.NonInclusion.NumberOfCompressedAccounts = nonInclusionNumberOfCompressedAccounts circuit.NonInclusion.Depth = nonInclusionTreeDepth assignment := &CombinedCircuit{ Inclusion: InclusionCircuit{ - Roots: inclusionRoots, - Leaves: inclusionLeaves, - InPathIndices: inclusionInPathIndices, - InPathElements: inclusionInPathElements, - NumberOfUtxos: inclusionNumberOfUtxos, - Depth: inclusionTreeDepth, + Roots: inclusionRoots, + Leaves: inclusionLeaves, + InPathIndices: inclusionInPathIndices, + InPathElements: inclusionInPathElements, + NumberOfCompressedAccounts: inclusionNumberOfCompressedAccounts, + Depth: inclusionTreeDepth, }, NonInclusion: NonInclusionCircuit{ - Roots: nonInclusionRoots, - Values: nonInclusionValues, - LeafLowerRangeValues: nonInclusionLeafLowerRangeValues, - LeafHigherRangeValues: nonInclusionLeafHigherRangeValues, - LeafIndices: nonInclusionLeafIndices, - InPathIndices: nonInclusionInPathIndices, - InPathElements: nonInclusionInPathElements, - NumberOfUtxos: nonInclusionNumberOfUtxos, - Depth: nonInclusionTreeDepth, + Roots: nonInclusionRoots, + Values: nonInclusionValues, + LeafLowerRangeValues: nonInclusionLeafLowerRangeValues, + LeafHigherRangeValues: nonInclusionLeafHigherRangeValues, + LeafIndices: nonInclusionLeafIndices, + InPathIndices: nonInclusionInPathIndices, + InPathElements: nonInclusionInPathElements, + NumberOfCompressedAccounts: nonInclusionNumberOfCompressedAccounts, + Depth: nonInclusionTreeDepth, }, } diff --git a/light-prover/prover/extractor.go b/light-prover/prover/extractor.go index 3cbb0ef589..a1578d8fb4 100644 --- a/light-prover/prover/extractor.go +++ b/light-prover/prover/extractor.go @@ -6,22 +6,22 @@ import ( "github.com/reilabs/gnark-lean-extractor/v2/extractor" ) -func ExtractLean(treeDepth uint32, numberOfUtxos uint32) (string, error) { - // Not checking for numberOfUtxos === 0 or treeDepth === 0 +func ExtractLean(treeDepth uint32, numberOfCompressedAccounts uint32) (string, error) { + // Not checking for numberOfCompressedAccounts === 0 or treeDepth === 0 // Initialising MerkleProofs slice with correct dimentions - roots := make([]frontend.Variable, numberOfUtxos) - leaves := make([]frontend.Variable, numberOfUtxos) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) + roots := make([]frontend.Variable, numberOfCompressedAccounts) + leaves := make([]frontend.Variable, numberOfCompressedAccounts) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) - for i := 0; i < int(numberOfUtxos); i++ { + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } inclusionCircuit := InclusionCircuit{ Depth: treeDepth, - NumberOfUtxos: numberOfUtxos, + NumberOfCompressedAccounts: numberOfCompressedAccounts, Roots: roots, Leaves: leaves, InPathIndices: inPathIndices, diff --git a/light-prover/prover/inclusion_circuit.go b/light-prover/prover/inclusion_circuit.go index 79057dd49f..d1cf5d768a 100644 --- a/light-prover/prover/inclusion_circuit.go +++ b/light-prover/prover/inclusion_circuit.go @@ -16,7 +16,7 @@ type InclusionCircuit struct { InPathIndices []frontend.Variable `gnark:"input"` InPathElements [][]frontend.Variable `gnark:"input"` - NumberOfUtxos uint32 + NumberOfCompressedAccounts uint32 Depth uint32 } @@ -27,21 +27,21 @@ func (circuit *InclusionCircuit) Define(api frontend.API) error { InPathElements: circuit.InPathElements, InPathIndices: circuit.InPathIndices, - NumberOfUtxos: circuit.NumberOfUtxos, + NumberOfCompressedAccounts: circuit.NumberOfCompressedAccounts, Depth: circuit.Depth, }) return nil } -func ImportInclusionSetup(treeDepth uint32, numberOfUtxos uint32, pkPath string, vkPath string) (*ProvingSystem, error) { - roots := make([]frontend.Variable, numberOfUtxos) - leaves := make([]frontend.Variable, numberOfUtxos) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) +func ImportInclusionSetup(treeDepth uint32, numberOfCompressedAccounts uint32, pkPath string, vkPath string) (*ProvingSystem, error) { + roots := make([]frontend.Variable, numberOfCompressedAccounts) + leaves := make([]frontend.Variable, numberOfCompressedAccounts) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) circuit := InclusionCircuit{ Depth: treeDepth, - NumberOfUtxos: numberOfUtxos, + NumberOfCompressedAccounts: numberOfCompressedAccounts, Roots: roots, Leaves: leaves, InPathIndices: inPathIndices, @@ -64,5 +64,5 @@ func ImportInclusionSetup(treeDepth uint32, numberOfUtxos uint32, pkPath string, return nil, err } - return &ProvingSystem{treeDepth, numberOfUtxos, 0, 0, pk, vk, ccs}, nil + return &ProvingSystem{treeDepth, numberOfCompressedAccounts, 0, 0, pk, vk, ccs}, nil } diff --git a/light-prover/prover/inclusion_proving_system.go b/light-prover/prover/inclusion_proving_system.go index 95f81be613..fd241703c8 100644 --- a/light-prover/prover/inclusion_proving_system.go +++ b/light-prover/prover/inclusion_proving_system.go @@ -24,7 +24,7 @@ type InclusionParameters struct { Inputs []InclusionInputs } -func (p *InclusionParameters) NumberOfUTXOs() uint32 { +func (p *InclusionParameters) NumberOfCompressedAccounts() uint32 { return uint32(len(p.Inputs)) } @@ -35,39 +35,39 @@ func (p *InclusionParameters) TreeDepth() uint32 { return uint32(len(p.Inputs[0].PathElements)) } -func (p *InclusionParameters) ValidateShape(treeDepth uint32, numOfUTXOs uint32) error { - if p.NumberOfUTXOs() != numOfUTXOs { - return fmt.Errorf("wrong number of utxos: %d", p.NumberOfUTXOs()) +func (p *InclusionParameters) ValidateShape(treeDepth uint32, numOfCompressedAccounts uint32) error { + if p.NumberOfCompressedAccounts() != numOfCompressedAccounts { + return fmt.Errorf("wrong number of compressed accounts: %d", p.NumberOfCompressedAccounts()) } if p.TreeDepth() != treeDepth { - return fmt.Errorf("wrong size of merkle proof for proof %d: %d", p.NumberOfUTXOs(), p.TreeDepth()) + return fmt.Errorf("wrong size of merkle proof for proof %d: %d", p.NumberOfCompressedAccounts(), p.TreeDepth()) } return nil } -func R1CSInclusion(treeDepth uint32, numberOfUtxos uint32) (constraint.ConstraintSystem, error) { - roots := make([]frontend.Variable, numberOfUtxos) - leaves := make([]frontend.Variable, numberOfUtxos) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) +func R1CSInclusion(treeDepth uint32, numberOfCompressedAccounts uint32) (constraint.ConstraintSystem, error) { + roots := make([]frontend.Variable, numberOfCompressedAccounts) + leaves := make([]frontend.Variable, numberOfCompressedAccounts) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) - for i := 0; i < int(numberOfUtxos); i++ { + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } circuit := InclusionCircuit{ - Depth: treeDepth, - NumberOfUtxos: numberOfUtxos, - Roots: roots, - Leaves: leaves, - InPathIndices: inPathIndices, - InPathElements: inPathElements, + Depth: treeDepth, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Roots: roots, + Leaves: leaves, + InPathIndices: inPathIndices, + InPathElements: inPathElements, } return frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit) } -func SetupInclusion(treeDepth uint32, numberOfUtxos uint32) (*ProvingSystem, error) { - ccs, err := R1CSInclusion(treeDepth, numberOfUtxos) +func SetupInclusion(treeDepth uint32, numberOfCompressedAccounts uint32) (*ProvingSystem, error) { + ccs, err := R1CSInclusion(treeDepth, numberOfCompressedAccounts) if err != nil { return nil, err } @@ -75,20 +75,20 @@ func SetupInclusion(treeDepth uint32, numberOfUtxos uint32) (*ProvingSystem, err if err != nil { return nil, err } - return &ProvingSystem{treeDepth, numberOfUtxos, 0, 0, pk, vk, ccs}, nil + return &ProvingSystem{treeDepth, numberOfCompressedAccounts, 0, 0, pk, vk, ccs}, nil } func (ps *ProvingSystem) ProveInclusion(params *InclusionParameters) (*Proof, error) { - if err := params.ValidateShape(ps.InclusionTreeDepth, ps.InclusionNumberOfUtxos); err != nil { + if err := params.ValidateShape(ps.InclusionTreeDepth, ps.InclusionNumberOfCompressedAccounts); err != nil { return nil, err } - inPathIndices := make([]frontend.Variable, ps.InclusionNumberOfUtxos) - roots := make([]frontend.Variable, ps.InclusionNumberOfUtxos) - leaves := make([]frontend.Variable, ps.InclusionNumberOfUtxos) - inPathElements := make([][]frontend.Variable, ps.InclusionNumberOfUtxos) + inPathIndices := make([]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) + roots := make([]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) + leaves := make([]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) - for i := 0; i < int(ps.InclusionNumberOfUtxos); i++ { + for i := 0; i < int(ps.InclusionNumberOfCompressedAccounts); i++ { roots[i] = params.Inputs[i].Root leaves[i] = params.Inputs[i].Leaf inPathIndices[i] = params.Inputs[i].PathIndex @@ -110,7 +110,7 @@ func (ps *ProvingSystem) ProveInclusion(params *InclusionParameters) (*Proof, er return nil, err } - logging.Logger().Info().Msg("Proof inclusion" + strconv.Itoa(int(ps.InclusionTreeDepth)) + " " + strconv.Itoa(int(ps.InclusionNumberOfUtxos))) + logging.Logger().Info().Msg("Proof inclusion" + strconv.Itoa(int(ps.InclusionTreeDepth)) + " " + strconv.Itoa(int(ps.InclusionNumberOfCompressedAccounts))) proof, err := groth16.Prove(ps.ConstraintSystem, ps.ProvingKey, witness) if err != nil { return nil, err @@ -120,12 +120,12 @@ func (ps *ProvingSystem) ProveInclusion(params *InclusionParameters) (*Proof, er } func (ps *ProvingSystem) VerifyInclusion(root []big.Int, leaf []big.Int, proof *Proof) error { - leaves := make([]frontend.Variable, ps.InclusionNumberOfUtxos) + leaves := make([]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) for i, v := range leaf { leaves[i] = v } - roots := make([]frontend.Variable, ps.InclusionNumberOfUtxos) + roots := make([]frontend.Variable, ps.InclusionNumberOfCompressedAccounts) for i, v := range root { roots[i] = v } diff --git a/light-prover/prover/inclusion_test.go b/light-prover/prover/inclusion_test.go index bf3675fa9d..9b0d7c0bf2 100644 --- a/light-prover/prover/inclusion_test.go +++ b/light-prover/prover/inclusion_test.go @@ -4,13 +4,14 @@ import ( "bufio" "encoding/json" "fmt" + "os" + "strings" + "testing" + "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/test" - "os" - "strings" - "testing" ) // Iterate over data from csv file "inclusion_test_data.tsv", which contains test data for the inclusion proof. @@ -38,14 +39,14 @@ func TestInclusion(t *testing.T) { err := json.Unmarshal([]byte(splitLine[1]), ¶ms) assert.Nil(err, "Error unmarshalling inputs: ", err) - var numberOfUtxos = params.NumberOfUTXOs() + var numberOfCompressedAccounts = params.NumberOfCompressedAccounts() var treeDepth = params.TreeDepth() - roots := make([]frontend.Variable, numberOfUtxos) - leaves := make([]frontend.Variable, numberOfUtxos) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) - for i := 0; i < int(numberOfUtxos); i++ { + roots := make([]frontend.Variable, numberOfCompressedAccounts) + leaves := make([]frontend.Variable, numberOfCompressedAccounts) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } @@ -60,15 +61,15 @@ func TestInclusion(t *testing.T) { } var circuit InclusionCircuit - circuit.Roots = make([]frontend.Variable, numberOfUtxos) - circuit.Leaves = make([]frontend.Variable, numberOfUtxos) - circuit.InPathIndices = make([]frontend.Variable, numberOfUtxos) - circuit.InPathElements = make([][]frontend.Variable, numberOfUtxos) - for i := 0; i < int(numberOfUtxos); i++ { + circuit.Roots = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.Leaves = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.InPathIndices = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.InPathElements = make([][]frontend.Variable, numberOfCompressedAccounts) + for i := 0; i < int(numberOfCompressedAccounts); i++ { circuit.InPathElements[i] = make([]frontend.Variable, treeDepth) } - circuit.NumberOfUtxos = numberOfUtxos + circuit.NumberOfCompressedAccounts = numberOfCompressedAccounts circuit.Depth = treeDepth // Check if the expected result is "true" or "false" @@ -76,22 +77,22 @@ func TestInclusion(t *testing.T) { if expectedResult == "0" { // Run the failing test assert.ProverFailed(&circuit, &InclusionCircuit{ - Roots: roots, - Leaves: leaves, - InPathIndices: inPathIndices, - InPathElements: inPathElements, - NumberOfUtxos: numberOfUtxos, - Depth: treeDepth, + Roots: roots, + Leaves: leaves, + InPathIndices: inPathIndices, + InPathElements: inPathElements, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Depth: treeDepth, }, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254), test.NoSerialization()) } else if expectedResult == "1" { // Run the passing test assert.ProverSucceeded(&circuit, &InclusionCircuit{ - Roots: roots, - Leaves: leaves, - InPathIndices: inPathIndices, - InPathElements: inPathElements, - NumberOfUtxos: numberOfUtxos, - Depth: treeDepth, + Roots: roots, + Leaves: leaves, + InPathIndices: inPathIndices, + InPathElements: inPathElements, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Depth: treeDepth, }, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254), test.NoSerialization()) } else { fmt.Println("Invalid expected result: ", expectedResult) diff --git a/light-prover/prover/marshal.go b/light-prover/prover/marshal.go index 261b035b53..92a1443ede 100644 --- a/light-prover/prover/marshal.go +++ b/light-prover/prover/marshal.go @@ -104,7 +104,7 @@ func (ps *ProvingSystem) WriteTo(w io.Writer) (int64, error) { return totalWritten, err } - binary.BigEndian.PutUint32(intBuf[:], uint32(ps.InclusionNumberOfUtxos)) + binary.BigEndian.PutUint32(intBuf[:], uint32(ps.InclusionNumberOfCompressedAccounts)) written, err = w.Write(intBuf[:]) totalWritten += int64(written) if err != nil { @@ -118,7 +118,7 @@ func (ps *ProvingSystem) WriteTo(w io.Writer) (int64, error) { return totalWritten, err } - binary.BigEndian.PutUint32(intBuf[:], uint32(ps.NonInclusionNumberOfUtxos)) + binary.BigEndian.PutUint32(intBuf[:], uint32(ps.NonInclusionNumberOfCompressedAccounts)) written, err = w.Write(intBuf[:]) totalWritten += int64(written) if err != nil { @@ -162,7 +162,7 @@ func (ps *ProvingSystem) UnsafeReadFrom(r io.Reader) (int64, error) { if err != nil { return totalRead, err } - ps.InclusionNumberOfUtxos = binary.BigEndian.Uint32(intBuf[:]) + ps.InclusionNumberOfCompressedAccounts = binary.BigEndian.Uint32(intBuf[:]) read, err = io.ReadFull(r, intBuf[:]) totalRead += int64(read) @@ -176,7 +176,7 @@ func (ps *ProvingSystem) UnsafeReadFrom(r io.Reader) (int64, error) { if err != nil { return totalRead, err } - ps.NonInclusionNumberOfUtxos = binary.BigEndian.Uint32(intBuf[:]) + ps.NonInclusionNumberOfCompressedAccounts = binary.BigEndian.Uint32(intBuf[:]) ps.ProvingKey = groth16.NewProvingKey(ecc.BN254) keyRead, err := ps.ProvingKey.UnsafeReadFrom(r) diff --git a/light-prover/prover/marshal_inclusion.go b/light-prover/prover/marshal_inclusion.go index 89d5edc80a..81817531a1 100644 --- a/light-prover/prover/marshal_inclusion.go +++ b/light-prover/prover/marshal_inclusion.go @@ -33,8 +33,8 @@ func (p *InclusionParameters) MarshalJSON() ([]byte, error) { func (p *InclusionParameters) CreateInclusionParametersJSON() InclusionParametersJSON { paramsJson := InclusionParametersJSON{} - paramsJson.Inputs = make([]InclusionProofInputsJSON, p.NumberOfUTXOs()) - for i := 0; i < int(p.NumberOfUTXOs()); i++ { + paramsJson.Inputs = make([]InclusionProofInputsJSON, p.NumberOfCompressedAccounts()) + for i := 0; i < int(p.NumberOfCompressedAccounts()); i++ { paramsJson.Inputs[i].Root = toHex(&p.Inputs[i].Root) paramsJson.Inputs[i].Leaf = toHex(&p.Inputs[i].Leaf) paramsJson.Inputs[i].PathIndex = p.Inputs[i].PathIndex diff --git a/light-prover/prover/marshal_non_inclusion.go b/light-prover/prover/marshal_non_inclusion.go index 421b98e3a9..08e248c8b4 100644 --- a/light-prover/prover/marshal_non_inclusion.go +++ b/light-prover/prover/marshal_non_inclusion.go @@ -36,8 +36,8 @@ func (p *NonInclusionParameters) MarshalJSON() ([]byte, error) { func (p *NonInclusionParameters) CreateNonInclusionParametersJSON() NonInclusionParametersJSON { paramsJson := NonInclusionParametersJSON{} - paramsJson.Inputs = make([]NonInclusionProofInputsJSON, p.NumberOfUTXOs()) - for i := 0; i < int(p.NumberOfUTXOs()); i++ { + paramsJson.Inputs = make([]NonInclusionProofInputsJSON, p.NumberOfCompressedAccounts()) + for i := 0; i < int(p.NumberOfCompressedAccounts()); i++ { paramsJson.Inputs[i].Root = toHex(&p.Inputs[i].Root) paramsJson.Inputs[i].Value = toHex(&p.Inputs[i].Value) paramsJson.Inputs[i].PathIndex = p.Inputs[i].PathIndex diff --git a/light-prover/prover/non_inclusion_circuit.go b/light-prover/prover/non_inclusion_circuit.go index d562c984b0..177c401007 100644 --- a/light-prover/prover/non_inclusion_circuit.go +++ b/light-prover/prover/non_inclusion_circuit.go @@ -20,7 +20,7 @@ type NonInclusionCircuit struct { InPathIndices []frontend.Variable `gnark:"input"` InPathElements [][]frontend.Variable `gnark:"input"` - NumberOfUtxos uint32 + NumberOfCompressedAccounts uint32 Depth uint32 } @@ -36,34 +36,34 @@ func (circuit *NonInclusionCircuit) Define(api frontend.API) error { InPathElements: circuit.InPathElements, InPathIndices: circuit.InPathIndices, - NumberOfUtxos: circuit.NumberOfUtxos, + NumberOfCompressedAccounts: circuit.NumberOfCompressedAccounts, Depth: circuit.Depth, } roots := abstractor.Call1(api, proof) - for i := 0; i < int(circuit.NumberOfUtxos); i++ { + for i := 0; i < int(circuit.NumberOfCompressedAccounts); i++ { api.AssertIsEqual(roots[i], circuit.Roots[i]) } return nil } -func ImportNonInclusionSetup(treeDepth uint32, numberOfUtxos uint32, pkPath string, vkPath string) (*ProvingSystem, error) { - roots := make([]frontend.Variable, numberOfUtxos) - values := make([]frontend.Variable, numberOfUtxos) +func ImportNonInclusionSetup(treeDepth uint32, numberOfCompressedAccounts uint32, pkPath string, vkPath string) (*ProvingSystem, error) { + roots := make([]frontend.Variable, numberOfCompressedAccounts) + values := make([]frontend.Variable, numberOfCompressedAccounts) - leafLowerRangeValues := make([]frontend.Variable, numberOfUtxos) - leafHigherRangeValues := make([]frontend.Variable, numberOfUtxos) - leafIndices := make([]frontend.Variable, numberOfUtxos) + leafLowerRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafHigherRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafIndices := make([]frontend.Variable, numberOfCompressedAccounts) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) - for i := 0; i < int(numberOfUtxos); i++ { + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } circuit := NonInclusionCircuit{ Depth: treeDepth, - NumberOfUtxos: numberOfUtxos, + NumberOfCompressedAccounts: numberOfCompressedAccounts, Roots: roots, Values: values, LeafLowerRangeValues: leafLowerRangeValues, @@ -90,5 +90,5 @@ func ImportNonInclusionSetup(treeDepth uint32, numberOfUtxos uint32, pkPath stri return nil, err } - return &ProvingSystem{0, 0, treeDepth, numberOfUtxos, pk, vk, ccs}, nil + return &ProvingSystem{0, 0, treeDepth, numberOfCompressedAccounts, pk, vk, ccs}, nil } diff --git a/light-prover/prover/non_inclusion_proving_system.go b/light-prover/prover/non_inclusion_proving_system.go index c775809937..6477beee4b 100644 --- a/light-prover/prover/non_inclusion_proving_system.go +++ b/light-prover/prover/non_inclusion_proving_system.go @@ -28,7 +28,7 @@ type NonInclusionParameters struct { Inputs []NonInclusionInputs } -func (p *NonInclusionParameters) NumberOfUTXOs() uint32 { +func (p *NonInclusionParameters) NumberOfCompressedAccounts() uint32 { return uint32(len(p.Inputs)) } @@ -39,47 +39,47 @@ func (p *NonInclusionParameters) TreeDepth() uint32 { return uint32(len(p.Inputs[0].PathElements)) } -func (p *NonInclusionParameters) ValidateShape(treeDepth uint32, numOfUTXOs uint32) error { - if p.NumberOfUTXOs() != numOfUTXOs { - return fmt.Errorf("wrong number of utxos, p.NumberOfUTXOs: %d, numOfUTXOs = %d", p.NumberOfUTXOs(), numOfUTXOs) +func (p *NonInclusionParameters) ValidateShape(treeDepth uint32, numOfCompressedAccounts uint32) error { + if p.NumberOfCompressedAccounts() != numOfCompressedAccounts { + return fmt.Errorf("wrong number of compressed accounts, p.NumberOfCompressedAccounts: %d, numOfCompressedAccounts = %d", p.NumberOfCompressedAccounts(), numOfCompressedAccounts) } if p.TreeDepth() != treeDepth { - return fmt.Errorf("wrong size of merkle proof for proof %d: %d", p.NumberOfUTXOs(), p.TreeDepth()) + return fmt.Errorf("wrong size of merkle proof for proof %d: %d", p.NumberOfCompressedAccounts(), p.TreeDepth()) } return nil } -func R1CSNonInclusion(treeDepth uint32, numberOfUtxos uint32) (constraint.ConstraintSystem, error) { - roots := make([]frontend.Variable, numberOfUtxos) - values := make([]frontend.Variable, numberOfUtxos) +func R1CSNonInclusion(treeDepth uint32, numberOfCompressedAccounts uint32) (constraint.ConstraintSystem, error) { + roots := make([]frontend.Variable, numberOfCompressedAccounts) + values := make([]frontend.Variable, numberOfCompressedAccounts) - leafLowerRangeValues := make([]frontend.Variable, numberOfUtxos) - leafHigherRangeValues := make([]frontend.Variable, numberOfUtxos) - leafIndices := make([]frontend.Variable, numberOfUtxos) + leafLowerRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafHigherRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafIndices := make([]frontend.Variable, numberOfCompressedAccounts) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) - for i := 0; i < int(numberOfUtxos); i++ { + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } circuit := NonInclusionCircuit{ - Depth: treeDepth, - NumberOfUtxos: numberOfUtxos, - Roots: roots, - Values: values, - LeafLowerRangeValues: leafLowerRangeValues, - LeafHigherRangeValues: leafHigherRangeValues, - LeafIndices: leafIndices, - InPathIndices: inPathIndices, - InPathElements: inPathElements, + Depth: treeDepth, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Roots: roots, + Values: values, + LeafLowerRangeValues: leafLowerRangeValues, + LeafHigherRangeValues: leafHigherRangeValues, + LeafIndices: leafIndices, + InPathIndices: inPathIndices, + InPathElements: inPathElements, } return frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit) } -func SetupNonInclusion(treeDepth uint32, numberOfUtxos uint32) (*ProvingSystem, error) { - ccs, err := R1CSNonInclusion(treeDepth, numberOfUtxos) +func SetupNonInclusion(treeDepth uint32, numberOfCompressedAccounts uint32) (*ProvingSystem, error) { + ccs, err := R1CSNonInclusion(treeDepth, numberOfCompressedAccounts) if err != nil { return nil, err } @@ -87,25 +87,25 @@ func SetupNonInclusion(treeDepth uint32, numberOfUtxos uint32) (*ProvingSystem, if err != nil { return nil, err } - return &ProvingSystem{0, 0, treeDepth, numberOfUtxos, pk, vk, ccs}, nil + return &ProvingSystem{0, 0, treeDepth, numberOfCompressedAccounts, pk, vk, ccs}, nil } func (ps *ProvingSystem) ProveNonInclusion(params *NonInclusionParameters) (*Proof, error) { - if err := params.ValidateShape(ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfUtxos); err != nil { + if err := params.ValidateShape(ps.NonInclusionTreeDepth, ps.NonInclusionNumberOfCompressedAccounts); err != nil { return nil, err } - roots := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) - values := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) + roots := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) + values := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) - leafLowerRangeValues := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) - leafHigherRangeValues := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) - leafIndices := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) + leafLowerRangeValues := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) + leafHigherRangeValues := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) + leafIndices := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) - inPathElements := make([][]frontend.Variable, ps.NonInclusionNumberOfUtxos) - inPathIndices := make([]frontend.Variable, ps.NonInclusionNumberOfUtxos) + inPathElements := make([][]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) + inPathIndices := make([]frontend.Variable, ps.NonInclusionNumberOfCompressedAccounts) - for i := 0; i < int(ps.NonInclusionNumberOfUtxos); i++ { + for i := 0; i < int(ps.NonInclusionNumberOfCompressedAccounts); i++ { roots[i] = params.Inputs[i].Root values[i] = params.Inputs[i].Value leafLowerRangeValues[i] = params.Inputs[i].LeafLowerRangeValue @@ -133,7 +133,7 @@ func (ps *ProvingSystem) ProveNonInclusion(params *NonInclusionParameters) (*Pro return nil, err } - logging.Logger().Info().Msg("Proof non-inclusion" + strconv.Itoa(int(ps.NonInclusionTreeDepth)) + " " + strconv.Itoa(int(ps.NonInclusionNumberOfUtxos))) + logging.Logger().Info().Msg("Proof non-inclusion" + strconv.Itoa(int(ps.NonInclusionTreeDepth)) + " " + strconv.Itoa(int(ps.NonInclusionNumberOfCompressedAccounts))) proof, err := groth16.Prove(ps.ConstraintSystem, ps.ProvingKey, witness) if err != nil { logging.Logger().Error().Msg("non-inclusion prove error: " + err.Error()) diff --git a/light-prover/prover/non_inclusion_test.go b/light-prover/prover/non_inclusion_test.go index 0e1c32f114..b757e0ad5c 100644 --- a/light-prover/prover/non_inclusion_test.go +++ b/light-prover/prover/non_inclusion_test.go @@ -4,13 +4,14 @@ import ( "bufio" "encoding/json" "fmt" + "os" + "strings" + "testing" + "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/test" - "os" - "strings" - "testing" ) // Iterate over data from csv file "inclusion_test_data.tsv", which contains test data for the inclusion proof. @@ -38,18 +39,18 @@ func TestNonInclusion(t *testing.T) { err := json.Unmarshal([]byte(splitLine[1]), ¶ms) assert.Nil(err, "Error unmarshalling inputs: ", err) - var numberOfUtxos = params.NumberOfUTXOs() + var numberOfCompressedAccounts = params.NumberOfCompressedAccounts() var treeDepth = params.TreeDepth() - roots := make([]frontend.Variable, numberOfUtxos) - values := make([]frontend.Variable, numberOfUtxos) - leafLowerRangeValues := make([]frontend.Variable, numberOfUtxos) - leafHigherRangeValues := make([]frontend.Variable, numberOfUtxos) - leafIndices := make([]frontend.Variable, numberOfUtxos) + roots := make([]frontend.Variable, numberOfCompressedAccounts) + values := make([]frontend.Variable, numberOfCompressedAccounts) + leafLowerRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafHigherRangeValues := make([]frontend.Variable, numberOfCompressedAccounts) + leafIndices := make([]frontend.Variable, numberOfCompressedAccounts) - inPathIndices := make([]frontend.Variable, numberOfUtxos) - inPathElements := make([][]frontend.Variable, numberOfUtxos) - for i := 0; i < int(numberOfUtxos); i++ { + inPathIndices := make([]frontend.Variable, numberOfCompressedAccounts) + inPathElements := make([][]frontend.Variable, numberOfCompressedAccounts) + for i := 0; i < int(numberOfCompressedAccounts); i++ { inPathElements[i] = make([]frontend.Variable, treeDepth) } @@ -66,18 +67,18 @@ func TestNonInclusion(t *testing.T) { } var circuit NonInclusionCircuit - circuit.Roots = make([]frontend.Variable, numberOfUtxos) - circuit.Values = make([]frontend.Variable, numberOfUtxos) - circuit.LeafLowerRangeValues = make([]frontend.Variable, numberOfUtxos) - circuit.LeafHigherRangeValues = make([]frontend.Variable, numberOfUtxos) - circuit.LeafIndices = make([]frontend.Variable, numberOfUtxos) - circuit.InPathIndices = make([]frontend.Variable, numberOfUtxos) - circuit.InPathElements = make([][]frontend.Variable, numberOfUtxos) - for i := 0; i < int(numberOfUtxos); i++ { + circuit.Roots = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.Values = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.LeafLowerRangeValues = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.LeafHigherRangeValues = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.LeafIndices = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.InPathIndices = make([]frontend.Variable, numberOfCompressedAccounts) + circuit.InPathElements = make([][]frontend.Variable, numberOfCompressedAccounts) + for i := 0; i < int(numberOfCompressedAccounts); i++ { circuit.InPathElements[i] = make([]frontend.Variable, treeDepth) } - circuit.NumberOfUtxos = numberOfUtxos + circuit.NumberOfCompressedAccounts = numberOfCompressedAccounts circuit.Depth = treeDepth // Check if the expected result is "true" or "false" @@ -85,28 +86,28 @@ func TestNonInclusion(t *testing.T) { if expectedResult == "0" { // Run the failing test assert.ProverFailed(&circuit, &NonInclusionCircuit{ - Roots: roots, - Values: values, - LeafLowerRangeValues: leafLowerRangeValues, - LeafHigherRangeValues: leafHigherRangeValues, - LeafIndices: leafIndices, - InPathIndices: inPathIndices, - InPathElements: inPathElements, - NumberOfUtxos: numberOfUtxos, - Depth: treeDepth, + Roots: roots, + Values: values, + LeafLowerRangeValues: leafLowerRangeValues, + LeafHigherRangeValues: leafHigherRangeValues, + LeafIndices: leafIndices, + InPathIndices: inPathIndices, + InPathElements: inPathElements, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Depth: treeDepth, }, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254), test.NoSerialization()) } else if expectedResult == "1" { // Run the passing test assert.ProverSucceeded(&circuit, &NonInclusionCircuit{ - Roots: roots, - Values: values, - LeafLowerRangeValues: leafLowerRangeValues, - LeafHigherRangeValues: leafHigherRangeValues, - LeafIndices: leafIndices, - InPathIndices: inPathIndices, - InPathElements: inPathElements, - NumberOfUtxos: numberOfUtxos, - Depth: treeDepth, + Roots: roots, + Values: values, + LeafLowerRangeValues: leafLowerRangeValues, + LeafHigherRangeValues: leafHigherRangeValues, + LeafIndices: leafIndices, + InPathIndices: inPathIndices, + InPathElements: inPathElements, + NumberOfCompressedAccounts: numberOfCompressedAccounts, + Depth: treeDepth, }, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254), test.NoSerialization()) } else { fmt.Println("Invalid expected result: ", expectedResult) diff --git a/light-prover/server/server.go b/light-prover/server/server.go index 16d91123c0..2a930805d9 100644 --- a/light-prover/server/server.go +++ b/light-prover/server/server.go @@ -5,11 +5,12 @@ import ( "encoding/json" "errors" "fmt" - "github.com/gorilla/handlers" "io" "light/light-prover/logging" "light/light-prover/prover" "net/http" + + "github.com/gorilla/handlers" //"github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -176,18 +177,18 @@ func (handler proveHandler) inclusionProof(buf []byte) (*prover.Proof, *Error) { } - var numberOfUtxos = uint32(len(params.Inputs)) + var numberOfCompressedAccounts = uint32(len(params.Inputs)) var ps *prover.ProvingSystem for _, provingSystem := range handler.provingSystem { - if provingSystem.InclusionNumberOfUtxos == numberOfUtxos { + if provingSystem.InclusionNumberOfCompressedAccounts == numberOfCompressedAccounts { ps = provingSystem break } } if ps == nil { - return nil, provingError(fmt.Errorf("no proving system for %d utxos", numberOfUtxos)) + return nil, provingError(fmt.Errorf("no proving system for %d compressedAccounts", numberOfCompressedAccounts)) } proof, err = ps.ProveInclusion(¶ms) @@ -208,17 +209,17 @@ func (handler proveHandler) nonInclusionProof(buf []byte) (*prover.Proof, *Error logging.Logger().Info().Msg(err.Error()) return nil, malformedBodyError(err) } - var numberOfUtxos = uint32(len(params.Inputs)) + var numberOfCompressedAccounts = uint32(len(params.Inputs)) var ps *prover.ProvingSystem for _, provingSystem := range handler.provingSystem { - if provingSystem.NonInclusionNumberOfUtxos == numberOfUtxos { + if provingSystem.NonInclusionNumberOfCompressedAccounts == numberOfCompressedAccounts { ps = provingSystem break } } if ps == nil { - return nil, provingError(fmt.Errorf("no proving system for %d utxos", numberOfUtxos)) + return nil, provingError(fmt.Errorf("no proving system for %d compressedAccounts", numberOfCompressedAccounts)) } proof, err = ps.ProveNonInclusion(¶ms) @@ -241,19 +242,19 @@ func (handler proveHandler) combinedProof(buf []byte) (*prover.Proof, *Error) { } - var inclusionNumberOfUtxos = uint32(len(params.InclusionParameters.Inputs)) - var nonInclusionNumberOfUtxos = uint32(len(params.NonInclusionParameters.Inputs)) + var inclusionNumberOfCompressedAccounts = uint32(len(params.InclusionParameters.Inputs)) + var nonInclusionNumberOfCompressedAccounts = uint32(len(params.NonInclusionParameters.Inputs)) var ps *prover.ProvingSystem for _, provingSystem := range handler.provingSystem { - if provingSystem.InclusionNumberOfUtxos == inclusionNumberOfUtxos && provingSystem.NonInclusionNumberOfUtxos == nonInclusionNumberOfUtxos { + if provingSystem.InclusionNumberOfCompressedAccounts == inclusionNumberOfCompressedAccounts && provingSystem.NonInclusionNumberOfCompressedAccounts == nonInclusionNumberOfCompressedAccounts { ps = provingSystem break } } if ps == nil { - return nil, provingError(fmt.Errorf("no proving system for %d inclusion utxos & %d non-inclusion", inclusionNumberOfUtxos, nonInclusionNumberOfUtxos)) + return nil, provingError(fmt.Errorf("no proving system for %d inclusion compressedAccounts & %d non-inclusion", inclusionNumberOfCompressedAccounts, nonInclusionNumberOfCompressedAccounts)) } proof, err = ps.ProveCombined(¶ms) if err != nil {