UnitTestBinInt.cpp 34.5 KB
Newer Older
Gerard Ryan's avatar
Gerard Ryan committed
1
/*
Gerard Ryan's avatar
Gerard Ryan committed
2
 * @file UnitTestBinInt
Gerard Ryan's avatar
Gerard Ryan committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * @author  TPOC: palisade@njit.edu
 *
 * @copyright Copyright (c) 2017, New Jersey Institute of Technology (NJIT)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
Gerard Ryan's avatar
Gerard Ryan committed
26

Gerard Ryan's avatar
Gerard Ryan committed
27
 /*
Gerard Ryan's avatar
Gerard Ryan committed
28 29
  * This code tests the binary integers in the math libraries of the PALISADE lattice encryption library.
  */
Gerard Ryan's avatar
Gerard Ryan committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

#define PROFILE
#include "include/gtest/gtest.h"
#include <iostream>

#include "../lib/lattice/dcrtpoly.h"
#include "math/backend.h"
#include "math/nbtheory.h"
#include "math/distrgen.h"
#include "lattice/elemparams.h"
#include "lattice/ilparams.h"
#include "lattice/ildcrtparams.h"
#include "lattice/ilelement.h"
#include "lattice/poly.h"
#include "utils/inttypes.h"
#include "utils/utilities.h"

using namespace std;
using namespace lbcrypto;

Gerard Ryan's avatar
Gerard Ryan committed
50 51 52 53
extern bool TestB2;
extern bool TestB4;
extern bool TestB6;
extern bool TestNative;
Gerard Ryan's avatar
Gerard Ryan committed
54 55

/************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
56
/* TESTING METHODS OF ALL THE INTEGER CLASSES   */
Gerard Ryan's avatar
Gerard Ryan committed
57 58
/************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
59 60 61 62 63 64 65
class UTBinInt : public ::testing::Test {
 protected:
};

template<typename T>
void assign_test(const string& msg) {
	T v;
Gerard Ryan's avatar
Gerard Ryan committed
66 67 68 69
	vector<uint64_t> vals( {27, uint64_t(1)<<10, uint64_t(1)<<25, uint64_t(1)<<35, uint64_t(1)<<55, } );

	for( auto tv : vals ) {
		v = uint64_t(tv);
Gerard Ryan's avatar
Gerard Ryan committed
70
		EXPECT_EQ(v.ConvertToInt(), tv) << msg;
Gerard Ryan's avatar
Gerard Ryan committed
71 72 73
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
74 75
TEST_F(UTBinInt,assign) {
	RUN_ALL_BACKENDS_INT(assign_test, "assign")
Gerard Ryan's avatar
Gerard Ryan committed
76 77
}

Gerard Ryan's avatar
Gerard Ryan committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
template<typename T>
void identity_test(const string& msg) {
	auto f = [](T& a, const string& msg) {
		T ZERO(0);
		T ONE(1);

		EXPECT_EQ(a, a + ZERO) << msg << " Failure testing a + 0";
		EXPECT_EQ(a, a += ZERO) << msg << " Failure testing a += 0";
		EXPECT_EQ(a, a * ONE) << msg << " Failure testing a * 1";
		EXPECT_EQ(a, a *= ONE) << msg << " Failure testing a *= 1";

		EXPECT_EQ(a, ZERO + a) << msg << " Failure testing 0 + a";
		EXPECT_EQ(a, ZERO += a) << msg << " Failure testing 0 += a";
		EXPECT_EQ(a, ONE * a) << msg << " Failure testing 1 * a";
		EXPECT_EQ(a, ONE *= a) << msg << " Failure testing 1 *= a";

		EXPECT_EQ(a*a, ONE *= a) << msg << " Failure on 1 *= a, twice";
	};

	T sm("3279");
	f(sm, msg + " small");
	T lg("1234567898765432");
	f(lg, msg + " small");
}
Gerard Ryan's avatar
Gerard Ryan committed
102

Gerard Ryan's avatar
Gerard Ryan committed
103 104
TEST_F(UTBinInt,identity) {
	RUN_BIG_BACKENDS_INT(identity_test,"identity")
Gerard Ryan's avatar
Gerard Ryan committed
105 106
}

Gerard Ryan's avatar
Gerard Ryan committed
107 108 109
/************************************************/
/* TESTING BASIC MATH METHODS AND OPERATORS     */
/************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
110 111
template<typename T>
void basic_math_test(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
112 113 114 115

  /************************************************/
  /* TESTING METHOD PLUS FOR ALL CONDITIONS       */
  /************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
116 117 118
  // The method "Plus" does addition on two Ts a,b
  // Returns a+b, which is stored in another T
  // calculatedResult ConvertToInt converts T
Gerard Ryan's avatar
Gerard Ryan committed
119 120
  // calculatedResult to integer

Gerard Ryan's avatar
Gerard Ryan committed
121
  T calculatedResult;
Gerard Ryan's avatar
Gerard Ryan committed
122
  uint64_t expectedResult;
Gerard Ryan's avatar
Gerard Ryan committed
123
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN SECOND NUMBER AND MSB
Gerard Ryan's avatar
Gerard Ryan committed
124 125
  // HAS NO OVERFLOW
  {
Gerard Ryan's avatar
Gerard Ryan committed
126 127
    T a("203450");
    T b("2034");
Gerard Ryan's avatar
Gerard Ryan committed
128 129 130 131 132

    calculatedResult = a.Plus(b);
    expectedResult = 205484;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
133
      << msg << " Failure testing plus_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
134
  }
Gerard Ryan's avatar
Gerard Ryan committed
135
  // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN SECOND NUMBER AND MSB
Gerard Ryan's avatar
Gerard Ryan committed
136 137
  // HAS NO OVERFLOW
  {
Gerard Ryan's avatar
Gerard Ryan committed
138 139
    T a("2034");
    T b("203450");
Gerard Ryan's avatar
Gerard Ryan committed
140 141 142 143 144 145


    calculatedResult = a.Plus(b);
    expectedResult = 205484;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
146
      << msg << " Failure testing plus_a_less_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
147
  }
Gerard Ryan's avatar
Gerard Ryan committed
148
  // TEST_F CASE WHEN MSB OF THE RESULT HAS BIT-OVERFLOW TO THE NEXT
Gerard Ryan's avatar
Gerard Ryan committed
149 150
  // BYTE
  {
Gerard Ryan's avatar
Gerard Ryan committed
151 152
    T a("768900");
    T b("16523408");
Gerard Ryan's avatar
Gerard Ryan committed
153 154 155 156 157

    calculatedResult = a.Plus(b);
    expectedResult = 17292308;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
158
      << msg << " Failure testing overflow_to_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
159
  }
Gerard Ryan's avatar
Gerard Ryan committed
160
  // TEST_F CASE WHEN MSB OF THE RESULT HAS BIT-OVERFLOW IN THE SAME
Gerard Ryan's avatar
Gerard Ryan committed
161 162
  // BYTE
  {
Gerard Ryan's avatar
Gerard Ryan committed
163 164
    T a("35");
    T b("1015");
Gerard Ryan's avatar
Gerard Ryan committed
165 166 167 168 169

    calculatedResult = a.Plus(b);
    expectedResult = 1050;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
170
      << msg << " Failure testing plus_no_overflow_to_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
171 172 173 174 175 176 177 178
  }

  /************************************************/
  /* TESTING OPERATOR += FOR ALL CONDITIONS       */
  /************************************************/

  // The operator "+=(Plus Equals)" does addition of two Big
  // Integers a,b Calculates a+b, and stores result in a ConvertToInt
Gerard Ryan's avatar
Gerard Ryan committed
179
  // converts T a to integer
Gerard Ryan's avatar
Gerard Ryan committed
180 181


