/* shim */ u3_noun _cqe_shim_fun(u3_noun zep, u3_noun tub) { u3_noun p_tub, q_tub; u3x_cell(tub, &p_tub, &q_tub); if ( c3n == u3du(q_tub) ) { return _fail(tub); } else { u3_noun p_zep, q_zep; u3_noun iq_tub = u3h(q_tub); u3x_cell(zep, &p_zep, &q_zep); if ( _(u3a_is_cat(p_zep)) && _(u3a_is_cat(q_zep)) && _(u3a_is_cat(iq_tub)) ) { if ( (iq_tub >= p_zep) && (iq_tub <= q_zep) ) { return _next(tub); } else return _fail(tub); } else { return u3m_bail(c3__fail); } } }
/* functions */ u3_noun u3qc_rsh(u3_atom a, u3_atom b, u3_atom c) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { return u3m_bail(c3__fail); } else if ( !_(u3a_is_cat(b)) ) { return 0; } else { c3_g a_g = a; c3_w b_w = b; c3_w len_w = u3r_met(a_g, c); if ( b_w >= len_w ) { return 0; } else { c3_w* sal_w = u3a_slaq(a_g, (len_w - b_w)); if ( 0 == sal_w ) { return u3m_bail(c3__fail); } u3r_chop(a_g, b_w, (len_w - b_w), 0, sal_w, c); // return u3a_moot(sal_w); return u3a_malt(sal_w); } } }
u3_noun _cqe_stew_fun(u3_noun hel, u3_noun tub) { u3_noun p_tub, q_tub; u3x_cell(tub, &p_tub, &q_tub); if ( c3n == u3du(q_tub) ) { return _fail(tub); } else { u3_noun iq_tub = u3h(q_tub); if ( !_(u3a_is_cat(iq_tub)) ) { return u3m_bail(c3__fail); } else while ( 1 ) { if ( c3n == u3du(hel) ) { return _fail(tub); } else { u3_noun n_hel, l_hel, r_hel; u3_noun pn_hel, qn_hel; c3_o bit_o; u3x_trel(hel, &n_hel, &l_hel, &r_hel); u3x_cell(n_hel, &pn_hel, &qn_hel); if ( (c3n == u3du(pn_hel)) ) { bit_o = __((iq_tub == pn_hel)); } else { u3_noun hpn_hel = u3h(pn_hel); u3_noun tpn_hel = u3t(pn_hel); if ( !_(u3a_is_cat(hpn_hel)) || !_(u3a_is_cat(tpn_hel)) ) { return _fail(tub); } else bit_o = __((iq_tub >= hpn_hel) && (iq_tub <= tpn_hel)); } if ( c3y == bit_o ) { return u3x_good (u3n_slam_on(u3k(qn_hel), u3k(tub))); } else { if ( c3y == _stew_wor(iq_tub, pn_hel) ) { hel = l_hel; } else hel = r_hel; } } } } }
static u3_noun _og_list(u3_noun a, u3_noun b, u3_noun c) { u3_noun l = u3_nul; if ( !_(u3a_is_cat(b)) ) { return u3m_bail(c3__fail); } while ( 0 != b ) { u3_noun x = u3qc_mix(a, c); u3_noun y = u3qc_mix(b, x); u3_noun d = u3qe_shas(c3_s4('o','g','-','b'), y); u3_noun m; u3z(x); u3z(y); if ( b < 256 ) { u3_noun e = u3qc_end(0, b, d); u3z(d); m = u3nc(b, e); b = 0; } else { m = u3nc(256, d); c = d; b -= 256; } l = u3nc(m, l); } return u3kb_flop(l); }
/* functions */ u3_noun u3qb_scag( u3_atom a, u3_noun b) { if ( !_(u3a_is_cat(a)) ) { return u3m_bail(c3__fail); } else { u3_noun acc; c3_w i_w = a; if ( !i_w ) return u3_nul; while ( i_w ) { if ( c3n == u3du(b) ) { return u3_nul; } acc = u3i_cell( u3h(b), acc ); b = u3t(b); i_w--; } return u3kb_flop(acc); } }
u3_noun u3qe_shal(u3_atom a, u3_atom b) { c3_assert(_(u3a_is_cat(a))); c3_y* fat_y = u3a_malloc(a + 1); u3r_bytes(0, a, fat_y, b); { c3_y dig_y[64]; #if defined(U3_OS_osx) CC_SHA512_CTX ctx_h; CC_SHA512_Init(&ctx_h); CC_SHA512_Update(&ctx_h, fat_y, a); CC_SHA512_Final(dig_y, &ctx_h); #else SHA512_CTX ctx_h; SHA512_Init(&ctx_h); SHA512_Update(&ctx_h, fat_y, a); SHA512_Final(dig_y, &ctx_h); #endif u3a_free(fat_y); return u3i_bytes(64, dig_y); } }
// good old linear search // static u3_noun _po_find(u3_noun buf, u3_noun a) { if ( !_(u3a_is_cat(a)) ) { return u3_nul; } else { c3_w i_w; c3_w a_w = a; for ( i_w = 0; i_w < 256; i_w++ ) { c3_y byt_y[3]; c3_w but_w; u3r_bytes((i_w * 3), 3, byt_y, buf); but_w = (byt_y[0] | (byt_y[1] << 8) | (byt_y[2] << 16)); if ( but_w == a_w ) { return u3nc(u3_nul, i_w); } } return u3_nul; } }
/* functions */ u3_noun u3qc_met(u3_atom a, u3_atom b) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { if ( 0 == b ) { return 0; } else return 1; } else { c3_w met_w = u3r_met(a, b); if ( !_(u3a_is_cat(met_w)) ) { return u3i_words(1, &met_w); } else return u3r_met(a, b); } }
/* functions */ u3_noun u3qc_cut(u3_atom a, u3_atom b, u3_atom c, u3_atom d) { if ( !_(u3a_is_cat(a)) || (a >= 32) ) { return u3m_bail(c3__fail); } if ( !_(u3a_is_cat(b)) ) { return 0; } if ( !_(u3a_is_cat(c)) ) { c = 0x7fffffff; } { c3_g a_g = a; c3_w b_w = b; c3_w c_w = c; c3_w len_w = u3r_met(a_g, d); if ( (0 == c_w) || (b_w >= len_w) ) { return 0; } if ( b_w + c_w > len_w ) { c_w = (len_w - b_w); } if ( (b_w == 0) && (c_w == len_w) ) { return u3k(d); } else { c3_w* sal_w = u3a_slaq(a_g, c_w); if ( 0 == sal_w ) { return u3m_bail(c3__fail); } u3r_chop(a_g, b_w, c_w, 0, sal_w, d); return u3a_malt(sal_w); } } }
/* functions */ u3_noun u3qa_mul(u3_atom a, u3_atom b) { if ( _(u3a_is_cat(a)) && _(u3a_is_cat(b)) ) { c3_d c = ((c3_d) a) * ((c3_d) b); return u3i_chubs(1, &c); } else { mpz_t a_mp, b_mp; u3r_mp(a_mp, a); u3r_mp(b_mp, b); mpz_mul(a_mp, a_mp, b_mp); mpz_clear(b_mp); return u3i_mp(a_mp); } }
/* functions */ u3_noun u3qa_lth(u3_atom a, u3_atom b) { if ( _(u3a_is_cat(a)) && _(u3a_is_cat(b)) ) { return __(a < b); } else { mpz_t a_mp, b_mp; u3_noun cmp; u3r_mp(a_mp, a); u3r_mp(b_mp, b); cmp = (mpz_cmp(a_mp, b_mp) < 0) ? c3y : c3n; mpz_clear(a_mp); mpz_clear(b_mp); return cmp; } }
static u3_noun _last(u3_noun zyc, u3_noun naz) { u3_noun p_zyc, q_zyc, p_naz, q_naz; u3x_cell(zyc, &p_zyc, &q_zyc); u3x_cell(naz, &p_naz, &q_naz); if ( !_(u3a_is_cat(p_zyc)) || !_(u3a_is_cat(q_zyc)) || !_(u3a_is_cat(p_naz)) || !_(u3a_is_cat(q_naz)) ) { return u3m_bail(c3__fail); } else { if ( p_zyc == p_naz ) { return (q_zyc > q_naz) ? u3k(zyc) : u3k(naz); } else { return (p_zyc > p_naz) ? u3k(zyc) : u3k(naz); } } }
/* stew */ static u3_noun _stew_wor(u3_noun ort, u3_noun wan) { if ( !_(u3a_is_cat(ort)) ) { return u3m_bail(c3__fail); } else { if ( c3n == u3du(wan) ) { if ( !_(u3a_is_cat(wan)) ) { return u3m_bail(c3__fail); } else return (ort < wan) ? c3y : c3n; } else { u3_noun h_wan = u3h(wan); if ( !_(u3a_is_cat(h_wan)) ) { return u3m_bail(c3__fail); } else return (ort < h_wan) ? c3y : c3n; } } }
u3_noun u3we_shal(u3_noun cor) { u3_noun a, b; if ( (u3_none == (a = u3r_at(u3x_sam_2, cor))) || (u3_none == (b = u3r_at(u3x_sam_3, cor))) || (c3n == u3ud(a)) || (c3n == u3a_is_cat(a)) || (c3n == u3ud(b)) ) { return u3m_bail(c3__exit); } else { return u3qe_shal(a, b); } }
/* functions */ u3_noun u3qb_slag(u3_atom a, u3_noun b) { if ( !_(u3a_is_cat(a)) ) { return u3m_bail(c3__fail); } else { c3_w len_w = a; while ( len_w ) { if ( c3n == u3du(b) ) { return u3_nul; } b = u3t(b); len_w--; } return u3k(b); } }
u3_noun u3qa_dec(u3_atom a) { if ( 0 == a ) { return u3m_error("decrement-underflow"); } else { if ( _(u3a_is_cat(a)) ) { return a - 1; } else { mpz_t a_mp; u3r_mp(a_mp, a); mpz_sub_ui(a_mp, a_mp, 1); return u3i_mp(a_mp); } } }
u3_noun u3we_shay(u3_noun cor) { u3_noun a, b; // static int few = 0; // if(few == 0) printf("foo\r\n"); // few++; few %= 1000; if ( (u3_none == (a = u3r_at(u3x_sam_2, cor))) || (u3_none == (b = u3r_at(u3x_sam_3, cor))) || (c3n == u3ud(a)) || (c3n == u3a_is_cat(a)) || (c3n == u3ud(b)) ) { return u3m_bail(c3__exit); } else { return u3qe_shay(a, b); } }
/* _http_octs_to_bod(): translate octet-stream noun into body. */ static u3_hbod* _http_octs_to_bod(u3_noun oct) { c3_w len_w; if ( !_(u3a_is_cat(u3h(oct))) ) { // 2GB max u3m_bail(c3__fail); return 0; } len_w = u3h(oct); { u3_hbod* bod_u = c3_malloc(len_w + sizeof(*bod_u)); bod_u->len_w = len_w; u3r_bytes(0, len_w, bod_u->hun_y, u3t(oct)); bod_u->nex_u = 0; u3z(oct); return bod_u; } }
u3_noun u3qe_argon2( // configuration params, u3_atom out, u3_atom type, u3_atom version, u3_atom threads, u3_atom mem_cost, u3_atom time_cost, u3_atom wik, u3_atom key, u3_atom wix, u3_atom extra, // input params u3_atom wid, u3_atom dat, u3_atom wis, u3_atom sat ) { c3_assert( _(u3a_is_cat(out)) && _(u3a_is_cat(type)) && _(u3a_is_cat(version)) && _(u3a_is_cat(threads)) && _(u3a_is_cat(mem_cost)) && _(u3a_is_cat(time_cost)) && _(u3a_is_cat(wik)) && _(u3a_is_cat(wix)) && _(u3a_is_cat(wid)) && _(u3a_is_cat(wis)) ); // flip endianness for argon2 key = u3qc_rev(3, wik, key); extra = u3qc_rev(3, wix, extra); dat = u3qc_rev(3, wid, dat); sat = u3qc_rev(3, wis, sat); // atoms to byte arrays c3_y bytes_key[wik]; u3r_bytes(0, wik, bytes_key, key); c3_y bytes_extra[wix]; u3r_bytes(0, wix, bytes_extra, extra); c3_y bytes_dat[wid]; u3r_bytes(0, wid, bytes_dat, dat); c3_y bytes_sat[wis]; u3r_bytes(0, wis, bytes_sat, sat); c3_y outhash[out]; argon2_context context = { outhash, // output array, at least [digest length] in size out, // digest length bytes_dat, // password array wid, // password length bytes_sat, // salt array wis, // salt length bytes_key, wik, // optional secret data bytes_extra, wix, // optional associated data time_cost, mem_cost, threads, threads, // performance cost configuration version, // algorithm version argon2_alloc, // custom memory allocation function argon2_free, // custom memory deallocation function ARGON2_DEFAULT_FLAGS // by default only internal memory is cleared }; int argon_res; switch ( type ) { default: fprintf(stderr, "\nunjetted argon2 variant %i\n", type); u3m_bail(c3__exit); break; // case c3__d: argon_res = argon2d_ctx(&context); break; // case c3__i: argon_res = argon2i_ctx(&context); break; // case c3__id: argon_res = argon2id_ctx(&context); break; // case c3__u: argon_res = argon2u_ctx(&context); break; } if ( ARGON2_OK != argon_res ) { fprintf(stderr, "\nargon2 error: %s\n", argon2_error_message(argon_res)); u3m_bail(c3__exit); } u3z(key); u3z(extra); u3z(dat); u3z(sat); return u3kc_rev(3, out, u3i_bytes(out, outhash)); }