Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Oct 13, 2024
1 parent 6d65caa commit d42df4d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 47 deletions.
18 changes: 3 additions & 15 deletions light-prover/prover/batch_append_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ func (circuit *BatchAppendCircuit) Define(api frontend.API) error {
// ( consider `HashchainHash` contains a batch of 5000 leaves but we want to use 5 proves to execute this update onchain)
api.AssertIsEqual(circuit.HashChainStartIndex, 0)

oldSubtreesHashChain := circuit.createHashChain(api, int(circuit.TreeHeight), circuit.Subtrees)
oldSubtreesHashChain := createHashChain(api, int(circuit.TreeHeight), circuit.Subtrees)
api.AssertIsEqual(oldSubtreesHashChain, circuit.OldSubTreeHashChain)

leavesHashChain := circuit.createHashChain(api, int(circuit.BatchSize), circuit.Leaves)
leavesHashChain := createHashChain(api, int(circuit.BatchSize), circuit.Leaves)
api.AssertIsEqual(leavesHashChain, circuit.HashchainHash)

newRoot, newSubtrees := circuit.batchAppend(api)
api.AssertIsEqual(newRoot, circuit.NewRoot)

newSubtreesHashChain := circuit.createHashChain(api, int(circuit.TreeHeight), newSubtrees)
newSubtreesHashChain := createHashChain(api, int(circuit.TreeHeight), newSubtrees)
api.AssertIsEqual(newSubtreesHashChain, circuit.NewSubTreeHashChain)

return nil
Expand Down Expand Up @@ -178,18 +178,6 @@ func (circuit *BatchAppendCircuit) getZeroValue(api frontend.API, level int) fro
return frontend.Variable(new(big.Int).SetBytes(ZERO_BYTES[level][:]))
}

func (circuit *BatchAppendCircuit) createHashChain(api frontend.API, length int, hashes []frontend.Variable) frontend.Variable {
if length == 0 {
return 0
}

hashChain := hashes[0]
for i := 1; i < length; i++ {
hashChain = abstractor.Call(api, poseidon.Poseidon2{In1: hashChain, In2: hashes[i]})
}
return hashChain
}

