Skip to content

Commit

Permalink
renamed leafIndex to nextIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jun 22, 2024
1 parent d014cde commit a5cadad
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 62 deletions.
4 changes: 2 additions & 2 deletions light-prover/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestNonInclusionHappyPath26_1_JSON(t *testing.T) {
],
"leafLowerRangeValue": "0x201",
"leafHigherRangeValue": "0x203",
"leafIndex": 46336290
"nextIndex": 46336290
}
]
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func TestCombinedHappyPath_JSON(t *testing.T) {
],
"leafLowerRangeValue": "0x201",
"leafHigherRangeValue": "0x203",
"leafIndex": 46336290
"nextIndex": 46336290
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ func MakeTestNonInclusionTrees(depth int, numberOfCompressedAccounts int) []NonI
invalidRootTree.Inputs[0].Root = *big.NewInt(999)
invalidRootPair := NonInclusionTreeValidPair{Tree: invalidRootTree, Valid: false}

invalidNextIndex := BuildValidTestNonInclusionTree(depth, numberOfCompressedAccounts, true)
invalidNextIndex.Inputs[0].NextIndex = 999
invalidNextIndexPair := NonInclusionTreeValidPair{Tree: invalidRootTree, Valid: false}

invalidLowValueTree := BuildTestNonInclusionTree(depth, numberOfCompressedAccounts, true, false, true)
invalidLowValuePair := NonInclusionTreeValidPair{Tree: invalidLowValueTree, Valid: false}

Expand All @@ -293,6 +297,7 @@ func MakeTestNonInclusionTrees(depth int, numberOfCompressedAccounts int) []NonI

