HI
I would like to use the Data.Hashable package from Haskell, would you please be able to advise what needs to be imported and in the daml code and any dependency needs to be set as by default Data.Hashable package is not recognized by DAML compiler
Daml shares a lot of its syntax with Haskell, but you cannot link Haskell code directly into Daml. There are cases in which porting Haskell code to Daml is relatively easy, but be aware that (contrary to Haskell) Daml’s evaluation semantics is strict, so pay attention to the code you are porting and beware of just copying.
Thanks. noted. I however want to calculate the hash code of a string; so if haskell library cannot be used are there any function available natively in DAML. I also tried implementing the function myself but need bit level “xor” function; do we have any package in DAML to perform bit level operations eg. XOR
I’m not sure whether we do, but as a general note, I would recommend to avoid computation-intensive work in Daml unless it’s strictly necessary. In this case, if you need parties to agree on the content of a string, you can compute the hash off-ledger and let the Daml logic compare those hashes or move them around however you see fit, rather than having the hashing happen in Daml itself.
EDIT: I stand corrected, see the point made by @Andreas_Lochbihler in his subsequent answer.
For computing the hash of a string, you can use Daml’s built-in SHA-256 function. This is implemented natively and therefore fairly efficient.
yes am using sh256 for now, but need a shorter version ; 64 chars is too long for my use case; truncating might result in collision and random pick might be unpredictable.
There is no randomness in Daml, so the second option is pretty much off the table.
Your first point here is confusing to me. No hash is collision-free. Truncating a SHA256 to, say, the first 32 characters, should not result in significantly more collision chances than using a 32-char hash in the first place.
One thing you could try to get shorter strings is to re-encode the SHA256 hash: we return it as hex, i.e. base 16; you could reencode it in base64, for example. That would result in shorter strings but wouldn’t increase the chance of collision as it’s still a full hash. You’d have to do the conversion in Daml, though, so I’ll point you back to Stefano’s comment on compute-intensive workflows in Daml.