Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Keccak256 hash to cadence #1327

Merged
merged 7 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/language/crypto.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub enum HashAlgorithm: UInt8 {
/// used in BLS signatures.
pub case KMAC128_BLS_BLS12_381 = 5

/// KECCAK_256 is the legacy Keccak algorithm with a 256-bits digest, as per the original submission to the NIST SHA3 competition.
/// KECCAK_256 is different than SHA3 and is used by Ethereum.
pub case KECCAK_256 = 6

/// Returns the hash of the given data
pub fun hash(_ data: [UInt8]): [UInt8]

Expand Down
10 changes: 5 additions & 5 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,17 +1389,17 @@ func TestExportResourceDictionaryValue(t *testing.T) {
actual := exportValueFromScript(t, script)
expected := cadence.NewDictionary([]cadence.KeyValuePair{
{
Key: cadence.String("a"),
Key: cadence.String("b"),
Value: cadence.NewResource([]cadence.Value{
cadence.NewUInt64(0),
cadence.NewInt(1),
cadence.NewInt(2),
}).WithType(fooResourceType),
},
{
Key: cadence.String("b"),
Key: cadence.String("a"),
Value: cadence.NewResource([]cadence.Value{
cadence.NewUInt64(0),
cadence.NewInt(2),
cadence.NewInt(1),
}).WithType(fooResourceType),
},
})
Expand Down Expand Up @@ -1985,7 +1985,7 @@ func TestExportJsonDeterministic(t *testing.T) {
bytes, err := json.Encode(event)

assert.NoError(t, err)
assert.Equal(t, "{\"type\":\"Event\",\"value\":{\"id\":\"S.test.Foo\",\"fields\":[{\"name\":\"bar\",\"value\":{\"type\":\"Int\",\"value\":\"2\"}},{\"name\":\"aaa\",\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}},{\"key\":{\"type\":\"Int\",\"value\":\"7\"},\"value\":{\"type\":\"String\",\"value\":\"d\"}},{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"b\"}}]}},{\"key\":{\"type\":\"Int\",\"value\":\"0\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"0\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}},{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}}]}},{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"\"}},{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"7\"},\"value\":{\"type\":\"String\",\"value\":\"b\"}}]}}]}}]}}\n", string(bytes))
assert.Equal(t, "{\"type\":\"Event\",\"value\":{\"id\":\"S.test.Foo\",\"fields\":[{\"name\":\"bar\",\"value\":{\"type\":\"Int\",\"value\":\"2\"}},{\"name\":\"aaa\",\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"\"}},{\"key\":{\"type\":\"Int\",\"value\":\"7\"},\"value\":{\"type\":\"String\",\"value\":\"b\"}},{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}}]}},{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"b\"}},{\"key\":{\"type\":\"Int\",\"value\":\"7\"},\"value\":{\"type\":\"String\",\"value\":\"d\"}},{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}}]}},{\"key\":{\"type\":\"Int\",\"value\":\"0\"},\"value\":{\"type\":\"Dictionary\",\"value\":[{\"key\":{\"type\":\"Int\",\"value\":\"2\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}},{\"key\":{\"type\":\"Int\",\"value\":\"3\"},\"value\":{\"type\":\"String\",\"value\":\"c\"}},{\"key\":{\"type\":\"Int\",\"value\":\"0\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}},{\"key\":{\"type\":\"Int\",\"value\":\"1\"},\"value\":{\"type\":\"String\",\"value\":\"a\"}}]}}]}}]}}\n", string(bytes))
}

var fooFields = []cadence.Field{
Expand Down
4 changes: 2 additions & 2 deletions runtime/resourcedictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,10 +966,10 @@ func TestRuntimeSResourceDictionaryValues_Destruction(t *testing.T) {

assert.Equal(t,
[]string{
`"destroying R"`,
"2",
`"destroying R"`,
"1",
`"destroying R"`,
"2",
},
loggedMessages,
)
Expand Down
12 changes: 12 additions & 0 deletions runtime/sema/crypto_algorithm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var HashAlgorithms = []CryptoAlgorithm{
HashAlgorithmSHA3_256,
HashAlgorithmSHA3_384,
HashAlgorithmKMAC128_BLS_BLS12_381,
HashAlgorithmKECCAK_256,
}

var SignatureAlgorithmType = newNativeEnumType(
Expand Down Expand Up @@ -182,6 +183,7 @@ const (
HashAlgorithmSHA3_256
HashAlgorithmSHA3_384
HashAlgorithmKMAC128_BLS_BLS12_381
HashAlgorithmKECCAK_256
)

func (algo HashAlgorithm) Name() string {
Expand All @@ -198,6 +200,8 @@ func (algo HashAlgorithm) Name() string {
return "SHA3_384"
case HashAlgorithmKMAC128_BLS_BLS12_381:
return "KMAC128_BLS_BLS12_381"
case HashAlgorithmKECCAK_256:
return "KECCAK_256"
}

panic(errors.NewUnreachableError())
Expand All @@ -222,6 +226,8 @@ func (algo HashAlgorithm) RawValue() uint8 {
return 4
case HashAlgorithmKMAC128_BLS_BLS12_381:
return 5
case HashAlgorithmKECCAK_256:
return 6
}

panic(errors.NewUnreachableError())
Expand All @@ -241,6 +247,8 @@ func (algo HashAlgorithm) DocString() string {
return HashAlgorithmDocStringSHA3_384
case HashAlgorithmKMAC128_BLS_BLS12_381:
return HashAlgorithmDocStringKMAC128_BLS_BLS12_381
case HashAlgorithmKECCAK_256:
return HashAlgorithmDocStringKECCAK_256
}

panic(errors.NewUnreachableError())
Expand Down Expand Up @@ -322,3 +330,7 @@ that can be used as the hashing algorithm for BLS signature scheme on the curve
This is a customized version of KMAC128 that is compatible with the hashing to curve
used in BLS signatures.
`
const HashAlgorithmDocStringKECCAK_256 = `
KECCAK_256 is the legacy Keccak algorithm with a 256-bits digest, as per the original submission to the NIST SHA3 competition.
KECCAK_256 is different than SHA3 and is used by Ethereum.
`
5 changes: 3 additions & 2 deletions runtime/sema/hashalgorithm_string.go

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

1 change: 1 addition & 0 deletions runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
HashAlgorithmSHA3_256 = sema.HashAlgorithmSHA3_256
HashAlgorithmSHA3_384 = sema.HashAlgorithmSHA3_384
HashAlgorithmKMAC128_BLS_BLS12_381 = sema.HashAlgorithmKMAC128_BLS_BLS12_381
HashAlgorithmKECCAK_256 = sema.HashAlgorithmKECCAK_256
)

type AccountKey struct {
Expand Down