diff --git a/go.mod b/go.mod index 418ef037..7baecc31 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 4e18ef3f..0edce5ea 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/proto/cscalist/proposal.proto b/proto/cscalist/proposal.proto index 2b371f57..03cd9a2a 100644 --- a/proto/cscalist/proposal.proto +++ b/proto/cscalist/proposal.proto @@ -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; } @@ -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; } diff --git a/x/cscalist/README.md b/x/cscalist/README.md index 4ebecaa5..0185506a 100644 --- a/x/cscalist/README.md +++ b/x/cscalist/README.md @@ -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. diff --git a/x/cscalist/client/cli/ldif.go b/x/cscalist/client/cli/ldif.go index f32dda05..9dd27de6 100644 --- a/x/cscalist/client/cli/ldif.go +++ b/x/cscalist/client/cli/ldif.go @@ -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" ) @@ -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) @@ -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) } diff --git a/x/cscalist/keeper/treap.go b/x/cscalist/keeper/treap.go index 0faddaf1..e1a5e280 100644 --- a/x/cscalist/keeper/treap.go +++ b/x/cscalist/keeper/treap.go @@ -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" ) @@ -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) ) diff --git a/x/cscalist/keeper/util.go b/x/cscalist/keeper/util.go index 758e3355..f6eebee8 100644 --- a/x/cscalist/keeper/util.go +++ b/x/cscalist/keeper/util.go @@ -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" @@ -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)) } diff --git a/x/cscalist/types/proposal.pb.go b/x/cscalist/types/proposal.pb.go index b65490d5..cc35a65f 100644 --- a/x/cscalist/types/proposal.pb.go +++ b/x/cscalist/types/proposal.pb.go @@ -28,9 +28,9 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type EditCSCAListProposal struct { Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - // Poseidon hashes of new CSCA public keys + // Keccak256 hashes of new CSCA public keys ToAdd []string `protobuf:"bytes,3,rep,name=toAdd,proto3" json:"toAdd,omitempty"` - // Poseidon hashes of existing CSCA public keys + // Keccak256 hashes of existing CSCA public keys ToRemove []string `protobuf:"bytes,4,rep,name=toRemove,proto3" json:"toRemove,omitempty"` } @@ -101,7 +101,7 @@ func (m *EditCSCAListProposal) GetToRemove() []string { type ReplaceCSCAListProposal struct { Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - // Poseidon hashes of all CSCA public keys + // Keccak256 hashes of all CSCA public keys Leaves []string `protobuf:"bytes,3,rep,name=leaves,proto3" json:"leaves,omitempty"` }