From 605d0cd1ccc98210a2cc6a97501eba698abbbab0 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:42:06 +0100 Subject: [PATCH] feat(logic)!: remove sha_hash/2 predicate --- x/logic/interpreter/registry.go | 2 -- x/logic/predicate/crypto.go | 37 --------------------------- x/logic/predicate/crypto_test.go | 43 -------------------------------- 3 files changed, 82 deletions(-) diff --git a/x/logic/interpreter/registry.go b/x/logic/interpreter/registry.go index 169652b9..c7dc50d5 100644 --- a/x/logic/interpreter/registry.go +++ b/x/logic/interpreter/registry.go @@ -118,8 +118,6 @@ var registry = map[string]any{ "read_string/3": predicate.ReadString, "eddsa_verify/4": predicate.EDDSAVerify, "ecdsa_verify/4": predicate.ECDSAVerify, - //nolint:staticcheck - "sha_hash/2": predicate.SHAHash, } // RegistryNames is the list of the predicate names in the Registry. diff --git a/x/logic/predicate/crypto.go b/x/logic/predicate/crypto.go index 8ba2275b..226ee797 100644 --- a/x/logic/predicate/crypto.go +++ b/x/logic/predicate/crypto.go @@ -12,43 +12,6 @@ import ( "github.com/okp4/okp4d/x/logic/util" ) -// SHAHash is a predicate that computes the Hash of the given Data. -// -// Deprecated: sha_hash/2 should not be used anymore as it will be removed in a future release. -// Use the new crypto_data_hash/3 predicate instead. -// -// The signature is as follows: -// -// sha_hash(+Data, -Hash) is det -// sha_hash(+Data, +Hash) is det -// -// Where: -// - Data represents the data to be hashed with the SHA-256 algorithm. -// - Hash is the variable that will contain Hashed value of Data. -// -// Note: Due to the principles of the hash algorithm (pre-image resistance), this predicate can only compute the hash -// value from input data, and cannot compute the original input data from the hash value. -// -// Examples: -// -// # Compute the hash of the given data and unify it with the given Hash. -// - sha_hash('Hello OKP4', Hash). -func SHAHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { - return engine.Delay(func(ctx context.Context) *engine.Promise { - switch d := env.Resolve(data).(type) { - case engine.Atom: - result, err := util.Hash(util.HashAlgSha256, []byte(d.String())) - if err != nil { - engine.Error(fmt.Errorf("sha_hash/2: failed to hash data: %w", err)) - } - - return engine.Unify(vm, hash, BytesToList(result), cont, env) - default: - return engine.Error(fmt.Errorf("sha_hash/2: invalid data type: %T, should be Atom", d)) - } - }) -} - // CryptoDataHash is a predicate that computes the Hash of the given Data using different algorithms. // // The signature is as follows: diff --git a/x/logic/predicate/crypto_test.go b/x/logic/predicate/crypto_test.go index 9defb334..c483de7c 100644 --- a/x/logic/predicate/crypto_test.go +++ b/x/logic/predicate/crypto_test.go @@ -29,48 +29,6 @@ func TestCryptoOperations(t *testing.T) { wantError error wantSuccess bool }{ - { - query: `sha_hash('foo', Hash).`, - wantResult: []types.TermResults{{ - "Hash": "[44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]", - }}, - wantSuccess: true, - }, - { - query: `sha_hash(Foo, Hash).`, - wantResult: []types.TermResults{}, - wantError: fmt.Errorf("sha_hash/2: invalid data type: engine.Variable, should be Atom"), - wantSuccess: false, - }, - { - query: `sha_hash('bar', - [44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]).`, - wantSuccess: false, - }, - { - query: `sha_hash('bar', - [252,222,43,46,219,165,107,244,8,96,31,183,33,254,155,92,51,141,16,238,66,158,160,79,174,85,17,182,143,191,143,185]).`, - wantResult: []types.TermResults{{}}, - wantSuccess: true, - }, - { - query: `sha_hash('bar', - [345,222,43,46,219,165,107,244,8,96,31,183,33,254,155,92,51,141,16,238,66,158,160,79,174,85,17,182,143,191,143,185]).`, - wantSuccess: false, - }, - { - program: `test :- sha_hash('bar', H), - H == [252,222,43,46,219,165,107,244,8,96,31,183,33,254,155,92,51,141,16,238,66,158,160,79,174,85,17,182,143,191,143,185].`, - query: `test.`, - wantResult: []types.TermResults{{}}, - wantSuccess: true, - }, - { - program: `test :- sha_hash('bar', H), - H == [2252,222,43,46,219,165,107,244,8,96,31,183,33,254,155,92,51,141,16,238,66,158,160,79,174,85,17,182,143,191,143,185].`, - query: `test.`, - wantSuccess: false, - }, { query: `hex_bytes(Hex, [44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]).`, @@ -189,7 +147,6 @@ func TestCryptoOperations(t *testing.T) { Convey("and a vm", func() { interpreter := testutil.NewLightInterpreterMust(ctx) - interpreter.Register2(engine.NewAtom("sha_hash"), SHAHash) interpreter.Register3(engine.NewAtom("crypto_data_hash"), CryptoDataHash) interpreter.Register2(engine.NewAtom("hex_bytes"), HexBytes)