forked from 61bcdefg/Hikari-LLVM15-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubstituteImpl.cpp
530 lines (484 loc) · 23.2 KB
/
SubstituteImpl.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Obfuscation/SubstituteImpl.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/Transforms/Obfuscation/CryptoUtils.h"
using namespace llvm;
#define NUMBER_ADD_SUBST 7
#define NUMBER_SUB_SUBST 6
#define NUMBER_AND_SUBST 6
#define NUMBER_OR_SUBST 6
#define NUMBER_XOR_SUBST 6
#define NUMBER_MUL_SUBST 2
static void addNeg(BinaryOperator *bo);
static void addDoubleNeg(BinaryOperator *bo);
static void addRand(BinaryOperator *bo);
static void addRand2(BinaryOperator *bo);
static void addSubstitution(BinaryOperator *bo);
static void addSubstitution2(BinaryOperator *bo);
static void addSubstitution3(BinaryOperator *bo);
static void subNeg(BinaryOperator *bo);
static void subRand(BinaryOperator *bo);
static void subRand2(BinaryOperator *bo);
static void subSubstitution(BinaryOperator *bo);
static void subSubstitution2(BinaryOperator *bo);
static void subSubstitution3(BinaryOperator *bo);
static void andSubstitution(BinaryOperator *bo);
static void andSubstitution2(BinaryOperator *bo);
static void andSubstitution3(BinaryOperator *bo);
static void andSubstitutionRand(BinaryOperator *bo);
static void andNor(BinaryOperator *bo);
static void andNand(BinaryOperator *bo);
static void orSubstitution(BinaryOperator *bo);
static void orSubstitution2(BinaryOperator *bo);
static void orSubstitution3(BinaryOperator *bo);
static void orSubstitutionRand(BinaryOperator *bo);
static void orNor(BinaryOperator *bo);
static void orNand(BinaryOperator *bo);
static void xorSubstitution(BinaryOperator *bo);
static void xorSubstitution2(BinaryOperator *bo);
static void xorSubstitution3(BinaryOperator *bo);
static void xorSubstitutionRand(BinaryOperator *bo);
static void xorNor(BinaryOperator *bo);
static void xorNand(BinaryOperator *bo);
static void mulSubstitution(BinaryOperator *bo);
static void mulSubstitution2(BinaryOperator *bo);
static void (*funcAdd[NUMBER_ADD_SUBST])(BinaryOperator *bo) = {
&addNeg, &addDoubleNeg, &addRand, &addRand2,
&addSubstitution, &addSubstitution2, &addSubstitution3};
static void (*funcSub[NUMBER_SUB_SUBST])(BinaryOperator *bo) = {
&subNeg, &subRand, &subRand2,
&subSubstitution, &subSubstitution2, &subSubstitution3};
static void (*funcAnd[NUMBER_AND_SUBST])(BinaryOperator *bo) = {
&andSubstitution, &andSubstitution2, &andSubstitution3,
&andSubstitutionRand, &andNor, &andNand};
static void (*funcOr[NUMBER_OR_SUBST])(BinaryOperator *bo) = {
&orSubstitution, &orSubstitution2, &orSubstitution3,
&orSubstitutionRand, &orNor, &orNand};
static void (*funcXor[NUMBER_XOR_SUBST])(BinaryOperator *bo) = {
&xorSubstitution, &xorSubstitution2, xorSubstitution3,
&xorSubstitutionRand, &xorNor, &xorNand};
static void (*funcMul[NUMBER_MUL_SUBST])(BinaryOperator *bo) = {
&mulSubstitution, &mulSubstitution2};
void SubstituteImpl::substituteAdd(BinaryOperator *bo) {
(*funcAdd[cryptoutils->get_range(NUMBER_ADD_SUBST)])(bo);
}
void SubstituteImpl::substituteSub(BinaryOperator *bo) {
(*funcSub[cryptoutils->get_range(NUMBER_SUB_SUBST)])(bo);
}
void SubstituteImpl::substituteAnd(BinaryOperator *bo) {
(*funcAnd[cryptoutils->get_range(NUMBER_AND_SUBST)])(bo);
}
void SubstituteImpl::substituteOr(BinaryOperator *bo) {
(*funcOr[cryptoutils->get_range(NUMBER_OR_SUBST)])(bo);
}
void SubstituteImpl::substituteXor(BinaryOperator *bo) {
(*funcXor[cryptoutils->get_range(NUMBER_XOR_SUBST)])(bo);
}
void SubstituteImpl::substituteMul(BinaryOperator *bo) {
(*funcMul[cryptoutils->get_range(NUMBER_MUL_SUBST)])(bo);
}
// Implementation of ~(a | b) and ~a & ~b
static BinaryOperator *buildNor(Value *a, Value *b, Instruction *insertBefore) {
switch (cryptoutils->get_range(2)) {
case 0: {
// ~(a | b)
BinaryOperator *op =
BinaryOperator::Create(Instruction::Or, a, b, "", insertBefore);
op = BinaryOperator::CreateNot(op, "", insertBefore);
return op;
}
case 1: {
// ~a & ~b
BinaryOperator *nota = BinaryOperator::CreateNot(a, "", insertBefore);
BinaryOperator *notb = BinaryOperator::CreateNot(b, "", insertBefore);
BinaryOperator *op =
BinaryOperator::Create(Instruction::And, nota, notb, "", insertBefore);
return op;
}
default:
llvm_unreachable("wtf?");
}
}
// Implementation of ~(a & b) and ~a | ~b
static BinaryOperator *buildNand(Value *a, Value *b,
Instruction *insertBefore) {
switch (cryptoutils->get_range(2)) {
case 0: {
// ~(a & b)
BinaryOperator *op =
BinaryOperator::Create(Instruction::And, a, b, "", insertBefore);
op = BinaryOperator::CreateNot(op, "", insertBefore);
return op;
}
case 1: {
// ~a | ~b
BinaryOperator *nota = BinaryOperator::CreateNot(a, "", insertBefore);
BinaryOperator *notb = BinaryOperator::CreateNot(b, "", insertBefore);
BinaryOperator *op =
BinaryOperator::Create(Instruction::Or, nota, notb, "", insertBefore);
return op;
}
default:
llvm_unreachable("wtf?");
}
}
// Implementation of a = b - (-c)
static void addNeg(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = -(-b + (-c))
static void addDoubleNeg(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::CreateNeg(bo->getOperand(0), "", bo);
BinaryOperator *op2 = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, op2, "", bo);
op = BinaryOperator::CreateNeg(op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b + r; a = a + c; a = a - r
static void addRand(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), co, "", bo);
op = BinaryOperator::Create(Instruction::Add, op, bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, co, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b - r; a = a + b; a = a + r
static void addRand2(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), co, "", bo);
op = BinaryOperator::Create(Instruction::Add, op, bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b + c => a = b - ~c - 1
static void addSubstitution(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 1);
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op1 = BinaryOperator::CreateNeg(co, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, op1, "", bo);
op = BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b + c => a = (b | c) + (b & c)
static void addSubstitution2(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::Create(
Instruction::And, bo->getOperand(0), bo->getOperand(1), "", bo);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Or, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b + c => a = (b ^ c) + (b & c) * 2
static void addSubstitution3(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 2);
BinaryOperator *op = BinaryOperator::Create(
Instruction::And, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Mul, op, co, "", bo);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op1, op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b + (-c)
static void subNeg(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, bo->getOperand(0), op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b + r; a = a - c; a = a - r
static void subRand(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), co, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, co, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b - r; a = a - c; a = a + r
static void subRand2(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), co, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b - c => a = (b & ~c) - (~b & c)
static void subSubstitution(BinaryOperator *bo) {
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
BinaryOperator *op =
BinaryOperator::Create(Instruction::And, op1, bo->getOperand(1), "", bo);
op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op2 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op1, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op2, op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b - c => a = (2 * (b & ~c)) - (b ^ c)
static void subSubstitution2(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 2);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::And, bo->getOperand(0), op, "", bo);
op = BinaryOperator::Create(Instruction::Mul, co, op, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b - c => a = b + ~c + 1
static void subSubstitution3(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 1);
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), op1, "", bo);
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b & c => a = (b ^ ~c) & b
static void andSubstitution(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op1 =
BinaryOperator::Create(Instruction::Xor, bo->getOperand(0), op, "", bo);
op = BinaryOperator::Create(Instruction::And, op1, bo->getOperand(0), "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b & c => a = (b | c) & ~(b ^ c)
static void andSubstitution2(BinaryOperator *bo) {
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
op1 = BinaryOperator::CreateNot(op1, "", bo);
BinaryOperator *op = BinaryOperator::Create(
Instruction::Or, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::And, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b & c => a = (~b | c) + (b + 1)
static void andSubstitution3(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 1);
BinaryOperator *op1 =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), co, "", bo);
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
op = BinaryOperator::Create(Instruction::Or, op, bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a & b <=> ~(~a | ~b) & (r | ~r)
static void andSubstitutionRand(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *opr = BinaryOperator::CreateNot(co, "", bo);
BinaryOperator *opa =
BinaryOperator::Create(Instruction::Or, op, op1, "", bo);
opr = BinaryOperator::Create(Instruction::Or, co, opr, "", bo);
op = BinaryOperator::CreateNot(opa, "", bo);
op = BinaryOperator::Create(Instruction::And, op, opr, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a & b => Nor(Nor(a, a), Nor(b, b))
static void andNor(BinaryOperator *bo) {
BinaryOperator *noraa = buildNor(bo->getOperand(0), bo->getOperand(0), bo);
BinaryOperator *norbb = buildNor(bo->getOperand(1), bo->getOperand(1), bo);
bo->replaceAllUsesWith(buildNor(noraa, norbb, bo));
}
// Implementation of a = a & b => Nand(Nand(a, b), Nand(a, b))
static void andNand(BinaryOperator *bo) {
BinaryOperator *nandab = buildNand(bo->getOperand(0), bo->getOperand(1), bo);
BinaryOperator *nandab2 = buildNand(bo->getOperand(0), bo->getOperand(1), bo);
bo->replaceAllUsesWith(buildNand(nandab, nandab2, bo));
}
// Implementation of a = a | b => a = (b & c) | (b ^ c)
static void orSubstitution(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::Create(
Instruction::And, bo->getOperand(0), bo->getOperand(1), "", bo);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a | b => a = (b + (b ^ c)) - (b & ~c)
static void orSubstitution2(BinaryOperator *bo) {
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op1, "", bo);
BinaryOperator *op = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, bo->getOperand(0), op, "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a | b => a = (b + c + 1) + ~(c & b)
static void orSubstitution3(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 1);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::And, bo->getOperand(1), bo->getOperand(0), "", bo);
op1 = BinaryOperator::CreateNot(op1, "", bo);
BinaryOperator *op = BinaryOperator::Create(
Instruction::Add, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo);
op = BinaryOperator::Create(Instruction::Add, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b | c => a = (((~a & r) | (a & ~r)) ^ ((~b & r) | (b &
// ~r))) | (~(~a | ~b) & (r | ~r))
static void orSubstitutionRand(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op2 = BinaryOperator::CreateNot(co, "", bo);
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::And, op, co, "", bo);
BinaryOperator *op4 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op2, "", bo);
BinaryOperator *op5 =
BinaryOperator::Create(Instruction::And, op1, co, "", bo);
BinaryOperator *op6 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), op2, "", bo);
op3 = BinaryOperator::Create(Instruction::Or, op3, op4, "", bo);
op4 = BinaryOperator::Create(Instruction::Or, op5, op6, "", bo);
op5 = BinaryOperator::Create(Instruction::Xor, op3, op4, "", bo);
op3 = BinaryOperator::Create(Instruction::Or, op, op1, "", bo);
op3 = BinaryOperator::CreateNot(op3, "", bo);
op4 = BinaryOperator::Create(Instruction::Or, co, op2, "", bo);
op4 = BinaryOperator::Create(Instruction::And, op3, op4, "", bo);
op = BinaryOperator::Create(Instruction::Or, op5, op4, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a | b => a = Nor(Nor(a, b), Nor(a, b))
static void orNor(BinaryOperator *bo) {
BinaryOperator *norab = buildNor(bo->getOperand(0), bo->getOperand(1), bo);
BinaryOperator *norab2 = buildNor(bo->getOperand(0), bo->getOperand(1), bo);
BinaryOperator *op = buildNor(norab, norab2, bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a | b => a = Nand(Nand(a, a), Nand(b, b))
static void orNand(BinaryOperator *bo) {
BinaryOperator *nandaa = buildNand(bo->getOperand(0), bo->getOperand(0), bo);
BinaryOperator *nandbb = buildNand(bo->getOperand(1), bo->getOperand(1), bo);
BinaryOperator *op = buildNand(nandaa, nandbb, bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b => a = (~a & b) | (a & ~b)
static void xorSubstitution(BinaryOperator *bo) {
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
op = BinaryOperator::Create(Instruction::And, bo->getOperand(1), op, "", bo);
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op1, "", bo);
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b => a = (b + c) - 2 * (b & c)
static void xorSubstitution2(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 2);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::And, bo->getOperand(0), bo->getOperand(1), "", bo);
op1 = BinaryOperator::Create(Instruction::Mul, co, op1, "", bo);
BinaryOperator *op = BinaryOperator::Create(
Instruction::Add, bo->getOperand(0), bo->getOperand(1), "", bo);
op = BinaryOperator::Create(Instruction::Sub, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b => a = b - (2 * (c & ~(b ^ c)) - c)
static void xorSubstitution3(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(bo->getType(), 2);
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo);
op1 = BinaryOperator::CreateNot(op1, "", bo);
op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), op1, "", bo);
op1 = BinaryOperator::Create(Instruction::Mul, co, op1, "", bo);
op1 =
BinaryOperator::Create(Instruction::Sub, op1, bo->getOperand(1), "", bo);
BinaryOperator *op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b <=> (a ^ r) ^ (b ^ r) <=> (~a & r | a & ~r) ^ (~b
// & r | b & ~r) note : r is a random number
static void xorSubstitutionRand(BinaryOperator *bo) {
ConstantInt *co = (ConstantInt *)ConstantInt::get(
bo->getType(), cryptoutils->get_uint64_t());
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
op = BinaryOperator::Create(Instruction::And, co, op, "", bo);
BinaryOperator *opr = BinaryOperator::CreateNot(co, "", bo);
BinaryOperator *op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), opr, "", bo);
BinaryOperator *op2 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
op2 = BinaryOperator::Create(Instruction::And, op2, co, "", bo);
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), opr, "", bo);
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo);
op1 = BinaryOperator::Create(Instruction::Or, op2, op3, "", bo);
op = BinaryOperator::Create(Instruction::Xor, op, op1, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b => a = Nor(Nor(Nor(a, a), Nor(b, b)), Nor(a, b))
static void xorNor(BinaryOperator *bo) {
BinaryOperator *noraa = buildNor(bo->getOperand(0), bo->getOperand(0), bo);
BinaryOperator *norbb = buildNor(bo->getOperand(1), bo->getOperand(1), bo);
BinaryOperator *nornoraanorbb = buildNor(noraa, norbb, bo);
BinaryOperator *norab = buildNor(bo->getOperand(0), bo->getOperand(1), bo);
BinaryOperator *op = buildNor(nornoraanorbb, norab, bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = a ^ b => a = Nand(Nand(Nand(a, a), b), Nand(a, Nand(b,
// b)))
static void xorNand(BinaryOperator *bo) {
BinaryOperator *nandaa = buildNand(bo->getOperand(0), bo->getOperand(0), bo);
BinaryOperator *nandnandaab = buildNand(nandaa, bo->getOperand(1), bo);
BinaryOperator *nandbb = buildNand(bo->getOperand(1), bo->getOperand(1), bo);
BinaryOperator *nandanandbb = buildNand(bo->getOperand(0), nandbb, bo);
BinaryOperator *op = buildNand(nandnandaab, nandanandbb, bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b * c => a = (((b | c) * (b & c)) + ((b & ~c) * (c &
// ~b)))
static void mulSubstitution(BinaryOperator *bo) {
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(0), "", bo);
op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), op1, "", bo);
BinaryOperator *op2 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
op2 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op2, "", bo);
BinaryOperator *op =
BinaryOperator::Create(Instruction::Mul, op2, op1, "", bo);
op1 = BinaryOperator::Create(Instruction::And, bo->getOperand(0),
bo->getOperand(1), "", bo);
op2 = BinaryOperator::Create(Instruction::Or, bo->getOperand(0),
bo->getOperand(1), "", bo);
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::Mul, op2, op1, "", bo);
op = BinaryOperator::Create(Instruction::Add, op3, op, "", bo);
bo->replaceAllUsesWith(op);
}
// Implementation of a = b * c => a = (((b | c) * (b & c)) + ((~(b | ~c)) * (b &
// ~c)))
static void mulSubstitution2(BinaryOperator *bo) {
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo);
BinaryOperator *op2 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op1, "", bo);
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::Or, bo->getOperand(0), op1, "", bo);
op3 = BinaryOperator::CreateNot(op3, "", bo);
op3 = BinaryOperator::Create(Instruction::Mul, op3, op2, "", bo);
BinaryOperator *op4 = BinaryOperator::Create(
Instruction::And, bo->getOperand(0), bo->getOperand(1), "", bo);
BinaryOperator *op5 = BinaryOperator::Create(
Instruction::Or, bo->getOperand(0), bo->getOperand(1), "", bo);
op5 = BinaryOperator::Create(Instruction::Mul, op5, op4, "", bo);
BinaryOperator *op =
BinaryOperator::Create(Instruction::Add, op5, op3, "", bo);
bo->replaceAllUsesWith(op);
}