From 61321db422fb141175236266e93fd2aad29ce2c9 Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Mon, 19 Apr 2021 13:47:29 +0200 Subject: [PATCH 1/2] Add SHA-512/256 hash operation --- go/ops.go | 4 + go/ops_test.go | 6 + go/proofs.pb.go | 212 +++++++++++++------------------- js/package.json | 3 +- js/src/generated/codecimpl.d.ts | 3 +- js/src/generated/codecimpl.js | 27 ++++ js/src/ops.spec.ts | 10 ++ js/src/ops.ts | 3 + js/yarn.lock | 5 + proofs.proto | 1 + rust/src/ics23.rs | 1 + rust/src/ops.rs | 11 +- 12 files changed, 159 insertions(+), 127 deletions(-) diff --git a/go/ops.go b/go/ops.go index ad961868..29316f24 100644 --- a/go/ops.go +++ b/go/ops.go @@ -131,6 +131,10 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) { hash := crypto.RIPEMD160.New() hash.Write(tmp) return hash.Sum(nil), nil + case HashOp_SHA512_256: + hash := crypto.SHA512_256.New() + hash.Write(preimage) + return hash.Sum(nil), nil } return nil, errors.Errorf("Unsupported hashop: %d", hashOp) } diff --git a/go/ops_test.go b/go/ops_test.go index 6f3c71ff..a71b5ced 100644 --- a/go/ops_test.go +++ b/go/ops_test.go @@ -199,6 +199,12 @@ func TestDoHash(t *testing.T) { // echo -n c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b | xxd -r -p | openssl dgst -rmd160 -hex expectedHash: "0bcb587dfb4fc10b36d57f2bba1878f139b75d24", }, + "sha512_256": { + hashOp: HashOp_SHA512_256, + preimage: "food", + // echo -n food | openssl dgst -sha512-256 -hex | cut -d' ' -f2 + expectedHash: "5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88", + }, } for name, tc := range cases { diff --git a/go/proofs.pb.go b/go/proofs.pb.go index 01a7ffbc..a973dcf8 100644 --- a/go/proofs.pb.go +++ b/go/proofs.pb.go @@ -26,12 +26,13 @@ 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_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 ) var HashOp_name = map[int32]string{ @@ -41,15 +42,17 @@ var HashOp_name = map[int32]string{ 3: "KECCAK", 4: "RIPEMD160", 5: "BITCOIN", + 6: "SHA512_256", } var HashOp_value = map[string]int32{ - "NO_HASH": 0, - "SHA256": 1, - "SHA512": 2, - "KECCAK": 3, - "RIPEMD160": 4, - "BITCOIN": 5, + "NO_HASH": 0, + "SHA256": 1, + "SHA512": 2, + "KECCAK": 3, + "RIPEMD160": 4, + "BITCOIN": 5, + "SHA512_256": 6, } func (x HashOp) String() string { @@ -1155,66 +1158,66 @@ func init() { func init() { proto.RegisterFile("proofs.proto", fileDescriptor_855156e15e7b8e99) } var fileDescriptor_855156e15e7b8e99 = []byte{ - // 936 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x96, 0xcf, 0x6f, 0xe3, 0x44, - 0x14, 0xc7, 0xe3, 0x24, 0xce, 0x8f, 0x97, 0xfe, 0x70, 0x47, 0x05, 0x59, 0xaa, 0x48, 0x8b, 0x2f, - 0x74, 0xbb, 0xa2, 0xb0, 0xc9, 0x36, 0x88, 0x03, 0x12, 0x4d, 0xea, 0x25, 0x56, 0x4b, 0x13, 0x26, - 0xd9, 0xd5, 0x22, 0x21, 0x59, 0xde, 0x74, 0xb2, 0xb1, 0x36, 0xb1, 0x2d, 0xdb, 0x45, 0xc9, 0x0a, - 0x84, 0xc4, 0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, 0xd4, 0x83, 0xdf, 0x14, 0x28, - 0x4c, 0x7f, 0x64, 0x11, 0x3c, 0x6f, 0xd9, 0x6d, 0x6a, 0x3e, 0xb3, 0x5e, 0x6a, 0x29, 0x5c, 0xbe, - 0x38, 0xa6, 0x76, 0x9b, 0xb6, 0xba, 0x2d, 0x4d, 0x41, 0x3f, 0x5c, 0xd2, 0xb3, 0xb6, 0x96, 0x26, - 0x9b, 0x50, 0x7a, 0x66, 0xbd, 0x34, 0x4f, 0xaa, 0x15, 0xbb, 0x6e, 0x7d, 0xa1, 0x65, 0x08, 0x81, - 0x8d, 0xa9, 0x70, 0x66, 0x75, 0xbb, 0x67, 0xa6, 0x96, 0x4d, 0xa0, 0xda, 0x53, 0x0e, 0xa9, 0x09, - 0x54, 0x7b, 0x3a, 0x85, 0x72, 0x64, 0x1b, 0x34, 0x6a, 0x7e, 0xf5, 0xdc, 0xa2, 0xa6, 0x8d, 0x9b, - 0x7d, 0xdd, 0x35, 0x3b, 0x5a, 0x7e, 0x56, 0x45, 0x6f, 0xae, 0x16, 0xea, 0xfa, 0xef, 0xd7, 0x65, - 0xe5, 0xea, 0xba, 0xac, 0xfc, 0x79, 0x5d, 0x56, 0x7e, 0xba, 0x29, 0xa7, 0xae, 0x6e, 0xca, 0xa9, - 0x3f, 0x6e, 0xca, 0xa9, 0x57, 0x39, 0xfe, 0x77, 0xa6, 0xfa, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, - 0x20, 0xff, 0xe9, 0x19, 0xde, 0x08, 0x00, 0x00, + // 940 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0x4b, 0x6f, 0xe3, 0x54, + 0x14, 0xce, 0x4d, 0x62, 0x27, 0x39, 0x69, 0x53, 0xf7, 0xaa, 0x20, 0x4b, 0x15, 0x69, 0xf1, 0x86, + 0x4e, 0x47, 0x14, 0x26, 0x99, 0x06, 0xb1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 0x63, 0x50, 0x42, + 0xf7, 0xd5, 0x20, 0x96, 0x99, 0xad, 0xe1, 0x0a, 0x8e, 0xf1, 0x0f, 0x82, 0xad, 0x86, 0x3f, 0x1a, + 0xb9, 0xf1, 0x88, 0x7a, 0xb1, 0x88, 0xfe, 0x21, 0x28, 0x94, 0x91, 0x79, 0xfc, 0x75, 0x0e, 0x9a, + 0x29, 0x22, 0x58, 0xb8, 0x06, 0x79, 0xcf, 0xf7, 0x84, 0x85, 0x48, 0x4f, 0x97, 0x16, 0x4b, 0x17, + 0x6b, 0xa6, 0x48, 0xc2, 0xc5, 0x8f, 0x40, 0x79, 0xe9, 0xc4, 0xbd, 0x81, 0xcc, 0x73, 0x5b, 0x1a, + 0xd5, 0x19, 0x96, 0x84, 0xe0, 0x0c, 0xfc, 0x19, 0x40, 0xcf, 0x1f, 0x05, 0x21, 0x8d, 0x22, 0x7a, + 0xa9, 0x67, 0x39, 0x7f, 0x57, 0xf2, 0x1b, 0x89, 0x62, 0xce, 0x72, 0xc6, 0xa0, 0x9e, 0x03, 0x85, + 0xf7, 0xde, 0xb8, 0x46, 0xa0, 0x8a, 0x0e, 0xb1, 0xf6, 0x0d, 0x9c, 0x68, 0xc0, 0xef, 0x58, 0x4a, + 0xda, 0xd7, 0x74, 0xa2, 0x01, 0x6b, 0x0d, 0x53, 0xe1, 0x23, 0x28, 0x06, 0x21, 0x65, 0x47, 0x9b, + 0x75, 0x23, 0xbd, 0x8a, 0x09, 0x92, 0x71, 0x46, 0x27, 0xb8, 0x02, 0x9b, 0x53, 0xbe, 0x98, 0x97, + 0xcc, 0x2a, 0x8b, 0x0d, 0xc9, 0x79, 0xce, 0xa7, 0xe8, 0x03, 0x50, 0x87, 0xd4, 0x7b, 0xc5, 0x87, + 0x84, 0x91, 0xb7, 0x92, 0x39, 0x62, 0x60, 0x2b, 0x20, 0x52, 0x8d, 0xdf, 0x05, 0x35, 0x08, 0x69, + 0xdf, 0x1d, 0xeb, 0x0a, 0x9f, 0x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+ 0x33, 0x2d, 0x83, 0x37, 0xa1, 0x40, 0xac, 0xb6, 0xf9, 0xe5, 0xe9, 0x93, 0xda, 0xc7, 0x5a, 0x96, + 0xd9, 0xd7, 0xad, 0x6e, 0xa3, 0x65, 0x5d, 0x68, 0x0a, 0x2e, 0x01, 0x08, 0x1b, 0x9b, 0xf9, 0x50, + 0x0f, 0x7f, 0x47, 0x90, 0x9f, 0x7e, 0x74, 0x99, 0xe1, 0x45, 0xcb, 0x6e, 0x13, 0xf3, 0x99, 0xf5, + 0x42, 0x4b, 0x31, 0xf1, 0xf9, 0x09, 0xb1, 0xdb, 0xa4, 0xd5, 0x6d, 0x69, 0x88, 0xf9, 0x61, 0x22, + 0x39, 0x6f, 0x6b, 0x69, 0xbc, 0x05, 0xc5, 0x67, 0xd6, 0x0b, 0xf3, 0xb4, 0x5a, 0xb1, 0xeb, 0xd6, + 0x17, 0x5a, 0x06, 0x63, 0x28, 0x4d, 0x81, 0x73, 0xab, 0xdb, 0x3d, 0x37, 0xb5, 0x6c, 0x42, 0xaa, + 0x3d, 0xe5, 0x24, 0x25, 0x21, 0xd5, 0x9e, 0x4e, 0x49, 0x2a, 0xde, 0x01, 0x8d, 0x98, 0x5f, 0x7d, + 0x6d, 0x11, 0xd3, 0x66, 0xce, 0xbe, 0xe9, 0x9a, 0x1d, 0x2d, 0x37, 0x8b, 0x32, 0x6b, 0x8e, 0xe6, + 0xeb, 0xfa, 0x1f, 0x37, 0x65, 0x74, 0x7d, 0x53, 0x46, 0x7f, 0xdd, 0x94, 0xd1, 0xcf, 0xb7, 0xe5, + 0xd4, 0xf5, 0x6d, 0x39, 0xf5, 0xe7, 0x6d, 0x39, 0xf5, 0x52, 0xe5, 0xbf, 0x37, 0xd5, 0x7f, 0x03, + 0x00, 0x00, 0xff, 0xff, 0xd4, 0x7d, 0x87, 0x8f, 0xee, 0x08, 0x00, 0x00, } func (m *ExistenceProof) Marshal() (dAtA []byte, err error) { @@ -2581,10 +2584,7 @@ func (m *ExistenceProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -2740,10 +2740,7 @@ func (m *NonExistenceProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -2933,10 +2930,7 @@ func (m *CommitmentProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3096,10 +3090,7 @@ func (m *LeafOp) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3236,10 +3227,7 @@ func (m *InnerOp) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3399,10 +3387,7 @@ func (m *ProofSpec) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3638,10 +3623,7 @@ func (m *InnerSpec) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3725,10 +3707,7 @@ func (m *BatchProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3848,10 +3827,7 @@ func (m *BatchEntry) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -3969,10 +3945,7 @@ func (m *CompressedBatchProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -4092,10 +4065,7 @@ func (m *CompressedBatchEntry) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -4325,10 +4295,7 @@ func (m *CompressedExistenceProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { @@ -4484,10 +4451,7 @@ func (m *CompressedNonExistenceProof) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthProofs - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthProofs } if (iNdEx + skippy) > l { diff --git a/js/package.json b/js/package.json index 6ec9e039..3bad1a52 100644 --- a/js/package.json +++ b/js/package.json @@ -47,6 +47,7 @@ "dependencies": { "protobufjs": "^6.8.8", "ripemd160": "^2.0.2", - "sha.js": "^2.4.11" + "sha.js": "^2.4.11", + "js-sha512": "^0.8.0" } } diff --git a/js/src/generated/codecimpl.d.ts b/js/src/generated/codecimpl.d.ts index 6da8b8a7..c05e5a12 100644 --- a/js/src/generated/codecimpl.d.ts +++ b/js/src/generated/codecimpl.d.ts @@ -8,7 +8,8 @@ export namespace ics23 { SHA512 = 2, KECCAK = 3, RIPEMD160 = 4, - BITCOIN = 5 + BITCOIN = 5, + SHA512_256 = 6 } /** diff --git a/js/src/generated/codecimpl.js b/js/src/generated/codecimpl.js index 0ec256b9..5d87204a 100644 --- a/js/src/generated/codecimpl.js +++ b/js/src/generated/codecimpl.js @@ -28,6 +28,7 @@ $root.ics23 = (function() { * @property {number} KECCAK=3 KECCAK value * @property {number} RIPEMD160=4 RIPEMD160 value * @property {number} BITCOIN=5 BITCOIN value + * @property {number} SHA512_256=6 SHA512_256 value */ ics23.HashOp = (function() { var valuesById = {}, values = Object.create(valuesById); @@ -37,6 +38,7 @@ $root.ics23 = (function() { values[valuesById[3] = "KECCAK"] = 3; values[valuesById[4] = "RIPEMD160"] = 4; values[valuesById[5] = "BITCOIN"] = 5; + values[valuesById[6] = "SHA512_256"] = 6; return values; })(); @@ -1161,6 +1163,7 @@ $root.ics23 = (function() { case 3: case 4: case 5: + case 6: break; } if (message.prehashKey != null && message.hasOwnProperty("prehashKey")) @@ -1173,6 +1176,7 @@ $root.ics23 = (function() { case 3: case 4: case 5: + case 6: break; } if (message.prehashValue != null && message.hasOwnProperty("prehashValue")) @@ -1185,6 +1189,7 @@ $root.ics23 = (function() { case 3: case 4: case 5: + case 6: break; } if (message.length != null && message.hasOwnProperty("length")) @@ -1245,6 +1250,10 @@ $root.ics23 = (function() { case 5: message.hash = 5; break; + case "SHA512_256": + case 6: + message.hash = 6; + break; } switch (object.prehashKey) { case "NO_HASH": @@ -1271,6 +1280,10 @@ $root.ics23 = (function() { case 5: message.prehashKey = 5; break; + case "SHA512_256": + case 6: + message.prehashKey = 6; + break; } switch (object.prehashValue) { case "NO_HASH": @@ -1297,6 +1310,10 @@ $root.ics23 = (function() { case 5: message.prehashValue = 5; break; + case "SHA512_256": + case 6: + message.prehashValue = 6; + break; } switch (object.length) { case "NO_PREFIX": @@ -1579,6 +1596,7 @@ $root.ics23 = (function() { case 3: case 4: case 5: + case 6: break; } if (message.prefix != null && message.hasOwnProperty("prefix")) @@ -1627,6 +1645,10 @@ $root.ics23 = (function() { case 5: message.hash = 5; break; + case "SHA512_256": + case 6: + message.hash = 6; + break; } if (object.prefix != null) if (typeof object.prefix === "string") @@ -2208,6 +2230,7 @@ $root.ics23 = (function() { case 3: case 4: case 5: + case 6: break; } return null; @@ -2268,6 +2291,10 @@ $root.ics23 = (function() { case 5: message.hash = 5; break; + case "SHA512_256": + case 6: + message.hash = 6; + break; } return message; }; diff --git a/js/src/ops.spec.ts b/js/src/ops.spec.ts index ff77dd19..504f09c6 100644 --- a/js/src/ops.spec.ts +++ b/js/src/ops.spec.ts @@ -50,6 +50,16 @@ describe("applyLeaf", () => { expect(applyLeaf(op, key, value)).toEqual(expected); }); + it("hashes food with sha-512/256", () => { + const op: ics23.ILeafOp = { hash: ics23.HashOp.SHA512_256 }; + const key = toAscii("fo"); + const value = toAscii("od"); + const expected = fromHex( + "5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88" + ); + expect(applyLeaf(op, key, value)).toEqual(expected); + }); + it("hashes foobar (different breakpoint)", () => { const op: ics23.ILeafOp = { hash: ics23.HashOp.SHA256 }; const key = toAscii("f"); diff --git a/js/src/ops.ts b/js/src/ops.ts index 988dff08..31c835a8 100644 --- a/js/src/ops.ts +++ b/js/src/ops.ts @@ -1,3 +1,4 @@ +import { sha512_256 } from "js-sha512"; import ripemd160 from "ripemd160"; import shajs from "sha.js"; @@ -109,6 +110,8 @@ export function doHash(hashOp: ics23.HashOp, preimage: Uint8Array): Uint8Array { return rp160(preimage); case ics23.HashOp.BITCOIN: return rp160(s256(preimage)); + case ics23.HashOp.SHA512_256: + return new Uint8Array(sha512_256.arrayBuffer(preimage)); } throw new Error(`Unsupported hashop: ${hashOp}`); } diff --git a/js/yarn.lock b/js/yarn.lock index 795dcfd1..d1d5e562 100644 --- a/js/yarn.lock +++ b/js/yarn.lock @@ -305,6 +305,11 @@ jasmine@^3.5.0: glob "^7.1.4" jasmine-core "~3.5.0" +js-sha512@^0.8.0: + version "0.8.0" + resolved "https://registry.yarnpkg.com/js-sha512/-/js-sha512-0.8.0.tgz#dd22db8d02756faccf19f218e3ed61ec8249f7d4" + integrity sha512-PWsmefG6Jkodqt+ePTvBZCSMFgN7Clckjd0O7su3I0+BW2QWUTJNzjktHsztGLhncP2h8mcF9V9Y2Ha59pAViQ== + js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" diff --git a/proofs.proto b/proofs.proto index 9dd39e61..12d9e2da 100644 --- a/proofs.proto +++ b/proofs.proto @@ -10,6 +10,7 @@ enum HashOp { KECCAK = 3; RIPEMD160 = 4; BITCOIN = 5; // ripemd160(sha256(x)) + SHA512_256 = 6; } /** diff --git a/rust/src/ics23.rs b/rust/src/ics23.rs index c6886a02..047e8b2a 100644 --- a/rust/src/ics23.rs +++ b/rust/src/ics23.rs @@ -252,6 +252,7 @@ pub enum HashOp { Ripemd160 = 4, /// ripemd160(sha256(x)) Bitcoin = 5, + Sha512_256 = 6, } ///* ///LengthOp defines how to process the key and value of the LeafOp diff --git a/rust/src/ops.rs b/rust/src/ops.rs index 867d54d9..26f074b7 100644 --- a/rust/src/ops.rs +++ b/rust/src/ops.rs @@ -1,6 +1,6 @@ use anyhow::{bail, ensure}; use ripemd160::Ripemd160; -use sha2::{Digest, Sha256, Sha512}; +use sha2::{Digest, Sha256, Sha512, Sha512Trunc256}; use sha3::Sha3_512; use std::convert::TryInto; @@ -41,6 +41,7 @@ fn do_hash(hash: HashOp, data: &[u8]) -> Result { HashOp::Bitcoin => Ok(Hash::from( Ripemd160::digest(Sha256::digest(data).as_slice()).as_slice(), )), + HashOp::Sha512_256 => Ok(Hash::from(Sha512Trunc256::digest(data).as_slice())), } } @@ -99,6 +100,14 @@ mod tests { "bitcoin hash fails" ); + let hash = do_hash(HashOp::Sha512_256, b"food")?; + ensure!( + hash == hex::decode( + "5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88" + )?, + "sha512/256 hash fails" + ); + Ok(()) } From 346d8d9b19661cc25318b0afc5df8dc894f831fb Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Mon, 19 Apr 2021 13:48:12 +0200 Subject: [PATCH 2/2] Implement FIXED32_LITTLE length operation --- go/ops.go | 7 ++++++- go/ops_test.go | 13 +++++++++++++ js/src/ops.spec.ts | 15 +++++++++++++++ js/src/ops.ts | 15 ++++++++++++++- rust/src/ops.rs | 12 ++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/go/ops.go b/go/ops.go index 29316f24..11309a6e 100644 --- a/go/ops.go +++ b/go/ops.go @@ -3,6 +3,7 @@ package ics23 import ( "bytes" "crypto" + "encoding/binary" // adds sha256 capability to crypto.SHA256 _ "crypto/sha256" @@ -158,11 +159,15 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) { return nil, errors.Errorf("Data was %d bytes, not 64", len(data)) } return data, 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 // TODO // case LengthOp_VAR_RLP: // case LengthOp_FIXED32_BIG: // case LengthOp_FIXED64_BIG: - // case LengthOp_FIXED32_LITTLE: // case LengthOp_FIXED64_LITTLE: } return nil, errors.Errorf("Unsupported lengthop: %d", lengthOp) diff --git a/go/ops_test.go b/go/ops_test.go index a71b5ced..e147634b 100644 --- a/go/ops_test.go +++ b/go/ops_test.go @@ -86,6 +86,19 @@ func TestLeafOp(t *testing.T) { // echo -n 04666f6f6420a48c2d4f67b9f80374938535285ed285819d8a5a8fc1fccd1e3244e437cf290d | xxd -r -p | sha256sum expected: fromHex("87e0483e8fb624aef2e2f7b13f4166cda485baa8e39f437c83d74c94bedb148f"), }, + "hash with length prefix (fixed 32-bit little-endian encoding)": { + op: &LeafOp{ + Hash: HashOp_SHA256, + Length: LengthOp_FIXED32_LITTLE, + // no prehash + }, + // echo -n food | xxs -ps + // and manually compute length bytes + key: []byte("food"), // 04000000666f6f64 + value: []byte("some longer text"), // 10000000736f6d65206c6f6e6765722074657874 + // echo -n 04000000666f6f6410000000736f6d65206c6f6e6765722074657874 | xxd -r -p | sha256sum + expected: fromHex("c853652437be02501c674744bf2a2b45d92a0a9f29c4b1044010fb3e2d43a949"), + }, } for name, tc := range cases { diff --git a/js/src/ops.spec.ts b/js/src/ops.spec.ts index 504f09c6..ff3376f6 100644 --- a/js/src/ops.spec.ts +++ b/js/src/ops.spec.ts @@ -86,6 +86,21 @@ describe("applyLeaf", () => { expect(applyLeaf(op, key, value)).toEqual(expected); }); + it("hashes with length prefix (fixed 32-bit little-endian encoding)", () => { + const op: ics23.ILeafOp = { + hash: ics23.HashOp.SHA256, + length: ics23.LengthOp.FIXED32_LITTLE + }; + // echo -n food | xxd -ps + const key = toAscii("food"); // 04000000666f6f64 + const value = toAscii("some longer text"); // 10000000736f6d65206c6f6e6765722074657874 + // echo -n 04000000666f6f6410000000736f6d65206c6f6e6765722074657874 | xxd -r -p | sha256sum + const expected = fromHex( + "c853652437be02501c674744bf2a2b45d92a0a9f29c4b1044010fb3e2d43a949" + ); + expect(applyLeaf(op, key, value)).toEqual(expected); + }); + it("hashes with prehash and length prefix", () => { const op: ics23.ILeafOp = { hash: ics23.HashOp.SHA256, diff --git a/js/src/ops.ts b/js/src/ops.ts index 31c835a8..54f6c1c8 100644 --- a/js/src/ops.ts +++ b/js/src/ops.ts @@ -134,11 +134,12 @@ function doLengthOp(lengthOp: ics23.LengthOp, data: Uint8Array): Uint8Array { throw new Error(`Length is ${data.length}, not 64 bytes`); } return data; + case ics23.LengthOp.FIXED32_LITTLE: + return new Uint8Array([...encodeFixed32LE(data.length), ...data]); // TODO // case LengthOp_VAR_RLP: // case LengthOp_FIXED32_BIG: // case LengthOp_FIXED64_BIG: - // case LengthOp_FIXED32_LITTLE: // case LengthOp_FIXED64_LITTLE: } throw new Error(`Unsupported lengthop: ${lengthOp}`); @@ -155,3 +156,15 @@ function encodeVarintProto(n: number): Uint8Array { enc = [...enc, l]; return new Uint8Array(enc); } + +function encodeFixed32LE(n: number): Uint8Array { + const enc = new Uint8Array(4); + let l = n; + for (let i = enc.length; i > 0; i--) { + /* tslint:disable */ + enc[Math.abs(i - enc.length)] = l % 256; + /* tslint:enable */ + l = Math.floor(l / 256); + } + return enc; +} diff --git a/rust/src/ops.rs b/rust/src/ops.rs index 26f074b7..cbbdcac4 100644 --- a/rust/src/ops.rs +++ b/rust/src/ops.rs @@ -55,6 +55,11 @@ fn do_length(length: LengthOp, data: &[u8]) -> Result { 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); + } _ => bail!("Unsupported LengthOp {:?}", length), } // if we don't error above or return custom string, just return item untouched (common case) @@ -125,6 +130,13 @@ mod tests { "proto prefix returned {}", hex::encode(&prefixed), ); + + let prefixed = do_length(LengthOp::Fixed32Little, b"food")?; + ensure!( + prefixed == hex::decode("04000000666f6f64")?, + "proto prefix returned {}", + hex::encode(&prefixed), + ); Ok(()) }