Gerard Ryan's avatar
Gerard Ryan committed
182
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN SECOND NUMBER AND MSB
Gerard Ryan's avatar
Gerard Ryan committed
183 184
  // HAS NO OVERFLOW
  {
Gerard Ryan's avatar
Gerard Ryan committed
185 186
    T a("2034");
    T b("203");
Gerard Ryan's avatar
Gerard Ryan committed
187 188 189 190 191 192 193

    a+=b;
    expectedResult = 2237;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
      << " Failure testing plus_equals_a_greater_than_b";
  }
Gerard Ryan's avatar
Gerard Ryan committed
194
  // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN SECOND NUMBER AND MSB
Gerard Ryan's avatar
Gerard Ryan committed
195 196
  // HAS NO OVERFLOW
  {
Gerard Ryan's avatar
Gerard Ryan committed
197 198
    T a("2034");
    T b("203450");
Gerard Ryan's avatar
Gerard Ryan committed
199 200 201 202 203

    a+=b;
    expectedResult = 205484;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
204
      << msg << " Failure testing plus_equals_a_less_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
205
  }
Gerard Ryan's avatar
Gerard Ryan committed
206
  // TEST_F CASE WHEN MSB OF THE RESULT HAS BIT-OVERFLOW TO THE NEXT
Gerard Ryan's avatar
Gerard Ryan committed
207 208
  // BYTE
  {
Gerard Ryan's avatar
Gerard Ryan committed
209 210
    T a("768900");
    T b("16523408");
Gerard Ryan's avatar
Gerard Ryan committed
211 212 213 214 215

    a+=b;
    expectedResult = 17292308;

    EXPECT_EQ(expectedResult,a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
216
      << msg << " Failure testing plus_equals_overflow_to_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
217
  }
Gerard Ryan's avatar
Gerard Ryan committed
218
  // TEST_F CASE WHEN MSB OF THE RESULT HAS BIT-OVERFLOW IN THE SAME
Gerard Ryan's avatar
Gerard Ryan committed
219 220
  // BYTE
  {
Gerard Ryan's avatar
Gerard Ryan committed
221 222
    T a("35");
    T b("1015");
Gerard Ryan's avatar
Gerard Ryan committed
223 224 225 226 227

    a+=b;
    expectedResult = 1050;

    EXPECT_EQ(expectedResult,a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
228
      << msg << " Failure testing plus_equals_no_overflow_to_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
229 230 231 232 233
  }
  /************************************************/
  /* TESTING METHOD MINUS FOR ALL CONDITIONS      */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
234 235
  // The method "Minus" does subtraction on two Ts a,b
  // Returns a-b, which is stored in another T
Gerard Ryan's avatar
Gerard Ryan committed
236 237
  // calculatedResult When a<b, the result is 0, since there is no
  // support for negative numbers as of now ConvertToInt converts
Gerard Ryan's avatar
Gerard Ryan committed
238
  // T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
239

Gerard Ryan's avatar
Gerard Ryan committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
//  {
//    // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
//    T a("20489");
//    T b("2034455");
//
//    calculatedResult = a.Minus(b);
//    expectedResult = 0;
//
//    //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMEBR RESULT SHOULD BE
//    //ZERO
//    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
//      << msg << " Failure testing minus_a_less_than_b";
//  }
Gerard Ryan's avatar
Gerard Ryan committed
254
  // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
255
  {
Gerard Ryan's avatar
Gerard Ryan committed
256 257
    T a("2048956567");
    T b("2048956567");
Gerard Ryan's avatar
Gerard Ryan committed
258 259 260 261 262

    calculatedResult = a.Minus(b);
    expectedResult = 0;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
263
      << msg << " Failure testing minus_a_equal_to_b";
Gerard Ryan's avatar
Gerard Ryan committed
264
  }
Gerard Ryan's avatar
Gerard Ryan committed
265
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
266
  {
Gerard Ryan's avatar
Gerard Ryan committed
267 268
    T a("2048956567");
    T b("2034455");
Gerard Ryan's avatar
Gerard Ryan committed
269 270 271 272 273

    calculatedResult = a.Minus(b);
    expectedResult = 2046922112;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
274
      << msg << " Failure testing minus_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
275
  }
Gerard Ryan's avatar
Gerard Ryan committed
276
  // TEST_F CASE WHEN SUBTRACTION NEEDS BORROW FROM NEXT BYTE
Gerard Ryan's avatar
Gerard Ryan committed
277
  {
Gerard Ryan's avatar
Gerard Ryan committed
278 279
    T a("196737");
    T b("65406");
Gerard Ryan's avatar
Gerard Ryan committed
280 281 282 283 284

    calculatedResult = a.Minus(b);
    expectedResult = 131331;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
285
      << msg << " Failure testing minus_borrow_from_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
286 287 288 289 290 291
  }

  /************************************************/
  /* TESTING OPERATOR -= FOR ALL CONDITIONS       */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
292
  // The operator "-=(Minus Equals)" does subtraction of two Big
Gerard Ryan's avatar
Gerard Ryan committed
293 294
  // Integers a,b Calculates a-b, and stores result in a Results to 0,
  // when a<b, since there is no concept of negative number as of now
Gerard Ryan's avatar
Gerard Ryan committed
295
  // ConvertToInt converts T a to integer
Gerard Ryan's avatar
Gerard Ryan committed
296 297 298 299 300 301 302 303 304 305 306 307 308
//  {
//    // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
//    T a("20489");
//    T b("2034455");
//
//    a-=b;
//    expectedResult = 0;
//
//    //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMBER RESULT SHOULD BE ZERO
//    EXPECT_EQ(expectedResult, a.ConvertToInt())
//      << msg << " Failure testing minus_equals_a_less_than_b";
//  }
Gerard Ryan's avatar
Gerard Ryan committed
309
  // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
310
  {
Gerard Ryan's avatar
Gerard Ryan committed
311 312
    T a("2048956567");
    T b("2048956567");
Gerard Ryan's avatar
Gerard Ryan committed
313 314 315 316 317

    a-=b;
    expectedResult = 0;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
318
      << msg << " Failure testing minus_equals_a_equal_to_b";
Gerard Ryan's avatar
Gerard Ryan committed
319
  }
Gerard Ryan's avatar
Gerard Ryan committed
320
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
321 322
  {

Gerard Ryan's avatar
Gerard Ryan committed
323 324
    T a("2048956567");
    T b("2034455");
Gerard Ryan's avatar
Gerard Ryan committed
325 326 327 328 329

    a-=b;
    expectedResult = 2046922112;

    EXPECT_EQ(expectedResult,a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
330
      << msg << " Failure testing minus_equals_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
331
  }
Gerard Ryan's avatar
Gerard Ryan committed
332
  // TEST_F CASE WHEN SUBTRACTION NEEDS BORROW FROM NEXT BYTE
Gerard Ryan's avatar
Gerard Ryan committed
333
  {
Gerard Ryan's avatar
Gerard Ryan committed
334 335
    T a("196737");
    T b("65406");
Gerard Ryan's avatar
Gerard Ryan committed
336 337 338 339 340

    a-=b;
    expectedResult = 131331;

    EXPECT_EQ(expectedResult,a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
341
      << msg << " Failure testing minus_equals_borrow_from_next_byte";
Gerard Ryan's avatar
Gerard Ryan committed
342 343 344 345 346 347
  }

  /************************************************/
  /* TESTING METHOD TIMES FOR ALL CONDITIONS      */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
348 349 350
  // The method "Times" does multiplication on two Ts
  // a,b Returns a*b, which is stored in another T
  // calculatedResult ConvertToInt converts T
Gerard Ryan's avatar
Gerard Ryan committed
351 352 353
  // calculatedResult to integer
  {
    //ask about the branching if (b.m_MSB==0 or 1)
Gerard Ryan's avatar
Gerard Ryan committed
354 355
    T a("1967");
    T b("654");
Gerard Ryan's avatar
Gerard Ryan committed
356 357 358 359 360

    calculatedResult = a*b;
    expectedResult = 1286418;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
361
      << msg << " Failure testing times_test";
Gerard Ryan's avatar
Gerard Ryan committed
362
  }
Gerard Ryan's avatar
Gerard Ryan committed
363

Gerard Ryan's avatar
Gerard Ryan committed
364 365 366 367
  /************************************************/
  /* TESTING METHOD DIVIDED_BY FOR ALL CONDITIONS */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
368 369 370 371
  // The method "Divided By" does division of T a by
  // another T b Returns a/b, which is stored in another
  // T calculatedResult ConvertToInt converts
  // T calculatedResult to integer When b=0, throws
Gerard Ryan's avatar
Gerard Ryan committed
372 373 374 375
  // error, since division by Zero is not allowed When a<b, returns 0,
  // since decimal value is not returned


Gerard Ryan's avatar
Gerard Ryan committed
376
  // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
377
  {
Gerard Ryan's avatar
Gerard Ryan committed
378 379
    T a("2048");
    T b("2034455");
Gerard Ryan's avatar
Gerard Ryan committed
380 381 382 383 384 385

    calculatedResult = a.DividedBy(b);
    expectedResult = 0;

    //RESULT SHOULD BE ZERO
    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
386
      << msg << " Failure testing divided_by_a_less_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
387
  }
Gerard Ryan's avatar
Gerard Ryan committed
388

Gerard Ryan's avatar
Gerard Ryan committed
389
  // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
390 391
  {

Gerard Ryan's avatar
Gerard Ryan committed
392 393
    T a("2048956567");
    T b("2048956567");
Gerard Ryan's avatar
Gerard Ryan committed
394 395 396 397 398

    calculatedResult = a.DividedBy(b);
    expectedResult = 1;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
399
      << msg << " Failure testing divided_by_a_equals_b";
Gerard Ryan's avatar
Gerard Ryan committed
400
  }
Gerard Ryan's avatar
Gerard Ryan committed
401

Gerard Ryan's avatar
Gerard Ryan committed
402
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN THE SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
403
  {
Gerard Ryan's avatar
Gerard Ryan committed
404 405
    T a("2048956567");
    T b("2034455");
Gerard Ryan's avatar
Gerard Ryan committed
406 407 408 409 410

    calculatedResult = a.DividedBy(b);
    expectedResult = 1007;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
411
      << msg << " Failure testing divided_by_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
412 413 414
  }

  {
Gerard Ryan's avatar
Gerard Ryan committed
415 416
	  T a("8096");
	  T b("4049");
Gerard Ryan's avatar
Gerard Ryan committed
417 418 419 420 421

	  calculatedResult = a.Mod(b);
	  expectedResult = 4047;

	  EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
422
		  << msg << " Failure testing Mod";
Gerard Ryan's avatar
Gerard Ryan committed
423 424
  }

Gerard Ryan's avatar
Gerard Ryan committed
425
  // TEST_F CASE FOR VERIFICATION OF ROUNDING OPERATION.
Gerard Ryan's avatar
Gerard Ryan committed
426 427

  {
Gerard Ryan's avatar
Gerard Ryan committed
428 429
	  T a("8096");
	  T b("4049");
Gerard Ryan's avatar
Gerard Ryan committed
430 431 432 433 434

	  calculatedResult = a.DivideAndRound(b);
	  expectedResult = 2;

	  EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
435
		  << msg << " Failure testing divided_and_rounding_by_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
436 437 438
  }

  /*{
Gerard Ryan's avatar
Gerard Ryan committed
439 440
    T a("204");
    T b("210");
Gerard Ryan's avatar
Gerard Ryan committed
441 442 443 444 445

    calculatedResult = a.DivideAndRound(b);
    expectedResult = 1;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
446
      << msg << " Failure testing divided_and_rounding_by_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
447 448
  }

Gerard Ryan's avatar
Gerard Ryan committed
449
  // TEST_F CASE FOR VERIFICATION OF ROUNDING OPERATION.
Gerard Ryan's avatar
Gerard Ryan committed
450
  {
Gerard Ryan's avatar
Gerard Ryan committed
451 452
	  T a("100");
	  T b("210");
Gerard Ryan's avatar
Gerard Ryan committed
453 454 455 456 457

	  calculatedResult = a.DivideAndRound(b);
	  expectedResult = 0;

	  EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
458
		  << msg << " Failure testing divided_and_rounding_by_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
459 460
  }*/

Gerard Ryan's avatar
Gerard Ryan committed
461
  // TEST_F CASE FOR VERIFICATION OF ROUNDING OPERATION.
Gerard Ryan's avatar
Gerard Ryan committed
462
  /*{
Gerard Ryan's avatar
Gerard Ryan committed
463 464 465
    T a("4048");
    T b("4049");
    T c("2");
Gerard Ryan's avatar
Gerard Ryan committed
466 467 468 469 470

    calculatedResult = a.MultiplyAndRound(c, b);
    expectedResult = 2;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
471
      << msg << " Failure testing divided_and_rounding_by_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
472 473 474
  }*/
}

Gerard Ryan's avatar
Gerard Ryan committed
475 476 477 478 479 480
TEST_F(UTBinInt,basic_math) {
	RUN_ALL_BACKENDS_INT(basic_math_test,"basic math")
}

template<typename T>
void basic_compare_test(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
481 482 483 484 485 486 487 488 489

  /************************************************/
  /* TESTING BASIC COMPARATOR METHODS AND OPERATORS */
  /**************************************************/

  /************************************************/
  /* TESTING METHOD COMPARE FOR ALL CONDITIONS    */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
490
  // The method "Compare" compares two Ts a,b
Gerard Ryan's avatar
Gerard Ryan committed
491 492 493 494 495 496 497 498
  // Returns:
  //    1, when a>b
  //    0, when a=b
  //   -1, when a<b
  //
  // Result is stored in signed integer, and then the result is
  // typecasted to int as EXPECT_EQ takes integer

Gerard Ryan's avatar
Gerard Ryan committed
499
  int c;
Gerard Ryan's avatar
Gerard Ryan committed
500 501
  int expectedResult;

Gerard Ryan's avatar
Gerard Ryan committed
502
  // TEST_F CASE WHEN FIRST NUMBER IS GREATER THAN SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
503
  {
Gerard Ryan's avatar
Gerard Ryan committed
504 505
    T a("112504");
    T b("46968");
Gerard Ryan's avatar
Gerard Ryan committed
506 507 508 509 510

    c = a.Compare(b);
    expectedResult = 1;

    EXPECT_EQ(expectedResult,(int)c)
Gerard Ryan's avatar
Gerard Ryan committed
511
      << msg << " Failure testing compare_a_greater_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
512
  }
Gerard Ryan's avatar
Gerard Ryan committed
513
  // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
514
  {
Gerard Ryan's avatar
Gerard Ryan committed
515 516
    T a("12504");
    T b("46968");
Gerard Ryan's avatar
Gerard Ryan committed
517 518 519 520 521

    c = a.Compare(b);
    expectedResult = -1;

    EXPECT_EQ(expectedResult,(int)c)
Gerard Ryan's avatar
Gerard Ryan committed
522
      << msg << " Failure testing compare_a_less_than_b";
Gerard Ryan's avatar
Gerard Ryan committed
523
  }
Gerard Ryan's avatar
Gerard Ryan committed
524
  // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
525
  {
Gerard Ryan's avatar
Gerard Ryan committed
526 527
    T a("34512504");
    T b("34512504");
Gerard Ryan's avatar
Gerard Ryan committed
528 529 530 531 532

    c = a.Compare(b);
    expectedResult = 0;

    EXPECT_EQ(expectedResult,(int)c)
Gerard Ryan's avatar
Gerard Ryan committed
533
      << msg << " Failure testing compare_a_equals_b";
Gerard Ryan's avatar
Gerard Ryan committed
534 535 536
  }
}

Gerard Ryan's avatar
Gerard Ryan committed
537 538 539 540 541 542
TEST_F(UTBinInt,basic_compare) {
	RUN_ALL_BACKENDS_INT(basic_compare_test,"basic compare")
}

template<typename T>
void mod_test(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
543 544 545 546 547

  /************************************************/
  /* TESTING METHOD MOD FOR ALL CONDITIONS        */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
548 549 550
  // The method "Mod" does modulus operation on two Ts
  // m,p Returns (m mod p), which is stored in another T
  // calculatedResult ConvertToInt converts T r to
Gerard Ryan's avatar
Gerard Ryan committed
551 552
  // integer

Gerard Ryan's avatar
Gerard Ryan committed
553
  T calculatedResult;
Gerard Ryan's avatar
Gerard Ryan committed
554
  uint64_t expectedResult;
Gerard Ryan's avatar
Gerard Ryan committed
555
  // TEST_F CASE WHEN THE NUMBER IS LESS THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
556
  {
Gerard Ryan's avatar
Gerard Ryan committed
557 558
    T m("27");
    T p("240");
Gerard Ryan's avatar
Gerard Ryan committed
559 560 561 562 563

    calculatedResult = m.Mod(p);
    expectedResult = 27;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
564
      << msg << " Failure testing number_less_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
565
  }
Gerard Ryan's avatar
Gerard Ryan committed
566
  // TEST_F CASE WHEN THE NUMBER IS GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
567
  {
Gerard Ryan's avatar
Gerard Ryan committed
568 569
    T m("93409673");
    T p("406");
Gerard Ryan's avatar
Gerard Ryan committed
570 571 572 573 574

    calculatedResult = m.Mod(p);
    expectedResult = 35;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
575
      << msg << " Failure testing number_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
576
  }
Gerard Ryan's avatar
Gerard Ryan committed
577
  // TEST_F CASE WHEN THE NUMBER IS DIVISIBLE BY MOD
Gerard Ryan's avatar
Gerard Ryan committed
578
  {
Gerard Ryan's avatar
Gerard Ryan committed
579 580
    T m("32768");
    T p("16");
Gerard Ryan's avatar
Gerard Ryan committed
581 582 583 584 585

    calculatedResult = m.Mod(p);
    expectedResult = 0;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
586
      << msg << " Failure testing number_dividible_by_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
587 588
  }

Gerard Ryan's avatar
Gerard Ryan committed
589
  // TEST_F CASE WHEN THE NUMBER IS EQUAL TO MOD
Gerard Ryan's avatar
Gerard Ryan committed
590
  {
Gerard Ryan's avatar
Gerard Ryan committed
591 592
    T m("67108913");
    T p("67108913");
Gerard Ryan's avatar
Gerard Ryan committed
593 594 595 596 597

    calculatedResult = m.Mod(p);
    expectedResult = 0;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
598
      << msg << " Failure testing number_equal_to_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
599 600
  }

Gerard Ryan's avatar
Gerard Ryan committed
601
#ifdef OUT
Gerard Ryan's avatar
Gerard Ryan committed
602 603 604 605 606
  /************************************************/
  /* TESTING METHOD MOD BARRETT FOR ALL CONDITIONS */
  /************************************************/


Gerard Ryan's avatar
Gerard Ryan committed
607
  /* 	The method "Divided By" does division of T m by another T p
Gerard Ryan's avatar
Gerard Ryan committed
608
	Function takes b as argument and operates on a
Gerard Ryan's avatar
Gerard Ryan committed
609
  	Returns a/b, which is stored in another T calculatedResult
Gerard Ryan's avatar
Gerard Ryan committed
610 611 612 613 614 615 616
	ConvertToInt converts BigInteger calculatedResult to integer
	When b=0, throws error, since division by Zero is not allowed
	When a<b, returns 0, since decimal value is not returned
  */



Gerard Ryan's avatar
Gerard Ryan committed
617
  // TEST_F CASE WHEN THE NUMBER IS LESS THAN MOD			//NOT GIVING PROPER OUTPUT AS OF NOW
Gerard Ryan's avatar
Gerard Ryan committed
618

Gerard Ryan's avatar
Gerard Ryan committed
619
  TEST_F(UTBinInt_METHOD_MOD_BARRETT,NUMBER_LESS_THAN_MOD){
Gerard Ryan's avatar
Gerard Ryan committed
620

Gerard Ryan's avatar
Gerard Ryan committed
621 622 623
    T a("9587");
    T b("3591");
    T c("177");
Gerard Ryan's avatar
Gerard Ryan committed
624

Gerard Ryan's avatar
Gerard Ryan committed
625
    T calculatedResult = a.ModBarrett(b,c);
Gerard Ryan's avatar
Gerard Ryan committed
626 627 628 629 630 631
    int expectedResult = 205484;

    std::cout<<"\n"<<d.ConvertToInt()<<"\n";	//for testing purpose

    //EXPECT_EQ(27,calculatedResult.ConvertToInt());
    }
Gerard Ryan's avatar
Gerard Ryan committed
632 633 634 635 636 637 638 639 640
#endif
}

TEST_F(UTBinInt,mod_operations) {
	RUN_ALL_BACKENDS_INT(mod_test,"mod")
}

template<typename T>
void mod_inverse(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
641 642 643
  /*************************************************/
  /* TESTING METHOD MOD INVERSE FOR ALL CONDITIONS */
  /*************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
644
  // The method "Mod Inverse" operates on Ts m,p
Gerard Ryan's avatar
Gerard Ryan committed
645 646 647 648 649
  // Returns {(m)^(-1)}mod p
  //    which is multiplicative inverse of m with respect to p, and is
  //    uses extended Euclidean algorithm m and p are co-primes (i,e GCD
  //    of m and p is 1)
  // If m and p are not co-prime, the method throws an error
Gerard Ryan's avatar
Gerard Ryan committed
650
  // ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
651

Gerard Ryan's avatar
Gerard Ryan committed
652
  T calculatedResult;
Gerard Ryan's avatar
Gerard Ryan committed
653 654
  uint64_t expectedResult;

Gerard Ryan's avatar
Gerard Ryan committed
655
  // TEST_F CASE WHEN THE NUMBER IS GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
656
  {
Gerard Ryan's avatar
Gerard Ryan committed
657 658
    T m("5");
    T p("108");
Gerard Ryan's avatar
Gerard Ryan committed
659 660 661 662 663

    calculatedResult = m.ModInverse(p);
    expectedResult = 65;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
664
      << msg << " Failure testing number_less_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
665
  }
Gerard Ryan's avatar
Gerard Ryan committed
666
  // TEST_F CASE WHEN THE NUMBER AND MOD ARE NOT CO-PRIME
Gerard Ryan's avatar
Gerard Ryan committed
667
  {
Gerard Ryan's avatar
Gerard Ryan committed
668 669
    T m("3017");
    T p("108");
Gerard Ryan's avatar
Gerard Ryan committed
670 671 672 673 674

    calculatedResult = m.ModInverse(p);
    expectedResult = 77;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
675
      << msg << " Failure testing number_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
676 677 678 679 680 681 682
  }

  //TESTCASE 

  //testcase that failed during testing.
  {

Gerard Ryan's avatar
Gerard Ryan committed
683 684
    T first("4974113608263");
    T second("486376675628");
Gerard Ryan's avatar
Gerard Ryan committed
685
    string modcorrect("110346851983");
Gerard Ryan's avatar
Gerard Ryan committed
686
    T modresult;
Gerard Ryan's avatar
Gerard Ryan committed
687 688 689 690

    modresult = first.Mod(second);

    EXPECT_EQ(modcorrect, modresult.ToString())
Gerard Ryan's avatar
Gerard Ryan committed
691
      <<msg << " Failure ModInverse() Mod regression test";
Gerard Ryan's avatar
Gerard Ryan committed
692 693


Gerard Ryan's avatar
Gerard Ryan committed
694 695
    T input ("405107564542978792");
    T modulus("1152921504606847009");
Gerard Ryan's avatar
Gerard Ryan committed
696
    string modIcorrect("844019068664266609");
Gerard Ryan's avatar
Gerard Ryan committed
697
    T modIresult;
Gerard Ryan's avatar
Gerard Ryan committed
698 699 700 701 702 703 704 705 706 707

    bool thrown = false;
    try {
      modIresult = input.ModInverse(modulus);
    }
    catch (...){
      thrown = true;
    }

    EXPECT_FALSE(thrown)
Gerard Ryan's avatar
Gerard Ryan committed
708
      << msg << " Failure testing ModInverse() non co-prime arguments";
Gerard Ryan's avatar
Gerard Ryan committed
709
    EXPECT_EQ(modIcorrect, modIresult.ToString())
Gerard Ryan's avatar
Gerard Ryan committed
710
      <<msg << " Failure ModInverse() regression test";
Gerard Ryan's avatar
Gerard Ryan committed
711 712
  }

Gerard Ryan's avatar
Gerard Ryan committed
713
#ifdef OUT
Gerard Ryan's avatar
Gerard Ryan committed
714
  {
Gerard Ryan's avatar
Gerard Ryan committed
715 716 717
	//BBI just hangs, do not run this test.
    T first("4974113608263");
    T second("0");
Gerard Ryan's avatar
Gerard Ryan committed
718
    string modcorrect("4974113608263");
Gerard Ryan's avatar
Gerard Ryan committed
719
    T modresult;
Gerard Ryan's avatar
Gerard Ryan committed
720 721 722 723

    modresult = first.Mod(second);

    EXPECT_EQ(modcorrect, modresult.ToString())
Gerard Ryan's avatar
Gerard Ryan committed
724
      <<msg << " Failure ModInverse() Mod(0)";
Gerard Ryan's avatar
Gerard Ryan committed
725
  }
Gerard Ryan's avatar
Gerard Ryan committed
726
#endif
Gerard Ryan's avatar
Gerard Ryan committed
727 728 729

}

Gerard Ryan's avatar
Gerard Ryan committed
730 731 732
TEST_F(UTBinInt,mod_inverse) {
	RUN_ALL_BACKENDS_INT(mod_inverse,"modinv")
}
Gerard Ryan's avatar
Gerard Ryan committed
733

Gerard Ryan's avatar
Gerard Ryan committed
734 735 736
template<typename T>
void mod_arithmetic(const string& msg) {
  T calculatedResult;
Gerard Ryan's avatar
Gerard Ryan committed
737 738 739 740
  uint64_t expectedResult;
  /************************************************/
  /* TESTING METHOD MODADD FOR ALL CONDITIONS     */
  /************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
741
  // The method "Mod Add" operates on Ts m,n,q
Gerard Ryan's avatar
Gerard Ryan committed
742 743 744
  //   Returns:
  //     (m+n)mod q
  //      = {(m mod q) + (n mod q)}mod q
Gerard Ryan's avatar
Gerard Ryan committed
745
  //   ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
746

Gerard Ryan's avatar
Gerard Ryan committed
747
  // TEST_F CASE WHEN THE FIRST NUMBER IS GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
748
  {
Gerard Ryan's avatar
Gerard Ryan committed
749 750 751
    T m("58059595");
    T n("3768");
    T q("4067");
Gerard Ryan's avatar
Gerard Ryan committed
752 753 754 755 756

    calculatedResult = m.ModAdd(n,q);
    expectedResult = 2871;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
757
      << msg << " Failure testing first_number_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
758
  }
Gerard Ryan's avatar
Gerard Ryan committed
759
  // TEST_F CASE WHEN THE SECOND NUMBER IS GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
760
  {
Gerard Ryan's avatar
Gerard Ryan committed
761 762 763
    T m("595");
    T n("376988");
    T q("4067");
Gerard Ryan's avatar
Gerard Ryan committed
764 765 766 767 768

    calculatedResult = m.ModAdd(n,q);
    expectedResult = 3419;

    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
769
      << msg << " Failure testing second_number_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
770
  }
Gerard Ryan's avatar
Gerard Ryan committed
771
  // TEST_F CASE WHEN THE BOTH NUMBERS ARE LESS THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
772
  {
Gerard Ryan's avatar
Gerard Ryan committed
773 774 775
    T m("595");
    T n("376");
    T q("4067");
Gerard Ryan's avatar
Gerard Ryan committed
776 777 778 779

    calculatedResult = m.ModAdd(n,q);
    expectedResult = 971;
    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
780
      << msg << " Failure testing both_numbers_less_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
781
  }
Gerard Ryan's avatar
Gerard Ryan committed
782
  // TEST_F CASE WHEN THE BOTH NUMBERS ARE GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
783 784
  {

Gerard Ryan's avatar
Gerard Ryan committed
785 786 787
    T m("59509095449");
    T n("37654969960");
    T q("4067");
Gerard Ryan's avatar
Gerard Ryan committed
788 789 790 791 792

    calculatedResult = m.ModAdd(n,q);
    expectedResult = 2861;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
793
      << msg << " Failure testing both_numbers_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
794 795 796 797 798 799
  }

  /************************************************/
  /* TESTING METHOD MODSUB FOR ALL CONDITIONS -*/
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
800
  // The method "Mod Sub" operates on Ts m,n,q
Gerard Ryan's avatar
Gerard Ryan committed
801 802 803 804 805 806
  //   Returns:
  //    (m-n)mod q
  //    = {(m mod q) - (n mod q)}mod q	when m>n
  //    = 0 when m=n
  //    = {(m mod q)+q-(n mod q)}mod q when m<n

Gerard Ryan's avatar
Gerard Ryan committed
807
  //   ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
808

Gerard Ryan's avatar
Gerard Ryan committed
809
  // TEST_F CASE WHEN THE FIRST NUMBER IS GREATER THAN MOD
Gerard Ryan's avatar
Gerard Ryan committed
810
  {
Gerard Ryan's avatar
Gerard Ryan committed
811 812 813
    T m("595");
    T n("399");
    T q("406");
Gerard Ryan's avatar
Gerard Ryan committed
814 815 816 817 818

    calculatedResult = m.ModSub(n,q);
    expectedResult = 196;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
819
      << msg << " Failure testing first_number_greater_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
820
  }
Gerard Ryan's avatar
Gerard Ryan committed
821
  // TEST_F CASE WHEN THE FIRST NUMBER LESS THAN SECOND NUMBER AND MOD
Gerard Ryan's avatar
Gerard Ryan committed
822
  {
Gerard Ryan's avatar
Gerard Ryan committed
823 824 825
    T m("39960");
    T n("595090959");
    T q("406756");
Gerard Ryan's avatar
Gerard Ryan committed
826 827 828 829 830 831

    calculatedResult = m.ModSub(n,q);
    expectedResult = 33029;

    //[{(a mod c)+ c} - (b mod c)] since a < b
    EXPECT_EQ(expectedResult,calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
832
      << msg << " Failure testing first_number_less_than_modulus";
Gerard Ryan's avatar
Gerard Ryan committed
833
  }
Gerard Ryan's avatar
Gerard Ryan committed
834
  // TEST_F CASE WHEN THE FIRST NUMBER EQUAL TO SECOND NUMBER
Gerard Ryan's avatar
Gerard Ryan committed
835
  {
Gerard Ryan's avatar
Gerard Ryan committed
836 837 838
    T m("595090959");
    T n("595090959");
    T q("406756");
Gerard Ryan's avatar
Gerard Ryan committed
839 840 841 842 843

    calculatedResult = m.ModSub(n,q);
    expectedResult = 0;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
844
      << msg << " Failure testing first_number_equals_second_number";
Gerard Ryan's avatar
Gerard Ryan committed
845 846 847 848 849 850
  }

  /************************************************/
  /* TESTING METHOD MODMUL FOR ALL CONDITIONS     */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
851
  // The method "Mod Mul" operates on Ts m,n,q
Gerard Ryan's avatar
Gerard Ryan committed
852
  //   Returns:  (m*n)mod q
Gerard Ryan's avatar
Gerard Ryan committed
853
  //              = {(m mod q)*(n mod q)}mod q
Gerard Ryan's avatar
Gerard Ryan committed
854
  // ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
855

Gerard Ryan's avatar
Gerard Ryan committed
856
  // FIRST > MOD
Gerard Ryan's avatar
Gerard Ryan committed
857
  {
Gerard Ryan's avatar
Gerard Ryan committed
858 859 860
    T m("38");
    T n("4");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
861

Gerard Ryan's avatar
Gerard Ryan committed
862
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
863 864 865
    uint64_t expectedResult = 24;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
866
      << msg << " Failure testing ModMul first > mod";
Gerard Ryan's avatar
Gerard Ryan committed
867 868 869 870
  }

  // FIRST == MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
871 872 873
    T m("32");
    T n("4");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
874

Gerard Ryan's avatar
Gerard Ryan committed
875
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
876 877 878
    uint64_t expectedResult = 0;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
879
      << msg << " Failure testing ModMul first == mod";
Gerard Ryan's avatar
Gerard Ryan committed
880 881 882 883
  }

  // SECOND > MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
884 885 886
    T m("3");
    T n("37");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
887

Gerard Ryan's avatar
Gerard Ryan committed
888
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
889 890 891
    uint64_t expectedResult = 15;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
892
      << msg << " Failure testing ModMul second > mod";
Gerard Ryan's avatar
Gerard Ryan committed
893 894 895 896
  }

  // SECOND == MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
897 898 899
    T m("3");
    T n("32");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
900

Gerard Ryan's avatar
Gerard Ryan committed
901
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
902
    uint64_t expectedResult = 0;
Gerard Ryan's avatar
Gerard Ryan committed
903 904

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
905
      << msg << " Failure testing ModMul second == mod";
Gerard Ryan's avatar
Gerard Ryan committed
906 907
  }

Gerard Ryan's avatar
Gerard Ryan committed
908 909
  // BOTH > MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
910 911 912
    T m("36");
    T n("37");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
913

Gerard Ryan's avatar
Gerard Ryan committed
914
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
915 916 917
    uint64_t expectedResult = 20;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
918
      << msg << " Failure testing ModMul both > mod";
Gerard Ryan's avatar
Gerard Ryan committed
919 920 921 922
  }

  // BOTH == MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
923 924 925
    T m("32");
    T n("32");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
926

Gerard Ryan's avatar
Gerard Ryan committed
927
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
928 929 930
    uint64_t expectedResult = 0;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
931
      << msg << " Failure testing ModMul both == mod";
Gerard Ryan's avatar
Gerard Ryan committed
932 933 934 935
  }

  // PRODUCT > MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
