Simplify aesc core structure

This makes ++en and ++de dramatically faster.
This commit is contained in:
Steve Dee 2014-04-18 18:33:56 -07:00
parent 57f684c3df
commit e4235f63db

View File

@ -9,87 +9,11 @@
/* declarations
*/
extern u2_ho_jet j2_mcj(Pt5, aesc, be)[];
extern u2_ho_jet j2_mcj(Pt5, aesc, en)[];
extern u2_ho_jet j2_mcj(Pt5, aesc, ex)[];
extern u2_ho_jet j2_mcj(Pt5, aesc, in)[];
#if 0
extern u2_ho_jet j2_mcj(Pt5, aesc, ix)[];
#endif
extern u2_ho_jet j2_mcj(Pt5, aesc, de)[];
/* functions
*/
u2_weak
j2_mcd(Pt5, aesc, be)(u2_wire wir_r,
u2_bean a,
u2_atom b,
u2_atom c)
{
AES_KEY key_u;
c3_y c_y[AES_BLOCK_SIZE];
key_u.rounds = AES_MAXNR;
if ( u2_cr_met(5, b) > 4 * (AES_MAXNR + 1) ) {
return u2_bl_bail(wir_r, c3__exit);
}
{
c3_w i_w;
c3_w b_w;
for ( i_w = 0; i_w < 4 * (AES_MAXNR + 1); i_w++ ) {
b_w = u2_cr_word(i_w, b);
// sigh
key_u.rd_key[i_w] = c3_flip32(b_w);
}
}
c3_assert(u2_cr_met(3, c) <= AES_BLOCK_SIZE);
u2_cr_bytes(0, AES_BLOCK_SIZE, c_y, c);
if ( u2_yes == a ) {
AES_encrypt(c_y, c_y, &key_u);
}
else {
// double sigh
{
c3_w i_w, j_w, tmp_w;
# define rk key_u.rd_key
for ( i_w = 0, j_w = 4 * AES_MAXNR; i_w < j_w; i_w += 4, j_w -= 4 ) {
tmp_w = rk[i_w ]; rk[i_w ] = rk[j_w ]; rk[j_w ] = tmp_w;
tmp_w = rk[i_w + 1]; rk[i_w + 1] = rk[j_w + 1]; rk[j_w + 1] = tmp_w;
tmp_w = rk[i_w + 2]; rk[i_w + 2] = rk[j_w + 2]; rk[j_w + 2] = tmp_w;
tmp_w = rk[i_w + 3]; rk[i_w + 3] = rk[j_w + 3]; rk[j_w + 3] = tmp_w;
}
# undef rk
}
AES_decrypt(c_y, c_y, &key_u);
}
return u2_ci_bytes(AES_BLOCK_SIZE, c_y);
}
u2_weak
j2_mc(Pt5, aesc, be)(u2_wire wir_r,
u2_noun cor)
{
u2_noun a, b, c;
if ( u2_no == u2_mean(cor, u2_cv_sam_2, &a,
u2_cv_sam_6, &b,
u2_cv_sam_7, &c, 0) ||
u2_no == u2_stud(b) ||
u2_no == u2_stud(c) ||
(a != u2_yes && a != u2_no) )
{
return u2_bl_bail(wir_r, c3__exit);
}
else {
return j2_mcd(Pt5, aesc, be)(wir_r, a, b, c);
}
}
u2_weak
j2_mcd(Pt5, aesc, en)(u2_wire wir_r,
u2_atom a,
@ -131,51 +55,7 @@
}
u2_weak
j2_mcd(Pt5, aesc, ex)(u2_wire wir_r,
u2_atom a)
{
AES_KEY key_u;
c3_y buf_y[32];
c3_w out_w[4 * (AES_MAXNR + 1)];
c3_assert(u2_cr_met(3, a) <= 32);
u2_cr_bytes(0, 32, buf_y, a);
if ( 0 != AES_set_encrypt_key(buf_y, 256, &key_u) ) {
return u2_bl_bail(wir_r, c3__exit);
}
else {
c3_w b_w;
c3_w i_w;
c3_assert(key_u.rounds == AES_MAXNR);
for ( i_w = 0; i_w < 4 * (AES_MAXNR + 1); i_w++ ) {
b_w = key_u.rd_key[i_w];
// hurk
out_w[i_w] = c3_flip32(b_w);
}
return u2_ci_words(4 * (AES_MAXNR + 1), out_w);
}
}
u2_weak
j2_mc(Pt5, aesc, ex)(u2_wire wir_r,
u2_noun cor)
{
u2_noun a;
if ( u2_no == u2_mean(cor, u2_cv_sam, &a, 0) ||
u2_no == u2_stud(a) )
{
return u2_bl_bail(wir_r, c3__exit);
}
else {
return j2_mcd(Pt5, aesc, ex)(wir_r, a);
}
}
u2_weak
j2_mcd(Pt5, aesc, in)(u2_wire wir_r,
j2_mcd(Pt5, aesc, de)(u2_wire wir_r,
u2_atom a,
u2_atom b)
{
@ -198,7 +78,7 @@
}
u2_weak
j2_mc(Pt5, aesc, in)(u2_wire wir_r,
j2_mc(Pt5, aesc, de)(u2_wire wir_r,
u2_noun cor)
{
u2_noun a, b;
@ -210,77 +90,27 @@
return u2_bl_bail(wir_r, c3__exit);
}
else {
return j2_mcd(Pt5, aesc, in)(wir_r, a, b);
return j2_mcd(Pt5, aesc, de)(wir_r, a, b);
}
}
#if 0
u2_weak
j2_mcd(Pt5, aesc, ix)(u2_wire wir_r,
u2_atom a)
{
AES_KEY key_u;
c3_w out_w[4 * (AES_MAXNR + 1)];
// TODO
}
u2_weak
j2_mc(Pt5, aesc, ix)(u2_wire wir_r,
u2_noun cor)
{
u2_atom a;
if ( u2_no == u2_mean(cor, u2_cv_sam, &a, 0) ||
u2_no == u2_stud(a) )
{
return u2_bl_bail(wir_r, c3__exit);
}
else {
return j2_mcd(Pt5, aesc, ix)(wir_r, a);
}
}
#endif
/* structures
*/
u2_ho_jet
j2_mcj(Pt5, aesc, be)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, be), Tier5, u2_none, u2_none },
{ }
};
u2_ho_jet
j2_mcj(Pt5, aesc, en)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, en), Tier5, u2_none, u2_none },
{ }
};
u2_ho_jet
j2_mcj(Pt5, aesc, ex)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, ex), Tier5, u2_none, u2_none },
j2_mcj(Pt5, aesc, de)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, de), Tier5, u2_none, u2_none },
{ }
};
u2_ho_jet
j2_mcj(Pt5, aesc, in)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, in), Tier5, u2_none, u2_none },
{ }
};
#if 0
u2_ho_jet
j2_mcj(Pt5, aesc, ix)[] = {
{ ".2", c3__lite, j2_mc(Pt5, aesc, ix), Tier5, u2_none, u2_none },
{ }
};
#endif
u2_ho_driver
j2_mbd(Pt5, aesc)[] = {
{ j2_sc(Pt5, aesc, be), j2_mcj(Pt5, aesc, be), 0, 0, u2_none },
{ j2_sc(Pt5, aesc, en), j2_mcj(Pt5, aesc, en), 0, 0, u2_none },
{ j2_sc(Pt5, aesc, ex), j2_mcj(Pt5, aesc, ex), 0, 0, u2_none },
{ j2_sc(Pt5, aesc, in), j2_mcj(Pt5, aesc, in), 0, 0, u2_none },
#if 0
{ j2_sc(Pt5, aesc, ix), j2_mcj(Pt5, aesc, ix), 0, 0, u2_none },
#endif
{ j2_sc(Pt5, aesc, de), j2_mcj(Pt5, aesc, de), 0, 0, u2_none },
{ }
};