Skip to content

Commit

Permalink
fix kzg
Browse files Browse the repository at this point in the history
  • Loading branch information
syntrust committed Dec 18, 2023
1 parent 03f139a commit f432473
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/es-utils/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func UploadBlobs(
5000000,
"",
"",
"30000000000",
"300000000",
chainID,
calldata,
)
Expand Down
15 changes: 7 additions & 8 deletions integration_tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"context"
"fmt"
"io"
"log"
"math/big"
"net/http"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
esLog "github.com/ethstorage/go-ethstorage/ethstorage/log"
Expand All @@ -27,10 +25,10 @@ const (
)

var (
contractAddr1GB = common.HexToAddress("0x0c47aa023A3be36CA148a17a91aA8C9268A350AE")
contractAddrDevnet1 = common.HexToAddress("0x9f9F5Fd89ad648f2C000C954d8d9C87743243eC5")
contractAddr1GB = common.HexToAddress("0x24d29453B4B4917117B4af022Ad1B0a9aBc2c7DE")
contractAddrDevnet2 = common.HexToAddress("0xb4B46bdAA835F8E4b4d8e208B6559cD267851051")
minerAddr = common.HexToAddress("0x534632D6d7aD1fe5f832951c97FDe73E4eFD9a77")
value = hexutil.EncodeUint64(10000000000000)
value = "1000000000000000"
lg = esLog.NewLogger(esLog.DefaultCLIConfig())
)

Expand Down Expand Up @@ -61,7 +59,7 @@ func readSlotFromContract(ctx context.Context, client *ethclient.Client, l1Contr
}
bs, err := client.CallContract(ctx, msg, nil)
if err != nil {
return nil, fmt.Errorf("Failed to get %s from contract: %v", fieldName, err)
return nil, fmt.Errorf("failed to get %s from contract: %v", fieldName, err)
}
return bs, nil
}
Expand All @@ -72,15 +70,16 @@ func readUintFromContract(ctx context.Context, client *ethclient.Client, l1Contr
return 0, err
}
value := new(big.Int).SetBytes(bs).Uint64()
log.Println("Read uint from contract", "field", fieldName, "value", value)
lg.Info("Read uint from contract", "field", fieldName, "value", value)
return value, nil
}