936 937 938
    T m("39");
    T n("37");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
939

Gerard Ryan's avatar
Gerard Ryan committed
940
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
941 942 943
    uint64_t expectedResult = 3;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
944
      << msg << " Failure testing ModMul product > mod";
Gerard Ryan's avatar
Gerard Ryan committed
945 946 947 948
  }

  // PRODUCT == MOD
  {
Gerard Ryan's avatar
Gerard Ryan committed
949 950 951
    T m("8");
    T n("4");
    T q("32");
Gerard Ryan's avatar
Gerard Ryan committed
952

Gerard Ryan's avatar
Gerard Ryan committed
953
    T calculatedResult = m.ModMul(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
954 955 956
    uint64_t expectedResult = 0;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
957
      << msg << " Failure testing ModMul product == mod";
Gerard Ryan's avatar
Gerard Ryan committed
958 959 960
  }


Gerard Ryan's avatar
Gerard Ryan committed
961 962 963 964
  /************************************************/
  /* TESTING METHOD MODEXP FOR ALL CONDITIONS     */
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
965
  // The method "Mod Exp" operates on Ts m,n,q
Gerard Ryan's avatar
Gerard Ryan committed
966 967
  // Returns:  (m^n)mod q
  //   = {(m mod q)^(n mod q)}mod q
Gerard Ryan's avatar
Gerard Ryan committed
968
  // ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
969 970

  {
Gerard Ryan's avatar
Gerard Ryan committed
971 972 973
    T m("39960");
    T n("9");
    T q("406756");
Gerard Ryan's avatar
Gerard Ryan committed
974

Gerard Ryan's avatar
Gerard Ryan committed
975
    T calculatedResult = m.ModExp(n,q);
Gerard Ryan's avatar
Gerard Ryan committed
976 977 978
    uint64_t expectedResult = 96776;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
979
      << msg << " Failure testing mod_exp_test";
Gerard Ryan's avatar
Gerard Ryan committed
980 981 982
  }
}

