-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.hpp
More file actions
2909 lines (2623 loc) · 105 KB
/
Program.hpp
File metadata and controls
2909 lines (2623 loc) · 105 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef PROGRAM_H
#define PROGRAM_H
#include <cassert> // assert()
#include <cstdint> // int_t
#include <iomanip> // set precision
#include <iostream> // default precision
#include <istream> // for bytecode
#include <random> // random bit
#include <stack> // stack for thread local ints (obsolete)
#include <string> // bytecode path
#include <string_view> // cisc instruction
#include <utility> // move
#include <vector> // register
#include "Constants.hpp"
#include "Shares/CInteger.hpp"
#include "Shares/Integer.hpp"
#include "help/Conv.hpp"
#include "help/Input.hpp"
#include "help/Util.hpp"
#include "../../programs/functions/comparisons.hpp"
#include "../../programs/functions/prob_truncation.hpp"
#ifndef S_TRUNC_PR
#if TRUNC_APPROACH == 1
#define S_TRUNC_PR trunc_2k_in_place
#else
#define S_TRUNC_PR trunc_pr_in_place
#endif
#endif
#define IS_ONLINE (current_phase == PHASE_LIVE && PRINT_IMPORTANT)
using std::string;
using std::vector;
namespace IR {
template <class int_t, class cint, class Share, class sint, template <int, class> class sbit,
class BitShare, int N>
class Machine;
template <class int_t, class cint, class Share, class sint, template <int, class> class sbit,
class BitShare, int N = 64>
class Program {
public:
#if BITLENGTH == 64
using BitType = cint;
#else
using BitType = int_t;
#endif
using IntType = int_t; // 64-bit integer (always IR::Integer<int64_t, uint64_t>)
/**
* Clear integer type with `BITLENGTH`-bit length (always IR::CInteger<INT_TYPE, UINT_TYPE>)
*/
using ClearIntType = cint;
static constexpr size_t BIT_LEN = N; // size of boolean registers should always be 64
/**
* Represents a MP-SPDZ instruction where parameters are stored in <regs>
*/
class Instruction {
public:
/**
* @param op an integer that represents an instruction opcode as defined by MP-SPDZ
* @param vec vectorization mostly 1 except for instructions that support operations
*/
explicit Instruction(const uint32_t& op, const int& vec);
/**
* Performs execution
* @param p program that provides the registers this instruction may use
* @param m machine provides memory cells instruction may use for execution
* @param pc program counter for instructions as JMP/JMPNZ etc.
*/
void execute(Program& p, Machine<int_t, cint, Share, sint, sbit, BitShare, N>& m,
size_t& pc) const;
const Opcode& get_opcode() const { return op; }
const size_t& set_immediate(const int64_t& im) {
n = im;
return n;
}
const int& add_reg(const int& reg) { return regs.emplace_back(reg); }
bool is_gf2n() const { return (static_cast<unsigned>(op) & 0x100) != 0; }
/**
* @return Returns the share type (sint,cint,int,...) this instructions operates on
* @warning Might not be defined for all instructions or instructions that operate on
* multiple types
*/
Type get_reg_type(const Opcode& op) const;
inline unsigned get_size() const { return size; }
string cisc; // for cisc command (LTZ, EQZ, ...)
private:
Opcode op; // opcode
unsigned size; // vectorized
size_t n; // immediate (some functions require a constant)
vector<int> regs; // required addresses in order given
};
public:
explicit Program(const string&& path, size_t thread);
Program(const Program& other) = delete;
Program(Program&& other) = default;
Program& operator=(const Program& other) = delete;
Program& operator=(Program&& other) = default;
/**
* Read bytecode from `path` and store each instruction in `prog`
* - also updates the max. register size required for each type
*
* @param m reference to machine to update the maximum memory address required
* @return
* - `true` if successful
* @return
* - otherwise `false`
*/
bool
load_program(Machine<int_t, cint, Share, sint, sbit, BitShare, N>& m); // parse bytecode file
/**
* Allocates the registers required during execution
*/
void setup(); // parse bytecode file
/**
* Runs the program by executing every instruction in <prog> and storing the results in the
* corresponding regiserts/memory cells
*
* @param m machine that started this program
* @param arg as defined by MP-SPDZ a thread might take an argument
* @param t_num thread number of this thread (0/1)
*/
void run(Machine<int_t, cint, Share, sint, sbit, BitShare, N>& m, const int& arg,
const int& t_num); // execute all instructions
inline int get_argument() const { return arg; }
void set_argument(const int& a) { arg = a; }
/**
* Reveals secret shares to all parties (moves secret shares into clear integer registers)
*
* @param regs parameters as defined by MP-SPDZ (alternatively see `load_program`)
* @param size to reaveal secret share vectors of size `size`
*/
void popen(const vector<int>& regs, const size_t& size);
/**
* `muls` as defined by MP-SPDZ -> secret share multiplication (arithm.)
*
* @param regs parameters as defined by MP-SPDZ (alternatively see `load_program`)
*/
void muls(const vector<int>& regs);
/**
* `mulm` as defined by MP-SPDZ -> secret share multiplication with public value (arithm.)
*
* @param regs parameters as defined by MP-SPDZ (alternatively see `load_program`)
* @param vec vector size
*/
void mulm(const vector<int>& regs, const size_t& vec);
/**
* Performs XOR on a set of arithmetic shares
*
* @param x, y, res have the same size
* @param x first parameter
* @param y seconde parameter
* @param res result for arithmetic XOR on shares where result[i] = x[i] xor y[i]
*/
void xor_arith(const vector<sint>& x, const vector<sint>& y, vector<sint>& res);
/**
* Performs dot product on secret arithmetic shares as defined by MP-SPDZ and stores result in
* the proper register
*
* @param regs parameters as defined by MP-SPDZ
* @param size vecotrization for multiple dot products
*/
void dotprods(const vector<int>& regs, const size_t& size);
/**
* `inputmixed` as defined by MP-SPDZ reads input from <input-file> to secret registers
*
* @param regs paramters for `ìnputmixed` as defined by MP-SPDZ (alternatively see
* `load_program`)
* @param vec vectorization to load vector of size <vec> into secret registers
*/
void inputmixed(const vector<int>& regs, const bool from_reg, const size_t& vec);
/**
* `fixinput` as defined by MP-SPDZ reads input from <input-file> to secret registers but reads
* input as a string opposed to bytes which is slower
*
* @param regs paramters for `ìnputmixed` as defined by MP-SPDZ (alternatively see
* `load_program`)
* @param vec vectorization to load vector of size <vec> into secret registers
*/
void fixinput(const vector<int>& regs, const size_t& vec);
/**
* `matmulsm` as defined by MP-SPDZ -> matrix multiplication on memory cells
*
* @param regs parameters for `matmulsm` as defined by MP-SPDZ (alternatively see
* `load_program`)
* @param m reference to machine that started this program
*/
void matmulsm(const vector<int>& regs, Machine<int_t, cint, Share, sint, sbit, BitShare, N>& m);
/**
* Matrix multiplication on local registers
*
* @param regs parameters as defined by MP-SPDZ
*/
void matmuls(const vector<int>& regs);
/**
* Helper method for `matmulsm`: performs dot product required for matrix
* multiplication
*
* @param regs parameters for `matmulsm` as defined by MP-SPDZ (alternatively see
* `load_program`)
* @param row_1 current row of first factor
* @param j current column of first factor/current row of second factor
* @param source1 first factor
* @param source2 second factor
*/
template <class iterator>
void matmulsm_prepare(const vector<int>& regs, const int& row_1, const int& j, iterator source1,
iterator source2);
/**
* Same as <https://github.com/data61/MP-SPDZ/blob/master/Processor/Processor.hpp#L684> but not
* optimized for memory space
*
* @param regs parameters as defined by MP-SPDZ
*/
void conv2ds(const vector<int>& regs);
/**
* For MP-SPDZ complex instructions set (CISC)
* - currently supported LTZ and EQZ
*
* @param regs parameters for the respective CISC instruction as defined by MP-SPDZ
* (alternatively see `load_program`)
* @param cisc name of the instruction
*/
void cisc(const vector<int>& regs, const std::string_view cisc);
/**
* `inputbvec` as defined by MP-SPDZ -> reads integers into secret boolean shares (`XOR_Shares`)
*
* @param regs parameters for `inputbvec` as defined by MP-SPDZ (alternatively see
* `load_program`)
*/
void inputbvec(const vector<int>& regs);
/**
* `inputb` as defined by MP-SPDZ -> reads bits into secret boolean shares (`XOR_Shares`)
* @param regs parameters for `inputb` as defined by MP-SPDZ (alternatively see `load_program`)
*/
void inputb(const vector<int>& regs);
/**
* `andrsvec` as defined by MP-SPDZ -> secret vector AND with a constant factor
*
* @param regs parameters for `inputb` as defined by MP-SPDZ (alternatively see `load_program`)
*/
void andrsvec(const vector<int>& regs);
private:
int precision; // used for printing
const string path; // path to bytecode file
size_t thread_id; // should be 0/1 as multithreading is not supported
int arg; // thread arg
int thread_num; // same as thread_id I think // TODO
vector<Instruction> prog; // all instructions
unsigned max_reg[REG_TYPES]; // stores max. register address for all types
vector<sint> s_register; // secret shares (`Additive_Share`)
vector<ClearIntType> c_register; // clear share
vector<IntType> i_register; // 64-bit integer
vector<sbitset_t<N, BitShare>> sb_register; // secret bits (`XOR_Shares`)
vector<BitType> cb_register; // clear bit
std::stack<IntType> i_stack; // stack MP-SPDZ declared obsolete
/**
* Updates the maximum register (`max_reg`) address for a specific type
*
* @param reg type of register effected
* @param sreg register address
* @param op added for debugging
*/
void update_max_reg(const Type& reg, const unsigned& sreg, const Opcode& op);
/**
* read 64-bit int from bytecode file (Big Endian)
* @param fd input stream to read from
*/
int64_t read_long(std::istream& fd) {
int64_t res = 0;
fd.read((char*)&res, 8);
return be64toh(res);
}
/**
* read 32-bit int from bytecode file (Big Endian)
* @param fd input stream to read from
*/
int32_t read_int(std::istream& fd) {
int32_t res = 0;
fd.read((char*)&res, 4);
return be32toh(res);
}
};
template <class int_t, class cint, class Share, class sint, template <int, class> class sbit,
class BitShare, int N>
bool Program<int_t, cint, Share, sint, sbit, BitShare, N>::load_program(
Machine<int_t, cint, Share, sint, sbit, BitShare, N>& m) {
std::ifstream fd(path, std::ios::in);
if (fd.fail()) {
log(Level::WARNING, "couldn't open file: ", path);
return false;
}
while (true) {
uint64_t num = read_long(fd);
if (fd.fail())
break;
int cur = 0x3ff & num;
size_t vec = num >> 10; // 1st 22bits for vectorized command
auto& inst = prog.emplace_back(cur, vec == 0 ? 1 : vec);
switch (inst.get_opcode()) {
// sreg + im(32)
case Opcode::LDSI:
case Opcode::JMPNZ:
case Opcode::JMPEQZ:
case Opcode::LDI:
case Opcode::LDINT:
case Opcode::RANDOMS:
case Opcode::COND_PRINT_STRB:
case Opcode::PRINTREG:
case Opcode::PRINTREGB:
case Opcode::PRINT4COND: {
unsigned sreg = inst.add_reg(read_int(fd)); // source
inst.set_immediate(read_int(fd)); // additional constant
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
break;
}
// sreg + mem_addr(64)
case Opcode::LDMS:
case Opcode::LDMC:
case Opcode::STMC:
case Opcode::LDMCB:
case Opcode::STMCB:
case Opcode::STMS:
case Opcode::STMINT:
case Opcode::LDMSB:
case Opcode::STMSB:
case Opcode::GLDMC:
case Opcode::GLDMS:
case Opcode::LDMINT: {
unsigned sreg = inst.add_reg(read_int(fd)); // dest
size_t mem_addr = inst.set_immediate(read_long(fd)); // source
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
m.update_max_mem(inst.get_reg_type(inst.get_opcode()), mem_addr + inst.get_size());
break;
}
// sreg + im(32) + im(32)
case Opcode::LDBITS: {
unsigned sreg = inst.add_reg(read_int(fd));
unsigned bits = inst.add_reg(read_int(fd));
inst.set_immediate(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + div_ceil(bits, BIT_LEN),
inst.get_opcode());
break;
}
case Opcode::XORS:
case Opcode::ANDS:
case Opcode::MULS: {
unsigned args = read_int(fd);
assert(args % 2 == 0);
for (size_t i = 1; i < args; i += 4) {
int size = inst.add_reg(read_int(fd)); // vector size
unsigned sreg = inst.add_reg(read_int(fd)); // destination
size = inst.get_opcode() == Opcode::MULS ? size : div_ceil(size, BIT_LEN);
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + size,
inst.get_opcode());
sreg = inst.add_reg(read_int(fd)); // factor 1
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + size,
inst.get_opcode());
sreg = inst.add_reg(read_int(fd)); // factor 2
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + size,
inst.get_opcode());
}
break;
}
// im(32) + sreg + sreg
case Opcode::NOTS: {
size_t bits = inst.set_immediate(read_int(fd));
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(bits, BIT_LEN), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(bits, BIT_LEN), inst.get_opcode());
break;
}
case Opcode::CONVCBIT2S: {
size_t bits = inst.set_immediate(read_int(fd));
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(bits, BIT_LEN), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CBIT, sreg + div_ceil(bits, BIT_LEN), inst.get_opcode());
break;
}
case Opcode::OPEN: {
uint32_t num = inst.set_immediate(read_int(fd));
read_int(fd); // check after opening (idk)
for (size_t i = 1; i < num; i += 2) {
unsigned creg = inst.add_reg(read_int(fd));
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, creg + inst.get_size(), inst.get_opcode());
update_max_reg(Type::SINT, sreg + inst.get_size(), inst.get_opcode());
}
break;
}
case Opcode::REVEAL: {
uint32_t num = read_int(fd);
for (size_t i = 0; i < num; i += 3) {
unsigned num = inst.add_reg(read_int(fd));
unsigned creg = inst.add_reg(read_int(fd)); // des
unsigned sreg = inst.add_reg(read_int(fd)); // source
update_max_reg(Type::CBIT, creg + div_ceil(num, BIT_LEN), inst.get_opcode());
update_max_reg(Type::SBIT, sreg + div_ceil(num, sizeof(sint) * 8),
inst.get_opcode());
}
break;
}
// im(32)
case Opcode::PRINT4:
case Opcode::JMP:
case Opcode::ACTIVE:
case Opcode::START:
case Opcode::STOP:
case Opcode::PRINT_CHR:
case Opcode::PRINT_FLOAT_PREC:
case Opcode::JOIN_TAPE:
inst.set_immediate(read_int(fd));
break;
case Opcode::REQBL: // requirement for modulus prime calculus
// min bit length
{
int ring = read_int(fd);
if (ring > 0) {
log(Level::ERROR, "compiled for fields not rings");
} else if (-ring != BITLENGTH) {
log(Level::ERROR, "Expected: ", -ring, " BUT compiled for rings 2^", BITLENGTH);
exit(EXIT_FAILURE);
}
break;
}
case Opcode::PRINT_FLOAT_PLAIN: {
assert(inst.get_size() == 1);
int reg = inst.add_reg(read_int(fd)); // significant
update_max_reg(Type::CINT, reg + inst.get_size(), inst.get_opcode());
reg = inst.add_reg(read_int(fd)); // exponent
update_max_reg(Type::CINT, reg + inst.get_size(), inst.get_opcode());
reg = inst.add_reg(read_int(fd)); // zero bit (zero if == 1)
update_max_reg(Type::CINT, reg + inst.get_size(), inst.get_opcode());
reg = inst.add_reg(read_int(fd)); // sign bit (neg if == 1)
update_max_reg(Type::CINT, reg + inst.get_size(), inst.get_opcode());
reg = inst.add_reg(read_int(fd)); // NaN (reg num if zero)
update_max_reg(Type::CINT, reg + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::FLOATOUTPUT:
inst.set_immediate(read_int(fd));
inst.add_reg(read_int(fd)); // significant
inst.add_reg(read_int(fd)); // exponent
inst.add_reg(read_int(fd)); // zero bit
inst.add_reg(read_int(fd)); // sign bit
break;
// reg
case Opcode::PRINT_REG_PLAIN:
case Opcode::PRINT_INT:
case Opcode::BIT:
case Opcode::JMPI:
case Opcode::CRASH:
case Opcode::NPLAYERS:
case Opcode::PUBINPUT:
case Opcode::THRESHOLD:
case Opcode::PLAYERID:
case Opcode::PUSHINT:
case Opcode::POPINT:
case Opcode::LDTN:
case Opcode::STARG:
case Opcode::LDARG: {
unsigned reg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), reg + inst.get_size(),
inst.get_opcode());
break;
}
// im(32) + reg
case Opcode::INTOUTPUT: {
unsigned im = inst.set_immediate(read_int(fd));
unsigned reg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), reg + inst.get_size(),
inst.get_opcode());
break;
}
case Opcode::PRINT_REG_SIGNED: {
unsigned im = inst.set_immediate(read_int(fd));
unsigned cbit = inst.add_reg(read_int(fd));
update_max_reg(Type::CBIT, cbit + div_ceil(im, BIT_LEN), inst.get_opcode());
break;
}
case Opcode::BITDECINT: {
unsigned args = read_int(fd);
unsigned source = inst.add_reg(read_int(fd)); // source
update_max_reg(Type::SINT, source + inst.get_size(), inst.get_opcode());
for (size_t i = 1; i < args; i++) {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
}
break;
}
case Opcode::CONCATS: {
unsigned args = read_int(fd);
unsigned dest = inst.add_reg(read_int(fd)); // dest
update_max_reg(Type::SINT, dest + 1, inst.get_opcode());
for (size_t i = 1; i < args; i += 2) {
unsigned off = inst.add_reg(read_int(fd)); // offset
unsigned sreg = inst.add_reg(read_int(fd));
dest += off;
update_max_reg(Type::SINT, sreg + off, inst.get_opcode());
update_max_reg(Type::SINT, dest, inst.get_opcode());
}
break;
}
case Opcode::TRANSPOSE: {
unsigned num = read_int(fd);
unsigned outs = inst.set_immediate(read_int(fd));
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(num - 1 - outs, BIT_LEN), inst.get_opcode());
for (size_t i = 2; i < num; ++i) {
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(outs, BIT_LEN), inst.get_opcode());
}
break;
}
case Opcode::PICKS: {
unsigned dest = inst.add_reg(read_int(fd));
unsigned source = inst.add_reg(read_int(fd));
unsigned off = inst.add_reg(read_int(fd));
int step(inst.set_immediate(read_int(fd)));
update_max_reg(Type::SINT, dest + inst.get_size(), inst.get_opcode());
update_max_reg(Type::SINT, source + off + step * vec + 1, inst.get_opcode());
break;
}
case Opcode::USE:
case Opcode::USE_INP: {
inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd));
inst.set_immediate(read_long(fd));
break;
}
case Opcode::FIXINPUT: {
inst.add_reg(read_int(fd)); // player-id
unsigned dest = inst.add_reg(read_int(fd)); // cint dest
inst.add_reg(read_int(fd)); // exponent
inst.add_reg(read_int(fd)); // input type (0 - int, 1 - float, 2 - double)
update_max_reg(Type::CINT, dest + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::INPUTPERSONAL: {
unsigned args = read_int(fd); // number of arguments
for (size_t i = 0; i < args; i += 4) {
unsigned vec = inst.add_reg(read_int(fd)); // vector size
inst.add_reg(read_int(fd)); // player-id
unsigned dest = inst.add_reg(read_int(fd)); // destination (sint)
inst.add_reg(read_int(fd)); // source (cint)
update_max_reg(Type::SINT, dest + vec, inst.get_opcode());
}
break;
}
case Opcode::INPUTMIXEDREG:
case Opcode::INPUTMIXED: {
unsigned num = read_int(fd);
for (size_t i = 0; i < num; ++i) {
uint32_t cur = inst.add_reg(read_int(fd));
if (cur == 2) {
log(Level::ERROR, "INPUTMIXED: only int/fix is supported");
}
uint32_t dest = inst.add_reg(read_int(fd));
if (cur == 1) { // fix-point
inst.add_reg(read_int(fd)); // precision
i++;
}
update_max_reg(Type::SINT, dest + inst.get_size(), inst.get_opcode());
inst.add_reg(read_int(fd)); // input PLAYER (regint for ...REG)
i += 2;
}
break;
}
case Opcode::INPUTB: {
unsigned num = read_int(fd);
assert(num % 4 == 0);
for (size_t i = 0; i < num; i += 4) {
inst.add_reg(read_int(fd)); // player id
unsigned bits = inst.add_reg(read_int(fd)); // number of bits in output (int)
inst.add_reg(read_int(fd)); // exponent 2^n
unsigned dest = inst.add_reg(read_int(fd)); // dest
update_max_reg(Type::SBIT, dest + div_ceil(bits, BIT_LEN), inst.get_opcode());
}
break;
}
case Opcode::INPUTBVEC: {
unsigned num = read_int(fd);
for (size_t i = 1; i < num; i += 3) {
unsigned bits = inst.add_reg(read_int(fd)) - 3;
inst.add_reg(read_int(fd)); // 2^n
inst.add_reg(read_int(fd)); // player id
i += bits;
for (unsigned j = 0; j < bits; ++j) {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + 1, inst.get_opcode());
}
}
break;
}
// sreg + sreg + im(32)
case Opcode::MULSI:
case Opcode::SUBSI:
case Opcode::SHRCI:
case Opcode::SHLCI:
case Opcode::ADDCI:
case Opcode::SUBCI:
case Opcode::MODCI:
case Opcode::DIVCI:
case Opcode::ADDSI:
case Opcode::SUBSFI:
case Opcode::SUBCFI:
case Opcode::XORCI:
case Opcode::ORCI:
case Opcode::ANDCI:
case Opcode::NOTC:
case Opcode::ADDCBI:
case Opcode::MULCBI:
case Opcode::XORCBI:
case Opcode::SHRCBI:
case Opcode::SHLCBI:
case Opcode::MULCI: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
inst.set_immediate(int(read_int(fd)));
break;
}
case Opcode::SUBMR: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, sreg + inst.get_size(), inst.get_opcode());
break;
}
// sreg + sreg
case Opcode::SHUFFLE:
case Opcode::MOVC:
case Opcode::MOVINT:
case Opcode::EQZC:
case Opcode::LTZC:
case Opcode::RAND:
case Opcode::PREFIXSUMS:
case Opcode::MOVS: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
break;
}
case Opcode::CONVINT: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::STMSI:
case Opcode::LDMSI: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::STMSBI:
case Opcode::LDMSBI: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::STMINTI:
case Opcode::LDMINTI: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
break;
}
case Opcode::STMCI:
case Opcode::LDMCI: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, sreg + inst.get_size(), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(), inst.get_opcode());
break;
}
// sreg + sreg + sreg
case Opcode::XORCB:
inst.set_immediate(read_int(fd)); // + BIT_LEN
case Opcode::ADDCB:
case Opcode::ADDS:
case Opcode::SUBC:
case Opcode::ADDC:
case Opcode::FLOORDIVC:
case Opcode::DIVC:
case Opcode::MODC:
case Opcode::MULC:
case Opcode::ORC:
case Opcode::ANDC:
case Opcode::XORC:
case Opcode::SHLC:
case Opcode::SHRC:
case Opcode::EQC:
case Opcode::LTC:
case Opcode::GTC:
case Opcode::SUBINT:
case Opcode::ADDINT:
case Opcode::MULINT:
case Opcode::DIVINT:
case Opcode::PRINT_COND_PLAIN:
case Opcode::SUBS: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(inst.get_reg_type(inst.get_opcode()), sreg + inst.get_size(),
inst.get_opcode());
break;
}
case Opcode::MULM:
case Opcode::SUBML:
case Opcode::ADDM: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, sreg + inst.get_size(),
inst.get_opcode()); // dest
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, sreg + inst.get_size(),
inst.get_opcode()); // sum1
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, sreg + inst.get_size(),
inst.get_opcode()); // sum2
break;
}
case Opcode::ANDM: {
inst.add_reg(read_int(fd)); // bits
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + inst.get_size(),
inst.get_opcode()); // dest
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + inst.get_size(),
inst.get_opcode()); // sum1
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CBIT, sreg + inst.get_size(),
inst.get_opcode()); // sum2
break;
}
case Opcode::CONVMODP: {
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::INT, sreg + inst.get_size(),
inst.get_opcode()); // dest
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::CINT, sreg + inst.get_size(),
inst.get_opcode()); // sum1
inst.set_immediate(read_int(fd));
break;
}
// im(32) + sreg + sreg
case Opcode::NOTCB:
case Opcode::MOVSB: {
unsigned num = inst.set_immediate(read_int(fd));
unsigned sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(num, BIT_LEN), inst.get_opcode());
sreg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, sreg + div_ceil(num, BIT_LEN), inst.get_opcode());
break;
}
case Opcode::BITDECS:
case Opcode::BITCOMS: {
unsigned num = read_int(fd);
unsigned bit = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, bit + div_ceil(num, BIT_LEN), inst.get_opcode());
for (size_t i = 1; i < num; ++i) {
bit = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, bit + 1, inst.get_opcode());
}
break;
}
case Opcode::CONVCINT: {
unsigned dest = inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd)); // source
update_max_reg(Type::CBIT, dest + 1, inst.get_opcode());
break;
}
case Opcode::CONVSINT: {
unsigned bits = inst.add_reg(read_int(fd));
unsigned dest = inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd)); // source
update_max_reg(Type::SBIT, dest + div_ceil(bits, BIT_LEN), inst.get_opcode());
break;
}
case Opcode::CONVCINTVEC: {
unsigned bits = read_int(fd) - 1;
inst.add_reg(read_int(fd)); // source
for (size_t i = 0; i < bits; ++i) {
unsigned dest = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, dest + div_ceil(inst.get_size(), BIT_LEN),
inst.get_opcode());
}
break;
}
case Opcode::ANDRSVEC: {
unsigned num = read_int(fd);
for (size_t i = 0; i < num;) {
unsigned one_op = inst.add_reg(read_int(fd));
unsigned vec = inst.add_reg(read_int(fd)); // vector size
for (size_t j = 2; j < one_op; ++j) {
unsigned reg = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, reg + div_ceil(vec, BIT_LEN), inst.get_opcode());
}
i += one_op;
}
break;
}
case Opcode::ANDRS: {
unsigned args = read_int(fd);
for (size_t i = 0; i < args; i += 4) {
unsigned vec = inst.add_reg(read_int(fd));
unsigned dest = inst.add_reg(read_int(fd));
update_max_reg(Type::SBIT, dest + div_ceil(vec, BIT_LEN), inst.get_opcode());
inst.add_reg(read_int(fd)); // source
inst.add_reg(read_int(fd)); // const factor
}
break;
}
case Opcode::MULRS: {
unsigned args = read_int(fd);
for (size_t i = 0; i < args; i += 4) {
unsigned vec = inst.add_reg(read_int(fd));
unsigned dest = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, dest + vec, inst.get_opcode());
inst.add_reg(read_int(fd)); // source
inst.add_reg(read_int(fd)); // const factor
}
break;
}
case Opcode::MATMULSM: {
unsigned dest = inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd)); // factor 1
inst.add_reg(read_int(fd)); // factor 2
int rows = inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd)); // cols/rows of 1st/2nd factor
int cols = inst.add_reg(read_int(fd));
update_max_reg(Type::SINT, dest + rows * cols, inst.get_opcode());
for (size_t i = 0; i < 6u; ++i) {
inst.add_reg(read_int(fd));
}
break;
}
case Opcode::MATMULS: {
unsigned args = read_int(fd);
assert(args % 6 == 0);
for (size_t i = 0; i < args; ++i) {
unsigned dest = inst.add_reg(read_int(fd)); // (sint)
inst.add_reg(read_int(fd)); // factor 1 (sint)
inst.add_reg(read_int(fd)); // factor 2 (sint)
int rows = inst.add_reg(read_int(fd));
inst.add_reg(read_int(fd)); // cols/rows of 1st/2nd factor