Skip to content

Commit 75c2d0d

Browse files
committed
refactor(zk_verifier): reuse HashToField from cometbls/crypto/bn254
Signed-off-by: Himess <95512809+Himess@users.noreply.github.com>
1 parent 92b34fd commit 75c2d0d

File tree

2 files changed

+4
-23
lines changed

2 files changed

+4
-23
lines changed

11-cometbls/zk_verifier.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
backend "github.com/consensys/gnark/backend/groth16"
1616
backend_bn254 "github.com/consensys/gnark/backend/groth16/bn254"
1717
"github.com/holiman/uint256"
18+
comet "github.com/unionlabs/cometbls/crypto/bn254"
1819
"golang.org/x/crypto/sha3"
1920
)
2021

@@ -155,26 +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-
}
177-
178159
func commitmentsHash(proofCommitment curve.G1Affine) fr.Element {
179160
var buffer [64]byte
180161

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

187-
return hashToField(buffer[:])
168+
return comet.HashToField(buffer[:])
188169
}
189170

190171
func inputsHash(header ProverLightHeader, trustedValidatorsHash []byte) fr.Element {
@@ -214,4 +195,4 @@ func inputsHash(header ProverLightHeader, trustedValidatorsHash []byte) fr.Eleme
214195
var e fr.Element
215196
e.SetBytes(hash[1:])
216197
return e
217-
}
198+
}

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)