This implementation enables streaming preimage bytes into a provable SHA256 hash function.
It is primarily an adaptation of StableLib's SHA256 class, modified to be provable.
Changes
Added a SHA256Hasher class.
Introduced an optional boolean reverseEndianness parameter to the bytesToWords, wordsToBytes, wordToBytes, and bytesToWord functions in the bit-slices.ts file.
Created a zkProgram that demonstrates similar functionality to SHA256Program but dynamically.
There is some redundant code in the hashBlocks method of the hasher class since the core logic is copied from the original SHA256.hash() function.
The dynamic SHA256 is slightly less efficient than the original SHA256 hash function, likely due to byte processing:
For Bytes12 input:
SHA256.hash --> 5036 rows
SHA256.update --> 5241 rows
It's noteworthy that a fluent interface or method chaining works as provable in o1js, which opens up possibilities for various future implementations.
Attempting to witness a hasher instance with Provable.witness is currently not possible, but it would be a valuable feature.
Observing how noble-hashes use abstract classes with method chaining suggests that it might be possible to migrate many binary hash functions to be provable in o1js, similar to noble-hashes.
Description
Changes
SHA256Hasher
class.reverseEndianness
parameter to thebytesToWords
,wordsToBytes
,wordToBytes
, andbytesToWord
functions in thebit-slices.ts
file.zkProgram
that demonstrates similar functionality to SHA256Program but dynamically.Notes
hashBlocks
method of the hasher class since the core logic is copied from the originalSHA256.hash()
function.Bytes12
input:SHA256.hash
--> 5036 rowsSHA256.update
--> 5241 rowsProvable.witness
is currently not possible, but it would be a valuable feature.