Skip to content

Commit

Permalink
BLAKE3 jets (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
joemfb committed Jan 25, 2024
2 parents f063220 + e9d3f32 commit 60986d3
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ versioned_http_file(
versioned_http_archive(
name = "urcrypt",
build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD",
sha256 = "afc1182e10eeebaeb2a111c2bd889747792d255e26aba7fdcd6751d0d3c2bb35",
sha256 = "d27ec04d3854da7c479dd815af92ffef986616bc7ff400022e2dfb7971853d86",
strip_prefix = "urcrypt-{version}",
url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz",
version = "43479c3262a11e20da5f6218f3b0b3d63931ceea",
version = "9ae5d604528bc54ae48430f55ebbb17b1ad7956c",
)

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, out_w;
if ( !u3r_word_fit(&wid_w, wid) || !u3r_word_fit(&out_w, out) ) {
return u3m_bail(c3__fail);
}
else {
c3_y key_y[32];
u3r_bytes(0, 32, key_y, key);
c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat);
u3i_slab sab_u;
u3i_slab_bare(&sab_u, 3, out_w);
c3_y* out_y = sab_u.buf_y;
urcrypt_blake3_hash(wid_w, dat_y, key_y, out, out_y);
u3a_free(dat_y);
return u3i_slab_mint(&sab_u);
}
}

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) ||
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(u3k(counter), u3i_bytes(64, block_y), block_len, flags_y));
}
}

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) ||
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));
}
}
51 changes: 44 additions & 7 deletions pkg/noun/jets/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,10 @@ static c3_c* _140_hex_secp_ha[] = {
0
};

static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake, c3y}, {}};
static c3_c* _140_hex_blake2b_ha[] = {
"c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934",
0
};
static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}};

static u3j_core _140_hex_blake_d[] =
{ { "blake2b", 7, _140_hex_blake2b_a, 0, _140_hex_blake2b_ha },
{ { "blake2b", 7, _140_hex_blake2b_a, 0, no_hashes },
{}
};
static c3_c* _140_hex_blake_ha[] = {
Expand Down Expand Up @@ -2325,8 +2322,48 @@ u3j_core _k139_d[] =
{}
};

static u3j_harm _138_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}};
static u3j_harm _138_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}};
static u3j_harm _138_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}};
static u3j_core _138_hex_blake3_d[] =
{ { "hash", 7, _138_hex_blake3_hash_a, 0, no_hashes },
{ "chunk-output", 7, _138_hex_blake3_chunk_output_a, 0, no_hashes },
{}
};
static u3j_core _138_hex_blake3_impl_d[] =
{ { "compress", 7, _138_hex_blake3_compress_a, 0, no_hashes },
{ "blake3", 7, 0, _138_hex_blake3_d, no_hashes },
{}
};
static u3j_core _138_hex_blake_d[] =
{ { "blake2b", 7, _140_hex_blake2b_a, 0, no_hashes },
{ "blake3-impl", 7, 0, _138_hex_blake3_impl_d, no_hashes },
{}
};

static u3j_core _138_hex_d[] =
{ { "lore", 63, _140_hex_lore_a, 0, no_hashes },
{ "leer", 63, _140_hex_leer_a, 0, no_hashes },
{ "loss", 63, _140_hex_loss_a, 0, no_hashes },
{ "lune", 127, _140_hex_lune_a, 0, no_hashes },

{ "coed", 63, 0, _140_hex_coed_d, no_hashes },
{ "aes", 31, 0, _140_hex_aes_d, no_hashes },

{ "hmac", 63, 0, _140_hex_hmac_d, no_hashes },
{ "argon", 31, 0, _140_hex_argon_d, no_hashes },
{ "blake", 31, 0, _138_hex_blake_d, no_hashes },
{ "kecc", 31, 0, _140_hex_kecc_d, no_hashes },
{ "ripemd", 31, 0, _140_hex_ripe_d, no_hashes },
{ "scr", 31, 0, _140_hex_scr_d, no_hashes },
{ "secp", 6, 0, _140_hex_secp_d, no_hashes },
{ "mimes", 31, 0, _140_hex_mimes_d, no_hashes },
{ "json", 31, 0, _139_hex_json_d, no_hashes },
{}
};

static u3j_core _138_pen_d[] =
{ { "hex", 7, 0, _139_hex_d, no_hashes },
{ { "hex", 7, 0, _138_hex_d, no_hashes },

{ "cell", 7, _140_pen_cell_a, 0, no_hashes },
{ "comb", 7, _140_pen_comb_a, 0, no_hashes },
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 @@ -206,7 +206,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

0 comments on commit 60986d3

Please sign in to comment.