Skip to content

Commit

Permalink
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 authored Nov 22, 2024
2 parents 2c1f3a4 + 9d6d106 commit eb6f21b
Show file tree
Hide file tree
Showing 78 changed files with 5,322 additions and 2,247 deletions.
1 change: 0 additions & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ require (
github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 // indirect
github.com/cosmos/crypto v0.1.2 // indirect
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.7.0
Expand Down
2 changes: 0 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 h1:V3WlarcZwlYY
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/crypto v0.1.2 h1:Yn500sPY+9sKVdhiPUSDtt8JOpBGMB515dOmla4zfls=
github.com/cosmos/crypto v0.1.2/go.mod h1:b6VWz3HczIpBaQPvI7KrbQeF3pXHh0al3T5e0uwMBQw=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down
6 changes: 4 additions & 2 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"cosmossdk.io/core/registry"

bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
Expand All @@ -18,13 +20,13 @@ func RegisterCrypto(registrar registry.AminoRegistrar) {
ed25519.PubKeyName)
registrar.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName)
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName)
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12381.PubKeyName)
registrar.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute)
registrar.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
registrar.RegisterConcrete(&ed25519.PrivKey{},
ed25519.PrivKeyName)
registrar.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName)
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName)
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12381.PrivKeyName)
}
6 changes: 4 additions & 2 deletions crypto/codec/pubkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"cosmossdk.io/errors"

cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
Expand Down Expand Up @@ -29,7 +31,7 @@ func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
return &secp256k1.PubKey{
Key: pk.Value,
}, nil
case bls12_381.PubKeyName:
case bls12381.PubKeyName:
return &bls12_381.PubKey{
Key: pk.Value,
}, nil
Expand Down Expand Up @@ -60,7 +62,7 @@ func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) {
}, nil
case *bls12_381.PubKey:
return cryptokeys.JSONPubkey{
KeyType: bls12_381.PubKeyName,
KeyType: bls12381.PubKeyName,
Value: pk.Bytes(),
}, nil
default:
Expand Down
21 changes: 0 additions & 21 deletions crypto/keys/bls12_381/const.go

This file was deleted.

7 changes: 4 additions & 3 deletions crypto/keys/bls12_381/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/cometbft/cometbft/crypto"
bls "github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {

// Type returns the type.
func (PrivKey) Type() string {
return KeyType
return bls.KeyType
}