Gerard Ryan's avatar
Gerard Ryan committed
983 984 985
TEST_F(UTBinInt,mod_arithmetic) {
	RUN_ALL_BACKENDS_INT(mod_arithmetic,"mod_arithmetic")
}
Gerard Ryan's avatar
Gerard Ryan committed
986

Gerard Ryan's avatar
Gerard Ryan committed
987 988 989 990
template<typename T>
void big_modexp(const string& msg) {
	bool dbg_flag = false;
	TimeVar t;
Gerard Ryan's avatar
Gerard Ryan committed
991

Gerard Ryan's avatar
Gerard Ryan committed
992 993 994 995
	TIC(t);
	T m("150802716267100577727763462252");
	T n("507060240091291760598681282151");
	T q("1014120480182583521197362564303");
Gerard Ryan's avatar
Gerard Ryan committed
996

Gerard Ryan's avatar
Gerard Ryan committed
997 998
	T calculatedResult = m.ModExp(n,q);
	T expectedResult("187237443793760596004690725849");
Gerard Ryan's avatar
Gerard Ryan committed
999

Gerard Ryan's avatar
Gerard Ryan committed
1000 1001 1002 1003 1004
	EXPECT_EQ(expectedResult, calculatedResult)
	<< msg << " Failure testing very big mod_exp_test";


	DEBUG("big_modexp time ns "<<TOC_NS(t));
Gerard Ryan's avatar
Gerard Ryan committed
1005 1006
}

