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

[KIP] - Add a new hook for hashing K terms #2568

Open
ChristianoBraga opened this issue Apr 28, 2022 · 5 comments
Open

[KIP] - Add a new hook for hashing K terms #2568

ChristianoBraga opened this issue Apr 28, 2022 · 5 comments
Labels

Comments

@ChristianoBraga
Copy link

ChristianoBraga commented Apr 28, 2022

Motivation

  • Is there a semantics which you are developing which this feature would simplify?

KPlutus - https://github.com/runtimeverification/plutus-core-semantics

  • Is this feature similar to a feature offered by another programming language?

Not that I am aware of.

  • Any other motivation?

It will remove a potential bootleneck at KPlutus that now uses #unparseKORE from K reflection and Sha3_256 functions to hash KPlutus values.

This feature is already present in the LLVM backend. It would only need to be exposed to the user, as far as I know.

Example K Code

Function hashK for instance would receive any K term and return an integer. It could perhaps be declared as

syntax {Sort} Int ::= #hashK(Sort)

I am not sure about the declaration. It was inspired by the declaration of #unparseKORE.

Documentation

Function #hashK produces an integer for any given K term. It may help, for instance, on storing terms on a hash table to represent a heap in a programming language.

Potential Alternatives/Work-arounds

In the KPlutus project we use #unparseKORE and Sha3_256 to implement such a function. Another alternative could be to implement a function that transforms any given KPlutus term into a string and to implement a hash function, all in K.

Testing Approach

This function would could be easily tested in KPlutus simply by rewriting #uplcHash in module uplc-hash.md accordingly or simply replacing the call to #uplcHash by #hashK.

@radumereuta
Copy link
Contributor

Here is the start of the discussion on slack. It has a lot more details about what is needed:
https://runtimeverification.slack.com/archives/C7E701MFG/p1651087319515199
Probably needed for both Haskell and LLVM backends.

@radumereuta
Copy link
Contributor

radumereuta commented May 5, 2022

Please investigate further what @dwightguth is suggesting in the slack channel.
There might be issues ensuring we have the same implementation in the Haskell and LLVM backends.

@SchmErik
Copy link
Contributor

SchmErik commented May 5, 2022

This issue is an optimization to a solution for creating an environment that maps variable names to terms.

If we create a map from variable names to the terms themselves as @dwightguth suggested, the heap could look like this. Note n, m, and p are variable names and v_1 is a term.

<heap>
  n |-> v_1
  m |-> v_1
  p |-> v_1
</heap>

This blows up the space consumption during program execution because v_1 can be repeated in the heap and some of these terms are massive.

To address this issue, our solution is to hash the term and create a map between the hash and the term itself and map the variable to the hash so the heap above looks like this:

<heap>
  n |-> hash(v_1)
  m |-> hash(v_1)
  p |-> hash(v_1)
</heap>

and there's a separate environment that maps hash(v_1) |-> v_1. Our current approach is to use #unparseKore and Sha3_256 to hash the terms however, this would be faster if there was a hook for this.

From our discussion today, we need to have this hash hook return the same values for haskell and llvm backends.

Urgency

We do not need this at the moment because we aren't experiencing any performance issues arising from our current hashing function. If this becomes an issue, we would like to prioritize this. If anyone has a better solution, please feel free to chime in!

@dwightguth
Copy link
Member

@ChristianoBraga is this still useful to you?

@ChristianoBraga
Copy link
Author

ChristianoBraga commented Oct 22, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants