Skip to content

Commit

Permalink
Fix for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
pons committed Feb 28, 2019
1 parent 46a8511 commit 90c4b35
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
52 changes: 32 additions & 20 deletions Int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ void Int::ShiftL32BitAndSub(Int *a,int n) {
// ------------------------------------------------

void Int::ShiftL(uint32_t n) {

if( n<64 ) {
shiftL((unsigned char)n, bits64);
shiftL((unsigned char)n, bits64);
} else {
uint32_t nb64 = n/64;
uint32_t nb = n%64;
Expand Down Expand Up @@ -520,10 +520,10 @@ void Int::ShiftR64Bit() {

}

// ------------------------------------------------
// ---------------------------------D---------------

void Int::ShiftR(uint32_t n) {

if( n<64 ) {
shiftR((unsigned char)n, bits64);
} else {
Expand Down Expand Up @@ -772,9 +772,9 @@ void Int::Div(Int *a,Int *mod) {
CLEAR();

// Size
int dSize = d.GetSize();
int tSize = rem.GetSize();
int qSize = tSize - dSize + 1;
uint32_t dSize = d.GetSize();
uint32_t tSize = rem.GetSize();
uint32_t qSize = tSize - dSize + 1;

// D1 normalize the divisor
uint32_t shift = bitLength(d.bits[dSize-1]);
Expand Down Expand Up @@ -1129,26 +1129,28 @@ void Int::Check() {
}

// Mult -------------------------------------------------------------------------------------------
a.SetBase10("25788151703741741859559789197707857");
b.SetBase10("150879472214070274535718959598325831");
a.SetBase10("3890902718436931151119442452387018319292503094706912504064239834754167");
b.SetBase10("474325684416838476798716793141429285759783676422570987096960746354");
e.SetBase10("1845555094921934741640873731771879197054909502699192730283220486240724687661257894226660948002650341240452881231721004292250660431557118");

t0 = Timer::get_tick();
for (i = 0; i < 10000; i++) c.Mult(&a, &b);
t1 = Timer::get_tick();

if (c.GetBase10() == "3890902718436931151119442452387018319292503094706912504064239834754167") {
if (c.IsEqual(&e)) {
printf("Mult() Results OK : ");
Timer::printResult("Mult", 10000, t0, t1);
} else {
printf("Mult() Results Wrong\nR=%s\nT=3890902718436931151119442452387018319292503094706912504064239834754167\n", c.GetBase10().c_str());
printf("Mult() Results Wrong\nR=%s\nT=%s\n",e.GetBase10().c_str(), c.GetBase10().c_str());
}

// Div -------------------------------------------------------------------------------------------
tTotal = 0.0;
for (int i = 0; i < 1000; i++) {
ok = true;
for (int i = 0; i < 1000 && ok; i++) {

a.Rand(BISIZE);
b.Rand(BISIZE / 2);
b.Rand(BISIZE/2);
d.Set(&a);
e.Set(&b);

Expand All @@ -1160,12 +1162,22 @@ void Int::Check() {
a.Mult(&e);
a.Add(&c);
if (!a.IsEqual(&d)) {
printf("Div() Results Wrong %d\n", i);
ok = false;
printf("Div() Results Wrong \nN: %s\nD: %s\nQ: %s\nR: %s\n",
d.GetBase16().c_str(),
b.GetBase16().c_str(),
a.GetBase16().c_str(),
c.GetBase16().c_str()

);
}

}
printf("Div() Results OK : ");
Timer::printResult("Div", 1000, 0, tTotal);

if(ok) {
printf("Div() Results OK : ");
Timer::printResult("Div", 1000, 0, tTotal);
}

// Modular arithmetic -------------------------------------------------------------------------------
// SecpK1 prime (needed for specific optimisation on the montgomery multiplication)
Expand Down Expand Up @@ -1241,7 +1253,7 @@ void Int::Check() {
}

t0 = Timer::get_tick();
for (int j = 0; j < 10000; j++) {
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < CPU_GRP_SIZE; i++) {
m[i].Rand(256);
}
Expand All @@ -1250,7 +1262,7 @@ void Int::Check() {
t1 = Timer::get_tick();

printf("IntGroup.ModInv() : ");
Timer::printResult("Inv", 10000 * CPU_GRP_SIZE, 0, t1 - t0);
Timer::printResult("Inv", 1000 * CPU_GRP_SIZE, 0, t1 - t0);


// ModSqrt ------------------------------------------------------------------------------------
Expand All @@ -1275,4 +1287,4 @@ void Int::Check() {
}
printf("ModSqrt() OK !\n");

}
}
5 changes: 3 additions & 2 deletions Int.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Int {
std::string GetC64Str(int nbDigit);

// Check function
static void Int::Check();
static void Check();


/*
Expand Down Expand Up @@ -201,9 +201,10 @@ static uint64_t inline __shiftright128(uint64_t a, uint64_t b,unsigned char n) {
return c;
}


static uint64_t inline __shiftleft128(uint64_t a, uint64_t b,unsigned char n) {
uint64_t c;
__asm__ ("movq %1,%0;shldq %3,%2,%0;" : "=D"(c) : "r"(a),"r"(b),"c"(n));
__asm__ ("movq %1,%0;shldq %3,%2,%0;" : "=D"(c) : "r"(b),"r"(a),"c"(n));
return c;
}

Expand Down

0 comments on commit 90c4b35

Please sign in to comment.