From b62eb2c0399da794577d280d75770fc9b4dbb20e Mon Sep 17 00:00:00 2001 From: mattstam Date: Wed, 14 Feb 2024 15:50:54 -0800 Subject: [PATCH] range panic --- plonky2x/verifier/system/groth16.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/plonky2x/verifier/system/groth16.go b/plonky2x/verifier/system/groth16.go index 70eeadff..efc87907 100644 --- a/plonky2x/verifier/system/groth16.go +++ b/plonky2x/verifier/system/groth16.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/hex" "encoding/json" + "fmt" "io" "math/big" "os" @@ -268,13 +269,23 @@ func (s *Groth16System) ProveCircuit(r1cs constraint.ConstraintSystem, pk groth1 const fpSize = 4 * 8 - // read in proof.json and extract the proof bytes proofBytes := _proof.Ar.Marshal() + // Ensure proofBytes contains enough data for the expected operation + expectedLength := fpSize * 8 + if len(proofBytes) < expectedLength { + return nil, nil, fmt.Errorf("proofBytes length is %d, expected at least %d", len(proofBytes), expectedLength) + } + proofs := make([]string, 8) - // Print out the proof for i := 0; i < 8; i++ { - proofs[i] = "0x" + hex.EncodeToString(proofBytes[i*fpSize:(i+1)*fpSize]) + start := i * fpSize + end := (i + 1) * fpSize + // Additional check to prevent slice bounds out of range panic + if end > len(proofBytes) { + return nil, nil, fmt.Errorf("attempt to slice beyond proofBytes length at segment %d", i) + } + proofs[i] = "0x" + hex.EncodeToString(proofBytes[start:end]) } publicWitness, _ := witness.Public()