// Sign signs the given byte array. If msg is larger than
Expand All @@ -70,7 +71,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {

// UnmarshalAmino overrides Amino binary marshaling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PrivKeySize {
if len(bz) != bls.PrivKeySize {
return errors.New("invalid privkey size")
}
privKey.Key = bz
Expand Down Expand Up @@ -119,7 +120,7 @@ func (pubKey PubKey) Bytes() []byte {

// Type returns the key's type.
func (PubKey) Type() string {
return KeyType
return bls.KeyType
}

// Equals returns true if the other's type is the same and their bytes are deeply equal.
Expand Down
49 changes: 21 additions & 28 deletions crypto/keys/bls12_381/key_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/tmhash"

bls12381 "github.com/cosmos/crypto/curves/bls12381"
"github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -32,20 +31,20 @@ var (

// NewPrivateKeyFromBytes build a new key from the given bytes.
func NewPrivateKeyFromBytes(bz []byte) (PrivKey, error) {
secretKey, err := bls12381.SecretKeyFromBytes(bz)
secretKey, err := bls12381.NewPrivateKeyFromBytes(bz)
if err != nil {
return PrivKey{}, err
}
return PrivKey{
Key: secretKey.Marshal(),
Key: secretKey.Bytes(),
}, nil
}

// GenPrivKey generates a new key.
func GenPrivKey() (PrivKey, error) {
secretKey, err := bls12381.RandKey()
secretKey, err := bls12381.GenPrivKey()
return PrivKey{
Key: secretKey.Marshal(),
Key: secretKey.Bytes(),
}, err
}

Expand All @@ -57,13 +56,13 @@ func (privKey PrivKey) Bytes() []byte {
// PubKey returns the private key's public key. If the privkey is not valid
// it returns a nil value.
func (privKey PrivKey) PubKey() cryptotypes.PubKey {
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
if err != nil {
return nil
}

return &PubKey{
Key: secretKey.PublicKey().Marshal(),
Key: secretKey.PubKey().Bytes(),
}
}

Expand All @@ -74,24 +73,23 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {

// Type returns the type.
func (PrivKey) Type() string {
return KeyType
return bls12381.KeyType
}

// Sign signs the given byte array. If msg is larger than
// MaxMsgLen, SHA256 sum will be signed instead of the raw bytes.
func (privKey PrivKey) Sign(msg []byte) ([]byte, error) {
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
if err != nil {
return nil, err
}

if len(msg) > MaxMsgLen {
if len(msg) > bls12381.MaxMsgLen {
hash := sha256.Sum256(msg)
sig := secretKey.Sign(hash[:])
return sig.Marshal(), nil
return secretKey.Sign(hash[:])
}
sig := secretKey.Sign(msg)
return sig.Marshal(), nil

return secretKey.Sign(msg)
}

// MarshalAmino overrides Amino binary marshaling.
Expand All @@ -101,7 +99,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {

// UnmarshalAmino overrides Amino binary marshaling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PrivKeySize {
if len(bz) != bls12381.PrivKeySize {
return errors.New("invalid privkey size")
}
privKey.Key = bz
Expand Down Expand Up @@ -135,35 +133,30 @@ var _ cryptotypes.PubKey = &PubKey{}
//
// The function will panic if the public key is invalid.
func (pubKey PubKey) Address() crypto.Address {
pk, _ := bls12381.PublicKeyFromBytes(pubKey.Key)
if len(pk.Marshal()) != PubKeySize {
pk, _ := bls12381.NewPublicKeyFromBytes(pubKey.Key)
if len(pk.Bytes()) != bls12381.PubKeySize {
panic("pubkey is incorrect size")
}
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
}

// VerifySignature verifies the given signature.
func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
if len(sig) != SignatureLength {
if len(sig) != bls12381.SignatureLength {
return false
}

pubK, err := bls12381.PublicKeyFromBytes(pubKey.Key)
pubK, err := bls12381.NewPublicKeyFromBytes(pubKey.Key)
if err != nil { // invalid pubkey
return false
}

if len(msg) > MaxMsgLen {
if len(msg) > bls12381.MaxMsgLen {
hash := sha256.Sum256(msg)
msg = hash[:]
}

ok, err := bls12381.VerifySignature(sig, [MaxMsgLen]byte(msg[:MaxMsgLen]), pubK)
if err != nil { // bad signature
return false
}

return ok
return pubK.VerifySignature(msg, sig)
}

// Bytes returns the byte format.
Expand All @@ -173,7 +166,7 @@ func (pubKey PubKey) Bytes() []byte {

// Type returns the key's type.
func (PubKey) Type() string {
return KeyType
return bls12381.KeyType
}

// Equals returns true if the other's type is the same and their bytes are deeply equal.
Expand Down
4 changes: 3 additions & 1 deletion crypto/keys/jsonkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keys

import (
bls "github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -26,7 +28,7 @@ func (pk JSONPubkey) Address() types.Address {
Key: pk.Value,
}
return secp256k1.Address()
case bls12_381.PubKeyName:
case bls.PubKeyName:
bls12_381 := bls12_381.PubKey{
Key: pk.Value,
}
Expand Down
4 changes: 3 additions & 1 deletion crypto/keys/multisig/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package multisig

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand All @@ -26,7 +28,7 @@ func init() {
AminoCdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName)
AminoCdc.RegisterConcrete(&bls12_381.PubKey{},
bls12_381.PubKeyName)
bls12381.PubKeyName)
AminoCdc.RegisterConcrete(&LegacyAminoPubKey{},
PubKeyAminoRoute)
}
80 changes: 80 additions & 0 deletions docs/build/building-modules/18-decimal-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
sidebar_position: 1
---
# Decimal Handling in Cosmos SDK

## Introduction

In the Cosmos SDK, there are two types of decimals: `LegacyDec` and `Dec`. `LegacyDec` is the older decimal type that is still available for use, while `Dec` is the newer, more performant decimal type. The implementation of `Dec` is adapted from Regen Network's `regen-ledger`, specifically from [this module](https://github.com/regen-network/regen-ledger/tree/main/types/math). Migrating from `LegacyDec` to `Dec` involves state-breaking changes, specifically:

* **Data Format**: The internal representation of decimals changes, affecting how data is stored and processed.
* **Precision Handling**: `Dec` supports flexible precision up to 34 decimal places, unlike `LegacyDec` which has a fixed precision of 18 decimal places.

These changes require a state migration to update existing decimal values to the new format. It is recommended to use `Dec` for new modules to leverage its enhanced performance and flexibility.

## Why the Change?

* Historically we have wrapped a `big.Int` to represent decimals in the Cosmos SDK and never had a decimal type. Finally, we have a decimal type that is more efficient and accurate.
* `Dec` uses the [apd](https://github.com/cockroachdb/apd) library for arbitrary precision decimals, suitable for accurate financial calculations.
* `Dec` operations are safer for concurrent use as they do not mutate the original values.
* `Dec` operations are faster and more efficient than `LegacyDec`.

## Using `Dec` in Modules that haven't used `LegacyDec`

If you are creating a new module or updating an existing module that has not used `LegacyDec`, you can directly use `Dec`.
Ensure proper error handling.

```
-- math.NewLegacyDecFromInt64(100)
++ math.NewDecFromInt64(100)
-- math.LegacyNewDecWithPrec(100, 18)
++ math.NewDecWithPrec(100, 18)
-- math.LegacyNewDecFromStr("100")
++ math.NewDecFromString("100")
-- math.LegacyNewDecFromStr("100.000000000000000000").Quo(math.LegacyNewDecFromInt(2))
++ foo, err := math.NewDecFromString("100.000000000000000000")
++ foo.Quo(math.NewDecFromInt(2))
-- math.LegacyNewDecFromStr("100.000000000000000000").Add(math.LegacyNewDecFromInt(2))
++ foo, err := math.NewDecFromString("100.000000000000000000")
++ foo.Add(math.NewDecFromInt(2))
-- math.LegacyNewDecFromStr("100.000000000000000000").Sub(math.LegacyNewDecFromInt(2))
++ foo, err := math.NewDecFromString("100.000000000000000000")
++ foo.Sub(math.NewDecFromInt(2))
```

## Modules migrating from `LegacyDec` to `Dec`

When migrating from `LegacyDec` to `Dec`, you need to update your module to use the new decimal type. **These types are state breaking changes and require a migration.**

## Precision Handling

The reason for the state breaking change is the difference in precision handling between the two decimal types:

* **LegacyDec**: Fixed precision of 18 decimal places.
* **Dec**: Flexible precision up to 34 decimal places using the apd library.

## Impact of Precision Change

The increase in precision from 18 to 34 decimal places allows for more detailed decimal values but requires data migration. This change in how data is formatted and stored is a key aspect of why the transition is considered state-breaking.

## Converting `LegacyDec` to `Dec` without storing the data

If you would like to convert a `LegacyDec` to a `Dec` without a state migration changing how the data is handled internally within the application logic and not how it's stored or represented. You can use the following methods.

```go
func LegacyDecToDec(ld LegacyDec) (Dec, error) {
return NewDecFromString(ld.String())
}
```

```go
func DecToLegacyDec(ld Dec) (LegacyDec, error) {
return LegacyDecFromString(ld.String())
}
```

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/crypto v0.1.2
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogogateway v1.2.0
github.com/cosmos/gogoproto v1.7.0
Expand Down
Loading

0 comments on commit eb6f21b

Please sign in to comment.