Skip to content

Commit 6067e86

Browse files
committed
fix: avoid panic on invalid pub inputs length
1 parent 2b7baa2 commit 6067e86

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

crates/batcher/go_verifiers_lib/verifier.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import "C"
1414
import (
1515
"bytes"
1616
"encoding/json"
17+
"fmt"
1718
"math/big"
1819

1920
"log"
@@ -123,9 +124,9 @@ func verifyGnarkGroth16Proof(proofBytesRef C.ListRef, pubInputBytesRef C.ListRef
123124
return err == nil
124125
}
125126

126-
func bytesToBigInts32(b []byte) []*big.Int {
127+
func bytesToBigInts32(b []byte) ([]*big.Int, error) {
127128
if len(b)%32 != 0 {
128-
panic("pubInputBytes length is not a multiple of 32")
129+
return nil, fmt.Errorf("invalid length")
129130
}
130131

131132
inputs := make([]*big.Int, 0, len(b)/32)
@@ -134,7 +135,7 @@ func bytesToBigInts32(b []byte) []*big.Int {
134135
bi := new(big.Int).SetBytes(chunk)
135136
inputs = append(inputs, bi)
136137
}
137-
return inputs
138+
return inputs, nil
138139
}
139140

140141
//export VerifyCircomGroth16ProofBN256
@@ -169,7 +170,11 @@ func VerifyCircomGroth16ProofBN256(proofBytesRef C.ListRef, pubInputBytesRef C.L
169170
return false
170171
}
171172

172-
inputs := bytesToBigInts32(pubInputBytes)
173+
inputs, err := bytesToBigInts32(pubInputBytes)
174+
if err != nil {
175+
log.Printf("Could not parse pub inputs: %v", err)
176+
return false
177+
}
173178

174179
err = verifier.VerifyRaw(vk, parsedProofData, inputs)
175180
if err != nil {

operator/pkg/operator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,9 @@ func (o *Operator) verifyGnarkGroth16Proof(proofBytes []byte, pubInputBytes []by
621621

622622
// verifyCircomGroth16Bn256Proof verifies a Circom Groth16 proof using BN256 curve.
623623
func (o *Operator) verifyCircomGroth16Bn256Proof(proofBytes []byte, pubInputBytes []byte, verificationKeyBytes []byte) bool {
624-
bytesToBigInts32 := func(b []byte) []*big.Int {
624+
bytesToBigInts32 := func(b []byte) ([]*big.Int, error) {
625625
if len(b)%32 != 0 {
626-
panic("pubInputBytes length is not a multiple of 32")
626+
return nil, fmt.Errorf("invalid length")
627627
}
628628

629629
inputs := make([]*big.Int, 0, len(b)/32)
@@ -632,7 +632,7 @@ func (o *Operator) verifyCircomGroth16Bn256Proof(proofBytes []byte, pubInputByte
632632
bi := new(big.Int).SetBytes(chunk)
633633
inputs = append(inputs, bi)
634634
}
635-
return inputs
635+
return inputs, nil
636636
}
637637

638638
proofData := &rapidsnark_types.ProofData{}
@@ -661,7 +661,11 @@ func (o *Operator) verifyCircomGroth16Bn256Proof(proofBytes []byte, pubInputByte
661661
return false
662662
}
663663

664-
inputs := bytesToBigInts32(pubInputBytes)
664+
inputs, err := bytesToBigInts32(pubInputBytes)
665+
if err != nil {
666+
log.Printf("Could not parse pub inputs: %v", err)
667+
return false
668+
}
665669

666670
err = rapidsnark_verifier.VerifyRaw(vk, parsedProofData, inputs)
667671
if err != nil {

0 commit comments

Comments
 (0)