Skip to content

Expose keyed Blake2b and Blake3 hash builtins - #150

Merged
someone235 merged 2 commits into
kaspanet:masterfrom
michaelsutton:all-blake-builtins
Jul 19, 2026
Merged

Expose keyed Blake2b and Blake3 hash builtins#150
someone235 merged 2 commits into
kaspanet:masterfrom
michaelsutton:all-blake-builtins

Conversation

@michaelsutton

Copy link
Copy Markdown
Contributor

Adds direct compiler support for:

blake2bWithKey(data, key)
blake3(data)
blake3WithKey(data, key)

All return byte[32] and lower to the corresponding script-engine opcodes. The existing blake2b builtin is documented alongside them.

Type details:

  • Hash data follows existing blake2b behavior and may be any stack value. Callers remain responsible for serialization; numeric values use minimally encoded Script-number bytes (see TODO in static_check.rs)
  • Blake2b keys are variable-length byte[], up to the engine’s 64-byte limit.
  • Blake3 keys must be byte[32]. A dynamic byte[] requires an explicit byte[32](key) cast.

Tests compare the compiled builtins against reference Blake2b and Blake3 implementations and execute the resulting scripts.

Lower the hash builtins to their script-engine opcodes, require fixed-size
Blake3 keys, and add documentation, highlighting, typing, and execution tests.
// TODO: Consider requiring byte[] data for all hash builtins so callers
// must make their serialization choices explicit; numeric values currently
// hash their minimally encoded Script-number bytes.
"blake3WithKey" => &[("data", "*"), ("key", "byte[32]")],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to replace * with byte[]


for (arg, (arg_name, expected_type_name)) in args.iter().zip(expected_args) {
for (arg, &(arg_name, expected_type_name)) in args.iter().zip(expected_args) {
if expected_type_name == "*" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After applying prev comment, you can revert this change

this currently deviates from other hash fns, but they are expected to catch up to this behavior, hence no need to introduce a temp wild card "*" mechanism
@someone235
someone235 merged commit 26e3b9f into kaspanet:master Jul 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants