mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
[R4R] Update secure RNG to generate bytes directly (#509)
* generate length 32 random bytes * fix test
This commit is contained in:
parent
1099dfbd7d
commit
fa8ae9647a
@ -82,10 +82,10 @@ func QueryCalcRandomNumberHashCmd(queryRoute string, cdc *codec.Codec) *cobra.Co
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
randomNumberHash := types.CalculateRandomHash(randomNumber[:], timestamp)
|
randomNumberHash := types.CalculateRandomHash(randomNumber, timestamp)
|
||||||
|
|
||||||
// Prepare random number, timestamp, and hash for output
|
// Prepare random number, timestamp, and hash for output
|
||||||
randomNumberStr := fmt.Sprintf("Random number: %s\n", string(randomNumber[:]))
|
randomNumberStr := fmt.Sprintf("Random number: %s\n", hex.EncodeToString(randomNumber))
|
||||||
timestampStr := fmt.Sprintf("Timestamp: %d\n", timestamp)
|
timestampStr := fmt.Sprintf("Timestamp: %d\n", timestamp)
|
||||||
randomNumberHashStr := fmt.Sprintf("Random number hash: %s", hex.EncodeToString(randomNumberHash))
|
randomNumberHashStr := fmt.Sprintf("Random number hash: %s", hex.EncodeToString(randomNumberHash))
|
||||||
output := []string{randomNumberStr, timestampStr, randomNumberHashStr}
|
output := []string{randomNumberStr, timestampStr, randomNumberHashStr}
|
||||||
|
@ -81,10 +81,10 @@ func GetCmdCreateAtomicSwap(cdc *codec.Codec) *cobra.Command {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
randomNumberHash := types.CalculateRandomHash(randomNumber[:], timestamp)
|
randomNumberHash := types.CalculateRandomHash(randomNumber, timestamp)
|
||||||
|
|
||||||
// Print random number, timestamp, and hash to user's console
|
// Print random number, timestamp, and hash to user's console
|
||||||
fmt.Printf("\nRandom number: %s\n", string(randomNumber[:]))
|
fmt.Printf("\nRandom number: %s\n", hex.EncodeToString(randomNumber))
|
||||||
fmt.Printf("Timestamp: %d\n", timestamp)
|
fmt.Printf("Timestamp: %d\n", timestamp)
|
||||||
fmt.Printf("Random number hash: %s\n\n", hex.EncodeToString(randomNumberHash))
|
fmt.Printf("Random number hash: %s\n\n", hex.EncodeToString(randomNumberHash))
|
||||||
|
|
||||||
@ -135,7 +135,10 @@ func GetCmdClaimAtomicSwap(cdc *codec.Codec) *cobra.Command {
|
|||||||
if len(strings.TrimSpace(args[1])) == 0 {
|
if len(strings.TrimSpace(args[1])) == 0 {
|
||||||
return fmt.Errorf("random-number cannot be empty")
|
return fmt.Errorf("random-number cannot be empty")
|
||||||
}
|
}
|
||||||
randomNumber := []byte(args[1])
|
randomNumber, err := hex.DecodeString(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
msg := types.NewMsgClaimAtomicSwap(from, swapID, randomNumber)
|
msg := types.NewMsgClaimAtomicSwap(from, swapID, randomNumber)
|
||||||
|
|
||||||
|
@ -175,7 +175,7 @@ func (k Keeper) ClaimAtomicSwap(ctx sdk.Context, from sdk.AccAddress, swapID []b
|
|||||||
sdk.NewAttribute(types.AttributeKeyRecipient, atomicSwap.Recipient.String()),
|
sdk.NewAttribute(types.AttributeKeyRecipient, atomicSwap.Recipient.String()),
|
||||||
sdk.NewAttribute(types.AttributeKeyAtomicSwapID, hex.EncodeToString(atomicSwap.GetSwapID())),
|
sdk.NewAttribute(types.AttributeKeyAtomicSwapID, hex.EncodeToString(atomicSwap.GetSwapID())),
|
||||||
sdk.NewAttribute(types.AttributeKeyRandomNumberHash, hex.EncodeToString(atomicSwap.RandomNumberHash)),
|
sdk.NewAttribute(types.AttributeKeyRandomNumberHash, hex.EncodeToString(atomicSwap.RandomNumberHash)),
|
||||||
sdk.NewAttribute(types.AttributeKeyRandomNumber, string(randomNumber)),
|
sdk.NewAttribute(types.AttributeKeyRandomNumber, hex.EncodeToString(randomNumber)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -3,9 +3,6 @@ package types
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
@ -14,21 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GenerateSecureRandomNumber generates cryptographically strong pseudo-random number
|
// GenerateSecureRandomNumber generates cryptographically strong pseudo-random number
|
||||||
func GenerateSecureRandomNumber() ([64]byte, error) {
|
func GenerateSecureRandomNumber() ([]byte, error) {
|
||||||
// Max is a 256-bits integer i.e. 2^256
|
bytes := make([]byte, 32)
|
||||||
max := new(big.Int)
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
max.Exp(big.NewInt(2), big.NewInt(256), nil)
|
return []byte{}, err
|
||||||
|
|
||||||
// Generate number in the range [0, max]
|
|
||||||
randomNumber, err := rand.Int(rand.Reader, max)
|
|
||||||
if err != nil {
|
|
||||||
return [64]byte{}, errors.New("random number generation error")
|
|
||||||
}
|
}
|
||||||
|
return bytes, nil
|
||||||
// Ensure length of 64 for hexadecimal encoding by padding with 0s
|
|
||||||
var paddedNumber [64]byte
|
|
||||||
copy(paddedNumber[:], fmt.Sprintf("%064x", randomNumber))
|
|
||||||
return paddedNumber, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CalculateRandomHash calculates the hash of a number and timestamp
|
// CalculateRandomHash calculates the hash of a number and timestamp
|
||||||
|
@ -36,7 +36,7 @@ func (suite *HashTestSuite) TestGenerateSecureRandomNumber() {
|
|||||||
secureRandomNumber, err := types.GenerateSecureRandomNumber()
|
secureRandomNumber, err := types.GenerateSecureRandomNumber()
|
||||||
suite.Nil(err)
|
suite.Nil(err)
|
||||||
suite.NotNil(secureRandomNumber)
|
suite.NotNil(secureRandomNumber)
|
||||||
suite.Equal(64, len(secureRandomNumber))
|
suite.Equal(32, len(secureRandomNumber))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *HashTestSuite) TestCalculateRandomHash() {
|
func (suite *HashTestSuite) TestCalculateRandomHash() {
|
||||||
|
Loading…
Reference in New Issue
Block a user