Skip to content

Commit

Permalink
Merge pull request #19 from reilabs/wz/contracts-friendly-response
Browse files Browse the repository at this point in the history
Make prover insertion response contract-friendly
  • Loading branch information
wzmuda authored Sep 9, 2024
2 parents 52a4a1d + 2dc2168 commit 55c29c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
7 changes: 5 additions & 2 deletions prover/insertion_proving_system.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package prover

import (
"crypto/sha256"
"fmt"
"github.com/consensys/gnark/backend"
"math/big"

gokzg4844 "github.com/crate-crypto/go-kzg-4844"
Expand Down Expand Up @@ -136,7 +138,8 @@ func (ps *ProvingSystem) ProveInsertion(params *InsertionParameters) (*Insertion
return nil, err
}
logging.Logger().Info().Msg("generating proof")
proof, err := groth16.Prove(ps.ConstraintSystem, ps.ProvingKey, witness)
proof, err := groth16.Prove(ps.ConstraintSystem, ps.ProvingKey, witness,
backend.WithProverHashToFieldFunction(sha256.New()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -174,5 +177,5 @@ func (ps *ProvingSystem) VerifyInsertion(ir *InsertionResponse, params *Insertio
if err != nil {
return err
}
return groth16.Verify(ir.Proof.Proof, ps.VerifyingKey, witness)
return groth16.Verify(ir.Proof.Proof, ps.VerifyingKey, witness, backend.WithVerifierHashToFieldFunction(sha256.New()))
}
46 changes: 36 additions & 10 deletions prover/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,31 @@ func toHex(i *big.Int) string {
type InsertionResponseJSON struct {
InputHash string `json:"inputHash"`
ExpectedEvaluation string `json:"expectedEvaluation"`
Commitment4844 string `json:"commitment4844"`
Commitment4844 []string `json:"commitment4844"`
Proof Proof `json:"proof"`
KzgProof string `json:"kzgProof"`
KzgProof []string `json:"kzgProof"`
}

func (r *InsertionResponse) MarshalJSON() ([]byte, error) {
kzgCommitmentParts := []string{
hex.EncodeToString(r.Commitment4844[:16]),
hex.EncodeToString(r.Commitment4844[16:32]),
hex.EncodeToString(r.Commitment4844[32:48]),
}

kzgProofParts := []string{
hex.EncodeToString(r.KzgProof[:16]),
hex.EncodeToString(r.KzgProof[16:32]),
hex.EncodeToString(r.KzgProof[32:48]),
}

return json.Marshal(
&InsertionResponseJSON{
InputHash: toHex(&r.InputHash),
ExpectedEvaluation: hex.EncodeToString(r.ExpectedEvaluation[:]),
Commitment4844: hex.EncodeToString(r.Commitment4844[:]),
Commitment4844: kzgCommitmentParts,
Proof: r.Proof,
KzgProof: hex.EncodeToString(r.KzgProof[:]),
KzgProof: kzgProofParts,
},
)
}
Expand All @@ -71,17 +83,31 @@ func (r *InsertionResponse) UnmarshalJSON(data []byte) error {
}
copy(r.ExpectedEvaluation[:], expectedEvaluation)

commitment4844, err := hex.DecodeString(aux.Commitment4844)
if err != nil || len(commitment4844) != 48 {
return fmt.Errorf("invalid Commitment4844: %s", aux.Commitment4844)
var commitment4844 []byte
for _, part := range aux.Commitment4844 {
partBytes, err := hex.DecodeString(part)
if err != nil || len(partBytes) != 16 {
return fmt.Errorf("invalid Commitment4844 part: %s", part)
}
commitment4844 = append(commitment4844, partBytes...)
}
if len(commitment4844) != 48 {
return fmt.Errorf("invalid concatenated commitment4844 length: %d", len(commitment4844))
}
copy(r.Commitment4844[:], commitment4844)

r.Proof = aux.Proof

kzgProof, err := hex.DecodeString(aux.KzgProof)
if err != nil || len(kzgProof) != 48 {
return fmt.Errorf("invalid KzgProof: %s", aux.KzgProof)
var kzgProof []byte
for _, part := range aux.KzgProof {
partBytes, err := hex.DecodeString(part)
if err != nil || len(partBytes) != 16 {
return fmt.Errorf("invalid KzgProof part: %s", part)
}
kzgProof = append(kzgProof, partBytes...)
}
if len(kzgProof) != 48 {
return fmt.Errorf("invalid concatenated KzgProof length: %d", len(kzgProof))
}
copy(r.KzgProof[:], kzgProof)

Expand Down

0 comments on commit 55c29c9

Please sign in to comment.