Skip to content

Commit

Permalink
Merge pull request #17 from reilabs/wz/4844-versioned-hash
Browse files Browse the repository at this point in the history
insertion_circuit: use versioned KZG hash
  • Loading branch information
wzmuda authored Aug 30, 2024
2 parents cd49443 + 12462b1 commit 568b3bb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ func TestInsertionHappyPath(t *testing.T) {
t.Fatal(err)
}

versionedKzgHash := prover.KzgToVersionedHash(ir.Commitment4844)
publicWitness, err := frontend.NewWitness(&prover.InsertionMbuCircuit{
InputHash: ir.InputHash,
ExpectedEvaluation: ir.ExpectedEvaluation[:],
Commitment4844: ir.Commitment4844[:],
ExpectedEvaluation: *prover.BytesToBn254BigInt(ir.ExpectedEvaluation[:]),
Commitment4844: *prover.BytesToBn254BigInt(versionedKzgHash[:]),
StartIndex: params.StartIndex,
PreRoot: params.PreRoot,
PostRoot: params.PostRoot,
Expand Down
15 changes: 12 additions & 3 deletions prover/circuit_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prover

import (
"crypto/sha256"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -320,8 +321,8 @@ func identitiesToBlob(ids []big.Int) *gokzg4844.Blob {
return &blob
}

// bytesToBn254BigInt converts a slice of bytes to a *big.Int and reduces it by BN254 modulus
func bytesToBn254BigInt(b []byte) *big.Int {
// BytesToBn254BigInt converts a slice of bytes to a *big.Int and reduces it by BN254 modulus
func BytesToBn254BigInt(b []byte) *big.Int {
n := new(big.Int).SetBytes(b)
modulus := bn254fr.Modulus()
return n.Mod(n, modulus)
Expand All @@ -338,7 +339,7 @@ func bigIntsToChallenge(input []big.Int) (challenge gokzg4844.Scalar) {
}

// Reduce keccak because gokzg4844 API expects that
hashBytes := bytesToBn254BigInt(keccak256.Hash(inputBytes)).Bytes()
hashBytes := BytesToBn254BigInt(keccak256.Hash(inputBytes)).Bytes()

copy(challenge[:], hashBytes)
return challenge
Expand All @@ -352,3 +353,11 @@ func treeDepth(leavesCount int) (height int) {
height = int(math.Ceil(math.Log2(float64(leavesCount))))
return
}

// KzgToVersionedHash converts a KZG commitment to a versioned hash.
// Implementation as per https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#helpers
func KzgToVersionedHash(commitment gokzg4844.KZGCommitment) (hash [32]byte) {
hash = sha256.Sum256(commitment[:])
hash[0] = 0x01 // magic number, must be there
return
}
11 changes: 6 additions & 5 deletions prover/insertion_circuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ func TestInsertionCircuit(t *testing.T) {
smallTree.Update(i, id)
}
incomingIdsTreeRoot := smallTree.Root()
incomingIdsTreeRoot = *bytesToBn254BigInt(incomingIdsTreeRoot.Bytes())
incomingIdsTreeRoot = *BytesToBn254BigInt(incomingIdsTreeRoot.Bytes())

ctx, err := gokzg4844.NewContext4096Secure()
require.NoError(t, err)
blob := identitiesToBlob(incomingIds)
commitment, err := ctx.BlobToKZGCommitment(blob, numGoRoutines)
require.NoError(t, err)
commitment4844 := *bytesToBn254BigInt(commitment[:])
versionedKzgHash := KzgToVersionedHash(commitment)
versionedKzgHashReduced := *BytesToBn254BigInt(versionedKzgHash[:])

challenge := bigIntsToChallenge([]big.Int{incomingIdsTreeRoot, commitment4844})
challenge := bigIntsToChallenge([]big.Int{incomingIdsTreeRoot, versionedKzgHashReduced})
proof, evaluation, err := ctx.ComputeKZGProof(blob, challenge, numGoRoutines)
require.NoError(t, err)
err = ctx.VerifyKZGProof(commitment, challenge, evaluation, proof)
Expand Down Expand Up @@ -75,8 +76,8 @@ func TestInsertionCircuit(t *testing.T) {

assignment := InsertionMbuCircuit{
InputHash: incomingIdsTreeRoot,
ExpectedEvaluation: evaluation[:],
Commitment4844: commitment4844,
ExpectedEvaluation: *BytesToBn254BigInt(evaluation[:]),
Commitment4844: versionedKzgHashReduced,
StartIndex: existingUsersCount,
PreRoot: preRoot,
PostRoot: postRoot,
Expand Down
11 changes: 6 additions & 5 deletions prover/insertion_proving_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (ps *ProvingSystem) ProveInsertion(params *InsertionParameters) (*Insertion
idCommsTree.Update(i, params.IdComms[i])
}
incomingIdsTreeRoot := idCommsTree.Root()
incomingIdsTreeRoot = *bytesToBn254BigInt(incomingIdsTreeRoot.Bytes())
incomingIdsTreeRoot = *BytesToBn254BigInt(incomingIdsTreeRoot.Bytes())

proofs := make([][]frontend.Variable, ps.BatchSize)
for i := 0; i < int(ps.BatchSize); i++ {
Expand All @@ -106,9 +106,10 @@ func (ps *ProvingSystem) ProveInsertion(params *InsertionParameters) (*Insertion
if err != nil {
return nil, err
}
commitment4844 := *bytesToBn254BigInt(commitment[:])
versionedKzgHash := KzgToVersionedHash(commitment)
versionedKzgHashReduced := *BytesToBn254BigInt(versionedKzgHash[:])

challenge := bigIntsToChallenge([]big.Int{incomingIdsTreeRoot, commitment4844})
challenge := bigIntsToChallenge([]big.Int{incomingIdsTreeRoot, versionedKzgHashReduced})
kzgProof, evaluation, err := ctx.ComputeKZGProof(blob, challenge, numGoRoutines)
if err != nil {
return nil, err
Expand All @@ -121,8 +122,8 @@ func (ps *ProvingSystem) ProveInsertion(params *InsertionParameters) (*Insertion

assignment := InsertionMbuCircuit{
InputHash: incomingIdsTreeRoot,
ExpectedEvaluation: evaluation[:],
Commitment4844: commitment4844,
ExpectedEvaluation: *BytesToBn254BigInt(evaluation[:]),
Commitment4844: versionedKzgHashReduced,
StartIndex: params.StartIndex,
PreRoot: params.PreRoot,
PostRoot: params.PostRoot,
Expand Down

0 comments on commit 568b3bb

Please sign in to comment.