Skip to content

Commit 8c3627e

Browse files
committed
refactor(zk_verifier): reuse HashToField from cometbls/crypto/bn254
1 parent 92b34fd commit 8c3627e

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

11-cometbls/zk_verifier.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"math/big"
1111
"time"
1212

13+
comet "github.com/unionlabs/cometbls/crypto/bn254"
1314
curve "github.com/consensys/gnark-crypto/ecc/bn254"
1415
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
1516
backend "github.com/consensys/gnark/backend/groth16"
@@ -155,25 +156,6 @@ func (zkp ZKP) Verify(trustedValidatorsHash []byte, header ProverLightHeader) er
155156
return verifyingKey.CommitmentKey.Verify(zkp.ProofCommitment, zkp.ProofCommitmentPoK)
156157
}
157158

158-
func hashToField(msg []byte) fr.Element {
159-
hmac := hmac.New(Hash, []byte(CometblsHMACKey))
160-
hmac.Write(msg)
161-
modMinusOne := new(big.Int).Sub(fr.Modulus(), big.NewInt(1))
162-
num := new(big.Int).SetBytes(hmac.Sum(nil))
163-
num.Mod(num, modMinusOne)
164-
num.Add(num, big.NewInt(1))
165-
val, overflow := uint256.FromBig(num)
166-
if overflow {
167-
panic("impossible; qed;")
168-
}
169-
valBytes := val.Bytes32()
170-
var element fr.Element
171-
err := element.SetBytesCanonical(valBytes[:])
172-
if err != nil {
173-
panic("impossible; qed;")
174-
}
175-
return element
176-
}
177159

178160
func commitmentsHash(proofCommitment curve.G1Affine) fr.Element {
179161
var buffer [64]byte
@@ -184,7 +166,7 @@ func commitmentsHash(proofCommitment curve.G1Affine) fr.Element {
184166
y := proofCommitment.Y.Bytes()
185167
copy(buffer[32:64], y[:])
186168

187-
return hashToField(buffer[:])
169+
return comet.HashToField(buffer[:])
188170
}
189171

190172
func inputsHash(header ProverLightHeader, trustedValidatorsHash []byte) fr.Element {
@@ -214,4 +196,4 @@ func inputsHash(header ProverLightHeader, trustedValidatorsHash []byte) fr.Eleme
214196
var e fr.Element
215197
e.SetBytes(hash[1:])
216198
return e
217-
}
199+
}

11-cometbls/zk_verifier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestVerifier(t *testing.T) {
2121
err = zkp.Verify(
2222

2323
trustedValHash,
24-
LightHeader{
24+
ProverLightHeader{
2525
ChainId: "union-devnet-1337",
2626
Height: 3405691582,
2727
Time: time.Unix(1710783278, 499600406),

0 commit comments

Comments
 (0)