From dc7bbb625baf1842645668659a377a0338a1caca Mon Sep 17 00:00:00 2001 From: ZhAnGeek Date: Wed, 12 Apr 2023 14:36:47 +0800 Subject: [PATCH] zkbnb-develop-v0.8.0-contract-solving-update --- internal/backend/bls12-377/groth16/commitment.go | 6 ++++-- internal/backend/bls12-381/groth16/commitment.go | 6 ++++-- internal/backend/bls24-315/groth16/commitment.go | 6 ++++-- internal/backend/bls24-317/groth16/commitment.go | 6 ++++-- internal/backend/bn254/groth16/commitment.go | 6 ++++-- internal/backend/bw6-633/groth16/commitment.go | 6 ++++-- internal/backend/bw6-761/groth16/commitment.go | 6 ++++-- .../template/zkpschemes/groth16/groth16.commitment.go.tmpl | 6 ++++-- 8 files changed, 32 insertions(+), 16 deletions(-) diff --git a/internal/backend/bls12-377/groth16/commitment.go b/internal/backend/bls12-377/groth16/commitment.go index 72506760a4..0067965d6b 100644 --- a/internal/backend/bls12-377/groth16/commitment.go +++ b/internal/backend/bls12-377/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bls12-377" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bls12-381/groth16/commitment.go b/internal/backend/bls12-381/groth16/commitment.go index 680f862419..4e22fb1059 100644 --- a/internal/backend/bls12-381/groth16/commitment.go +++ b/internal/backend/bls12-381/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bls12-381" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bls24-315/groth16/commitment.go b/internal/backend/bls24-315/groth16/commitment.go index 3cb966f8f0..5ef8b0736f 100644 --- a/internal/backend/bls24-315/groth16/commitment.go +++ b/internal/backend/bls24-315/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bls24-315" "github.com/consensys/gnark-crypto/ecc/bls24-315/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bls24-317/groth16/commitment.go b/internal/backend/bls24-317/groth16/commitment.go index 00b3713dd6..40238829c9 100644 --- a/internal/backend/bls24-317/groth16/commitment.go +++ b/internal/backend/bls24-317/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bls24-317" "github.com/consensys/gnark-crypto/ecc/bls24-317/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bn254/groth16/commitment.go b/internal/backend/bn254/groth16/commitment.go index d3930c0f08..9790e2dcd8 100644 --- a/internal/backend/bn254/groth16/commitment.go +++ b/internal/backend/bn254/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bw6-633/groth16/commitment.go b/internal/backend/bw6-633/groth16/commitment.go index d1243f342c..b98bc25598 100644 --- a/internal/backend/bw6-633/groth16/commitment.go +++ b/internal/backend/bw6-633/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bw6-633" "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/backend/bw6-761/groth16/commitment.go b/internal/backend/bw6-761/groth16/commitment.go index c332669a12..891e633015 100644 --- a/internal/backend/bw6-761/groth16/commitment.go +++ b/internal/backend/bw6-761/groth16/commitment.go @@ -17,6 +17,7 @@ package groth16 import ( + "crypto/sha256" curve "github.com/consensys/gnark-crypto/ecc/bw6-761" "github.com/consensys/gnark-crypto/ecc/bw6-761/fr" "github.com/consensys/gnark/constraint" @@ -24,6 +25,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } diff --git a/internal/generator/backend/template/zkpschemes/groth16/groth16.commitment.go.tmpl b/internal/generator/backend/template/zkpschemes/groth16/groth16.commitment.go.tmpl index da74e65321..b2073c89bf 100644 --- a/internal/generator/backend/template/zkpschemes/groth16/groth16.commitment.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/groth16/groth16.commitment.go.tmpl @@ -1,4 +1,5 @@ import ( + "crypto/sha256" {{- template "import_fr" . }} {{- template "import_curve" . }} "github.com/consensys/gnark/constraint" @@ -6,6 +7,7 @@ import ( ) func solveCommitmentWire(commitmentInfo *constraint.Commitment, commitment *curve.G1Affine, publicCommitted []*big.Int) (fr.Element, error) { - res, err := fr.Hash(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1), []byte(constraint.CommitmentDst), 1) - return res[0], err + bytes := sha256.Sum256(commitmentInfo.SerializeCommitment(commitment.Marshal(), publicCommitted, (fr.Bits-1)/8+1)) + bi := new(big.Int).SetBytes(bytes[:]) + return *new(fr.Element).SetBigInt(bi), nil } \ No newline at end of file