type BatchAppendParameters struct {
OldSubTreeHashChain *big.Int `json:"oldSubTreeHashChain"`
NewSubTreeHashChain *big.Int `json:"newSubTreeHashChain"`
Expand Down
20 changes: 20 additions & 0 deletions light-prover/prover/batch_circuit_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package prover

import (
"light/light-prover/prover/poseidon"

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

func createHashChain(api frontend.API, length int, hashes []frontend.Variable) frontend.Variable {
if length == 0 {
return frontend.Variable(0)
}

hashChain := hashes[0]
for i := 1; i < length; i++ {
hashChain = abstractor.Call(api, poseidon.Poseidon2{In1: hashChain, In2: hashes[i]})
}
return hashChain
}
30 changes: 1 addition & 29 deletions light-prover/prover/batch_update_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
type BatchUpdateCircuit struct {
OldRoot frontend.Variable `gnark:",public"`
NewRoot frontend.Variable `gnark:",public"`
HashChainStartIndex frontend.Variable `gnark:",public"`
LeavesHashchainHash frontend.Variable `gnark:",public"`

Leaves []frontend.Variable `gnark:"input"`
Expand All @@ -29,11 +28,9 @@ type BatchUpdateCircuit struct {
}

func (circuit *BatchUpdateCircuit) Define(api frontend.API) error {
calculatedHashchainHash := circuit.createHashChain(api, int(circuit.BatchSize), circuit.Leaves)
calculatedHashchainHash := createHashChain(api, int(circuit.BatchSize), circuit.Leaves)
api.AssertIsEqual(calculatedHashchainHash, circuit.LeavesHashchainHash)

api.AssertIsEqual(circuit.HashChainStartIndex, 0)

emptyLeaf := frontend.Variable(0)
newRoot := circuit.OldRoot

Expand Down Expand Up @@ -61,28 +58,6 @@ func (circuit *BatchUpdateCircuit) merkleRoot(api frontend.API, leaf frontend.Va
return currentHash
}

func (circuit *BatchUpdateCircuit) incrementBits(api frontend.API, bits []frontend.Variable) []frontend.Variable {
carry := frontend.Variable(1)
for i := 0; i < len(bits); i++ {
newBit := api.Xor(bits[i], carry)
carry = api.And(bits[i], carry)
bits[i] = newBit
}
return bits
}

func (circuit *BatchUpdateCircuit) createHashChain(api frontend.API, length int, hashes []frontend.Variable) frontend.Variable {
if length == 0 {
return frontend.Variable(0)
}

hashChain := hashes[0]
for i := 1; i < length; i++ {
hashChain = abstractor.Call(api, poseidon.Poseidon2{In1: hashChain, In2: hashes[i]})
}
return hashChain
}

type BatchUpdateParameters struct {
OldRoot *big.Int
NewRoot *big.Int
Expand Down Expand Up @@ -147,7 +122,6 @@ func (ps *ProvingSystemV2) ProveBatchUpdate(params *BatchUpdateParameters) (*Pro
oldRoot := frontend.Variable(params.OldRoot)
newRoot := frontend.Variable(params.NewRoot)
leavesHashchainHash := frontend.Variable(params.LeavesHashchainHash)
hashChainStartIndex := frontend.Variable(params.HashChainStartIndex)

leaves := make([]frontend.Variable, len(params.Leaves))
pathIndices := make([]frontend.Variable, len(params.PathIndices))
Expand All @@ -169,7 +143,6 @@ func (ps *ProvingSystemV2) ProveBatchUpdate(params *BatchUpdateParameters) (*Pro
Leaves: leaves,
PathIndices: pathIndices,
MerkleProofs: merkleProofs,
HashChainStartIndex: hashChainStartIndex,
Height: ps.TreeHeight,
BatchSize: ps.BatchSize,
}
Expand Down Expand Up @@ -203,7 +176,6 @@ func R1CSBatchUpdate(height uint32, batchSize uint32) (constraint.ConstraintSyst
Leaves: leaves,
PathIndices: pathIndices,
MerkleProofs: merkleProofs,
HashChainStartIndex: frontend.Variable(0),
Height: height,
BatchSize: batchSize,
}
Expand Down
3 changes: 0 additions & 3 deletions light-prover/prover/marshal_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type BatchUpdateProofInputsJSON struct {
Leaves []string `json:"leaves"`
MerkleProofs [][]string `json:"newMerkleProofs"`
PathIndices []uint32 `json:"pathIndices"`
HashChainStartIndex uint32 `json:"hashChainStartIndex"`
Height uint32 `json:"height"`
BatchSize uint32 `json:"batchSize"`
}
Expand All @@ -40,7 +39,6 @@ func (p *BatchUpdateParameters) CreateBatchUpdateParametersJSON() BatchUpdatePar
paramsJson.Inputs.OldRoot = toHex(p.OldRoot)
paramsJson.Inputs.NewRoot = toHex(p.NewRoot)
paramsJson.Inputs.LeavesHashchainHash = toHex(p.LeavesHashchainHash)
paramsJson.Inputs.HashChainStartIndex = p.HashChainStartIndex
paramsJson.Inputs.Height = p.Height
paramsJson.Inputs.BatchSize = p.BatchSize

Expand Down Expand Up @@ -95,7 +93,6 @@ func (p *BatchUpdateParameters) UpdateWithJSON(params BatchUpdateParametersJSON)
return err
}

p.HashChainStartIndex = params.Inputs.HashChainStartIndex
p.Height = params.Inputs.Height
p.BatchSize = params.Inputs.BatchSize

Expand Down

0 comments on commit d42df4d

Please sign in to comment.