Implementing grpc in gnark v0.8.1, how to convert Proof, Verification Key & Public Witness to go-native type?

34 views Asked by At

In gnark v0.8.1,

We first need to convert those 3 to go-native i.e. we convert them to []byte.
I tried implementing Serialize() and DeSerialize(), but I am facing errors.

Issue: groth16.Proof is an interface and its implementation depends upon curve. So, how do we handle them at the same time, during []byte conversion and type cast them.

func Serialize(x any) ([]byte, error) {
    var buf bytes.Buffer
    enc := gob.NewEncoder(&buf)
    if err := enc.Encode(x); err != nil {
        return nil, err
    }
    fmt.Printf("buf.Bytes(): %v\n", buf.Bytes())
    return buf.Bytes(), nil
}

func DeSerialize(x []byte, returnType string) (interface{}, error) {
    fmt.Printf("x: %v\n", x)
    fmt.Printf("returnType: %v\n", returnType)
    buf := bytes.NewBuffer(x)
    dec := gob.NewDecoder(buf)
    switch returnType {
    case "proof":
        var proof groth16.Proof
        gob.Register(internal.Proof{})
        if err := dec.Decode(&proof); err != nil {
            fmt.Printf("----> Decode err: %v\n", err)
            return groth16.NewProof(ecc.BN254), err
        }
        return proof, nil

    case "publicWitness":
        var w witness.Witness
        if err := dec.Decode(&w); err != nil {
            res, _ := frontend.NewWitness(nil, ecc.BN254.ScalarField())
            return res, err
        }
        return w, nil

    case "verificationKey":
        var vk groth16.VerifyingKey
        if err := dec.Decode(&vk); err != nil {
            return vk, err
        }
        return vk, nil
    default:
        return nil, errors.New("wrong returnType")
    }

}

In this above code, I am getting error: Decode err: gob: local interface type *groth16.Proof can only be decoded from remote interface type; received concrete type

Proof = struct {
    Ar G1Affine = struct {
        X[4] uint;
        Y Element;
    };
    Krs G1Affine;
    Bs G2Affine = struct {
        X E2 = struct {
            A0 Element;
            A1 Element;
        };
        Y E2;
    };
    Commitment G1Affine;
    CommitmentPok G1Affine;
}
0

There are 0 answers