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

feat: implement challenger proving fault with zkVM proof #386

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
32 changes: 16 additions & 16 deletions kroma-bindings/bindings/colosseum.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions kroma-bindings/bindings/colosseum_more.go

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions kroma-bindings/bindings/types.go
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions kroma-chain-ops/genesis/testdata/allocs-l1.json

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions kroma-validator/challenge/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package challenge
Pangssu marked this conversation as resolved.
Show resolved Hide resolved

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)

type Client struct {
rpcURL string
httpClient *http.Client
}

type rpcRequest struct {
JsonRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params any `json:"params"`
Id int `json:"id"`
}

type rpcResponse struct {
JsonRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *jsonRPCError `json:"error"`
Id int `json:"id"`
}

type jsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
}

func NewClient(rpcURL string, networkTimeout time.Duration) *Client {
return &Client{
rpcURL: rpcURL,
httpClient: &http.Client{
Timeout: networkTimeout,
},
}
}

func send[T any](ctx context.Context, client *Client, method string, params []any) (*T, error) {
reqBody := rpcRequest{
JsonRPC: "2.0",
Method: method,
Params: params,
Id: 0,
}
reqBytes, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, client.rpcURL, bytes.NewReader(reqBytes))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

res, err := client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer res.Body.Close()

resBytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}

var response rpcResponse
if err = json.Unmarshal(resBytes, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

if response.Error != nil {
return nil, fmt.Errorf("error occurred from RPC provider: %w", response.Error)
}

var result T
err = json.Unmarshal(response.Result, &result)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response result: %w", err)
}

return &result, nil
}

func (j *jsonRPCError) Error() string { return fmt.Sprintf("[%d] %s", j.Code, j.Message) }
147 changes: 0 additions & 147 deletions kroma-validator/challenge/fetcher.go

This file was deleted.

96 changes: 96 additions & 0 deletions kroma-validator/challenge/proof_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package challenge

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
)

type zkEVMProveResponse struct {
FinalPair []byte `json:"final_pair"`
Proof []byte `json:"proof"`
}

type ProofAndPair struct {
Proof []*big.Int
Pair []*big.Int
}

type ZkEVMProofFetcher interface {
FetchProofAndPair(ctx context.Context, trace string) (*ProofAndPair, error)
}

func (c *Client) FetchProofAndPair(ctx context.Context, trace string) (*ProofAndPair, error) {
proveResult, err := send[zkEVMProveResponse](ctx, c, "prove", []any{trace})
if err != nil {
return nil, fmt.Errorf("failed to request prove: %w", err)
}

proofAndPair := &ProofAndPair{
Proof: Decode(proveResult.Proof),
Pair: Decode(proveResult.FinalPair),
}

return proofAndPair, nil
}

func Decode(data []byte) []*big.Int {
result := make([]*big.Int, len(data)/32)

for i := 0; i < len(data)/32; i++ {
// The best is data is given in Big Endian.
for j := 0; j < 16; j++ {
data[i*32+j], data[i*32+31-j] = data[i*32+31-j], data[i*32+j]
}
result[i] = new(big.Int).SetBytes(data[i*32 : (i+1)*32])
}

return result
}

type HexBytes []byte

type ZkVMProofResponse struct {
RequestStatus RequestStatusType `json:"request_status"`
VKeyHash common.Hash `json:"vkey_hash"`
RequestID string `json:"request_id"`
PublicValues HexBytes `json:"public_values"`
Proof HexBytes `json:"proof"`
}

type ZkVMProofFetcher interface {
Spec(ctx context.Context) (*SpecResponse, error)
RequestProve(ctx context.Context, blockHash string, l1Head string, witness string) (*RequestStatusType, error)
GetProof(ctx context.Context, blockHash string, l1Head string) (*ZkVMProofResponse, error)
}

func (c *Client) RequestProve(ctx context.Context, blockHash string, l1Head string, witness string) (*RequestStatusType, error) {
return send[RequestStatusType](ctx, c, "requestProve", []any{blockHash, l1Head, witness})
}

func (c *Client) GetProof(ctx context.Context, blockHash string, l1Head string) (*ZkVMProofResponse, error) {
return send[ZkVMProofResponse](ctx, c, "getProof", []any{blockHash, l1Head})
}

// UnmarshalJSON handles the conversion from a hex string to a byte array.
func (h *HexBytes) UnmarshalJSON(data []byte) error {
// Remove quotes around the hex string
str := string(data)
if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}

// Decode the hex string to byte array
str = strings.TrimPrefix(str, "0x")
decoded, err := hex.DecodeString(str)
if err != nil {
return err
}

*h = decoded
return nil
}
Loading