Skip to content

Commit 0bb5314

Browse files
noel2004colinlyguo
andauthored
fix: byte48 type required in prover (#1627)
Signed-off-by: noelwei <[email protected]> Co-authored-by: colin <[email protected]>
1 parent 09790c4 commit 0bb5314

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

common/types/message/message.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"math/big"
78

89
"github.com/scroll-tech/go-ethereum/common"
910
"github.com/scroll-tech/go-ethereum/common/hexutil"
@@ -52,15 +53,58 @@ type EuclidV2ChunkTaskDetail struct {
5253
PrevMsgQueueHash common.Hash `json:"prev_msg_queue_hash"`
5354
}
5455

56+
// it is a hex encoded big with fixed length on 48 bytes
57+
type Byte48 struct {
58+
hexutil.Big
59+
}
60+
61+
func (e Byte48) MarshalText() ([]byte, error) {
62+
i := e.ToInt()
63+
// overrite encode big
64+
if sign := i.Sign(); sign < 0 {
65+
// sanity check
66+
return nil, fmt.Errorf("Byte48 must be positive integer")
67+
} else {
68+
s := i.Text(16)
69+
if len(s) > 96 {
70+
return nil, fmt.Errorf("Integer Exceed 384bit")
71+
}
72+
return []byte(fmt.Sprintf("0x%0*s", 96, s)), nil
73+
}
74+
}
75+
76+
func isString(input []byte) bool {
77+
return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
78+
}
79+
80+
// hexutil.Big has limition of 256bit so we have to override it ...
81+
func (e *Byte48) UnmarshalJSON(input []byte) error {
82+
if !isString(input) {
83+
return fmt.Errorf("not hex string")
84+
}
85+
86+
b, err := hexutil.Decode(string(input[1 : len(input)-1]))
87+
if err != nil {
88+
return err
89+
}
90+
if len(b) != 48 {
91+
return fmt.Errorf("not a 48 bytes hex string: %d", len(b))
92+
}
93+
var dec big.Int
94+
dec.SetBytes(b)
95+
*e = Byte48{(hexutil.Big)(dec)}
96+
return nil
97+
}
98+
5599
// BatchTaskDetail is a type containing BatchTask detail.
56100
type BatchTaskDetail struct {
57101
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
58102
ChunkProofs []ChunkProof `json:"chunk_proofs"`
59103
BatchHeader interface{} `json:"batch_header"`
60104
BlobBytes []byte `json:"blob_bytes"`
61-
KzgProof hexutil.Big `json:"kzg_proof"`
62-
KzgCommitment hexutil.Big `json:"kzg_commitment"`
63-
ChallengeDigest hexutil.Big `json:"challenge_digest"`
105+
KzgProof Byte48 `json:"kzg_proof"`
106+
KzgCommitment Byte48 `json:"kzg_commitment"`
107+
ChallengeDigest common.Hash `json:"challenge_digest"`
64108
}
65109

66110
// BundleTaskDetail consists of all the information required to describe the task to generate a proof for a bundle of batches.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package message
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestBytes48(t *testing.T) {
9+
ti := &Byte48{}
10+
ti.UnmarshalText([]byte("0x1"))
11+
if s, err := ti.MarshalText(); err == nil {
12+
if len(s) != 98 {
13+
panic(fmt.Sprintf("wrong str: %s", s))
14+
}
15+
}
16+
ti.UnmarshalText([]byte("0x0"))
17+
if s, err := ti.MarshalText(); err == nil {
18+
if len(s) != 98 {
19+
panic(fmt.Sprintf("wrong str: %s", s))
20+
}
21+
}
22+
}

coordinator/internal/logic/provertask/batch_prover_task.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ func (bp *BatchProverTask) getBatchTaskDetail(dbBatch *orm.Batch, chunkInfos []*
291291
// | z | y | kzg_commitment | kzg_proof |
292292
// |---------|---------|----------------|-----------|
293293
// | bytes32 | bytes32 | bytes48 | bytes48 |
294-
taskDetail.KzgProof = hexutil.Big(*new(big.Int).SetBytes(dbBatch.BlobDataProof[112:160]))
295-
taskDetail.KzgCommitment = hexutil.Big(*new(big.Int).SetBytes(dbBatch.BlobDataProof[64:112]))
296-
taskDetail.ChallengeDigest = hexutil.Big(*new(big.Int).SetBytes(dbBatch.BlobDataProof[0:32])) // FIXME: Challenge = ChallengeDigest % BLS_MODULUS, get the original ChallengeDigest.
294+
taskDetail.KzgProof = message.Byte48{Big: hexutil.Big(*new(big.Int).SetBytes(dbBatch.BlobDataProof[112:160]))}
295+
taskDetail.KzgCommitment = message.Byte48{Big: hexutil.Big(*new(big.Int).SetBytes(dbBatch.BlobDataProof[64:112]))}
296+
taskDetail.ChallengeDigest = common.BytesToHash(dbBatch.BlobDataProof[0:32]) // FIXME: Challenge = ChallengeDigest % BLS_MODULUS, get the original ChallengeDigest.
297297
return taskDetail, nil
298298
}

0 commit comments

Comments
 (0)