Skip to content
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

optimization: generate zk proof for 2 samples at once #167

Merged
merged 31 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d325a50
new zkp
syntrust Dec 22, 2023
fce41cf
types change
syntrust Dec 22, 2023
5a6f2fe
apply api update
syntrust Dec 26, 2023
80072fe
fix
syntrust Dec 26, 2023
1028b0d
minor
syntrust Dec 27, 2023
9bfdb87
Merge branch 'main' of https://github.com/ethstorage/es-node into zkp2
syntrust Dec 27, 2023
3694b6e
merge and fix tests
syntrust Dec 27, 2023
23368bb
fixes
syntrust Dec 28, 2023
69da9b8
Merge branch 'main' of https://github.com/ethstorage/es-node into zkp2
syntrust Jan 3, 2024
9b067c2
fix ci
syntrust Jan 3, 2024
e08f11f
backward compatable of one proof per sample
syntrust Jan 3, 2024
82d2942
add zk version flag / make mask big int
syntrust Jan 4, 2024
d21a82d
fix tests
syntrust Jan 4, 2024
9a4baa8
fix test
syntrust Jan 4, 2024
b83a46f
fix test
syntrust Jan 4, 2024
f60bd3f
support zkp version
syntrust Jan 4, 2024
4c2d1f7
minor
syntrust Jan 5, 2024
7700741
fix tests
syntrust Jan 5, 2024
cc1212b
Merge branch 'main' of https://github.com/ethstorage/es-node into zkp2
syntrust Jan 5, 2024
9958f62
merge
syntrust Jan 5, 2024
da40f7d
fix
syntrust Jan 5, 2024
5199a89
fix
syntrust Jan 5, 2024
4ab2cd5
fix
syntrust Jan 5, 2024
01a1536
Merge branch 'main' of https://github.com/ethstorage/es-node into zkp2
syntrust Jan 9, 2024
0ab63b1
rename flag
syntrust Jan 9, 2024
7f97038
fix comments
syntrust Jan 9, 2024
0d52efe
fix build
syntrust Jan 9, 2024
1d7892d
rename flag
syntrust Jan 9, 2024
38740d5
fix comments
syntrust Jan 9, 2024
ca9d137
read zk mode
syntrust Jan 10, 2024
34bc3ec
Merge branch 'main' of https://github.com/ethstorage/es-node into zkp2
syntrust Jan 10, 2024
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FROM node:16-alpine
COPY --from=builder /es-node/build/ /es-node/build/

# For zk proof
RUN npm install -g snarkjs@0.7.0
RUN npm install -g snarkjs
RUN apk add --no-cache curl grep