trees = append(trees, validPair)
trees = append(trees, invalidRootPair)
trees = append(trees, invalidNextIndexPair)
trees = append(trees, invalidLowValuePair)
trees = append(trees, invalidHighValuePair)
trees = append(trees, invalidInPathIndicesPairAddOne)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func BuildTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random
var leafLower = big.NewInt(0)
var leafUpper = big.NewInt(2)
var pathIndex int
var leafIndex int
var nextIndex int
if random {
leafLower = big.NewInt(int64(rangeIn(0, 1000)))
leafUpper.Add(leafUpper, leafLower)
numberOfLeaves := 1 << depth
leafIndex = rand.Intn(numberOfLeaves)
nextIndex = rand.Intn(numberOfLeaves)
if valid {
value.Add(leafLower, big.NewInt(1))
} else {
Expand All @@ -201,7 +201,7 @@ func BuildTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random
} else {
leafLower = big.NewInt(1)
leafUpper = big.NewInt(123)
leafIndex = 1
nextIndex = 1
if valid {
value = big.NewInt(2)
} else {
Expand All @@ -210,7 +210,7 @@ func BuildTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random
pathIndex = 0
}

leaf, err := poseidon.Hash([]*big.Int{leafLower, big.NewInt(int64(leafIndex)), leafUpper})
leaf, err := poseidon.Hash([]*big.Int{leafLower, big.NewInt(int64(nextIndex)), leafUpper})
if err != nil {
fmt.Println("error: ", err)
}
Expand All @@ -221,7 +221,7 @@ func BuildTestNonInclusionTree(depth int, numberOfCompressedAccounts int, random
inputs[i].Root = tree.Root()
inputs[i].LeafLowerRangeValue = *leafLower
inputs[i].LeafHigherRangeValue = *leafUpper
inputs[i].LeafIndex = uint32(leafIndex)
inputs[i].NextIndex = uint32(nextIndex)
}

return prover.NonInclusionParameters{
Expand Down
22 changes: 11 additions & 11 deletions light-prover/prover/circuit_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ type Proof struct {
}

type ProvingSystem struct {
InclusionTreeDepth uint32
InclusionTreeDepth uint32
InclusionNumberOfCompressedAccounts uint32
NonInclusionTreeDepth uint32
NonInclusionTreeDepth uint32
NonInclusionNumberOfCompressedAccounts uint32
ProvingKey groth16.ProvingKey
VerifyingKey groth16.VerifyingKey
ConstraintSystem constraint.ConstraintSystem
ProvingKey groth16.ProvingKey
VerifyingKey groth16.VerifyingKey
ConstraintSystem constraint.ConstraintSystem
}

// ProveParentHash gadget generates the ParentHash
Expand All @@ -51,7 +51,7 @@ type InclusionProof struct {
InPathElements [][]frontend.Variable

NumberOfCompressedAccounts uint32
Depth uint32
Depth uint32
}

func (gadget InclusionProof) DefineGadget(api frontend.API) interface{} {
Expand All @@ -74,21 +74,21 @@ type NonInclusionProof struct {

LeafLowerRangeValues []frontend.Variable
LeafHigherRangeValues []frontend.Variable
LeafIndices []frontend.Variable
NextIndices []frontend.Variable

InPathIndices []frontend.Variable
InPathElements [][]frontend.Variable

NumberOfCompressedAccounts uint32
Depth uint32
Depth uint32
}

func (gadget NonInclusionProof) DefineGadget(api frontend.API) interface{} {
currentHash := make([]frontend.Variable, gadget.NumberOfCompressedAccounts)
for proofIndex := 0; proofIndex < int(gadget.NumberOfCompressedAccounts); proofIndex++ {
leaf := LeafHashGadget{
LeafLowerRangeValue: gadget.LeafLowerRangeValues[proofIndex],
LeafIndex: gadget.LeafIndices[proofIndex],
NextIndex: gadget.NextIndices[proofIndex],
LeafHigherRangeValue: gadget.LeafHigherRangeValues[proofIndex],
Value: gadget.Values[proofIndex]}
currentHash[proofIndex] = abstractor.Call(api, leaf)
Expand Down Expand Up @@ -117,7 +117,7 @@ func (gadget CombinedProof) DefineGadget(api frontend.API) interface{} {

type LeafHashGadget struct {
LeafLowerRangeValue frontend.Variable
LeafIndex frontend.Variable
NextIndex frontend.Variable
LeafHigherRangeValue frontend.Variable
Value frontend.Variable
}
Expand All @@ -130,7 +130,7 @@ func (gadget LeafHashGadget) DefineGadget(api frontend.API) interface{} {
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})
return abstractor.Call(api, poseidon.Poseidon3{In1: gadget.LeafLowerRangeValue, In2: gadget.NextIndex, In3: gadget.LeafHigherRangeValue})
}

// Assert A is less than B.
Expand Down
30 changes: 15 additions & 15 deletions light-prover/prover/combined_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ type CombinedCircuit struct {

func (circuit *CombinedCircuit) Define(api frontend.API) error {
abstractor.CallVoid(api, InclusionProof{
Roots: circuit.Inclusion.Roots,
Leaves: circuit.Inclusion.Leaves,
InPathElements: circuit.Inclusion.InPathElements,
InPathIndices: circuit.Inclusion.InPathIndices,
NumberOfCompressedAccounts: circuit.Inclusion.NumberOfCompressedAccounts,
Depth: circuit.Inclusion.Depth,
Roots: circuit.Inclusion.Roots,
Leaves: circuit.Inclusion.Leaves,
InPathElements: circuit.Inclusion.InPathElements,
InPathIndices: circuit.Inclusion.InPathIndices,
NumberOfCompressedAccounts: circuit.Inclusion.NumberOfCompressedAccounts,
Depth: circuit.Inclusion.Depth,
})

abstractor.CallVoid(api, NonInclusionProof{
Roots: circuit.NonInclusion.Roots,
Values: circuit.NonInclusion.Values,
LeafLowerRangeValues: circuit.NonInclusion.LeafLowerRangeValues,
LeafHigherRangeValues: circuit.NonInclusion.LeafHigherRangeValues,
LeafIndices: circuit.NonInclusion.LeafIndices,
InPathIndices: circuit.NonInclusion.InPathIndices,
InPathElements: circuit.NonInclusion.InPathElements,
NumberOfCompressedAccounts: circuit.NonInclusion.NumberOfCompressedAccounts,
Depth: circuit.NonInclusion.Depth,
Roots: circuit.NonInclusion.Roots,
Values: circuit.NonInclusion.Values,
LeafLowerRangeValues: circuit.NonInclusion.LeafLowerRangeValues,
LeafHigherRangeValues: circuit.NonInclusion.LeafHigherRangeValues,
NextIndices: circuit.NonInclusion.NextIndices,
InPathIndices: circuit.NonInclusion.InPathIndices,
InPathElements: circuit.NonInclusion.InPathElements,
NumberOfCompressedAccounts: circuit.NonInclusion.NumberOfCompressedAccounts,
Depth: circuit.NonInclusion.Depth,
})
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions light-prover/prover/combined_proving_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func InitializeCombinedCircuit(inclusionTreeDepth uint32, inclusionNumberOfCompr
Values: nonInclusionValues,
LeafLowerRangeValues: nonInclusionLeafLowerRangeValues,
LeafHigherRangeValues: nonInclusionLeafHigherRangeValues,
LeafIndices: nonInclusionLeafIndices,
NextIndices: nonInclusionLeafIndices,
InPathIndices: nonInclusionInPathIndices,
InPathElements: nonInclusionInPathElements,
NumberOfCompressedAccounts: nonInclusionNumberOfCompressedAccounts,
Expand Down Expand Up @@ -127,7 +127,7 @@ func (ps *ProvingSystem) ProveCombined(params *CombinedParameters) (*Proof, erro
circuit.NonInclusion.Values[i] = params.NonInclusionParameters.Inputs[i].Value
circuit.NonInclusion.LeafLowerRangeValues[i] = params.NonInclusionParameters.Inputs[i].LeafLowerRangeValue
circuit.NonInclusion.LeafHigherRangeValues[i] = params.NonInclusionParameters.Inputs[i].LeafHigherRangeValue
circuit.NonInclusion.LeafIndices[i] = params.NonInclusionParameters.Inputs[i].LeafIndex
circuit.NonInclusion.NextIndices[i] = params.NonInclusionParameters.Inputs[i].NextIndex
circuit.NonInclusion.InPathIndices[i] = params.NonInclusionParameters.Inputs[i].PathIndex
circuit.NonInclusion.InPathElements[i] = make([]frontend.Variable, ps.NonInclusionTreeDepth)
for j := 0; j < int(ps.NonInclusionTreeDepth); j++ {
Expand Down
6 changes: 3 additions & 3 deletions light-prover/prover/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestCombined(t *testing.T) {
nonInclusionValues[i] = v.Value
nonInclusionLeafLowerRangeValues[i] = v.LeafLowerRangeValue
nonInclusionLeafHigherRangeValues[i] = v.LeafHigherRangeValue
nonInclusionLeafIndices[i] = v.LeafIndex
nonInclusionLeafIndices[i] = v.NextIndex
nonInclusionInPathIndices[i] = v.PathIndex
for j, v2 := range v.PathElements {
nonInclusionInPathElements[i][j] = v2
Expand All @@ -108,7 +108,7 @@ func TestCombined(t *testing.T) {
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.NextIndices = 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++ {
Expand All @@ -132,7 +132,7 @@ func TestCombined(t *testing.T) {
Values: nonInclusionValues,
LeafLowerRangeValues: nonInclusionLeafLowerRangeValues,
LeafHigherRangeValues: nonInclusionLeafHigherRangeValues,
LeafIndices: nonInclusionLeafIndices,
NextIndices: nonInclusionLeafIndices,
InPathIndices: nonInclusionInPathIndices,
InPathElements: nonInclusionInPathElements,
NumberOfCompressedAccounts: nonInclusionNumberOfCompressedAccounts,
Expand Down
6 changes: 3 additions & 3 deletions light-prover/prover/marshal_non_inclusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type NonInclusionProofInputsJSON struct {
PathElements []string `json:"pathElements"`
LeafLowerRangeValue string `json:"leafLowerRangeValue"`
LeafHigherRangeValue string `json:"leafHigherRangeValue"`
LeafIndex uint32 `json:"leafIndex"`
NextIndex uint32 `json:"nextIndex"`
}

type NonInclusionParametersJSON struct {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (p *NonInclusionParameters) CreateNonInclusionParametersJSON() NonInclusion
}
paramsJson.Inputs[i].LeafLowerRangeValue = toHex(&p.Inputs[i].LeafLowerRangeValue)
paramsJson.Inputs[i].LeafHigherRangeValue = toHex(&p.Inputs[i].LeafHigherRangeValue)
paramsJson.Inputs[i].LeafIndex = p.Inputs[i].LeafIndex
paramsJson.Inputs[i].NextIndex = p.Inputs[i].NextIndex
}
return paramsJson
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (p *NonInclusionParameters) UpdateWithJSON(params NonInclusionParametersJSO
if err != nil {
return err
}
p.Inputs[i].LeafIndex = params.Inputs[i].LeafIndex
p.Inputs[i].NextIndex = params.Inputs[i].NextIndex
}
return nil
}
26 changes: 13 additions & 13 deletions light-prover/prover/non_inclusion_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type NonInclusionCircuit struct {
// private inputs
LeafLowerRangeValues []frontend.Variable `gnark:"input"`
LeafHigherRangeValues []frontend.Variable `gnark:"input"`
LeafIndices []frontend.Variable `gnark:"input"`
NextIndices []frontend.Variable `gnark:"input"`

InPathIndices []frontend.Variable `gnark:"input"`
InPathElements [][]frontend.Variable `gnark:"input"`

NumberOfCompressedAccounts uint32
Depth uint32
Depth uint32
}

func (circuit *NonInclusionCircuit) Define(api frontend.API) error {
Expand All @@ -31,13 +31,13 @@ func (circuit *NonInclusionCircuit) Define(api frontend.API) error {

LeafLowerRangeValues: circuit.LeafLowerRangeValues,
LeafHigherRangeValues: circuit.LeafHigherRangeValues,
LeafIndices: circuit.LeafIndices,
NextIndices: circuit.NextIndices,

InPathElements: circuit.InPathElements,
InPathIndices: circuit.InPathIndices,

NumberOfCompressedAccounts: circuit.NumberOfCompressedAccounts,
Depth: circuit.Depth,
Depth: circuit.Depth,
}
roots := abstractor.Call1(api, proof)
for i := 0; i < int(circuit.NumberOfCompressedAccounts); i++ {
Expand All @@ -62,15 +62,15 @@ func ImportNonInclusionSetup(treeDepth uint32, numberOfCompressedAccounts uint32
}

circuit := NonInclusionCircuit{
Depth: treeDepth,
NumberOfCompressedAccounts: numberOfCompressedAccounts,
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,
NextIndices: leafIndices,
InPathIndices: inPathIndices,
InPathElements: inPathElements,
}

ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
Expand Down
8 changes: 4 additions & 4 deletions light-prover/prover/non_inclusion_proving_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type NonInclusionInputs struct {

LeafLowerRangeValue big.Int
LeafHigherRangeValue big.Int
LeafIndex uint32
NextIndex uint32
}

type NonInclusionParameters struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func R1CSNonInclusion(treeDepth uint32, numberOfCompressedAccounts uint32) (cons
Values: values,
LeafLowerRangeValues: leafLowerRangeValues,
LeafHigherRangeValues: leafHigherRangeValues,
LeafIndices: leafIndices,
NextIndices: leafIndices,
InPathIndices: inPathIndices,
InPathElements: inPathElements,
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (ps *ProvingSystem) ProveNonInclusion(params *NonInclusionParameters) (*Pro
values[i] = params.Inputs[i].Value
leafLowerRangeValues[i] = params.Inputs[i].LeafLowerRangeValue
leafHigherRangeValues[i] = params.Inputs[i].LeafHigherRangeValue
leafIndices[i] = params.Inputs[i].LeafIndex
leafIndices[i] = params.Inputs[i].NextIndex
inPathIndices[i] = params.Inputs[i].PathIndex
inPathElements[i] = make([]frontend.Variable, ps.NonInclusionTreeDepth)
for j := 0; j < int(ps.NonInclusionTreeDepth); j++ {
Expand All @@ -123,7 +123,7 @@ func (ps *ProvingSystem) ProveNonInclusion(params *NonInclusionParameters) (*Pro
Values: values,
LeafLowerRangeValues: leafLowerRangeValues,
LeafHigherRangeValues: leafHigherRangeValues,
LeafIndices: leafIndices,
NextIndices: leafIndices,
InPathIndices: inPathIndices,
InPathElements: inPathElements,
}
Expand Down
8 changes: 4 additions & 4 deletions light-prover/prover/non_inclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestNonInclusion(t *testing.T) {
values[i] = v.Value
leafLowerRangeValues[i] = v.LeafLowerRangeValue
leafHigherRangeValues[i] = v.LeafHigherRangeValue
leafIndices[i] = v.LeafIndex
leafIndices[i] = v.NextIndex
inPathIndices[i] = v.PathIndex
for j, v2 := range v.PathElements {
inPathElements[i][j] = v2
Expand All @@ -71,7 +71,7 @@ func TestNonInclusion(t *testing.T) {
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.NextIndices = make([]frontend.Variable, numberOfCompressedAccounts)
circuit.InPathIndices = make([]frontend.Variable, numberOfCompressedAccounts)
circuit.InPathElements = make([][]frontend.Variable, numberOfCompressedAccounts)
for i := 0; i < int(numberOfCompressedAccounts); i++ {
Expand All @@ -90,7 +90,7 @@ func TestNonInclusion(t *testing.T) {
Values: values,
LeafLowerRangeValues: leafLowerRangeValues,
LeafHigherRangeValues: leafHigherRangeValues,
LeafIndices: leafIndices,
NextIndices: leafIndices,
InPathIndices: inPathIndices,
InPathElements: inPathElements,
NumberOfCompressedAccounts: numberOfCompressedAccounts,
Expand All @@ -103,7 +103,7 @@ func TestNonInclusion(t *testing.T) {
Values: values,
LeafLowerRangeValues: leafLowerRangeValues,
LeafHigherRangeValues: leafHigherRangeValues,
LeafIndices: leafIndices,
NextIndices: leafIndices,
InPathIndices: inPathIndices,
InPathElements: inPathElements,
NumberOfCompressedAccounts: numberOfCompressedAccounts,
Expand Down

0 comments on commit a5cadad

Please sign in to comment.