ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
cbitvector.h
Go to the documentation of this file.
1 
19 #ifndef CBITVECTOR_H_
20 #define CBITVECTOR_H_
21 
22 #include "typedefs.h"
23 #include "crypto/crypto.h"
24 #include <math.h>
25 #include <iostream>
26 #include <iomanip>
27 
29 static const BYTE REVERSE_NIBBLE_ORDER[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE, 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF };
31 static const BYTE REVERSE_BYTE_ORDER[256] = { 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8,
32  0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C,
33  0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2,
34  0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96,
35  0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1,
36  0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85,
37  0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD,
38  0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B,
39  0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF,
40  0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF };
41 
46 static const BYTE RESET_BIT_POSITIONS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
51 static const BYTE RESET_BIT_POSITIONS_INV[9] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF };
52 
54 static const BYTE GET_BIT_POSITIONS[9] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00 };
55 
57 static const BYTE GET_BIT_POSITIONS_INV[9] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00 };
58 
60 static const int INT_MASK[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
61 
63 static const int FIRST_MASK_SHIFT[8] = { 0xFF00, 0x7F80, 0x3FC0, 0x1FE0, 0x0FF0, 0x07F8, 0x03FC, 0x01FE };
68 static const BYTE MASK_BIT[8] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
69 
74 static const BYTE BIT[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
75 
80 static const BYTE CMASK_BIT[8] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe };
81 
86 static const BYTE C_BIT[8] = { 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F };
88 static const BYTE MASK_SET_BIT[2][8] = { { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 } };
89 
90 
91 
96 static const BYTE MASK_SET_BIT_C[2][8] = { { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 }, { 0, 0, 0, 0, 0, 0, 0, 0 } };
97 
102 static const BYTE SET_BIT_C[2][8] = { { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 }, { 0, 0, 0, 0, 0, 0, 0, 0 } };
103 
104 const BYTE SELECT_BIT_POSITIONS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
105 
106 #ifdef MACHINE_SIZE_32
107 static const REGISTER_SIZE TRANSPOSITION_MASKS[6] =
108 { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
109 static const REGISTER_SIZE TRANSPOSITION_MASKS_INV[6] =
110 { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000};
111 #else
112 #ifdef MACHINE_SIZE_64
113 
114 static const REGISTER_SIZE TRANSPOSITION_MASKS[6] = { 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF };
115 static const REGISTER_SIZE TRANSPOSITION_MASKS_INV[6] = { 0xAAAAAAAAAAAAAAAA, 0xCCCCCCCCCCCCCCCC, 0xF0F0F0F0F0F0F0F0, 0xFF00FF00FF00FF00, 0xFFFF0000FFFF0000, 0xFFFFFFFF00000000 };
116 #else
117 #endif
118 #endif
119 
120 static const size_t SHIFTVAL = 3;
121 
123 class CBitVector {
124 public:
125 
126  //Constructor code begins here...
127 
130  Init();
131  }
136  CBitVector(uint32_t bits) {
137  Init();
138  Create(bits);
139  }
145  CBitVector(uint32_t bits, crypto* crypt) {
146  Init();
147  Create(bits, crypt);
148  }
149  //Constructor code ends here...
150 
151  //Basic Primitive function of allocation and deallocation begins here.
156  void Init() {
157  m_pBits = NULL;
158  m_nByteSize = 0;
159  }
160 
166  delCBitVector();
167  }
168 
172  void delCBitVector() {
173  if (m_nByteSize > 0) {
174  free(m_pBits);
175  }
176 
177  m_nByteSize = 0;
178  m_pBits = NULL;
179  }
180  //Basic Primitive function of allocation and deallocation ends here.
181 
182 
183  //Create function supported by CBitVector starts here...
191  void FillRand(uint32_t bits, crypto* crypt);
192 
193 
194 
195  /* Create in bits and bytes */
196 
204  void Create(uint64_t bits);
205 
206  //void Create(int bits) {Create((uint64_t) bits);}
207 
216  void CreateBytes(uint64_t bytes) {
217  Create(bytes << 3);
218  }
226  void CreateZeros(uint64_t bits) {
227  Create(bits);
228  memset(m_pBits, 0, m_nByteSize);
229  }
230 
239  void Create(uint64_t bits, crypto* crypt);
240 
241 
248  void Create(uint64_t numelements, uint64_t elementlength);
249 
258  void Create(uint64_t numelements, uint64_t elementlength, crypto* crypt);
259 
268  void Create(uint64_t numelementsDimA, uint64_t numelementsDimB, uint64_t elementlength);
278  void Create(uint64_t numelementsDimA, uint64_t numelementsDimB, uint64_t elementlength, crypto* crypt);
279  //Create function supported by CBitVector ends here...
280 
281 
282 
283  /*
284  * Management operations
285  */
286 
291  void ResizeinBytes(int newSizeBytes);
292 
298  void Reset() {
299  memset(m_pBits, 0, m_nByteSize);
300  }
301 
307  void ResetFromTo(int frombyte, int tobyte) {
308  memset(m_pBits + frombyte, 0, tobyte - frombyte);
309  }
310 
314  void SetToOne() {
315  memset(m_pBits, 0xFF, m_nByteSize);
316  }
317 
322  int GetSize() {
323  return m_nByteSize;
324  }
325 
331  BOOL IsEqual(CBitVector& vec);
332 
340  BOOL IsEqual(CBitVector& vec, int from, int to);
341 
347  void SetElementLength(int elelen) {
348  m_nElementLength = elelen;
349  }
350 
351 
356  uint64_t GetElementLength() {
357  return m_nElementLength;
358  }
359 
360  /*
361  * Copy operations
362  */
363 
369  void Copy(CBitVector& vec) {
370  Copy(vec.GetArr(), 0, vec.GetSize());
371  }
381  void Copy(CBitVector& vec, int pos, int len) {
382  Copy(vec.GetArr(), pos, len);
383  }
384 
392  void Copy(BYTE* p, int pos, int len);
393 
395  void XOR_no_mask(int p, int bitPos, int bitLen);
396 
398  unsigned int GetInt(int bitPos, int bitLen);
399 #define GetIntBitsFromLen(x, from, len) ( ( (x & ( ( (2<<(len))-1) << from )) >> from) & 0xFF)
400 #define GetMask(len) (( (1<<(len))-1))
401 
408  void ORByte(int pos, BYTE p);
409 
410  /*
411  * Bitwise operations
412  */
413 
421  BYTE GetBit(int idx) {
422  return !!(m_pBits[idx >> 3] & MASK_BIT[idx & 0x7]);
423  }
431  void SetBit(int idx, BYTE b) {
432  m_pBits[idx >> 3] = (m_pBits[idx >> 3] & CMASK_BIT[idx & 0x7]) | MASK_SET_BIT_C[!(b & 0x01)][idx & 0x7];
433  }
434  /* Deprecated */
442  void XORBit(int idx, BYTE b) {
443  m_pBits[idx >> 3] ^= MASK_SET_BIT_C[!(b & 0x01)][idx & 0x7];
444  }
445 
446  /* Deprecated */
454  void ANDBit(int idx, BYTE b) {
455  if (!b)
456  m_pBits[idx >> 3] &= CMASK_BIT[idx & 0x7];
457  }
458 
459  //used to access bits in the regular order
460 
467  BYTE GetBitNoMask(int idx) {
468  return !!(m_pBits[idx >> 3] & BIT[idx & 0x7]);
469  }
470 
477  void SetBitNoMask(int idx, BYTE b) {
478  m_pBits[idx >> 3] = (m_pBits[idx >> 3] & C_BIT[idx & 0x7]) | SET_BIT_C[!(b & 0x01)][idx & 0x7];
479  }
480 
487  void XORBitNoMask(int idx, BYTE b) {
488  m_pBits[idx >> 3] ^= SET_BIT_C[!(b & 0x01)][idx & 0x7];
489  }
490 
491  /* Deprecated */
498  void ANDBitNoMask(int idx, BYTE b) {
499  if (!b)
500  m_pBits[idx >> 3] &= C_BIT[idx & 0x7];
501  }
502 
503  /*
504  * Single byte operations
505  */
506 
512  void SetByte(int idx, BYTE p) {
513  m_pBits[idx] = p;
514  }
515 
521  BYTE GetByte(int idx) {
522  return m_pBits[idx];
523  }
524 
531  void XORByte(int idx, BYTE b) {
532  m_pBits[idx] ^= b;
533  }
539  void ANDByte(int idx, BYTE b) {
540  m_pBits[idx] &= b;
541  }
542 
543  /*
544  * Get Operations
545  */
546 
554  void GetBits(BYTE* p, int pos, int len);
555 
563  void GetBytes(BYTE* p, int pos, int len);
564 
568  template<class T> void GetBytes(T* dst, T* src, T* lim);
569 
577  template<class T> T Get(int pos, int len) {
578  T val = 0;
579  GetBits((BYTE*) &val, pos, len);
580  return val;
581  }
582 
583  /*
584  * Set Operations
585  */
593  void SetBits(BYTE* p, uint64_t pos, uint64_t len);
594 
602  void SetBits(BYTE* p, int pos, int len) {
603  SetBits(p, (uint64_t) pos, (uint64_t) len);
604  }
605 
613  void SetBitsPosOffset(BYTE* p, int ppos, int pos, int len);
614 
622  void SetBytes(BYTE* p, int pos, int len);
623 
628  template<class T> void SetBytes(T* dst, T* src, T* lim);
629 
636  template<class T> void Set(T val, int pos, int len) {
637  SetBits((BYTE*) &val, (uint64_t) pos, (uint64_t) len);
638  }
639 
645  void SetBitsToZero(int bitpos, int bitlen);
646 
647  /*
648  * XOR Operations
649  */
650 
658  void XORBytes(BYTE* p, int pos, int len);
665  void XORBytes(BYTE* p, int len) {
666  XORBytes(p, 0, len);
667  }
668 
677  void XORVector(CBitVector &vec, int pos, int len) {
678  XORBytes(vec.GetArr(), pos, len);
679  }
680 
685  template<class T> void XOR(T val, int pos, int len) {
686  XORBits((BYTE*) &val, pos, len);
687  }
688 
696  void XORBits(BYTE* p, int pos, int len);
697 
705  void XORBitsPosOffset(BYTE* p, int ppos, int pos, int len);
706 
711  template<class T> void XORBytes(T* dst, T* src, T* lim);
712 
714  void XORRepeat(BYTE* p, int pos, int len, int num);
715 
724  void XORBytesReverse(BYTE* p, int pos, int len);
725 
726  /*
727  * AND Operations
728  */
729 
737  void ANDBytes(BYTE* p, int pos, int len);
738 
743  template<class T> void ANDBytes(T* dst, T* src, T* lim);
744 
745  /*
746  * Set operations
747  */
757  void SetXOR(BYTE* p, BYTE* q, int pos, int len);
758 
768  void SetAND(BYTE* p, BYTE* q, int pos, int len);
769 
770  /*
771  * Buffer access operations
772  */
773 
777  BYTE* GetArr() {
778  return m_pBits;
779  }
780 
786  void AttachBuf(BYTE* p, int size = -1) {
787  m_pBits = p;
788  m_nByteSize = size;
789  }
790 
796  void AttachBuf(BYTE* p, uint64_t size = -1) {
797  m_pBits = p;
798  m_nByteSize = size;
799  }
800 /* The definition of this method is similar to that of Init(). I don't understand why you need to methods
804  which performs exactly the same work. */
805  void DetachBuf() {
806  m_pBits = NULL;
807  m_nByteSize = 0;
808  }
809 
810  /*
811  * Print Operations
812  */
813 
820  void Print(int fromBit, int toBit);
821 
825  void PrintHex();
826 
832  void PrintHex(int fromByte, int toByte);
833 
837  void PrintBinary() {
838  Print(0, m_nByteSize << 3);
839  }
840 
846  void PrintContent();
847 
853  void PrintBinaryMasked(int from, int to);
854 
855  /*
856  * If the cbitvector is abstracted to an array of elements with m_nElementLength bits size, these methods can be used for easier access
857  */
858 
864  template<class T> T Get(int i) {
865  return Get<T>(i * m_nElementLength, m_nElementLength);
866  }
873  template<class T> void Set(T val, int i) {
874  Set<T>(val, i * m_nElementLength, m_nElementLength);
875  }
876  /*
877  * The same as the above methods only for two-dimensional access
878  */
885  template<class T> T Get2D(int i, int j) {
886  return Get<T>((i * m_nNumElementsDimB + j) * m_nElementLength, m_nElementLength);
887  }
888 
896  template<class T> void Set2D(T val, int i, int j) {
897  Set<T>(val, (i * m_nNumElementsDimB + j) * m_nElementLength, m_nElementLength);
898  }
899  //useful when accessing elements using an index
900 
901  //View the cbitvector as a rows x columns matrix and transpose
902  void EklundhBitTranspose(int rows, int columns);
903  void SimpleTranspose(int rows, int columns);
904 
905 private:
906  BYTE* m_pBits;
907  uint64_t m_nByteSize;
908  uint64_t m_nBits; //The exact number of bits
909  uint64_t m_nElementLength;
911  uint64_t m_nNumElements;
912  uint64_t m_nNumElementsDimB;
913 };
914 
915 #endif /* BITVECTOR_H_ */
void XORBit(int idx, BYTE b)
Definition: cbitvector.h:442
CBitVector(uint32_t bits)
Definition: cbitvector.h:136
void XOR_no_mask(int p, int bitPos, int bitLen)
Definition: cbitvector.cpp:431
void SetXOR(BYTE *p, BYTE *q, int pos, int len)
Definition: cbitvector.cpp:337
void Create(uint64_t bits)
Definition: cbitvector.cpp:40
Definition: pk-crypto.h:59
void PrintBinary()
Definition: cbitvector.h:837
void SetAND(BYTE *p, BYTE *q, int pos, int len)
Definition: cbitvector.cpp:342
void XORBytesReverse(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:194
void SetElementLength(int elelen)
Definition: cbitvector.h:347
BYTE GetBitNoMask(int idx)
Definition: cbitvector.h:467
Crypto primitive class.
void ORByte(int pos, BYTE p)
Definition: cbitvector.cpp:245
void XORBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:269
BYTE GetBit(int idx)
Definition: cbitvector.h:421
void FillRand(uint32_t bits, crypto *crypt)
Definition: cbitvector.cpp:22
void Copy(CBitVector &vec, int pos, int len)
Definition: cbitvector.h:381
Definition: crypto.h:58
void PrintBinaryMasked(int from, int to)
Definition: cbitvector.cpp:371
void Reset()
Definition: cbitvector.h:298
void SetBits(BYTE *p, int pos, int len)
Definition: cbitvector.h:602
void DetachBuf()
Definition: cbitvector.h:805
void SetByte(int idx, BYTE p)
Definition: cbitvector.h:512
int GetSize()
Definition: cbitvector.h:322
void delCBitVector()
Definition: cbitvector.h:172
void Init()
Definition: cbitvector.h:156
void XOR(T val, int pos, int len)
Definition: cbitvector.h:685
void XORBits(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:210
BYTE GetByte(int idx)
Definition: cbitvector.h:521
BOOL IsEqual(CBitVector &vec)
Definition: cbitvector.cpp:403
T Get(int pos, int len)
Definition: cbitvector.h:577
void SetToOne()
Definition: cbitvector.h:314
void XORBitNoMask(int idx, BYTE b)
Definition: cbitvector.h:487
void ANDBitNoMask(int idx, BYTE b)
Definition: cbitvector.h:498
void SetBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:302
void SetBits(BYTE *p, uint64_t pos, uint64_t len)
Definition: cbitvector.cpp:103
void GetBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:250
T Get2D(int i, int j)
Definition: cbitvector.h:885
void PrintContent()
Definition: cbitvector.cpp:380
void ANDBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:321
void SetBit(int idx, BYTE b)
Definition: cbitvector.h:431
void XORRepeat(BYTE *p, int pos, int len, int num)
Definition: cbitvector.cpp:288
CBitVector(uint32_t bits, crypto *crypt)
Definition: cbitvector.h:145
void GetBits(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:162
void SetBitNoMask(int idx, BYTE b)
Definition: cbitvector.h:477
unsigned int GetInt(int bitPos, int bitLen)
Definition: cbitvector.cpp:444
~CBitVector()
Definition: cbitvector.h:165
void CreateBytes(uint64_t bytes)
Definition: cbitvector.h:216
T Get(int i)
Definition: cbitvector.h:864
uint64_t GetElementLength()
Definition: cbitvector.h:356
void XORBytes(BYTE *p, int len)
Definition: cbitvector.h:665
void ANDBit(int idx, BYTE b)
Definition: cbitvector.h:454
void CreateZeros(uint64_t bits)
Definition: cbitvector.h:226
void XORByte(int idx, BYTE b)
Definition: cbitvector.h:531
void Set(T val, int pos, int len)
Definition: cbitvector.h:636
void Print(int fromBit, int toBit)
Definition: cbitvector.cpp:347
void SetBitsToZero(int bitpos, int bitlen)
Definition: cbitvector.cpp:148
CBitVector()
Definition: cbitvector.h:129
void AttachBuf(BYTE *p, uint64_t size=-1)
Definition: cbitvector.h:796
Typedefs Implementation.
void Set(T val, int i)
Definition: cbitvector.h:873
void XORVector(CBitVector &vec, int pos, int len)
Definition: cbitvector.h:677
void AttachBuf(BYTE *p, int size=-1)
Definition: cbitvector.h:786
BYTE * GetArr()
Definition: cbitvector.h:777
void ResizeinBytes(int newSizeBytes)
Definition: cbitvector.cpp:79
void XORBitsPosOffset(BYTE *p, int ppos, int pos, int len)
Definition: cbitvector.cpp:204
void ResetFromTo(int frombyte, int tobyte)
Definition: cbitvector.h:307
void SetBitsPosOffset(BYTE *p, int ppos, int pos, int len)
Definition: cbitvector.cpp:141
Definition: cbitvector.h:123
void ANDByte(int idx, BYTE b)
Definition: cbitvector.h:539
void PrintHex()
Definition: cbitvector.cpp:364
void Copy(CBitVector &vec)
Definition: cbitvector.h:369
void Set2D(T val, int i, int j)
Definition: cbitvector.h:896