From d583e12161752926a12c85bb0dfc9887fb26f315 Mon Sep 17 00:00:00 2001 From: Larry <26318510+larry0x@users.noreply.github.com> Date: Tue, 21 Nov 2023 20:04:15 +0000 Subject: [PATCH] feat: Add support for Blake2b/2s/3 hash functions (#212) * update proto files * update rust files * update go files (blake3 unimplemented) * fix a typo in readme * update js files * add tests * remove test case for keccak due to a confusion (#211) * add missing implementations for a few `LengthOp`s * implement a few missing `LengthOp`s in Go * fix Go linter warnings * fix Rust dev dependencies * add tests for new length ops implementations * chore: bump golang.org/x/crypto from 0.13.0 to 0.14.0 in /go (#206) Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.13.0 to 0.14.0. - [Commits](https://github.com/golang/crypto/compare/v0.13.0...v0.14.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Carlos Rodriguez * chore: bump bufbuild/buf-setup-action from 1.26.1 to 1.27.1 (#215) Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.26.1 to 1.27.1. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.26.1...v1.27.1) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Carlos Rodriguez * chore: bump eslint from 8.50.0 to 8.52.0 in /js (#214) Bumps [eslint](https://github.com/eslint/eslint) from 8.50.0 to 8.52.0. - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md) - [Commits](https://github.com/eslint/eslint/compare/v8.50.0...v8.52.0) --- updated-dependencies: - dependency-name: eslint dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Carlos Rodriguez * go mod tidy * remove print statement * review comment: rename blake2b -> blake2b_512 and blake2s -> blake2s_256 * go mod tidy --------- Signed-off-by: dependabot[bot] Co-authored-by: Carlos Rodriguez Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- README.md | 2 +- go/Makefile | 2 +- go/go.mod | 1 + go/go.sum | 2 + go/ops.go | 38 ++++++- go/proofs.pb.go | 169 +++++++++++++++-------------- js/src/generated/codecimpl.d.ts | 3 + js/src/generated/codecimpl.js | 6 + js/src/ops.spec.ts | 32 +++++- js/src/ops.ts | 9 ++ proto/cosmos/ics23/v1/proofs.proto | 17 +-- rust/Cargo.toml | 6 +- rust/src/cosmos.ics23.v1.rs | 9 ++ rust/src/cosmos.ics23.v1.serde.rs | 9 ++ rust/src/host_functions.rs | 28 +++++ rust/src/ops.rs | 57 ++++++++++ rust/src/proto_descriptor.bin | Bin 12321 -> 12491 bytes testdata/TestDoHashData.json | 27 ++++- testdata/TestLeafOpData.json | 30 +++++ 19 files changed, 348 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index 82f39ad1..56b4aea3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ | ------------------ | ------------------------------------------------- | ---------------------------------------------- | | [Go](./go) | [![Go Test][go-test-badge]][go-test-link] | [![Go Cov][go-cov-badge]][go-cov-link] | | [Rust](./rust) | [![Rust Test][rust-test-badge]][rust-test-link] | [![Rust Cov][rust-cov-badge]][rust-cov-link] | -| [TypeScript](./ts) | [![TypeScript Test][ts-test-badge]][ts-test-link] | [![TypeScript Cov][ts-cov-badge]][ts-cov-link] | +| [TypeScript](./js) | [![TypeScript Test][ts-test-badge]][ts-test-link] | [![TypeScript Cov][ts-cov-badge]][ts-cov-link] | [cosmos-link]: https://cosmos.network [go-test-link]: https://github.com/cosmos/ics23/actions/workflows/go.yml diff --git a/go/Makefile b/go/Makefile index 041a2d5b..07016588 100644 --- a/go/Makefile +++ b/go/Makefile @@ -1,7 +1,7 @@ test: go test . -.PHONY: test +.PHONY: test ##### Linting ##### golangci_lint_cmd=golangci-lint diff --git a/go/go.mod b/go/go.mod index 84e737e8..fd04f337 100644 --- a/go/go.mod +++ b/go/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/google/go-cmp v0.5.9 // indirect golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect + golang.org/x/sys v0.14.0 // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go/go.sum b/go/go.sum index 4eb7952a..42bdba59 100644 --- a/go/go.sum +++ b/go/go.sum @@ -8,6 +8,8 @@ golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA= golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= diff --git a/go/ops.go b/go/ops.go index eb24eb96..06601de7 100644 --- a/go/ops.go +++ b/go/ops.go @@ -12,6 +12,10 @@ import ( _ "crypto/sha256" // adds sha512 capability to crypto.SHA512 _ "crypto/sha512" + // adds blake2b capability to crypto.BLAKE2b_512 + _ "golang.org/x/crypto/blake2b" + // adds blake2s capability to crypto.BLAKE2s_256 + _ "golang.org/x/crypto/blake2s" // adds ripemd160 capability to crypto.RIPEMD160 _ "golang.org/x/crypto/ripemd160" //nolint:staticcheck ) @@ -181,6 +185,22 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) { return nil, err } return shaHash.Sum(nil), nil + case HashOp_BLAKE2B_512: + blakeHash := crypto.BLAKE2b_512.New() + _, err := blakeHash.Write(preimage) + if err != nil { + return nil, err + } + return blakeHash.Sum(nil), nil + case HashOp_BLAKE2S_256: + blakeHash := crypto.BLAKE2s_256.New() + _, err := blakeHash.Write(preimage) + if err != nil { + return nil, err + } + return blakeHash.Sum(nil), nil + // TODO: there doesn't seem to be an "official" implementation of BLAKE3 in Go, + // so we are unable to support it for now } return nil, fmt.Errorf("unsupported hashop: %d", hashOp) } @@ -239,16 +259,28 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) { return nil, fmt.Errorf("data was %d bytes, not 64", len(data)) } return data, nil + case LengthOp_FIXED32_BIG: + res := make([]byte, 4, 4+len(data)) + binary.BigEndian.PutUint32(res[:4], uint32(len(data))) + res = append(res, data...) + return res, nil case LengthOp_FIXED32_LITTLE: res := make([]byte, 4, 4+len(data)) binary.LittleEndian.PutUint32(res[:4], uint32(len(data))) res = append(res, data...) return res, nil + case LengthOp_FIXED64_BIG: + res := make([]byte, 8, 8+len(data)) + binary.BigEndian.PutUint64(res[:8], uint64(len(data))) + res = append(res, data...) + return res, nil + case LengthOp_FIXED64_LITTLE: + res := make([]byte, 8, 8+len(data)) + binary.LittleEndian.PutUint64(res[:8], uint64(len(data))) + res = append(res, data...) + return res, nil // TODO // case LengthOp_VAR_RLP: - // case LengthOp_FIXED32_BIG: - // case LengthOp_FIXED64_BIG: - // case LengthOp_FIXED64_LITTLE: } return nil, fmt.Errorf("unsupported lengthop: %d", lengthOp) } diff --git a/go/proofs.pb.go b/go/proofs.pb.go index 4003abcc..dc401d1d 100644 --- a/go/proofs.pb.go +++ b/go/proofs.pb.go @@ -26,13 +26,16 @@ type HashOp int32 const ( // NO_HASH is the default if no data passed. Note this is an illegal argument some places. - HashOp_NO_HASH HashOp = 0 - HashOp_SHA256 HashOp = 1 - HashOp_SHA512 HashOp = 2 - HashOp_KECCAK HashOp = 3 - HashOp_RIPEMD160 HashOp = 4 - HashOp_BITCOIN HashOp = 5 - HashOp_SHA512_256 HashOp = 6 + HashOp_NO_HASH HashOp = 0 + HashOp_SHA256 HashOp = 1 + HashOp_SHA512 HashOp = 2 + HashOp_KECCAK HashOp = 3 + HashOp_RIPEMD160 HashOp = 4 + HashOp_BITCOIN HashOp = 5 + HashOp_SHA512_256 HashOp = 6 + HashOp_BLAKE2B_512 HashOp = 7 + HashOp_BLAKE2S_256 HashOp = 8 + HashOp_BLAKE3 HashOp = 9 ) var HashOp_name = map[int32]string{ @@ -43,16 +46,22 @@ var HashOp_name = map[int32]string{ 4: "RIPEMD160", 5: "BITCOIN", 6: "SHA512_256", + 7: "BLAKE2B_512", + 8: "BLAKE2S_256", + 9: "BLAKE3", } var HashOp_value = map[string]int32{ - "NO_HASH": 0, - "SHA256": 1, - "SHA512": 2, - "KECCAK": 3, - "RIPEMD160": 4, - "BITCOIN": 5, - "SHA512_256": 6, + "NO_HASH": 0, + "SHA256": 1, + "SHA512": 2, + "KECCAK": 3, + "RIPEMD160": 4, + "BITCOIN": 5, + "SHA512_256": 6, + "BLAKE2B_512": 7, + "BLAKE2S_256": 8, + "BLAKE3": 9, } func (x HashOp) String() string { @@ -1163,71 +1172,73 @@ func init() { func init() { proto.RegisterFile("cosmos/ics23/v1/proofs.proto", fileDescriptor_5e599a3f914c9389) } var fileDescriptor_5e599a3f914c9389 = []byte{ - // 1024 bytes of a gzipped File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bd, 0x52, 0x7f, - 0x2a, 0xc7, 0x30, 0x86, 0x95, 0xa1, 0xe3, 0xb0, 0xde, 0x6e, 0x1f, 0x6a, 0x72, 0x7c, 0x04, 0xda, - 0xdb, 0xe1, 0x20, 0x69, 0x04, 0xda, 0xdb, 0x19, 0x82, 0x12, 0x78, 0x0d, 0x64, 0xa2, 0x7d, 0xfb, - 0x5d, 0x9d, 0x68, 0x3a, 0x4b, 0xf6, 0x7d, 0x5b, 0x6b, 0xc9, 0xc9, 0x71, 0x2f, 0x63, 0x73, 0x6f, - 0xaa, 0xf2, 0xf9, 0xef, 0x97, 0x59, 0xf4, 0xe6, 0x32, 0x8b, 0xfe, 0xbc, 0xcc, 0xa2, 0x57, 0x57, - 0xd9, 0xc8, 0x9b, 0xab, 0x6c, 0xe4, 0x8f, 0xab, 0x6c, 0xe4, 0x87, 0xdc, 0xa9, 0x19, 0xf4, 0xce, - 0x8e, 0xf3, 0x1d, 0xc7, 0xda, 0x9e, 0xf8, 0xab, 0x3c, 0x75, 0xbe, 0xe0, 0x8b, 0xe3, 0x04, 0xff, - 0xab, 0x2c, 0xfd, 0x1b, 0x00, 0x00, 0xff, 0xff, 0xf3, 0x9d, 0xc3, 0xd4, 0x75, 0x0a, 0x00, 0x00, + // 1048 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x56, 0xcb, 0x6e, 0x23, 0x45, + 0x14, 0x75, 0xd9, 0x6e, 0x3f, 0xae, 0x27, 0x49, 0x4f, 0x29, 0x82, 0x66, 0xc2, 0x38, 0x51, 0x4b, + 0x48, 0x21, 0x8c, 0x1c, 0x62, 0x27, 0xe1, 0x39, 0x1a, 0x6c, 0xa7, 0x67, 0x6c, 0x62, 0x62, 0x53, + 0x36, 0xa3, 0x81, 0x4d, 0xab, 0xe3, 0x94, 0xe3, 0xd6, 0xb8, 0x1f, 0xea, 0xee, 0x44, 0xce, 0x6c, +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xd6, 0x94, 0x93, 0xe3, 0x28, 0x53, 0x73, 0x34, 0x55, 0xfa, 0xfc, 0xf7, 0xab, 0x2c, 0x7a, 0x73, + 0x95, 0x45, 0x7f, 0x5e, 0x65, 0xd1, 0xeb, 0xeb, 0x6c, 0xe4, 0xcd, 0x75, 0x36, 0xf2, 0xc7, 0x75, + 0x36, 0xf2, 0xc3, 0xc6, 0xa9, 0x19, 0x74, 0xcf, 0x8e, 0x73, 0x6d, 0xc7, 0xda, 0x9e, 0xf8, 0x09, + 0x3d, 0x75, 0xbe, 0xe0, 0x83, 0xe3, 0x04, 0xff, 0x09, 0x2d, 0xfc, 0x1b, 0x00, 0x00, 0xff, 0xff, + 0x3f, 0x0e, 0x3c, 0xb5, 0xa4, 0x0a, 0x00, 0x00, } func (m *ExistenceProof) Marshal() (dAtA []byte, err error) { diff --git a/js/src/generated/codecimpl.d.ts b/js/src/generated/codecimpl.d.ts index 49af554a..de86820e 100644 --- a/js/src/generated/codecimpl.d.ts +++ b/js/src/generated/codecimpl.d.ts @@ -10,6 +10,9 @@ export namespace ics23 { RIPEMD160 = 4, BITCOIN = 5, SHA512_256 = 6, + BLAKE2B_512 = 7, + BLAKE2S_256 = 8, + BLAKE3 = 9, } /** diff --git a/js/src/generated/codecimpl.js b/js/src/generated/codecimpl.js index dbf1a844..41753c86 100644 --- a/js/src/generated/codecimpl.js +++ b/js/src/generated/codecimpl.js @@ -29,6 +29,9 @@ $root.ics23 = (function() { * @property {number} RIPEMD160=4 RIPEMD160 value * @property {number} BITCOIN=5 BITCOIN value * @property {number} SHA512_256=6 SHA512_256 value + * @property {number} BLAKE2B_512=7 BLAKE2B value + * @property {number} BLAKE2S_256=8 BLAKE2S value + * @property {number} BLAKE3=9 BLAKE3 value */ ics23.HashOp = (function() { var valuesById = {}, values = Object.create(valuesById); @@ -39,6 +42,9 @@ $root.ics23 = (function() { values[valuesById[4] = "RIPEMD160"] = 4; values[valuesById[5] = "BITCOIN"] = 5; values[valuesById[6] = "SHA512_256"] = 6; + values[valuesById[7] = "BLAKE2B_512"] = 7; + values[valuesById[8] = "BLAKE2S_256"] = 8; + values[valuesById[9] = "BLAKE3"] = 9; return values; })(); diff --git a/js/src/ops.spec.ts b/js/src/ops.spec.ts index fc57ee69..25452d5a 100644 --- a/js/src/ops.spec.ts +++ b/js/src/ops.spec.ts @@ -29,11 +29,41 @@ describe("doHash", () => { expect(hash).toEqual(fromHex("b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46")); }); - it("'bitcoin' hashes food", () => { + it("bitcoin hashes food", () => { // echo -n c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b | xxd -r -p | openssl dgst -rmd160 -hex const hash = doHash(ics23.HashOp.BITCOIN, toAscii("food")); expect(hash).toEqual(fromHex("0bcb587dfb4fc10b36d57f2bba1878f139b75d24")); }); + + it("blake2b hashes food", () => { + // use this online tool: https://toolkitbay.com/tkb/tool/BLAKE2b_512 + const hash = doHash(ics23.HashOp.BLAKE2B_512, toAscii("food")); + expect(hash).toEqual( + fromHex( + "b1f115361afc179415d93d4f58dc2fc7d8fa434192d7cb9b65fca592f6aa904103d1f12b28655c2355478e10908ab002c418dc52a4367d8e645309cd25e3a504", + ), + ); + }); + + it("blake2s hashes food", () => { + // use this online tool: https://toolkitbay.com/tkb/tool/BLAKE2s_256 + const hash = doHash(ics23.HashOp.BLAKE2S_256, toAscii("food")); + expect(hash).toEqual( + fromHex( + "5a1ec796f11f3dfc7e8ca5de13828edf2e910eb7dd41caaac356a4acbefb1758", + ), + ); + }); + + it("blake3 hashes food", () => { + // use this online tool: https://connor4312.github.io/blake3/index.html + const hash = doHash(ics23.HashOp.BLAKE3, toAscii("food")); + expect(hash).toEqual( + fromHex( + "f775a8ccf8cb78cd1c63ade4e9802de4ead836b36cea35242accf31d2c6a3697", + ), + ); + }); }); describe("applyLeaf", () => { diff --git a/js/src/ops.ts b/js/src/ops.ts index 982fa54e..84cacf48 100644 --- a/js/src/ops.ts +++ b/js/src/ops.ts @@ -1,3 +1,6 @@ +import { blake2b } from "@noble/hashes/blake2b"; +import { blake2s } from "@noble/hashes/blake2s"; +import { blake3 } from "@noble/hashes/blake3"; import { ripemd160 } from "@noble/hashes/ripemd160"; import { sha256 } from "@noble/hashes/sha256"; import { sha512, sha512_256 } from "@noble/hashes/sha512"; @@ -91,6 +94,12 @@ export function doHash(hashOp: ics23.HashOp, preimage: Uint8Array): Uint8Array { return ripemd160(sha256(preimage)); case ics23.HashOp.SHA512_256: return sha512_256(preimage); + case ics23.HashOp.BLAKE2B_512: + return blake2b(preimage); + case ics23.HashOp.BLAKE2S_256: + return blake2s(preimage); + case ics23.HashOp.BLAKE3: + return blake3(preimage); } throw new Error(`Unsupported hashop: ${hashOp}`); } diff --git a/proto/cosmos/ics23/v1/proofs.proto b/proto/cosmos/ics23/v1/proofs.proto index beac166b..b72ff238 100644 --- a/proto/cosmos/ics23/v1/proofs.proto +++ b/proto/cosmos/ics23/v1/proofs.proto @@ -6,13 +6,16 @@ option go_package = "github.com/cosmos/ics23/go;ics23"; enum HashOp { // NO_HASH is the default if no data passed. Note this is an illegal argument some places. - NO_HASH = 0; - SHA256 = 1; - SHA512 = 2; - KECCAK = 3; - RIPEMD160 = 4; - BITCOIN = 5; // ripemd160(sha256(x)) - SHA512_256 = 6; + NO_HASH = 0; + SHA256 = 1; + SHA512 = 2; + KECCAK = 3; + RIPEMD160 = 4; + BITCOIN = 5; // ripemd160(sha256(x)) + SHA512_256 = 6; + BLAKE2B_512 = 7; + BLAKE2S_256 = 8; + BLAKE3 = 9; } /** diff --git a/rust/Cargo.toml b/rust/Cargo.toml index a70d9be7..821ba924 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -15,6 +15,8 @@ members = ["codegen", "no-std-check"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] anyhow = {version = "1.0.40", default-features = false} +blake2 = {version = "0.10.6", optional = true, default-features = false} +blake3 = {version = "1.5.0", optional = true, default-features = false} bytes = {version = "1.0.1", default-features = false} hex = {version = "0.4.3", default-features = false, features = ["alloc"]} prost = {version = "0.12", default-features = false, features = ["prost-derive"]} @@ -25,6 +27,8 @@ serde = {version = "1.0", optional = true, default-features = false} informalsystems-pbjson = { version = "0.6.0", optional = true, default-features = false } [dev-dependencies] +blake2 = {version = "0.10.6"} +blake3 = {version = "1.5.0"} ripemd = {version = "0.1.1"} serde = {version = "1.0", features = ["derive"]} serde_json = {version = "1.0.64"} @@ -36,6 +40,6 @@ default = ["std", "host-functions"] std = ["prost/std", "bytes/std", "hex/std", "anyhow/std", "informalsystems-pbjson/std", "serde/std"] -host-functions = ["sha2", "sha3", "ripemd"] +host-functions = ["sha2", "sha3", "ripemd", "blake2", "blake3"] serde = ["dep:serde", "informalsystems-pbjson"] diff --git a/rust/src/cosmos.ics23.v1.rs b/rust/src/cosmos.ics23.v1.rs index d3c2226a..7268cb32 100644 --- a/rust/src/cosmos.ics23.v1.rs +++ b/rust/src/cosmos.ics23.v1.rs @@ -275,6 +275,9 @@ pub enum HashOp { /// ripemd160(sha256(x)) Bitcoin = 5, Sha512256 = 6, + Blake2b512 = 7, + Blake2s256 = 8, + Blake3 = 9, } impl HashOp { /// String value of the enum field names used in the ProtoBuf definition. @@ -290,6 +293,9 @@ impl HashOp { HashOp::Ripemd160 => "RIPEMD160", HashOp::Bitcoin => "BITCOIN", HashOp::Sha512256 => "SHA512_256", + HashOp::Blake2b512 => "BLAKE2B_512", + HashOp::Blake2s256 => "BLAKE2S_256", + HashOp::Blake3 => "BLAKE3", } } /// Creates an enum from field names used in the ProtoBuf definition. @@ -302,6 +308,9 @@ impl HashOp { "RIPEMD160" => Some(Self::Ripemd160), "BITCOIN" => Some(Self::Bitcoin), "SHA512_256" => Some(Self::Sha512256), + "BLAKE2B_512" => Some(Self::Blake2b512), + "BLAKE2S_256" => Some(Self::Blake2s256), + "BLAKE3" => Some(Self::Blake3), _ => None, } } diff --git a/rust/src/cosmos.ics23.v1.serde.rs b/rust/src/cosmos.ics23.v1.serde.rs index 28b22860..0348cc7b 100644 --- a/rust/src/cosmos.ics23.v1.serde.rs +++ b/rust/src/cosmos.ics23.v1.serde.rs @@ -987,6 +987,9 @@ impl serde::Serialize for HashOp { Self::Ripemd160 => "RIPEMD160", Self::Bitcoin => "BITCOIN", Self::Sha512256 => "SHA512_256", + Self::Blake2b512 => "BLAKE2B_512", + Self::Blake2s256 => "BLAKE2S_256", + Self::Blake3 => "BLAKE3", }; serializer.serialize_str(variant) } @@ -1005,6 +1008,9 @@ impl<'de> serde::Deserialize<'de> for HashOp { "RIPEMD160", "BITCOIN", "SHA512_256", + "BLAKE2B_512", + "BLAKE2S_256", + "BLAKE3", ]; struct GeneratedVisitor; @@ -1054,6 +1060,9 @@ impl<'de> serde::Deserialize<'de> for HashOp { "RIPEMD160" => Ok(HashOp::Ripemd160), "BITCOIN" => Ok(HashOp::Bitcoin), "SHA512_256" => Ok(HashOp::Sha512256), + "BLAKE2B_512" => Ok(HashOp::Blake2b512), + "BLAKE2S_256" => Ok(HashOp::Blake2s256), + "BLAKE3" => Ok(HashOp::Blake3), _ => Err(serde::de::Error::unknown_variant(value, FIELDS)), } } diff --git a/rust/src/host_functions.rs b/rust/src/host_functions.rs index 720fdabc..b9eb5b24 100644 --- a/rust/src/host_functions.rs +++ b/rust/src/host_functions.rs @@ -16,11 +16,21 @@ pub trait HostFunctionsProvider { /// Ripemd160 hash function. fn ripemd160(message: &[u8]) -> [u8; 20]; + + /// BLAKE2b-512 hash function. + fn blake2b_512(message: &[u8]) -> [u8; 64]; + + /// BLAKE2s-256 hash function. + fn blake2s_256(message: &[u8]) -> [u8; 32]; + + /// BLAKE3 hash function. + fn blake3(message: &[u8]) -> [u8; 32]; } #[cfg(any(feature = "host-functions", test))] pub mod host_functions_impl { use crate::host_functions::HostFunctionsProvider; + use blake2::{Blake2b512, Blake2s256}; use ripemd::Ripemd160; use sha2::{Digest, Sha256, Sha512, Sha512_256}; use sha3::Sha3_512; @@ -61,5 +71,23 @@ pub mod host_functions_impl { buf.copy_from_slice(&digest); buf } + + fn blake2b_512(message: &[u8]) -> [u8; 64] { + let digest = Blake2b512::digest(message); + let mut buf = [0u8; 64]; + buf.copy_from_slice(&digest); + buf + } + + fn blake2s_256(message: &[u8]) -> [u8; 32] { + let digest = Blake2s256::digest(message); + let mut buf = [0u8; 32]; + buf.copy_from_slice(&digest); + buf + } + + fn blake3(message: &[u8]) -> [u8; 32] { + blake3::hash(message).into() + } } } diff --git a/rust/src/ops.rs b/rust/src/ops.rs index 69ecab2f..d7da9a15 100644 --- a/rust/src/ops.rs +++ b/rust/src/ops.rs @@ -46,6 +46,9 @@ pub(crate) fn do_hash(hash: HashOp, data: &[u8]) -> Ha HashOp::Ripemd160 => Hash::from(H::ripemd160(data)), HashOp::Bitcoin => Hash::from(H::ripemd160(&H::sha2_256(data)[..])), HashOp::Sha512256 => Hash::from(H::sha2_512_truncated(data)), + HashOp::Blake2b512 => Hash::from(H::blake2b_512(data)), + HashOp::Blake2s256 => Hash::from(H::blake2s_256(data)), + HashOp::Blake3 => Hash::from(H::blake3(data)), } } @@ -59,11 +62,26 @@ pub(crate) fn do_length(length: LengthOp, data: &[u8]) -> Result { len.extend(data); return Ok(len); } + LengthOp::Fixed32Big => { + let mut len = (data.len() as u32).to_be_bytes().to_vec(); + len.extend(data); + return Ok(len); + } LengthOp::Fixed32Little => { let mut len = (data.len() as u32).to_le_bytes().to_vec(); len.extend(data); return Ok(len); } + LengthOp::Fixed64Big => { + let mut len = (data.len() as u64).to_be_bytes().to_vec(); + len.extend(data); + return Ok(len); + } + LengthOp::Fixed64Little => { + let mut len = (data.len() as u64).to_le_bytes().to_vec(); + len.extend(data); + return Ok(len); + } _ => bail!("Unsupported LengthOp {:?}", length), } // if we don't error above or return custom string, just return item untouched (common case) @@ -121,6 +139,24 @@ mod tests { hash == decode("5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88"), "sha512/256 hash fails" ); + + let hash = do_hash::(HashOp::Blake2b512, b"food"); + assert!( + hash == decode("b1f115361afc179415d93d4f58dc2fc7d8fa434192d7cb9b65fca592f6aa904103d1f12b28655c2355478e10908ab002c418dc52a4367d8e645309cd25e3a504"), + "blake2b hash fails" + ); + + let hash = do_hash::(HashOp::Blake2s256, b"food"); + assert!( + hash == decode("5a1ec796f11f3dfc7e8ca5de13828edf2e910eb7dd41caaac356a4acbefb1758"), + "blake2s hash fails" + ); + + let hash = do_hash::(HashOp::Blake3, b"food"); + assert!( + hash == decode("f775a8ccf8cb78cd1c63ade4e9802de4ead836b36cea35242accf31d2c6a3697"), + "blake3 hash fails" + ); } #[test] @@ -135,12 +171,33 @@ mod tests { hex::encode(&prefixed), ); + let prefixed = do_length(LengthOp::Fixed32Big, b"food").unwrap(); + assert!( + prefixed == decode("00000004666f6f64"), + "proto prefix returned {}", + hex::encode(&prefixed), + ); + let prefixed = do_length(LengthOp::Fixed32Little, b"food").unwrap(); assert!( prefixed == decode("04000000666f6f64"), "proto prefix returned {}", hex::encode(&prefixed), ); + + let prefixed = do_length(LengthOp::Fixed64Big, b"food").unwrap(); + assert!( + prefixed == decode("0000000000000004666f6f64"), + "proto prefix returned {}", + hex::encode(&prefixed), + ); + + let prefixed = do_length(LengthOp::Fixed64Little, b"food").unwrap(); + assert!( + prefixed == decode("0400000000000000666f6f64"), + "proto prefix returned {}", + hex::encode(&prefixed), + ); } #[test] diff --git a/rust/src/proto_descriptor.bin b/rust/src/proto_descriptor.bin index 4d627f3e7b41ca64a1e3215a3fa22688bd717a1b..bb31fa4652c1db9c45d6e53a76fac351d10fef2b 100644 GIT binary patch delta 3672 zcmYjTU2GKB6`pfvc6R2@{$9g^*Na*IJ7A!3M3lBEQW9)T8z~qkLBSY88xm0Cg!+eu zs?t7Gs#K|vR-?QI#R=kps8Zz@R5?&Y9|}#l`q{|v)B83*I$X(T4<_~vZ+^TI zC@FoN@mM8{{}_HAEbatP9F3E3l-r1R7oTV}%w%{3TBB5!6FL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