func callVerify(calldata []byte) error {
ctx := context.Background()
client, err := ethclient.DialContext(ctx, l1Endpoint)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
lg.Error("Failed to connect to the Ethereum client", "error", err)
return err
}
defer client.Close()
msg := ethereum.CallMsg{
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/kzg_poseidon_prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package integration

import (
"os"
"path/filepath"
"testing"

Expand All @@ -14,11 +15,6 @@ import (
"github.com/ethstorage/go-ethstorage/ethstorage/prover"
)

const (
prPath = "../ethstorage/prover"
zkeyFile = "blob_poseidon.zkey"
)

func TestKZGPoseidonProver_GenerateZKProofs(t *testing.T) {
type args struct {
encodingKeys []common.Hash
Expand All @@ -44,6 +40,10 @@ func TestKZGPoseidonProver_GenerateZKProofs(t *testing.T) {
}

proverPath, _ := filepath.Abs(prPath)
zkeyFull := filepath.Join(proverPath, snarkLibDir, zkeyFile)
if _, err := os.Stat(zkeyFull); os.IsNotExist(err) {
t.Fatalf("%s not found", zkeyFull)
}
prv := prover.NewKZGPoseidonProver(proverPath, zkeyFile, esLog.NewLogger(esLog.DefaultCLIConfig()))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
77 changes: 56 additions & 21 deletions integration_tests/kzg_prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ package integration
import (
"bytes"
"context"
"log"
"math/big"
"os"
"testing"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethstorage/go-ethstorage/cmd/es-utils/utils"
"github.com/ethstorage/go-ethstorage/ethstorage/prover"
)
Expand Down Expand Up @@ -81,49 +82,83 @@ func uploadBlob(t *testing.T, data []byte) common.Hash {
if err != nil {
t.Fatalf("Get chain id failed %v", err)
}
pk := os.Getenv(pkName)
key, err := crypto.HexToECDSA(pk)
if err != nil {
t.Fatalf("Invalid private key: %s, err: %v", pk, err)
}
signer := crypto.PubkeyToAddress(key.PublicKey)
lg.Info("Get signer address", "signer", signer.Hex())
n, err := client.NonceAt(ctx, signer, big.NewInt(rpc.LatestBlockNumber.Int64()))
if err != nil {
t.Fatalf("Error getting nonce: %v", err)
}
blbKey := "0x0000000000000000000000000000000000000000000000000000000000000001"
blbIdx := common.Big0
length := new(big.Int).SetInt64(128 * 1024)
// calldata := "0x4581a920000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000"

// key: "0x0000000000000000000000000000000000000000000000000000000000000001"
// blobIdx: 0
// length: 128*1024
calldata := "0x4581a920000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000"
h := crypto.Keccak256Hash([]byte("putBlob(bytes32,uint256,uint256)"))
mid := h[0:4]
bytes32Type, _ := abi.NewType("bytes32", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
args := abi.Arguments{
{Type: bytes32Type},
{Type: uint256Type},
{Type: uint256Type},
}
values := []interface{}{common.HexToHash(blbKey), blbIdx, length}
dataField, err := args.Pack(values...)
if err != nil {
t.Fatalf("Error getting calldata: %v", err)
}
calldata := append(mid, dataField...)
valBig, _ := new(big.Int).SetString(value, 10)
estimatedGas, err := client.EstimateGas(ctx, ethereum.CallMsg{
From: signer,
To: &contractAddr1GB,
Value: valBig,
Data: calldata,
})
if err != nil {
lg.Crit("Estimate gas failed", "error", err.Error())
}
lg.Info("Estimated gas done", "gas", estimatedGas)

tx := utils.SendBlobTx(
l1Endpoint,
contractAddr1GB,
os.Getenv(pkName),
pk,
data,
true,
-1,
int64(n),
value,
510000,
"",
"",
"10000000000",
"100000000",
chainID.String(),
calldata,
"0x"+common.Bytes2Hex(calldata),
)
log.Printf("Blob transaction submitted %v", tx.Hash())
lg.Info("Blob transaction submitted", "hash", tx.Hash())
receipt, err := bind.WaitMined(ctx, client, tx)
if err != nil {
log.Fatal("Get transaction receipt err:", err)
}
if receipt.Status == 0 {
log.Fatal("Blob transaction failed")
lg.Crit("Get transaction receipt err", "error", err)
}
log.Printf("Blob transaction success! Gas used %v", receipt.GasUsed)
eventTopics := receipt.Logs[0].Topics
kvIndex := new(big.Int).SetBytes(eventTopics[1][:])
kvSize := new(big.Int).SetBytes(eventTopics[2][:])
dataHash := eventTopics[3]
log.Printf("Put Blob with kvIndex=%v kvSize=%v, dataHash=%x", kvIndex, kvSize, dataHash)
// if receipt.Status == 0 {
// lg.Crit("Blob transaction failed")
// }
lg.Info("Blob transaction success!", "GasUsed", receipt.GasUsed)
dataHash := receipt.Logs[0].Topics[3]
lg.Info("Put Blob done", "datahash", dataHash)
return dataHash
}

func verifyInclusive(trunkIdx uint64, peInput []byte) error {
ctx := context.Background()
client, err := ethclient.DialContext(ctx, l1Endpoint)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
lg.Crit("Failed to connect to the Ethereum client.", "error", err)
}
defer client.Close()

Expand Down
27 changes: 27 additions & 0 deletions integration_tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# usage:
# env ES_NODE_SIGNER_PRIVATE_KEY=<private_key> ./run_tests.sh

if [ -z "$ES_NODE_SIGNER_PRIVATE_KEY" ]; then
echo "Please provide 'ES_NODE_SIGNER_PRIVATE_KEY' as an environment variable"
exit 1
fi

if [ ${#ES_NODE_SIGNER_PRIVATE_KEY} -ne 64 ]; then
echo "Error: ES_NODE_SIGNER_PRIVATE_KEY should have a length of 64"
exit 1
fi

# download blob_poseidon.zkey if not yet
zkey_file="./ethstorage/prover/snarkjs/blob_poseidon.zkey"
if [ ! -e ${zkey_file} ]; then
echo "${zkey_file} not found, start downloading..."
file_id="1ZLfhYeCXMnbk6wUiBADRAn1mZ8MI_zg-"
html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${file_id}"`
curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Eo 'confirm=[a-zA-Z0-9\-_]+'`&id=${file_id}" -o ${zkey_file}
fi

# go test -timeout 0 github.com/ethstorage/go-ethstorage/integration_tests

go test -timeout 0 -run ^TestKZGProver_GenerateKZGProof$ github.com/ethstorage/go-ethstorage/integration_tests
6 changes: 3 additions & 3 deletions integration_tests/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSignerFactoryFromConfig(t *testing.T) {
HDPath: "",
},
common.HexToAddress("0xd7cc258C5438a392cA1D7873020d5B9971568c00"),
contractAddrDevnet1,
contractAddrDevnet2,
false,
},
{
Expand All @@ -45,7 +45,7 @@ func TestSignerFactoryFromConfig(t *testing.T) {
HDPath: "m/44'/60'/0'/0/0",
},
common.HexToAddress("0x627306090abab3a6e1400e9345bc60c78a8bef57"),
contractAddrDevnet1,
contractAddrDevnet2,
false,
},
{
Expand All @@ -58,7 +58,7 @@ func TestSignerFactoryFromConfig(t *testing.T) {
Address: "0x13259366de990b0431e2c97cea949362bb68df12",
},
common.HexToAddress("0x13259366DE990B0431E2C97CEa949362BB68df12"),
contractAddrDevnet1,
contractAddrDevnet2,
false,
},
{
Expand Down
14 changes: 10 additions & 4 deletions integration_tests/zk_prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ const (
snarkBuildDir = "snarkbuild"
proofName = "proof_blob_poseidon.json"
publicName = "public_blob_poseidon.json"
prPath = "../ethstorage/prover"
zkeyFile = "blob_poseidon.zkey"
)

func TestZKProver_GenerateZKProof(t *testing.T) {
proverPath, _ := filepath.Abs(prPath)
zkeyFull := filepath.Join(proverPath, snarkLibDir, zkeyFile)
if _, err := os.Stat(zkeyFull); os.IsNotExist(err) {
t.Fatalf("%s not found", zkeyFull)
}
type args struct {
encodingKey common.Hash
chunkIdx uint64
Expand Down Expand Up @@ -66,9 +73,8 @@ func TestZKProver_GenerateZKProof(t *testing.T) {
false,
},
}
path, _ := filepath.Abs("../ethstorage/prover")
libDir := filepath.Join(path, snarkLibDir)
p := prover.NewZKProverInternal(path, "blob_poseidon.zkey", lg)
libDir := filepath.Join(proverPath, snarkLibDir)
p := prover.NewZKProverInternal(proverPath, zkeyFile, lg)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
maskGo, err := GenerateMask(tt.args.encodingKey, tt.args.chunkIdx)
Expand All @@ -81,7 +87,7 @@ func TestZKProver_GenerateZKProof(t *testing.T) {
t.Errorf("ZKProver.GenerateZKProof() error = %v, wantErr %v", err, tt.wantErr)
return
}
buildDir := filepath.Join(path, snarkBuildDir, strings.Join([]string{
buildDir := filepath.Join(proverPath, snarkBuildDir, strings.Join([]string{
tt.args.encodingKey.Hex(),
fmt.Sprint(tt.args.chunkIdx),
}, "-"))
Expand Down

0 comments on commit f432473

Please sign in to comment.