Skip to content

Commit

Permalink
feat(logic)!: remove sha_hash/2 predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Nov 13, 2023
1 parent b5a8320 commit 605d0cd
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 82 deletions.
2 changes: 0 additions & 2 deletions x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 0 additions & 37 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 0 additions & 43 deletions x/logic/predicate/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]).`,
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 605d0cd

Please sign in to comment.