Gerard Ryan's avatar
Gerard Ryan committed
1007 1008 1009
TEST_F(UTBinInt,big_modexp) {
	RUN_BIG_BACKENDS_INT(big_modexp,"big_modexp")
}
Gerard Ryan's avatar
Gerard Ryan committed
1010

Gerard Ryan's avatar
Gerard Ryan committed
1011 1012 1013 1014 1015
template<typename T>
void power_2_modexp(const string& msg) {
	T m("2");
	T n("50");
	T q("16");
Gerard Ryan's avatar
Gerard Ryan committed
1016

Gerard Ryan's avatar
Gerard Ryan committed
1017 1018 1019 1020 1021 1022 1023 1024
	T calculatedResult = m.ModExp(n,q);
	T expectedResult(0);

	EXPECT_EQ( expectedResult, calculatedResult ) << msg << " Failure testing TWO.ModExp(50,16)";
}

TEST_F(UTBinInt,power_2_modexp) {
	RUN_ALL_BACKENDS_INT(power_2_modexp,"power_2_modexp")
Gerard Ryan's avatar
Gerard Ryan committed
1025 1026
}

Gerard Ryan's avatar
Gerard Ryan committed
1027 1028
template<typename T>
void shift(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
1029 1030 1031 1032 1033 1034 1035 1036 1037

  /****************************/
  /* TESTING SHIFT OPERATORS  */
  /****************************/

  /*******************************************************/
  /* TESTING OPERATOR LEFT SHIFT (<<) FOR ALL CONDITIONS */
  /*******************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
1038
  // The operator 'Left Shift' operates on T a, and it
Gerard Ryan's avatar
Gerard Ryan committed
1039 1040
  // is shifted by a number

Gerard Ryan's avatar
Gerard Ryan committed
1041
  // Returns: a<<(num), and the result is stored in Terger
Gerard Ryan's avatar
Gerard Ryan committed
1042 1043 1044 1045 1046 1047
  // calculatedResult 'a' is left shifted by 'num' number of bits, and
  // filled up by 0s from right which is equivalent to a * (2^num)
  //
  //        example:
  //            4<<3 => (100)<<3 => (100000) => 32
  //           this is equivalent to: 4* (2^3) => 4*8 =32
Gerard Ryan's avatar
Gerard Ryan committed
1048
  //ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
1049

Gerard Ryan's avatar
Gerard Ryan committed
1050
  // TEST_F CASE WHEN SHIFT IS LESS THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1051
  {
Gerard Ryan's avatar
Gerard Ryan committed
1052
    T a("39960");
Gerard Ryan's avatar
Gerard Ryan committed
1053 1054
    usshort shift = 3;

Gerard Ryan's avatar
Gerard Ryan committed
1055
    T calculatedResult = a<<(shift);
Gerard Ryan's avatar
Gerard Ryan committed
1056 1057 1058
    uint64_t expectedResult = 319680;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1059
      << msg << " Failure testing shift_less_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1060
  }
Gerard Ryan's avatar
Gerard Ryan committed
1061
  // TEST_F CASE WHEN SHIFT IS GREATER THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1062
  {
Gerard Ryan's avatar
Gerard Ryan committed
1063
    T a("39960");
Gerard Ryan's avatar
Gerard Ryan committed
1064 1065
    usshort shift = 6;

Gerard Ryan's avatar
Gerard Ryan committed
1066
    T calculatedResult = a<<(shift);
Gerard Ryan's avatar
Gerard Ryan committed
1067 1068 1069
    uint64_t expectedResult = 2557440;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1070
      << msg << " Failure testing shift_greater_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1071 1072 1073 1074 1075 1076 1077
  }


  /************************************************/
  /* TESTING OPERATOR LEFT SHIFT EQUALS (<<=) FOR ALL CONDITIONS -*/
  /************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
1078
  // The operator 'Left Shift Equals' operates on T a,
Gerard Ryan's avatar
Gerard Ryan committed
1079 1080 1081 1082 1083 1084 1085
  // and it is shifted by a number
  // Returns:
  // a<<(num), and the result is stored in 'a'
  // 'a' is left shifted by 'num' number of bits, and filled up by 0s
  // from right which is equivalent to a * (2^num)
  // example :4<<3 => (100)<<3 => (100000) => 32
  // this is equivalent to: 4* (2^3) => 4*8 =32
Gerard Ryan's avatar
Gerard Ryan committed
1086
  // ConvertToInt converts T a to integer
Gerard Ryan's avatar
Gerard Ryan committed
1087 1088 1089 1090




Gerard Ryan's avatar
Gerard Ryan committed
1091
  // TEST_F CASE WHEN SHIFT IS LESS THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1092
  {
Gerard Ryan's avatar
Gerard Ryan committed
1093
    T a("39960");
Gerard Ryan's avatar
Gerard Ryan committed
1094 1095 1096 1097 1098 1099
    usshort num = 3;

    a<<=(num);
    uint64_t expectedResult = 319680;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1100
      << msg << " Failure testing shift_less_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1101
  }
Gerard Ryan's avatar
Gerard Ryan committed
1102
  // TEST_F CASE WHEN SHIFT IS GREATER THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1103
  {
Gerard Ryan's avatar
Gerard Ryan committed
1104
    T a("39960");
Gerard Ryan's avatar
Gerard Ryan committed
1105 1106 1107 1108 1109 1110
    usshort num = 6;

    a<<=(num);
    uint64_t expectedResult = 2557440;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1111
      << msg << " Failure testing shift_greater_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1112 1113 1114 1115 1116 1117
  }


  /********************************************************/
  /* TESTING OPERATOR RIGHT SHIFT (>>) FOR ALL CONDITIONS */
  /********************************************************/
Gerard Ryan's avatar
Gerard Ryan committed
1118
  // The operator 'Right Shift' operates on T a, and it
Gerard Ryan's avatar
Gerard Ryan committed
1119 1120
  // is shifted by a number

Gerard Ryan's avatar
Gerard Ryan committed
1121
  // Returns: a>>(num), and the result is stored in T
Gerard Ryan's avatar
Gerard Ryan committed
1122 1123 1124 1125 1126 1127
  // calculated. Result 'a' is right shifted by 'num' number of bits,
  // and filled up by 0s from left which is equivalent to a / (2^num)

  //  ex:4>>3 => (100000)>>3 => (000100) => 4

  // this is equivalent to: 32*(2^3) => 32/8 = 4
Gerard Ryan's avatar
Gerard Ryan committed
1128
  // ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
1129 1130


Gerard Ryan's avatar
Gerard Ryan committed
1131
  // TEST_F CASE WHEN SHIFT IS LESS THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1132
  {
Gerard Ryan's avatar
Gerard Ryan committed
1133
    T a("39965675");
Gerard Ryan's avatar
Gerard Ryan committed
1134 1135
    usshort shift = 3;

Gerard Ryan's avatar
Gerard Ryan committed
1136
    T calculatedResult = a>>(shift);
Gerard Ryan's avatar
Gerard Ryan committed
1137 1138 1139
    uint64_t expectedResult = 4995709;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1140
      << msg << " Failure testing shift_less_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1141
  }
Gerard Ryan's avatar
Gerard Ryan committed
1142
  // TEST_F CASE WHEN SHIFT IS GREATER THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1143
  {
Gerard Ryan's avatar
Gerard Ryan committed
1144
    T a("39965675");
Gerard Ryan's avatar
Gerard Ryan committed
1145 1146
    usshort shift = 6;

Gerard Ryan's avatar
Gerard Ryan committed
1147
    T calculatedResult = a>>(shift);
Gerard Ryan's avatar
Gerard Ryan committed
1148 1149 1150
    uint64_t expectedResult = 624463;

    EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1151
      << msg << " Failure testing shift_greater_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1152 1153 1154 1155 1156 1157 1158
  }


  /***************************************************************/
  /* TESTING OPERATOR RIGHT SHIFT EQUALS(>>=) FOR ALL CONDITIONS */
  /***************************************************************/

Gerard Ryan's avatar
Gerard Ryan committed
1159
  // The operator 'Right Shift Equals' operates on T a,
Gerard Ryan's avatar
Gerard Ryan committed
1160 1161 1162 1163 1164 1165 1166 1167 1168
  // and it is shifted by a number

  // Returns: a>>=(num), and the result is stored in a 'a' is right
  // shifted by 'num' number of bits, and filled up by 0s from left
  // which is equivalent to a / (2^num)

  //   ex:4>>3 => (100000)>>3 => (000100) => 4

  //   this is equivalent to: 32*(2^3) => 32/8 = 4
Gerard Ryan's avatar
Gerard Ryan committed
1169
  //   ConvertToInt converts T calculatedResult to integer
Gerard Ryan's avatar
Gerard Ryan committed
1170 1171


Gerard Ryan's avatar
Gerard Ryan committed
1172
  // TEST_F CASE WHEN SHIFT IS LESS THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1173
  {
Gerard Ryan's avatar
Gerard Ryan committed
1174
    T a("39965675");
Gerard Ryan's avatar
Gerard Ryan committed
1175 1176 1177 1178 1179 1180
    usshort shift = 3;

    a>>=(shift);
    uint64_t expectedResult = 4995709;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1181
      << msg << " Failure testing shift_less_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1182
  }
Gerard Ryan's avatar
Gerard Ryan committed
1183
  // TEST_F CASE WHEN SHIFT IS GREATER THAN 4 (MAX SHIFT DONE AT A TIME)
Gerard Ryan's avatar
Gerard Ryan committed
1184
  {
Gerard Ryan's avatar
Gerard Ryan committed
1185
    T a("39965675");
Gerard Ryan's avatar
Gerard Ryan committed
1186 1187 1188 1189 1190 1191
    usshort shift = 6;

    a>>=(shift);
    uint64_t expectedResult = 624463;

    EXPECT_EQ(expectedResult, a.ConvertToInt())
Gerard Ryan's avatar
Gerard Ryan committed
1192
      << msg << " Failure testing shift_greater_than_max_shift";
Gerard Ryan's avatar
Gerard Ryan committed
1193 1194 1195
  }
}

