aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/tests/performance/06_crypto-1.sy
blob: 337baf1ca68ff3a1581e3982b79b9f9e7e25755e (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


int state = 19260817;

int get_random() {
  state = state + (state * 8192);
  state = state + (state / 131072);
  state = state + (state * 32);
  return state;
}

int rotl1(int x) {
  return x * 2 + x % 2;
}

int rotl5(int x) {
  return x * 32 + x % 32;
}

int rotl30(int x) {
  return x * 1073741824 + x % 1073741824;
}

int _and(int a, int b) {
  // int c = 0;
  // int x = 0;
  // while (x <= 32) {
  //   c = c + c;
  //   if (a < 0) {
  //     if (b < 0) {
  //       c = c + 1;
  //     }
  //   }
  //   a = a + a;
  //   b = b + b;
  //   x = x + 1;
  // }
  // return c;
  return a + b;
}

int _not(int x) {
  return -1 - x;
}

int _xor(int a, int b) {
  return a - _and(a, b) + b - _and(a, b);
}

int _or(int a, int b) {
  return _xor(_xor(a, b), _and(a, b));
}

// performs a SHA-1 hash to output.
//
// input is a list of ints, but only the lower 8 bits are used. Input must be
// larger than the smallest 64 multiple.
void pseudo_sha1(int input[], int input_len, int output[]) {
  int h0 = 1732584193;
  int h1 = -271733879;
  int h2 = -1732584194;
  int h3 = 271733878;
  int h4 = -1009589776;

  int a;
  int b;
  int c;
  int d;
  int e;
  int f;
  int k;

  // preprocessing
  int orig_len = input_len;
  input[input_len] = 0x80;
  input_len = input_len + 1;
  while (input_len % 64 != 60) {
    input[input_len] = 0;
    input_len = input_len + 1;
  }
  input[input_len] = orig_len / 16777216 % 256;
  input[input_len + 1] = orig_len / 65536 % 256;
  input[input_len + 2] = orig_len / 256 % 256;
  input[input_len + 3] = orig_len % 256;
  input_len = input_len + 4;

  int chunk_start = 0;
  int words[80] = {0};
  while (chunk_start < input_len) {
    a = h0;
    b = h1;
    c = h2;
    d = h3;
    e = h4;

    // populate words
    int i = 0;
    while (i < 16) {
      words[i] = input[chunk_start + i * 4] * 16777216 +
                 input[chunk_start + i * 4 + 1] * 65536 +
                 input[chunk_start + i * 4 + 2] * 256 +
                 input[chunk_start + i * 4 + 3] * 1;
      i = i + 1;
    }
    while (i < 80) {
      words[i] =
          rotl1(_xor(_xor(_xor(words[i - 3], words[i - 8]), words[i - 14]),
                     words[i - 16]));
      i = i + 1;
    }

    // main loop
    i = 0;
    while (i < 80) {
      if (i < 20) {
        f = _or(_and(b, c), _and(_not(b), d));
        k = 1518500249;
      } else if (i < 40) {
        f = _xor(_xor(b, c), d);
        k = 1859775361;
      } else if (i < 60) {
        f = _or(_or(_and(b, c), _and(b, d)), _and(c, d));
        k = -1894007588;
      } else if (i < 80) {
        f = _xor(_xor(b, c), d);
        k = -899497722;
      }
      int t = rotl5(a) + f + e + k + words[i];
      e = d;
      d = c;
      c = rotl30(b);
      b = a;
      a = t;
      i = i + 1;
    }
    h0 = h0 + a;
    h1 = h1 + b;
    h2 = h2 + c;
    h3 = h3 + d;
    h4 = h4 + e;

    chunk_start = chunk_start + 64;
  }
  output[0] = h0;
  output[1] = h1;
  output[2] = h2;
  output[3] = h3;
  output[4] = h4;
}

int buffer[32768] = {0};

int main() {
  int rounds = 12;
  int i = 0;
  int output[5] = {0, 0, 0, 0, 0};
  int output_buf[5];
  state = getint();
  rounds = getint();

  starttime();

  output_buf[0] = 0;
  output_buf[1] = 0;
  output_buf[2] = 0;
  output_buf[3] = 0;
  output_buf[4] = 0;

  while (rounds > 0) {
    int len = 32000;
    int i = 0;
    while (i < len) {
      buffer[i] = get_random() % 256;
      i = i + 1;
    }
    pseudo_sha1(buffer, len, output);
    i = 0;
    while (i < 5) {
      output_buf[i] = _xor(output_buf[i], output[i]);
      i = i + 1;
    }
    rounds = rounds - 1;
  }
  stoptime();

  putarray(5, output_buf);
  return 0;
}