zkpbinding: allow to handle serialisation format of gnark >= v0.9.0

An upgrade from gnark v0.8.X to v0.9.0 changes serialization format of verifying/proving keys
and proofs. In neo-go zkpbinding package we have to support both at least for now, because
gnark@v0.9.0 requires minimum go 1.19.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-10-05 12:44:43 +03:00
parent 03951c94b0
commit ff260a6a9b

View file

@ -242,8 +242,8 @@ func GenerateVerifier(cfg Config) error {
return errors.New("unexpected len of constant-size part of serialized verifying key") return errors.New("unexpected len of constant-size part of serialized verifying key")
} }
kvkLen := binary.BigEndian.Uint32(vkBytes[verifyingKeyConstantPartLen-4:]) kvkLen := binary.BigEndian.Uint32(vkBytes[verifyingKeyConstantPartLen-4:])
if len(vkBytes) != verifyingKeyConstantPartLen+int(kvkLen*bls12381.SizeOfG1AffineCompressed) { if len(vkBytes) < verifyingKeyConstantPartLen+int(kvkLen*bls12381.SizeOfG1AffineCompressed) {
return errors.New("unexpected len of serialized verifying key") return fmt.Errorf("unexpected len of serialized verifying key: expected at least %d got %d", verifyingKeyConstantPartLen+int(kvkLen*bls12381.SizeOfG1AffineCompressed), len(vkBytes))
} }
var ( var (
@ -340,14 +340,14 @@ func GetVerifyProofArgs(proof groth16.Proof, publicWitness witness.Witness) (*Ve
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to serialize proof: %w", err) return nil, fmt.Errorf("failed to serialize proof: %w", err)
} }
if n != proofSizeCompressed { if n < proofSizeCompressed {
return nil, fmt.Errorf("unexpected serialized proof length: %d", n) return nil, fmt.Errorf("unexpected serialized proof length: expected at least %d, got %d", proofSizeCompressed, n)
} }
proofBytes := slice.Copy(buf.Bytes()) proofBytes := slice.Copy(buf.Bytes())
aBytes := proofBytes[:bls12381.SizeOfG1AffineCompressed] aBytes := proofBytes[:bls12381.SizeOfG1AffineCompressed]
bBytes := proofBytes[bls12381.SizeOfG1AffineCompressed : bls12381.SizeOfG1AffineCompressed+bls12381.SizeOfG2AffineCompressed] bBytes := proofBytes[bls12381.SizeOfG1AffineCompressed : bls12381.SizeOfG1AffineCompressed+bls12381.SizeOfG2AffineCompressed]
cBytes := proofBytes[bls12381.SizeOfG1AffineCompressed+bls12381.SizeOfG2AffineCompressed:] cBytes := proofBytes[bls12381.SizeOfG1AffineCompressed+bls12381.SizeOfG2AffineCompressed : bls12381.SizeOfG1AffineCompressed+bls12381.SizeOfG2AffineCompressed+bls12381.SizeOfG1AffineCompressed]
publicWitnessBytes, err := publicWitness.MarshalBinary() publicWitnessBytes, err := publicWitness.MarshalBinary()
if err != nil { if err != nil {