Gerard Ryan's avatar
Gerard Ryan committed
1196 1197 1198 1199
TEST_F(UTBinInt,shift) {
	RUN_ALL_BACKENDS_INT(shift,"shift")
}

Gerard Ryan's avatar
Gerard Ryan committed
1200 1201 1202 1203
/****************************************/
/* TESTING METHOD  BitStringToBigInteger */
/****************************************/

Gerard Ryan's avatar
Gerard Ryan committed
1204 1205 1206
template<typename T>
void binString(const string& msg) {
	//TEST_F CASE FOR STATIC METHOD BitStringToBigInteger in BigInteger
Gerard Ryan's avatar
Gerard Ryan committed
1207

Gerard Ryan's avatar
Gerard Ryan committed
1208 1209
	string binaryString = "1011101101110001111010111011000000011";
	T b = T::BitStringToBigInteger(binaryString);
Gerard Ryan's avatar
Gerard Ryan committed
1210

Gerard Ryan's avatar
Gerard Ryan committed
1211 1212 1213
	T expectedResult("100633769475");
	EXPECT_EQ(expectedResult, b)
	<< msg << " Failure testing BitStringToBigInteger";
Gerard Ryan's avatar
Gerard Ryan committed
1214 1215
}

Gerard Ryan's avatar
Gerard Ryan committed
1216 1217 1218
TEST_F(UTBinInt,binString) {
	RUN_ALL_BACKENDS_INT(binString,"binString")
}
Gerard Ryan's avatar
Gerard Ryan committed
1219

