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

BLAKE3 jets #535

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ versioned_http_file(
versioned_http_archive(
name = "urcrypt",
build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD",
sha256 = "00ec597c14c418802d5db2d6a68cf83bd4f5419071b95f979374d3184599d6c8",
sha256 = "52209677868ccbb8bc87102dd4558b09f78bf57c4470b0feded30f411d13cc97",
strip_prefix = "urcrypt-{version}",
url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz",
version = "b970baefa6e0a680fffa2b2ee19c956a4ae20355",
version = "f331b6a9d1a86d42573165ff649700bf9c13b005",
)

versioned_http_archive(
Expand Down
111 changes: 107 additions & 4 deletions pkg/noun/jets/e/blake.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "noun.h"
#include "urcrypt.h"


static u3_atom
_cqe_blake(u3_atom wid, u3_atom dat,
_cqe_blake2b(u3_atom wid, u3_atom dat,
u3_atom wik, u3_atom dak,
u3_atom out)
{
Expand Down Expand Up @@ -39,7 +38,7 @@
}

u3_noun
u3we_blake(u3_noun cor)
u3we_blake2b(u3_noun cor)
{
u3_noun msg, key, out, // arguments
wid, dat, // destructured msg
Expand All @@ -54,6 +53,110 @@
{
return u3m_bail(c3__exit);
} else {
return u3l_punt("blake", _cqe_blake(wid, dat, wik, dak, out));
return u3l_punt("blake2b", _cqe_blake2b(wid, dat, wik, dak, out));
}
}

static u3_atom
_cqe_blake3_hash(u3_atom wid, u3_atom dat,
u3_atom key, u3_atom out)
{
c3_w wid_w;
if ( !u3r_word_fit(&wid_w, wid) ) {
// impossible to represent an atom this large
return u3m_bail(c3__fail);
}
else {
c3_y out_y[64], key_y[32];
c3_w out_w = c3_max(1, c3_min(out, 64));
c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat);
u3r_bytes(0, 32, key_y, key);
urcrypt_blake3_hash(wid_w, dat_y, key_y, out_w, out_y);
u3a_free(dat_y);
return u3i_bytes(out_w, out_y);
}
}

u3_noun
u3we_blake3_hash(u3_noun cor)
{
u3_noun out, msg, // arguments
wid, dat, // destructured msg
key; // context

if ( c3n == u3r_mean(cor, u3x_sam_2, &out,
u3x_sam_3, &msg,
u3x_con_sam_2, &key, 0) ||
u3ud(out) ||
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) ||
u3ud(key))
{
return u3m_bail(c3__exit);
} else {
return u3l_punt("blake3_hash", _cqe_blake3_hash(wid, dat, key, out));
}
}

static u3_noun
_cqe_blake3_chunk_output(u3_atom wid, u3_atom dat, u3_atom cv, u3_atom counter, u3_atom flags)
{
c3_w wid_w;
if ( !u3r_word_fit(&wid_w, wid) ) {
return u3m_bail(c3__fail);
} else {
c3_y cv_y[32], block_y[64], block_len;
c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat);
c3_d counter_d = u3r_chub(0, counter);
c3_y flags_y = u3r_byte(0, flags);
u3r_bytes(0, 32, cv_y, cv);
urcrypt_blake3_chunk_output(wid_w, dat_y, cv_y, block_y, &block_len, &counter_d, &flags_y);
return u3i_cell(u3i_bytes(32, cv_y), u3i_qual(u3i_chub(counter_d), u3i_bytes(64, block_y), block_len, u3i_bytes(1, &flags_y)));
joemfb marked this conversation as resolved.
Show resolved Hide resolved
}
}

u3_noun
u3we_blake3_chunk_output(u3_noun cor)
{
u3_noun counter, msg, // arguments
wid, dat, // destructured msg
key, flags; // context

if ( c3n == u3r_mean(cor, u3x_sam_2, &counter,
u3x_sam_3, &msg,
u3x_con_sam_2, &key,
u3x_con_sam_3, &flags, 0) ||
u3ud(counter) ||
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) ||
u3ud(key) || u3ud(flags))
{
return u3m_bail(c3__exit);
} else {
return u3l_punt("blake3_chunk_output", _cqe_blake3_chunk_output(wid, dat, key, counter, flags));
}
}

static u3_atom
_cqe_blake3_compress(u3_atom cv, u3_atom counter,
u3_atom block, u3_atom block_len, u3_atom flags)
{
c3_y cv_y[32], block_y[64], out_y[64];
u3r_bytes(0, 32, cv_y, cv);
u3r_bytes(0, 64, block_y, block);
urcrypt_blake3_compress(cv_y, block_y, block_len, counter, flags, out_y);
return u3i_bytes(64, out_y);
}

u3_noun
u3we_blake3_compress(u3_noun cor)
{
u3_noun output = u3x_at(u3x_sam, cor);
u3_noun cv, counter, block, block_len, flags; // destructured output

if ( u3r_quil(output, &cv, &counter, &block, &block_len, &flags) ||
u3ud(cv) || u3ud(block) || u3ud(block_len) || u3ud(counter) || u3ud(flags))
{
return u3m_bail(c3__exit);
} else {
return u3l_punt("blake3_compress", _cqe_blake3_compress(cv, counter, block, block_len, flags));
}
}
29 changes: 28 additions & 1 deletion pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,40 @@ static c3_c* _140_hex_secp_ha[] = {
0
};

static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake, c3y}, {}};
static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}};
static c3_c* _140_hex_blake2b_ha[] = {
"c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934",
0
};
static u3j_harm _140_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}};
static c3_c* _140_hex_blake3_hash_ha[] = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
0
};
static u3j_harm _140_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}};
static c3_c* _140_hex_blake3_compress_ha[] = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
0
};
static u3j_harm _140_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}};
static c3_c* _140_hex_blake3_chunk_output_ha[] = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
0
};
static u3j_core _140_hex_blake3_d[] =
{ { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha },
{ "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha },
{ "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha },
{}
};
static c3_c* _140_hex_blake3_ha[] = { 0 };
static u3j_core _140_hex_blake_d[] =
{ { "blake2b", 7, _140_hex_blake2b_a, 0, _140_hex_blake2b_ha },

{ "blake3", 7, 0, _140_hex_blake3_d, _140_hex_blake3_ha },
{ "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha },
{ "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha },
{ "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha },
joemfb marked this conversation as resolved.
Show resolved Hide resolved
{}
};
static c3_c* _140_hex_blake_ha[] = {
Expand Down
5 changes: 4 additions & 1 deletion pkg/noun/jets/w.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@

u3_noun u3we_argon2(u3_noun);

u3_noun u3we_blake(u3_noun);
u3_noun u3we_blake2b(u3_noun);
u3_noun u3we_blake3_hash(u3_noun);
u3_noun u3we_blake3_chunk_output(u3_noun);
u3_noun u3we_blake3_compress(u3_noun);

u3_noun u3we_ripe(u3_noun);

Expand Down