Skip to content

refactor(zk_verifier): reuse HashToField from cometbls/crypto/bn254 #4565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions 11-cometbls/zk_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
backend "github.com/consensys/gnark/backend/groth16"
backend_bn254 "github.com/consensys/gnark/backend/groth16/bn254"
"github.com/holiman/uint256"
comet "github.com/unionlabs/cometbls/crypto/bn254"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -155,26 +156,6 @@ func (zkp ZKP) Verify(trustedValidatorsHash []byte, header ProverLightHeader) er
return verifyingKey.CommitmentKey.Verify(zkp.ProofCommitment, zkp.ProofCommitmentPoK)
}

func hashToField(msg []byte) fr.Element {
hmac := hmac.New(Hash, []byte(CometblsHMACKey))
hmac.Write(msg)
modMinusOne := new(big.Int).Sub(fr.Modulus(), big.NewInt(1))
num := new(big.Int).SetBytes(hmac.Sum(nil))
num.Mod(num, modMinusOne)
num.Add(num, big.NewInt(1))
val, overflow := uint256.FromBig(num)
if overflow {
panic("impossible; qed;")
}
valBytes := val.Bytes32()
var element fr.Element
err := element.SetBytesCanonical(valBytes[:])
if err != nil {
panic("impossible; qed;")
}
return element
}

func commitmentsHash(proofCommitment curve.G1Affine) fr.Element {
var buffer [64]byte

Expand All @@ -184,7 +165,7 @@ func commitmentsHash(proofCommitment curve.G1Affine) fr.Element {
y := proofCommitment.Y.Bytes()
copy(buffer[32:64], y[:])

return hashToField(buffer[:])
return comet.HashToField(buffer[:])
}

func inputsHash(header ProverLightHeader, trustedValidatorsHash []byte) fr.Element {
Expand Down
2 changes: 1 addition & 1 deletion 11-cometbls/zk_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestVerifier(t *testing.T) {
err = zkp.Verify(

trustedValHash,
LightHeader{
ProverLightHeader{
ChainId: "union-devnet-1337",
Height: 3405691582,
Time: time.Unix(1710783278, 499600406),
Expand Down