summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/jets/e/shax.c
blob: 856d0faa3d350b441616b83ce1ffc35d49f6ff0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/// @file

#include "jets/k.h"
#include "jets/q.h"
#include "jets/w.h"

#include "noun.h"
#include "urcrypt.h"


  static u3_atom
  _cqe_shay(u3_atom wid,
            u3_atom dat)
  {
    c3_w len_w;
    if ( !u3r_word_fit(&len_w, wid) ) {
      return u3m_bail(c3__fail);
    }
    else {
      c3_y  out_y[32];
      c3_y* dat_y = u3r_bytes_alloc(0, len_w, dat);
      urcrypt_shay(dat_y, len_w, out_y);
      u3a_free(dat_y);
      return u3i_bytes(32, out_y);
    }
  }

  static u3_atom
  _cqe_shax(u3_atom a)
  {
    c3_w  len_w;
    c3_y  out_y[32];
    c3_y* dat_y = u3r_bytes_all(&len_w, a);
    urcrypt_shay(dat_y, len_w, out_y);
    u3a_free(dat_y);
    return u3i_bytes(32, out_y);
  }

  static u3_atom
  _cqe_shal(u3_atom wid,
            u3_atom dat)
  {
    c3_w len_w;
    if ( !u3r_word_fit(&len_w, wid) ) {
      return u3m_bail(c3__fail);
    }
    else {
      c3_y  out_y[64];
      c3_y* dat_y = u3r_bytes_alloc(0, len_w, dat);
      urcrypt_shal(dat_y, len_w, out_y);
      u3a_free(dat_y);
      return u3i_bytes(64, out_y);
    }
  }

  static u3_atom
  _cqe_shas(u3_atom sal,
            u3_atom ruz)
  {
    c3_w sal_w, ruz_w;
    c3_y *sal_y, *ruz_y, out_y[32];

    sal_y = u3r_bytes_all(&sal_w, sal);
    ruz_y = u3r_bytes_all(&ruz_w, ruz);
    urcrypt_shas(sal_y, sal_w, ruz_y, ruz_w, out_y);
    u3a_free(sal_y);
    u3a_free(ruz_y);
    return u3i_bytes(32, out_y);
  }

  u3_noun
  u3we_shax(u3_noun cor)
  {
    u3_noun a;

    if ( (u3_none == (a = u3r_at(u3x_sam, cor))) ||
         (c3n == u3ud(a)) )
    {
      return u3m_bail(c3__exit);
    } else {
      return _cqe_shax(a);
    }
  }

  u3_noun
  u3we_shay(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 == u3ud(b)) )
    {
      return u3m_bail(c3__exit);
    } else {
      return _cqe_shay(a, b);
    }
  }

  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 == u3ud(b)) )
    {
      return u3m_bail(c3__exit);
    } else {
      return _cqe_shal(a, b);
    }
  }

  u3_noun
  u3we_shas(u3_noun cor)
  {
    u3_noun sal, ruz;

    if ( (u3_none == (sal = u3r_at(u3x_sam_2, cor))) ||
         (u3_none == (ruz = u3r_at(u3x_sam_3, cor))) ||
         (c3n == u3ud(sal)) ||
         (c3n == u3ud(ruz)) )
    {
      return u3m_bail(c3__exit);
    } else {
      return _cqe_shas(sal, ruz);
    }
  }

  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 = _cqe_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);
  }

  u3_noun
  u3qeo_raw(u3_atom a,
            u3_atom b)
  {
    u3_noun x = u3qc_mix(b, a);
    u3_noun c = _cqe_shas(c3_s4('o','g','-','a'), x);
    u3_noun l = _og_list(a, b, c);
    u3_noun r = u3qc_can(0, l);

    u3z(l);
    u3z(c);
    u3z(x);

    return r;
  }

  u3_noun
  u3weo_raw(u3_noun cor)
  {
    u3_noun a, b;

    if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) ) {
      return u3m_bail(c3__exit);
    } else {
      return u3qeo_raw(a, b);
    }
  }