Skip to content

Commit

Permalink
fix: Enhance error handling across gnark-ffi package
Browse files Browse the repository at this point in the history
- Enhanced error handling across three critical go files in recursion/gnark-ffi, ensuring each function returns an error along with its intended output if any error occurs.
- Improved `randomPolynomial` function to return an error along with the polynomial and added necessary error checks.
- Improved safety by adding error handling for `SetString` and `SetRandom` operations, the program will now terminate in case of any error.
- Added error handling for critical function calls, such as `os.MkdirAll` and `vk.ExportSolidity` in build.go.
- Strengthened guard against unexpected panic conditions in `prove.go` by enabling error handling for `ReadFrom` methods for the R1CS, proving key, and verifier key.
  • Loading branch information
huitseeker committed Sep 18, 2024
1 parent 8d1e0dd commit f0eb26d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
10 changes: 8 additions & 2 deletions recursion/gnark-ffi/go/sp1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ func Build(dataDir string) {
}

// Create the build directory.
os.MkdirAll(dataDir, 0755)
err = os.MkdirAll(dataDir, 0755)
if err != nil {
panic(err)
}

// Write the solidity verifier.
solidityVerifierFile, err := os.Create(dataDir + "/" + verifierContractPath)
if err != nil {
panic(err)
}
vk.ExportSolidity(solidityVerifierFile)
err = vk.ExportSolidity(solidityVerifierFile)
if err != nil {
panic(err)
}
defer solidityVerifierFile.Close()

// Write the R1CS.
Expand Down
13 changes: 10 additions & 3 deletions recursion/gnark-ffi/go/sp1/prove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func Prove(dataDir string, witnessPath string) Proof {
panic(err)
}
scs := plonk.NewCS(ecc.BN254)
scs.ReadFrom(scsFile)
if _, err := scs.ReadFrom(scsFile); err != nil {
panic(err)
}
defer scsFile.Close()

// Read the proving key.
Expand All @@ -33,7 +35,10 @@ func Prove(dataDir string, witnessPath string) Proof {
}
pk := plonk.NewProvingKey(ecc.BN254)
bufReader := bufio.NewReaderSize(pkFile, 1024*1024)
pk.UnsafeReadFrom(bufReader)
_, err = pk.UnsafeReadFrom(bufReader)
if err != nil {
panic(err)
}
defer pkFile.Close()

// Read the verifier key.
Expand All @@ -42,7 +47,9 @@ func Prove(dataDir string, witnessPath string) Proof {
panic(err)
}
vk := plonk.NewVerifyingKey(ecc.BN254)
vk.ReadFrom(vkFile)
if _, err := vk.ReadFrom(vkFile); err != nil {
panic(err)
}
defer vkFile.Close()

// Read the file.
Expand Down
17 changes: 12 additions & 5 deletions recursion/gnark-ffi/go/sp1/trusted_setup/trusted_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
func sanityCheck(srs *kzg_bn254.SRS) {
// we can now use the SRS to verify a proof
// create a polynomial
f := randomPolynomial(60)
f, err := randomPolynomial(60)
if err != nil {
log.Fatal(err)
}

// commit the polynomial
digest, err := kzg_bn254.Commit(f, srs.Pk)
Expand All @@ -27,7 +30,9 @@ func sanityCheck(srs *kzg_bn254.SRS) {

// compute opening proof at a random point
var point fr.Element
point.SetString("4321")
if _, err := point.SetString("4321"); err != nil {
log.Fatal(err)
}
proof, err := kzg_bn254.Open(f, point, srs.Pk)
if err != nil {
log.Fatal(err)
Expand All @@ -46,12 +51,14 @@ func sanityCheck(srs *kzg_bn254.SRS) {
}
}

func randomPolynomial(size int) []fr.Element {
func randomPolynomial(size int) ([]fr.Element, error) {
f := make([]fr.Element, size)
for i := 0; i < size; i++ {
f[i].SetRandom()
if _, err := f[i].SetRandom(); err != nil {
return nil, err
}
}
return f
return f, nil
}

// eval returns p(point) where p is interpreted as a polynomial
Expand Down
4 changes: 3 additions & 1 deletion recursion/gnark-ffi/go/sp1/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func Verify(verifyCmdDataDir string, verifyCmdProof string, verifyCmdVkeyHash st
panic(err)
}
vk := plonk.NewVerifyingKey(ecc.BN254)
vk.ReadFrom(vkFile)
if _, err := vk.ReadFrom(vkFile); err != nil {
panic(err)
}

// Compute the public witness.
circuit := Circuit{
Expand Down

0 comments on commit f0eb26d

Please sign in to comment.