Gerard Ryan's avatar
Gerard Ryan committed
1220 1221
template<typename T>
void expNoMod(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
1222

Gerard Ryan's avatar
Gerard Ryan committed
1223 1224 1225 1226
  T x("56");
  T result = x.Exp(10);

  T expectedResult("303305489096114176");
Gerard Ryan's avatar
Gerard Ryan committed
1227
  EXPECT_EQ(expectedResult, result)
Gerard Ryan's avatar
Gerard Ryan committed
1228 1229 1230 1231 1232
    << msg << " Failure testing exp";
}

TEST_F(UTBinInt,expNoMod) {
	RUN_ALL_BACKENDS_INT(expNoMod,"expNoMod")
Gerard Ryan's avatar
Gerard Ryan committed
1233 1234
}

Gerard Ryan's avatar
Gerard Ryan committed
1235 1236 1237
template<typename T>
void convToDouble(const string& msg) {
  T x("104037585658683683");
Gerard Ryan's avatar
Gerard Ryan committed
1238 1239
  double xInDouble = 104037585658683683;

Gerard Ryan's avatar
Gerard Ryan committed
1240 1241 1242 1243 1244
  EXPECT_EQ(xInDouble, x.ConvertToDouble()) << msg;
}

TEST_F(UTBinInt,convToDouble) {
	RUN_ALL_BACKENDS_INT(convToDouble,"convToDouble")
Gerard Ryan's avatar
Gerard Ryan committed
1245 1246
}

