Skip to content

Commit

Permalink
Merge pull request #20 from rarimo/fix/csca-tree-changes
Browse files Browse the repository at this point in the history
Migrate to keccak256 in CSCA tree, bump LDIF SDK
  • Loading branch information
violog authored May 3, 2024
2 parents 067df19 + 03c4321 commit bad658f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/holiman/uint256 v1.2.2
github.com/iden3/go-iden3-crypto v0.0.16
github.com/improbable-eng/grpc-web v0.15.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.20.0
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66
github.com/rarimo/ldif-sdk v0.4.5-rc.3
github.com/rarimo/ldif-sdk v0.4.6-rc.1
github.com/rs/cors v1.10.1
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -130,7 +131,6 @@ require (
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/iden3/go-iden3-crypto v0.0.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
Expand Down Expand Up @@ -1414,8 +1413,8 @@ github.com/rarimo/cosmos-sdk v0.46.7 h1:jU2PiWzc+19SF02cXM0O0puKPeH1C6Q6t2lzJ9s1
github.com/rarimo/cosmos-sdk v0.46.7/go.mod h1:fqKqz39U5IlEFb4nbQ72951myztsDzFKKDtffYJ63nk=
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66 h1:1KAU4rfWWJwAJ/kencagL3k5BXN3HhP004QNkEUgvDE=
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66/go.mod h1:5Pt9Lk8w7fWhyRO/NMb5x8DRhF2lESRVPT5uOlezInQ=
github.com/rarimo/ldif-sdk v0.4.5-rc.3 h1:KlS3PBmCG4uWM8jb8qhbw1jovtuJ6hyTY0oRftJYvwA=
github.com/rarimo/ldif-sdk v0.4.5-rc.3/go.mod h1:ONLsbueY7tnVHEt/v4jL1f0j0U4/jCfgxFyXI9k8yDo=
github.com/rarimo/ldif-sdk v0.4.6-rc.1 h1:kbjXm0A2EX9GlrDcRpRX3+bw3JQacKOr2wjbf5LnN6w=
github.com/rarimo/ldif-sdk v0.4.6-rc.1/go.mod h1:ONLsbueY7tnVHEt/v4jL1f0j0U4/jCfgxFyXI9k8yDo=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
6 changes: 3 additions & 3 deletions proto/cscalist/proposal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ option go_package = "github.com/rarimo/rarimo-core/x/cscalist/types";
message EditCSCAListProposal {
string title = 1;
string description = 2;
// Poseidon hashes of new CSCA public keys
// Keccak256 hashes of new CSCA public keys
repeated string toAdd = 3;
// Poseidon hashes of existing CSCA public keys
// Keccak256 hashes of existing CSCA public keys
repeated string toRemove = 4;
}

Expand All @@ -21,6 +21,6 @@ message EditCSCAListProposal {
message ReplaceCSCAListProposal {
string title = 1;
string description = 2;
// Poseidon hashes of all CSCA public keys
// Keccak256 hashes of all CSCA public keys
repeated string leaves = 3;
}
5 changes: 3 additions & 2 deletions x/cscalist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ Changing the list is possible via proposal.
## Concepts

The CSCA certificates can only be obtained from the ICAO website in form of an LDIF file.
The file is parsed by our [LDIF SDK](https://github.com/rarimo/ldif-sdk) onto the hash values of raw public keys (Poseidon hash with special splitting scheme for 4096-bit RSA keys).
The file is parsed by our [LDIF SDK](https://github.com/rarimo/ldif-sdk) onto the hash values of raw public keys (Keccak256).
The obtained hashes are used as leaves for the Merkle tree.

Dynamic treap-based Merkle tree is used to store the CSCA public key hashes, see [treap](https://en.wikipedia.org/wiki/Treap).
The reason is good consistency of treap with Cosmos KVStore, while updates of the list may be quite frequent.
For each node the priority is deterministically derived from the key with formula: `priority = poseidon_hash(key) mod MAX_UINT64`, where `key` is Poseidon hash of public key and `MAX_UINT64 = 2^64-1`.
For each node the priority is deterministically derived from the key with formula:
`priority = keccak256(key) mod MAX_UINT64`, where `key` is a hash of public key and `MAX_UINT64 = 2^64-1`.

In order to update the tree the proposal can be created. There are two types:
- `ReplaceCSCAListProposal` - replaces the whole tree with a new one, very gas-consuming, suits for initial setup.
Expand Down
9 changes: 3 additions & 6 deletions x/cscalist/client/cli/ldif.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/iden3/go-iden3-crypto/keccak256"
"github.com/rarimo/ldif-sdk/ldif"
"github.com/rarimo/ldif-sdk/mt"
"github.com/rarimo/ldif-sdk/utils"
"github.com/rarimo/rarimo-core/x/cscalist/types"
"github.com/spf13/cobra"
)
Expand All @@ -24,7 +24,7 @@ func cmdParseLDIF() *cobra.Command {
rawFormat = "raw"
hashFormat = "hash"
rootFormat = "root"
defaultFormat = rawFormat
defaultFormat = hashFormat
)
outputFormatValues := fmt.Sprintf("%s, %s, %s", rawFormat, hashFormat, rootFormat)

Expand Down Expand Up @@ -81,10 +81,7 @@ automatically, see ldif-tree-diff)`,

case hashFormat:
for _, pk := range pubKeys {
hash, err := utils.PoseidonHashBig(pk)
if err != nil {
return fmt.Errorf("hash public key: %w", err)
}
hash := keccak256.Hash(pk)
if _, err = fmt.Fprintln(dst, hexutil.Encode(hash)); err != nil {
return fmt.Errorf("write to destination: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions x/cscalist/keeper/treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/rarimo/ldif-sdk/mt"
"github.com/iden3/go-iden3-crypto/keccak256"
"github.com/rarimo/ldif-sdk/utils"
"github.com/rarimo/rarimo-core/x/cscalist/types"
)
Expand Down Expand Up @@ -157,11 +157,11 @@ func (t Treap) merkleHashNodes(ctx sdk.Context, left, right string) string {
return hash(r.Hash, l.Hash)
}

// priority = poseidon(key) % (2^64-1)
// priority = keccak256(key) % (2^64-1)
func derivePriority(key string) uint64 {
var (
hex = hexutil.MustDecode(key)
keyHash = new(big.Int).SetBytes(mt.MustPoseidon(hex))
keyHash = new(big.Int).SetBytes(keccak256.Hash(hex))
u64 = new(big.Int).SetUint64(math.MaxUint64)
)

Expand Down
6 changes: 3 additions & 3 deletions x/cscalist/keeper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/rarimo/ldif-sdk/mt"
"github.com/iden3/go-iden3-crypto/keccak256"
)

const emptyHex = "0x"
Expand All @@ -28,8 +28,8 @@ func hash(a, b string) string {
bb := hexutil.MustDecode(b)

if bytes.Compare(aa, bb) < 0 {
return hexutil.Encode(mt.MustPoseidon(aa, bb))
return hexutil.Encode(keccak256.Hash(aa, bb))
}

return hexutil.Encode(mt.MustPoseidon(bb, aa))
return hexutil.Encode(keccak256.Hash(bb, aa))
}
6 changes: 3 additions & 3 deletions x/cscalist/types/proposal.pb.go

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

0 comments on commit bad658f

Please sign in to comment.