# Entrypoint
Expand Down
2 changes: 1 addition & 1 deletion GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,5 @@ nvm use 20
```
### Install snarkjs
```sh
npm install -g snarkjs@0.7.0
npm install -g snarkjs
```
10 changes: 10 additions & 0 deletions ethstorage/miner/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
PriorityGasPriceFlagName = "miner.priority-gas-price"
ZKeyFileNameFlagName = "miner.zkey"
ZKWorkingDirFlagName = "miner.zk-working-dir"
ZKProverVersionFlagName = "miner.zk-prover-version"
syntrust marked this conversation as resolved.
Show resolved Hide resolved
ThreadsPerShardFlagName = "miner.threads-per-shard"
MinimumProfitFlagName = "miner.min-profit"
)
Expand Down Expand Up @@ -62,6 +63,12 @@ func CLIFlags(envPrefix string) []cli.Flag {
Value: DefaultConfig.ZKWorkingDir,
EnvVar: rollup.PrefixEnvVar(envPrefix, "ZK_WORKING_DIR"),
},
cli.Uint64Flag{
Name: ZKProverVersionFlagName,
Usage: "ZK prover version, 1: one proof per sample, 2: one proof for multiple samples. Default: 2",
Value: DefaultConfig.ZKProverVersion,
EnvVar: rollup.PrefixEnvVar(envPrefix, "ZK_PROVER_VERSION"),
},
cli.Uint64Flag{
Name: ThreadsPerShardFlagName,
Usage: "Number of threads per shard",
Expand All @@ -79,6 +86,7 @@ type CLIConfig struct {
MinimumProfit *big.Int
ZKeyFileName string
ZKWorkingDir string
ZKProverVersion uint64
ThreadsPerShard uint64
}

Expand Down Expand Up @@ -107,6 +115,7 @@ func (c CLIConfig) ToMinerConfig() (Config, error) {
cfg.PriorityGasPrice = c.PriorityGasPrice
cfg.MinimumProfit = c.MinimumProfit
cfg.ZKeyFileName = c.ZKeyFileName
cfg.ZKProverVersion = c.ZKProverVersion
cfg.ThreadsPerShard = c.ThreadsPerShard
return cfg, nil
}
Expand All @@ -119,6 +128,7 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig {
MinimumProfit: types.GlobalBig(ctx, MinimumProfitFlagName),
ZKeyFileName: ctx.GlobalString(ZKeyFileNameFlagName),
ZKWorkingDir: ctx.GlobalString(ZKWorkingDirFlagName),
ZKProverVersion: ctx.GlobalUint64(ZKProverVersionFlagName),
ThreadsPerShard: ctx.GlobalUint64(ThreadsPerShardFlagName),
}
return cfg
Expand Down
4 changes: 3 additions & 1 deletion ethstorage/miner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
PriorityGasPrice *big.Int
ZKeyFileName string
ZKWorkingDir string
ZKProverVersion uint64
ThreadsPerShard uint64
SignerFnFactory signer.SignerFactory
SignerAddr common.Address
Expand All @@ -46,8 +47,9 @@ var DefaultConfig = Config{

GasPrice: nil,
PriorityGasPrice: nil,
ZKeyFileName: "blob_poseidon.zkey",
ZKeyFileName: "blob_poseidon2.zkey",
ZKWorkingDir: filepath.Join("build", "bin"),
ZKProverVersion: 2,
ThreadsPerShard: uint64(2 * runtime.NumCPU()),
MinimumProfit: common.Big0,
}
14 changes: 11 additions & 3 deletions ethstorage/miner/l1_mining_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
rewardDenominator = 10000
)

var (
mineSig = crypto.Keccak256Hash([]byte(`mine(uint256,uint256,address,uint256,bytes32[],uint256[],bytes[],bytes[])`))
)

func NewL1MiningAPI(l1 *eth.PollingClient, lg log.Logger) *l1MiningAPI {
return &l1MiningAPI{l1, lg}
}
Expand Down Expand Up @@ -81,6 +85,7 @@ func (m *l1MiningAPI) GetDataHashes(ctx context.Context, contract common.Address
func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Address, rst result, cfg Config) (common.Hash, error) {
m.lg.Debug("Submit mined result", "shard", rst.startShardId, "block", rst.blockNumber, "nonce", rst.nonce)
uint256Type, _ := abi.NewType("uint256", "", nil)
uint256Array, _ := abi.NewType("uint256[]", "", nil)
addrType, _ := abi.NewType("address", "", nil)
bytes32Array, _ := abi.NewType("bytes32[]", "", nil)
bytesArray, _ := abi.NewType("bytes[]", "", nil)
Expand All @@ -90,17 +95,20 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add
{Type: addrType},
{Type: uint256Type},
{Type: bytes32Array},
{Type: uint256Array},
{Type: bytesArray},
{Type: bytesArray},
}.Pack(
rst.blockNumber,
new(big.Int).SetUint64(rst.startShardId),
rst.miner,
new(big.Int).SetUint64(rst.nonce),
rst.encodedData,
rst.proofs,
rst.masks,
rst.inclusiveProofs,
rst.decodeProof,
)
h := crypto.Keccak256Hash([]byte(`mine(uint256,uint256,address,uint256,bytes32[],bytes[])`))
calldata := append(h[0:4], dataField...)
calldata := append(mineSig[0:4], dataField...)

gasPrice := cfg.GasPrice
if gasPrice == nil || gasPrice.Cmp(common.Big0) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion ethstorage/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type L1API interface {
}

type MiningProver interface {
GetStorageProof(encodedKVs []byte, encodingKey common.Hash, sampleIdxInKv uint64) ([]byte, error)
GetStorageProof(encodedKVs [][]byte, encodingKey []common.Hash, sampleIdxInKv []uint64) ([]*big.Int, [][]byte, [][]byte, error)
}

type miningInfo struct {
Expand Down
5 changes: 3 additions & 2 deletions ethstorage/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ func newMiner(t *testing.T, storageMgr *es.StorageManager, client *eth.PollingCl
GasPrice: nil,
PriorityGasPrice: new(big.Int).SetUint64(10),
ThreadsPerShard: 1,
ZKeyFileName: "blob_poseidon.zkey",
ZKProverVersion: 2,
ZKeyFileName: "blob_poseidon2.zkey",
}
l1api := NewL1MiningAPI(client, lg)
zkWorkingDir, _ := filepath.Abs("../prover")
pvr := prover.NewKZGPoseidonProver(zkWorkingDir, defaultConfig.ZKeyFileName, lg)
pvr := prover.NewKZGPoseidonProver(zkWorkingDir, defaultConfig.ZKeyFileName, defaultConfig.ZKProverVersion, lg)
fd := new(event.Feed)
miner := New(defaultConfig, storageMgr, l1api, &pvr, fd, lg)
return miner
Expand Down
44 changes: 23 additions & 21 deletions ethstorage/miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func (t *taskItem) String() string {
}

type result struct {
blockNumber *big.Int
startShardId uint64
miner common.Address
nonce uint64
encodedData []common.Hash
proofs [][]byte
blockNumber *big.Int
startShardId uint64
miner common.Address
nonce uint64
encodedData []common.Hash
masks []*big.Int
inclusiveProofs [][]byte
decodeProof [][]byte
}

// worker is the main object which takes care of storage mining
Expand Down Expand Up @@ -425,23 +427,21 @@ func (w *worker) mineTask(t *taskItem) (bool, error) {
return false, err
}
w.lg.Info("Got sample data", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "kvIdxs", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv)
proofs := make([][]byte, len(kvIdxs))
for i := 0; i < len(dataSet); i++ {
ps, err := w.prover.GetStorageProof(dataSet[i], encodingKeys[i], sampleIdxsInKv[i])
if err != nil {
w.lg.Error("Get storage proof error", "kvIdx", kvIdxs[i], "sampleIdxsInKv", sampleIdxsInKv[i], "error", err.Error())
return false, fmt.Errorf("get proof err: %v", err)
}
w.lg.Info("Got storage proof", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "kvIdx", kvIdxs[i], "sampleIdxsInKv", sampleIdxsInKv[i])
proofs[i] = ps
masks, decodeProof, inclusiveProofs, err := w.prover.GetStorageProof(dataSet, encodingKeys, sampleIdxsInKv)
if err != nil {
w.lg.Error("Get storage proof error", "kvIdx", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv, "error", err.Error())
return false, fmt.Errorf("get proof err: %v", err)
}
w.lg.Info("Got storage proof", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "kvIdx", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv)
newResult := &result{
blockNumber: t.blockNumber,
startShardId: t.shardIdx,
miner: t.miner,
nonce: nonce,
encodedData: encodedSamples,
proofs: proofs,
blockNumber: t.blockNumber,
startShardId: t.shardIdx,
miner: t.miner,
nonce: nonce,
encodedData: encodedSamples,
masks: masks,
decodeProof: decodeProof,
inclusiveProofs: inclusiveProofs,
}
// push result to the result map
w.resultLock.Lock()
Expand Down Expand Up @@ -483,6 +483,7 @@ func (w *worker) getMiningData(t *task, sampleIdx []uint64) ([][]byte, []uint64,
}
kvHashes, err := w.l1API.GetDataHashes(context.Background(), w.storageMgr.ContractAddress(), kvIdxs)
if err != nil {
w.lg.Error("Get data hashes error", "kvIdxs", kvIdxs, "error", err.Error())
return nil, nil, nil, nil, nil, err
}
for i := uint64(0); i < checksLen; i++ {
Expand All @@ -500,6 +501,7 @@ func (w *worker) getMiningData(t *task, sampleIdx []uint64) ([][]byte, []uint64,
if !exist {
err = fmt.Errorf("kv not found: index=%d", kvIdxs[i])
}
w.lg.Error("Get data error", "index", kvIdxs[i], "error", err.Error())
return nil, nil, nil, nil, nil, err
}
}
Expand Down
7 changes: 6 additions & 1 deletion ethstorage/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ func (n *EsNode) initMiner(ctx context.Context, cfg *Config) error {
return nil
}
l1api := miner.NewL1MiningAPI(n.l1Source, n.log)
pvr := prover.NewKZGPoseidonProver(cfg.Mining.ZKWorkingDir, cfg.Mining.ZKeyFileName, n.log)
pvr := prover.NewKZGPoseidonProver(
cfg.Mining.ZKWorkingDir,
cfg.Mining.ZKeyFileName,
cfg.Mining.ZKProverVersion,
n.log,
)
n.miner = miner.New(cfg.Mining, n.storageManager, l1api, &pvr, n.feed, n.log)
log.Info("Initialized miner")
return nil
Expand Down
Loading