Skip to content

Commit 2834bf6

Browse files
authored
fix: issue in finality sig generation cmd in crypto tools (#16)
1 parent ea4a65f commit 2834bf6

File tree

1 file changed

+42
-18
lines changed
  • deployments/rollup-bsn-demo/crypto-ops-tool/cmd/crypto-ops

1 file changed

+42
-18
lines changed

deployments/rollup-bsn-demo/crypto-ops-tool/cmd/crypto-ops/main.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type SerializableRandListInfo struct {
7373
SRListHex []string `json:"sr_list_hex"` // hex encoded private randomness
7474
PRListHex []string `json:"pr_list_hex"` // hex encoded public randomness
7575
CommitmentHex string `json:"commitment_hex"` // hex encoded commitment
76+
StartHeight uint64 `json:"start_height"` // original start height of the randomness
77+
NumPubRand uint64 `json:"num_pub_rand"` // number of pub randomness values
7678
ProofListData []struct {
7779
Total uint64 `json:"total"`
7880
Index uint64 `json:"index"`
@@ -82,11 +84,13 @@ type SerializableRandListInfo struct {
8284
}
8385

8486
// ConvertToSerializable converts datagen.RandListInfo to SerializableRandListInfo
85-
func ConvertToSerializable(randListInfo *datagen.RandListInfo) (*SerializableRandListInfo, error) {
87+
func ConvertToSerializable(randListInfo *datagen.RandListInfo, startHeight, numPubRand uint64) (*SerializableRandListInfo, error) {
8688
serializable := &SerializableRandListInfo{
8789
SRListHex: make([]string, len(randListInfo.SRList)),
8890
PRListHex: make([]string, len(randListInfo.PRList)),
8991
CommitmentHex: hex.EncodeToString(randListInfo.Commitment),
92+
StartHeight: startHeight,
93+
NumPubRand: numPubRand,
9094
ProofListData: make([]struct {
9195
Total uint64 `json:"total"`
9296
Index uint64 `json:"index"`
@@ -352,7 +356,7 @@ func verifyPublicRandomnessCommitment(contractAddr, consumerBtcPk string, expect
352356
return nil
353357
}
354358

355-
func submitFinalitySignature(r *mathrand.Rand, contractAddr string, randListInfo *datagen.RandListInfo, consumerFpSk *btcec.PrivateKey, blockHeight uint64) error {
359+
func submitFinalitySignature(r *mathrand.Rand, contractAddr string, randListInfo *datagen.RandListInfo, consumerFpSk *btcec.PrivateKey, blockHeight, startHeight uint64) error {
356360
fmt.Fprintln(os.Stderr, " → Generating mock block to vote on...")
357361

358362
// Follow exact test pattern: btcPK -> bip340PK -> MarshalHex()
@@ -371,13 +375,23 @@ func submitFinalitySignature(r *mathrand.Rand, contractAddr string, randListInfo
371375
// Create message to sign (exactly like the tests)
372376
msgToSign := append(sdk.Uint64ToBigEndian(blockHeight), blockToVote.AppHash...)
373377

374-
// Calculate randomness index (assuming randomness starts from height 1)
375-
if blockHeight < 1 {
376-
return fmt.Errorf("block height must be >= 1, got %d", blockHeight)
378+
// Calculate randomness index relative to the start height
379+
if blockHeight < startHeight {
380+
return fmt.Errorf("block height %d is before randomness start height %d", blockHeight, startHeight)
377381
}
378-
randIndex := int(blockHeight - 1)
382+
383+
// Calculate the valid range for this randomness batch
384+
endHeight := startHeight + uint64(len(randListInfo.SRList)) - 1
385+
if blockHeight > endHeight {
386+
return fmt.Errorf("block height %d is outside the committed randomness range [%d-%d] (start_height=%d, num_pub_rand=%d)",
387+
blockHeight, startHeight, endHeight, startHeight, len(randListInfo.SRList))
388+
}
389+
390+
randIndex := int(blockHeight - startHeight)
379391
if randIndex >= len(randListInfo.SRList) {
380-
return fmt.Errorf("block height %d requires randomness index %d, but only %d randomness values available", blockHeight, randIndex, len(randListInfo.SRList))
392+
// This should never happen with the above checks, but keep as safety net
393+
return fmt.Errorf("internal error: block height %d requires randomness index %d, but only %d randomness values available (start_height=%d)",
394+
blockHeight, randIndex, len(randListInfo.SRList), startHeight)
381395
}
382396

383397
// Generate EOTS signature using the calculated randomness index
@@ -529,7 +543,7 @@ func generatePublicRandomnessCommitment(r *mathrand.Rand, consumerFpSk *btcec.Pr
529543
}
530544

531545
// Generate finality signature (crypto only, no chain submission)
532-
func generateFinalitySignature(r *mathrand.Rand, randListInfo *datagen.RandListInfo, consumerFpSk *btcec.PrivateKey, blockHeight uint64, blockHash []byte) (*bbn.BIP340PubKey, []byte, []byte, *merkle.Proof, error) {
546+
func generateFinalitySignature(r *mathrand.Rand, randListInfo *datagen.RandListInfo, consumerFpSk *btcec.PrivateKey, blockHeight, startHeight uint64, blockHash []byte) (*bbn.BIP340PubKey, []byte, []byte, *merkle.Proof, error) {
533547
fmt.Fprintln(os.Stderr, " → Generating finality signature...")
534548

535549
// Follow exact test pattern: btcPK -> bip340PK -> MarshalHex()
@@ -541,13 +555,23 @@ func generateFinalitySignature(r *mathrand.Rand, randListInfo *datagen.RandListI
541555
// Create message to sign (exactly like the tests)
542556
msgToSign := append(sdk.Uint64ToBigEndian(blockHeight), blockHash...)
543557

544-
// Calculate randomness index (assuming randomness starts from height 1)
545-
if blockHeight < 1 {
546-
return nil, nil, nil, nil, fmt.Errorf("block height must be >= 1, got %d", blockHeight)
558+
// Calculate randomness index relative to the start height
559+
if blockHeight < startHeight {
560+
return nil, nil, nil, nil, fmt.Errorf("block height %d is before randomness start height %d", blockHeight, startHeight)
547561
}
548-
randIndex := int(blockHeight - 1)
562+
563+
// Calculate the valid range for this randomness batch
564+
endHeight := startHeight + uint64(len(randListInfo.SRList)) - 1
565+
if blockHeight > endHeight {
566+
return nil, nil, nil, nil, fmt.Errorf("block height %d is outside the committed randomness range [%d-%d] (start_height=%d, num_pub_rand=%d)",
567+
blockHeight, startHeight, endHeight, startHeight, len(randListInfo.SRList))
568+
}
569+
570+
randIndex := int(blockHeight - startHeight)
549571
if randIndex >= len(randListInfo.SRList) {
550-
return nil, nil, nil, nil, fmt.Errorf("block height %d requires randomness index %d, but only %d randomness values available", blockHeight, randIndex, len(randListInfo.SRList))
572+
// This should never happen with the above checks, but keep as safety net
573+
return nil, nil, nil, nil, fmt.Errorf("internal error: block height %d requires randomness index %d, but only %d randomness values available (start_height=%d)",
574+
blockHeight, randIndex, len(randListInfo.SRList), startHeight)
551575
}
552576

553577
// Generate EOTS signature using the calculated randomness index
@@ -712,7 +736,7 @@ func main() {
712736
}
713737

714738
// Convert to serializable format
715-
serializable, err := ConvertToSerializable(randListInfo)
739+
serializable, err := ConvertToSerializable(randListInfo, startHeight, numPubRand)
716740
if err != nil {
717741
log.Fatalf("Failed to convert randListInfo to serializable: %v", err)
718742
}
@@ -780,7 +804,7 @@ func main() {
780804
}
781805

782806
// Generate finality signature (crypto only)
783-
bip340PK, publicRandomness, signature, proof, err := generateFinalitySignature(r, randListInfo, fpSk, blockHeight, blockHash)
807+
bip340PK, publicRandomness, signature, proof, err := generateFinalitySignature(r, randListInfo, fpSk, blockHeight, serializable.StartHeight, blockHash)
784808
if err != nil {
785809
log.Fatalf("Failed to generate finality signature: %v", err)
786810
}
@@ -845,7 +869,7 @@ func main() {
845869
log.Fatalf("Failed to generate public randomness commitment: %v", err)
846870
}
847871

848-
serializable, err := ConvertToSerializable(randListInfo)
872+
serializable, err := ConvertToSerializable(randListInfo, startHeight, numPubRand)
849873
if err != nil {
850874
log.Fatalf("Failed to convert randListInfo to serializable: %v", err)
851875
}
@@ -900,7 +924,7 @@ func main() {
900924
log.Fatalf("Failed to convert serializable to randListInfo: %v", err)
901925
}
902926

903-
err = submitFinalitySignature(r, contractAddr, randListInfo, fpSk, blockHeight)
927+
err = submitFinalitySignature(r, contractAddr, randListInfo, fpSk, blockHeight, serializable.StartHeight)
904928
if err != nil {
905929
log.Fatalf("Failed to submit finality signature: %v", err)
906930
}
@@ -943,7 +967,7 @@ func main() {
943967
log.Fatalf("Failed to generate public randomness commitment: %v", err)
944968
}
945969

946-
err = submitFinalitySignature(r, contractAddr, randListInfo, fpSk, startHeight)
970+
err = submitFinalitySignature(r, contractAddr, randListInfo, fpSk, startHeight, startHeight)
947971
if err != nil {
948972
log.Fatalf("Failed to submit finality signature: %v", err)
949973
}

0 commit comments

Comments
 (0)