From c1566b1dbde4542a8d35b792991752932e5c5996 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 12 May 2020 16:46:47 +1000 Subject: [PATCH] feat: add two filecoin hashes, without Sum() implementations * sha2-256-trunc254-padded * poseidon-bls12_381-a2-fc1 Ref: https://github.com/multiformats/multicodec/pull/161 Ref: https://github.com/multiformats/multicodec/pull/171 Ref: https://github.com/multiformats/multicodec/pull/170 --- multihash.go | 82 +++++++++++++++++++++++++---------------------- multihash_test.go | 4 +++ spec/multicodec | 2 +- spec/multihash | 2 +- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/multihash.go b/multihash.go index cd44006..370e259 100644 --- a/multihash.go +++ b/multihash.go @@ -68,7 +68,9 @@ const ( // Deprecated: use MURMUR3_128 MURMUR3 = MURMUR3_128 - X11 = 0x1100 + SHA2_256_TRUNC254_PADDED = 0x1012 + X11 = 0x1100 + POSEIDON_BLS12_381_A1_FC1 = 0xb401 ) func init() { @@ -93,47 +95,51 @@ func init() { // Names maps the name of a hash to the code var Names = map[string]uint64{ - "identity": IDENTITY, - "sha1": SHA1, - "sha2-256": SHA2_256, - "sha2-512": SHA2_512, - "sha3": SHA3_512, - "sha3-224": SHA3_224, - "sha3-256": SHA3_256, - "sha3-384": SHA3_384, - "sha3-512": SHA3_512, - "dbl-sha2-256": DBL_SHA2_256, - "murmur3-128": MURMUR3_128, - "keccak-224": KECCAK_224, - "keccak-256": KECCAK_256, - "keccak-384": KECCAK_384, - "keccak-512": KECCAK_512, - "shake-128": SHAKE_128, - "shake-256": SHAKE_256, - "x11": X11, - "md5": MD5, + "identity": IDENTITY, + "sha1": SHA1, + "sha2-256": SHA2_256, + "sha2-512": SHA2_512, + "sha3": SHA3_512, + "sha3-224": SHA3_224, + "sha3-256": SHA3_256, + "sha3-384": SHA3_384, + "sha3-512": SHA3_512, + "dbl-sha2-256": DBL_SHA2_256, + "murmur3-128": MURMUR3_128, + "keccak-224": KECCAK_224, + "keccak-256": KECCAK_256, + "keccak-384": KECCAK_384, + "keccak-512": KECCAK_512, + "shake-128": SHAKE_128, + "shake-256": SHAKE_256, + "sha2-256-trunc254-padded": SHA2_256_TRUNC254_PADDED, + "x11": X11, + "md5": MD5, + "poseidon-bls12_381-a2-fc1": POSEIDON_BLS12_381_A1_FC1, } // Codes maps a hash code to it's name var Codes = map[uint64]string{ - IDENTITY: "identity", - SHA1: "sha1", - SHA2_256: "sha2-256", - SHA2_512: "sha2-512", - SHA3_224: "sha3-224", - SHA3_256: "sha3-256", - SHA3_384: "sha3-384", - SHA3_512: "sha3-512", - DBL_SHA2_256: "dbl-sha2-256", - MURMUR3_128: "murmur3-128", - KECCAK_224: "keccak-224", - KECCAK_256: "keccak-256", - KECCAK_384: "keccak-384", - KECCAK_512: "keccak-512", - SHAKE_128: "shake-128", - SHAKE_256: "shake-256", - X11: "x11", - MD5: "md5", + IDENTITY: "identity", + SHA1: "sha1", + SHA2_256: "sha2-256", + SHA2_512: "sha2-512", + SHA3_224: "sha3-224", + SHA3_256: "sha3-256", + SHA3_384: "sha3-384", + SHA3_512: "sha3-512", + DBL_SHA2_256: "dbl-sha2-256", + MURMUR3_128: "murmur3-128", + KECCAK_224: "keccak-224", + KECCAK_256: "keccak-256", + KECCAK_384: "keccak-384", + KECCAK_512: "keccak-512", + SHAKE_128: "shake-128", + SHAKE_256: "shake-256", + SHA2_256_TRUNC254_PADDED: "sha2-256-trunc254-padded", + X11: "x11", + POSEIDON_BLS12_381_A1_FC1: "poseidon-bls12_381-a2-fc1", + MD5: "md5", } // DefaultLengths maps a hash code to it's default length diff --git a/multihash_test.go b/multihash_test.go index 6e0653f..a6f6563 100644 --- a/multihash_test.go +++ b/multihash_test.go @@ -32,6 +32,8 @@ var tCodes = map[uint64]string{ 0x19: "shake-256", 0x1100: "x11", 0xd5: "md5", + 0x1012: "sha2-256-trunc254-padded", + 0xb401: "poseidon-bls12_381-a2-fc1", } type TestCase struct { @@ -54,6 +56,8 @@ var testCases = []TestCase{ {"1af97f7818a28edfdfce5ec66dbdc7e871813816d7d585fe1f12475ded5b6502b7723b74e2ee36f2651a10a8eaca72aa9148c3c761aaceac8f6d6cc64381ed39", 0x19, "shake-256"}, {"4bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7", 0x14, "sha3-512"}, {"d41d8cd98f00b204e9800998ecf8427e", 0xd5, "md5"}, + {"14fcb37dc45fa9a3c492557121bd4d461c0db40e5dcfcaa98498bd238486c307", 0x1012, "sha2-256-trunc254-padded"}, + {"14fcb37dc45fa9a3c492557121bd4d461c0db40e5dcfcaa98498bd238486c307", 0xb401, "poseidon-bls12_381-a2-fc1"}, } func (tc TestCase) Multihash() (Multihash, error) { diff --git a/spec/multicodec b/spec/multicodec index ba90699..bf5c480 160000 --- a/spec/multicodec +++ b/spec/multicodec @@ -1 +1 @@ -Subproject commit ba906993c21b6de66740d966926367bd741ee0be +Subproject commit bf5c4806e71d462d1ac9f232fef3b70e1a5c6dc4 diff --git a/spec/multihash b/spec/multihash index cde1aef..4950f31 160000 --- a/spec/multihash +++ b/spec/multihash @@ -1 +1 @@ -Subproject commit cde1aef8158d193d73012b7d730013f05c2f7063 +Subproject commit 4950f3172282dcc8f2c116937279c81a0c0327f1