Skip to content

Commit

Permalink
remove redundant parameter validation logic for batch address append …
Browse files Browse the repository at this point in the history
…circuit, handle panics in tests
  • Loading branch information
sergeytimoshin committed Jan 3, 2025
1 parent 2861442 commit 67f7c1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
24 changes: 7 additions & 17 deletions prover/server/prover/batch_address_append_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,6 @@ func (params *BatchAddressAppendParameters) CreateWitness() (*BatchAddressTreeAp
return nil, fmt.Errorf("tree height cannot be 0")
}

// Validate array sizes match BatchSize
if len(params.LowElementValues) != int(params.BatchSize) ||
len(params.LowElementNextIndices) != int(params.BatchSize) ||
len(params.LowElementNextValues) != int(params.BatchSize) ||
len(params.LowElementIndices) != int(params.BatchSize) ||
len(params.NewElementValues) != int(params.BatchSize) {
return nil, fmt.Errorf("array lengths must match BatchSize")
}

// Validate proof lengths match TreeHeight
for i := 0; i < int(params.BatchSize); i++ {
if len(params.LowElementProofs[i]) != int(params.TreeHeight) ||
len(params.NewElementProofs[i]) != int(params.TreeHeight) {
return nil, fmt.Errorf("proof lengths must match TreeHeight")
}
}

circuit := &BatchAddressTreeAppendCircuit{
BatchSize: params.BatchSize,
TreeHeight: params.TreeHeight,
Expand Down Expand Up @@ -211,6 +194,13 @@ func (params *BatchAddressAppendParameters) CreateWitness() (*BatchAddressTreeAp
return circuit, nil
}
func (p *BatchAddressAppendParameters) ValidateShape() error {
if p.BatchSize == 0 {
return fmt.Errorf("batch size cannot be 0")
}
if p.TreeHeight == 0 {
return fmt.Errorf("tree height cannot be 0")
}

expectedArrayLen := int(p.BatchSize)
expectedProofLen := int(p.TreeHeight)

Expand Down
11 changes: 11 additions & 0 deletions prover/server/prover/batch_address_append_circuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestBatchAddressAppendCircuit(t *testing.T) {
batchSize uint32
startIndex uint64
modifyParams func(*BatchAddressAppendParameters)
wantPanic bool
}{
{
name: "Invalid OldRoot",
Expand Down Expand Up @@ -139,6 +140,7 @@ func TestBatchAddressAppendCircuit(t *testing.T) {
modifyParams: func(p *BatchAddressAppendParameters) {
p.LowElementValues = p.LowElementValues[:len(p.LowElementValues)-1]
},
wantPanic: true,
},
{
name: "Invalid proof length",
Expand All @@ -148,6 +150,7 @@ func TestBatchAddressAppendCircuit(t *testing.T) {
modifyParams: func(p *BatchAddressAppendParameters) {
p.LowElementProofs[0] = p.LowElementProofs[0][:len(p.LowElementProofs[0])-1]
},
wantPanic: true,
},
{
name: "Empty arrays",
Expand Down Expand Up @@ -182,6 +185,14 @@ func TestBatchAddressAppendCircuit(t *testing.T) {

tc.modifyParams(params)

if tc.wantPanic {
assert.Panics(func() {
witness, _ := params.CreateWitness()
test.IsSolved(&circuit, witness, ecc.BN254.ScalarField())
})
return
}

witness, err := params.CreateWitness()
if err != nil {
return
Expand Down

0 comments on commit 67f7c1a

Please sign in to comment.