Gerard Ryan's avatar
Gerard Ryan committed
1247 1248 1249 1250 1251 1252 1253 1254 1255
template<typename T>
void getDigitAtIndex(const string& msg) {
	T x(0xa);

	EXPECT_EQ(x.GetDigitAtIndexForBase(1,2), 0ULL) << msg;
	EXPECT_EQ(x.GetDigitAtIndexForBase(2,2), 1ULL) << msg;
	EXPECT_EQ(x.GetDigitAtIndexForBase(3,2), 0ULL) << msg;
	EXPECT_EQ(x.GetDigitAtIndexForBase(4,2), 1ULL) << msg;
}
Gerard Ryan's avatar
Gerard Ryan committed
1256

Gerard Ryan's avatar
Gerard Ryan committed
1257 1258
TEST_F(UTBinInt,getDigitAtIndex) {
	RUN_ALL_BACKENDS_INT(getDigitAtIndex,"getDigitAtIndex")
Gerard Ryan's avatar
Gerard Ryan committed
1259 1260
}

Gerard Ryan's avatar
Gerard Ryan committed
1261 1262
template<typename T>
void GetBitAtIndex(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
1263
  bool dbg_flag = false;
Gerard Ryan's avatar
Gerard Ryan committed
1264
  T x(1);
Gerard Ryan's avatar
Gerard Ryan committed
1265

Gerard Ryan's avatar
Gerard Ryan committed
1266
  x <<= 55; //x has one bit at 55
Gerard Ryan's avatar
Gerard Ryan committed
1267

Gerard Ryan's avatar
Gerard Ryan committed
1268
  x += T(2); //x has one bit at 2
Gerard Ryan's avatar
Gerard Ryan committed
1269 1270

  DEBUG("x "<<x);
Gerard Ryan's avatar
Gerard Ryan committed
1271
  DEBUG(x.GetInternalRepresentation());
Gerard Ryan's avatar
Gerard Ryan committed
1272 1273

  // index is 1 for lsb!
Gerard Ryan's avatar
Gerard Ryan committed
1274 1275
  EXPECT_EQ(x.GetBitAtIndex(1), 0) << msg;
  EXPECT_EQ(x.GetBitAtIndex(2), 1) << msg;
Gerard Ryan's avatar
Gerard Ryan committed
1276

Gerard Ryan's avatar
Gerard Ryan committed
1277 1278
  for (auto idx = 3; idx < 55; idx++){
    EXPECT_EQ(x.GetBitAtIndex(idx), 0) << msg;
Gerard Ryan's avatar
Gerard Ryan committed
1279
  }
Gerard Ryan's avatar
Gerard Ryan committed
1280
  EXPECT_EQ(x.GetBitAtIndex(56), 1) << msg;
Gerard Ryan's avatar
Gerard Ryan committed
1281 1282

}
Gerard Ryan's avatar
Gerard Ryan committed
1283

Gerard Ryan's avatar
Gerard Ryan committed
1284 1285 1286
TEST_F(UTBinInt,GetBitAtIndex) {
	RUN_ALL_BACKENDS_INT(GetBitAtIndex,"GetBitAtIndex")
}
Gerard Ryan's avatar
Gerard Ryan committed
1287

Gerard Ryan's avatar
Gerard Ryan committed
1288 1289
template<typename T>
void GetInternalRepresentation(const string& msg) {
Gerard Ryan's avatar
Gerard Ryan committed
1290
  bool dbg_flag = false;
Gerard Ryan's avatar
Gerard Ryan committed
1291
  T x(1);
Gerard Ryan's avatar
Gerard Ryan committed
1292

Gerard Ryan's avatar
Gerard Ryan committed
1293 1294
  x <<= 100; //x has one bit at 128
  x += T(2); //x has one bit at 2
Gerard Ryan's avatar
Gerard Ryan committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

  auto x_limbs = x.GetInternalRepresentation();

  if (dbg_flag) {
    DEBUG(x_limbs);
    DEBUG("x_limbs "<< x_limbs);
    DEBUG("x "<<x);
  }

  //define what is correct based on math backend selected
Gerard Ryan's avatar
Gerard Ryan committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
  string correct("2 0 0 16");

  if( typeid(T) == typeid(M6Integer) )
	  correct = "2 68719476736";

  EXPECT_EQ(correct, x_limbs) << msg;
}

TEST_F(UTBinInt,GetInternalRepresentation) {
	RUN_BIG_BACKENDS_INT(GetInternalRepresentation,"GetInternalRepresentation")
Gerard Ryan's avatar
Gerard Ryan committed
1315
}