Skip to content

Commit

Permalink
fix: is less
Browse files Browse the repository at this point in the history
tests: add test cases, readme add test instructions

disable nx cache in nx json

Disable nx cloud cache
  • Loading branch information
ananas-block committed Jun 21, 2024
1 parent 8be1695 commit b28be3e
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Build CLI
run: |
source ./scripts/devenv.sh
npx nx build @lightprotocol/zk-compression-cli
npx nx build @lightprotocol/zk-compression-cli --skip-nx-cache
- name: Run stateless.js tests
run: |
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/light-sdk-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
include:
- name: sdk-tests-light-rpc-cli
sub-tests: '[
"@lightprotocol/zk-compression-cli",
"@lightprotocol/circuit-lib.js",
"@lightprotocol/prover.js",
"@lightprotocol/zk-compression-cli",
]'
steps:
- name: Checkout sources
Expand All @@ -38,6 +38,11 @@ jobs:
- name: Setup and build
uses: ./.github/actions/setup-and-build

- name: Build CLI
run: |
source ./scripts/devenv.sh
npx nx build @lightprotocol/zk-compression-cli --skip-nx-cache
- name: ${{ matrix.name }}
run: |
source ./scripts/devenv.sh
Expand Down
3 changes: 3 additions & 0 deletions circuit-lib/circuit-lib.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"nx": {
"targets": {
"build": {
"inputs": [
"{projectRoot}/cli"
],
"outputs": [
"{projectRoot}/lib"
]
Expand Down
20 changes: 19 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,23 @@
"keywords": [
"oclif"
],
"types": "dist/index.d.ts"
"types": "dist/index.d.ts",
"nx": {
"targets": {
"build": {
"inputs": [
"{projectRoot}/js",
"{projectRoot}/programs",
"{projectRoot}/gnark-prover",
"{projectRoot}/forester"
],
"outputs": [
"{projectRoot}/bin",
"{projectRoot}/dist",
"{projectRoot}/lib",
"{projectRoot}/test_bin"
]
}
}
}
}
2 changes: 1 addition & 1 deletion cli/src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function spawnBinary(command: string, args: string[] = []) {

const out = fs.openSync(`${logDir}/${binaryName}.log`, "a");
const err = fs.openSync(`${logDir}/${binaryName}.log`, "a");

console.log("Spawning binary: ", command, args);
const spawnedProcess = spawn(command, args, {
stdio: ["ignore", out, err],
shell: false,
Expand Down
1 change: 1 addition & 0 deletions cli/src/utils/processProverServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import {
executeCommand,
killProcess,
killProcessByPort,
spawnBinary,
Expand Down
1 change: 1 addition & 0 deletions js/compressed-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"targets": {
"build": {
"inputs": [
"{workspaceRoot}/cli",
"{workspaceRoot}/target/idl",
"{workspaceRoot}/target/types"
]
Expand Down
1 change: 1 addition & 0 deletions js/stateless.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"targets": {
"build": {
"inputs": [
"{workspaceRoot}/cli",
"{workspaceRoot}/target/idl",
"{workspaceRoot}/target/types"
]
Expand Down
11 changes: 11 additions & 0 deletions light-prover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ Bucket # % Histogram
[100ms, +Inf] 0 0.00%
```


## Unit Tests
To run specific tests cd into respective folder (merkle-tree/prover) and `go test -v -run <function-name>`

1. Integration tests
`go test`
2. Generate csv Test Data for combined, inclusion, non-inclusion
`cd merkle-tree && go test`
3. Unit Tests
`cd prover && go test`

## Docker

```shell
Expand Down
5 changes: 3 additions & 2 deletions light-prover/merkle-tree/test_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package merkle_tree

import (
"fmt"
"github.com/iden3/go-iden3-crypto/poseidon"
"light/light-prover/prover"
"math/big"
"math/rand"

"github.com/iden3/go-iden3-crypto/poseidon"
)

type PoseidonNode interface {
Expand Down Expand Up @@ -191,7 +192,7 @@ func BuildTestNonInclusionTree(depth int, numberOfUtxos int, random bool, valid
value.Add(leafLower, big.NewInt(1))
} else {
if lowValue {
value.Add(leafLower, big.NewInt(-1))
value.Sub(leafLower, big.NewInt(1))
} else {
value.Add(leafUpper, big.NewInt(1))
}
Expand Down
29 changes: 20 additions & 9 deletions light-prover/merkle-tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ type InclusionTreeValidPair struct {
// - `validPair`: A valid tree constructed with input parameters. The Valid field is set to `true`.
// - `invalidRootPair`: A valid tree but the root value is invalidated by setting it to an integer 999. The Valid field is set to `false`.
// - `invalidLeafPair`: A valid tree where a leaf value is invalidated by setting it to an integer 999. The Valid field wis set to `false`.
// - `invalidInPathIndicesPair`: A valid tree but the InPathIndices value is invalidated by setting it to an integer 999. The Valid field is set to `false`.
// - `invalidInPathIndicesPair`: A valid tree but the InPathIndices value is invalidated by adding 1 to the index. The Valid field is set to `false`.
// - `invalidInPathIndicesPair`: A valid tree but the InPathIndices value is invalidated by subtracting 1 from the index. The Valid field is set to `false`.
// - `invalidInPathElementsPair`: A valid tree where the InPathElements is invalidated by setting a value to an integer 999. The Valid field is set to `false`.
//
// Example usage:
Expand All @@ -191,9 +192,13 @@ func MakeTestIncludedTrees(depth int, numberOfUtxos int) []InclusionTreeValidPai
invalidLeafTree.Inputs[0].Leaf = *big.NewInt(999)
invalidLeafPair := InclusionTreeValidPair{Tree: invalidLeafTree, Valid: false}

invalidInPathIndicesTree := BuildTestTree(depth, numberOfUtxos, true)
invalidInPathIndicesTree.Inputs[0].PathIndex = 999
invalidInPathIndicesPair := InclusionTreeValidPair{Tree: invalidInPathIndicesTree, Valid: false}
invalidInPathIndicesTreeAddOne := BuildTestTree(depth, numberOfUtxos, true)
invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex = invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex + 1
invalidInPathIndicesPairAddOne := InclusionTreeValidPair{Tree: invalidInPathIndicesTreeAddOne, Valid: false}

invalidInPathIndicesTreeSubOne := BuildTestTree(depth, numberOfUtxos, true)
invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex = invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex - 1
invalidInPathIndicesPairSubOne := InclusionTreeValidPair{Tree: invalidInPathIndicesTreeSubOne, Valid: false}

invalidInPathElementsTree := BuildTestTree(depth, numberOfUtxos, true)
invalidInPathElementsTree.Inputs[0].PathElements[0] = *big.NewInt(999)
Expand All @@ -202,7 +207,8 @@ func MakeTestIncludedTrees(depth int, numberOfUtxos int) []InclusionTreeValidPai
trees = append(trees, validPair)
trees = append(trees, invalidRootPair)
trees = append(trees, invalidLeafPair)
trees = append(trees, invalidInPathIndicesPair)
trees = append(trees, invalidInPathIndicesPairAddOne)
trees = append(trees, invalidInPathIndicesPairSubOne)
trees = append(trees, invalidInPathElementsPair)
return trees
}
Expand Down Expand Up @@ -273,9 +279,13 @@ func MakeTestNonInclusionTrees(depth int, numberOfUtxos int) []NonInclusionTreeV
invalidHighValueTree := BuildTestNonInclusionTree(depth, numberOfUtxos, true, false, false)
invalidHighValuePair := NonInclusionTreeValidPair{Tree: invalidHighValueTree, Valid: false}

invalidInPathIndicesTree := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true)
invalidInPathIndicesTree.Inputs[0].PathIndex = 999
invalidInPathIndicesPair := NonInclusionTreeValidPair{Tree: invalidInPathIndicesTree, Valid: false}
invalidInPathIndicesTreeAddOne := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true)
invalidInPathIndicesTreeAddOne.Inputs[0].PathIndex += 1
invalidInPathIndicesPairAddOne := NonInclusionTreeValidPair{Tree: invalidInPathIndicesTreeAddOne, Valid: false}

invalidInPathIndicesTreeSubOne := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true)
invalidInPathIndicesTreeSubOne.Inputs[0].PathIndex -= 1
invalidInPathIndicesPairSubOne := NonInclusionTreeValidPair{Tree: invalidInPathIndicesTreeSubOne, Valid: false}

invalidInPathElementsTree := BuildValidTestNonInclusionTree(depth, numberOfUtxos, true)
invalidInPathElementsTree.Inputs[0].PathElements[0] = *big.NewInt(999)
Expand All @@ -285,7 +295,8 @@ func MakeTestNonInclusionTrees(depth int, numberOfUtxos int) []NonInclusionTreeV
trees = append(trees, invalidRootPair)
trees = append(trees, invalidLowValuePair)
trees = append(trees, invalidHighValuePair)
trees = append(trees, invalidInPathIndicesPair)
trees = append(trees, invalidInPathIndicesPairAddOne)
trees = append(trees, invalidInPathIndicesPairSubOne)
trees = append(trees, invalidInPathElementsPair)
return trees
}
60 changes: 40 additions & 20 deletions light-prover/prover/circuit_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"fmt"
"light/light-prover/logging"
"light/light-prover/prover/poseidon"
"math/big"
"os"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"

"github.com/reilabs/gnark-lean-extractor/v2/abstractor"
)

Expand All @@ -27,19 +29,19 @@ type ProvingSystem struct {
ConstraintSystem constraint.ConstraintSystem
}

// ProofRound gadget generates the ParentHash
type ProofRound struct {
Direction frontend.Variable
Hash frontend.Variable
Sibling frontend.Variable
// ProveParentHash gadget generates the ParentHash
type ProveParentHash struct {
Bit frontend.Variable
Hash frontend.Variable
Sibling frontend.Variable
}

func (gadget ProofRound) DefineGadget(api frontend.API) interface{} {
api.AssertIsBoolean(gadget.Direction)
d1 := api.Select(gadget.Direction, gadget.Sibling, gadget.Hash)
d2 := api.Select(gadget.Direction, gadget.Hash, gadget.Sibling)
sum := abstractor.Call(api, poseidon.Poseidon2{In1: d1, In2: d2})
return sum
func (gadget ProveParentHash) DefineGadget(api frontend.API) interface{} {
api.AssertIsBoolean(gadget.Bit)
d1 := api.Select(gadget.Bit, gadget.Sibling, gadget.Hash)
d2 := api.Select(gadget.Bit, gadget.Hash, gadget.Sibling)
hash := abstractor.Call(api, poseidon.Poseidon2{In1: d1, In2: d2})
return hash
}

type InclusionProof struct {
Expand Down Expand Up @@ -108,11 +110,8 @@ type CombinedProof struct {
}

func (gadget CombinedProof) DefineGadget(api frontend.API) interface{} {
x := abstractor.Call(api, gadget.InclusionProof)
y := abstractor.Call(api, gadget.NonInclusionProof)
if x == nil || y == nil {
return nil
}
abstractor.Call(api, gadget.InclusionProof)
abstractor.Call(api, gadget.NonInclusionProof)
return nil
}

Expand All @@ -123,14 +122,35 @@ type LeafHashGadget struct {
Value frontend.Variable
}

// Limit the number of bits to 248 + 1,
// since we truncate address values to 31 bytes.
func (gadget LeafHashGadget) DefineGadget(api frontend.API) interface{} {
api.AssertIsDifferent(gadget.LeafLowerRangeValue, gadget.Value)
api.AssertIsLessOrEqual(gadget.LeafLowerRangeValue, gadget.Value)
api.AssertIsDifferent(gadget.LeafHigherRangeValue, gadget.Value)
api.AssertIsLessOrEqual(gadget.Value, gadget.LeafHigherRangeValue)
// Lower bound is less than value
AssertIsLess{A: gadget.LeafLowerRangeValue, B: gadget.Value, N: 248}.DefineGadget(api)
// Value is less than upper bound
AssertIsLess{A: gadget.Value, B: gadget.LeafHigherRangeValue, N: 248}.DefineGadget(api)
return abstractor.Call(api, poseidon.Poseidon3{In1: gadget.LeafLowerRangeValue, In2: gadget.LeafIndex, In3: gadget.LeafHigherRangeValue})
}

// Assert A is less than B.
type AssertIsLess struct {
A frontend.Variable
B frontend.Variable
N int
}

// To prevent overflows N (the number of bits) must not be greater than 252 + 1,
// see https://github.com/zkopru-network/zkopru/issues/116
func (gadget AssertIsLess) DefineGadget(api frontend.API) interface{} {
// Add 2^N to B to ensure a positive number
oneShifted := new(big.Int).Lsh(big.NewInt(1), uint(gadget.N))
num := api.Add(gadget.A, api.Sub(oneShifted, gadget.B))
bin := api.ToBinary(num, gadget.N+1)
api.AssertIsEqual(0, bin[gadget.N])
return nil
}

type MerkleRootGadget struct {
Hash frontend.Variable
Index frontend.Variable
Expand All @@ -141,7 +161,7 @@ type MerkleRootGadget struct {
func (gadget MerkleRootGadget) DefineGadget(api frontend.API) interface{} {
currentPath := api.ToBinary(gadget.Index, gadget.Depth)
for i := 0; i < gadget.Depth; i++ {
gadget.Hash = abstractor.Call(api, ProofRound{Direction: currentPath[i], Hash: gadget.Hash, Sibling: gadget.Path[i]})
gadget.Hash = abstractor.Call(api, ProveParentHash{Bit: currentPath[i], Hash: gadget.Hash, Sibling: gadget.Path[i]})
}
return gadget.Hash
}
Expand Down
7 changes: 4 additions & 3 deletions light-prover/prover/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

func TestCombined(t *testing.T) {
Expand Down
Loading

0 comments on commit b28be3e

Please sign in to comment.