using pow in long long

Hi guys, is there anyway to use the pow function for long long?

i`m trying to do a modulo

1
2
3
4
5
6
unsigned long long sendAlice(const unsigned long long base, const unsigned long long secretKey,const unsigned long long modulo)
{
    long long aliceValue=pow(base,secretKey) % modulo;

    return aliceValue;
}


if i can`t use long long in pow, what are the other ways around it?
You can use any arithmetic type.
See overload (4) http://en.cppreference.com/w/cpp/numeric/math/pow

However, the results are subject to floating point loss of precision, and if the numbers are too large, floating point overflow.

See: http://ideone.com/SIK0hh

thank you! however, i`m dealing with pretty big numbers as i`m doing a diffie-hellman key exchange. So i think there will definitely be overflows, are there any ways to go about this?
Diffie-Hellman relies on accurate integer results; std::pow() won't do at all.
In any case, the Diffie-Hellman requires only modular exponentiation; and specialized algorithms exist for this. http://en.wikipedia.org/wiki/Modular_exponentiation

Perhaps you should just use a library. For instance, GMP http://gmplib.org/ has direct support for modular exponentiation (mpz_powm()).
thanks! i found another library which is NTL, may i know that it is suitable for the usage of Diffie-Hellman? However, i am clueless on how to implement it in my .cpp files. I followed the instructions stated on the website here for installation but i was unsuccessful on doing so as i am unable to run makeDesc as stated

http://www.shoup.net/ntl/doc/tour-win.html


can you advise me on how to install it if it is usable for my program? thanks in advance
> i found another library which is NTL, may i know that it is suitable for the usage of Diffie-Hellman?

Yes, it is a good, easy to use library, and it has everything that you would need.


> i am unable to run makeDesc as stated

1. Which is the compiler, version and platform? (eg. Visual Studio 2010 on Xp or GCC 4.7.2 on MinGW64)

2. Were you able to compile MakeDesc.cpp and create an executable?

3. If not, what were the compiler diagnostic (error) messages?

PS: Did you add <WinNtl unzipped dir >\WinNTL-xxx\include to the set of include directories to be searched by the compiler?

Last edited on
1. i`m using code blocks 10.05, compiler is mingw32-g++
2. i was not able to make the executable!

the error msg is as below.

Compiling: C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:10:25: error: NTL/version.h: No such file or directory
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:715: warning: deprecated conversion from string constant to 'char*'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:715: warning: deprecated conversion from string constant to 'char*'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp: In function 'int main()':
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:731: error: 'NTL_VERSION' was not declared in this scope
Last edited on
Add <WinNtl unzipped dir >\WinNTL-xxx\include to the set of include directories to be searched by the compiler

From the IDE, it would be something like
Menu => Project => Build Settings => Search Directories => Compiler

Ignore this warning: deprecated conversion from string constant to 'char*'.
It is probably just poorly written, nevertheless working, code.
i added the following path to the compiler on the additional paths

C:\Users\Larry\Desktop\WinNTL-6_0_0\include\NTL


however, it still gives me the same error.

am i doing it right?


Last edited on
> error: NTL/version.h: No such file or directory

The subdirectory NTL is specified in the #include directive.

So just C:\Users\Larry\Desktop\WinNTL-6_0_0\include
thanks! i fixed the compiler issue.

However 23 more errors came out! lol

Compiling: C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:715: warning: deprecated conversion from string constant to 'char*'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.cpp:715: warning: deprecated conversion from string constant to 'char*'
Linking console executable: C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.exe
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x9b): undefined reference to `touch_double(double*)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0xeb): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x104): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x134): undefined reference to `touch_double(double*)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x177): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x190): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x1fea): undefined reference to `val_ulong(unsigned long)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x200e): undefined reference to `touch_ulong(unsigned long*)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x202b): undefined reference to `val_uint(unsigned int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x204f): undefined reference to `touch_uint(unsigned int*)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x206c): undefined reference to `val_size_t(unsigned int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x2090): undefined reference to `touch_size_t(unsigned int*)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x2195): undefined reference to `val_int(int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x21b1): undefined reference to `val_uint(unsigned int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x21f8): undefined reference to `val_int(int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x2215): undefined reference to `val_uint(unsigned int)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x225c): undefined reference to `val_long(long)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x2278): undefined reference to `val_ulong(unsigned long)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x22bf): undefined reference to `val_long(long)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x22dc): undefined reference to `val_ulong(unsigned long)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x232d): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x239f): undefined reference to `val_double(double)'
C:\Users\Larry\Desktop\WinNTL-6_0_0\MakeDesc\MakeDesc.o:MakeDesc.cpp:(.text+0x2407): undefined reference to `val_long(long)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
23 errors, 2 warnings


i don`t get why they are undefined when the methods are declared above
> i don`t get why they are undefined when the methods are declared above

They may be declared, but they are not defined.

you might consider compiling and running the program MakeDesc,
whose source files are in directory "MakeDesc".


Add all the other C++ source files in directory "MakeDesc" to the project.

i have try this one,,you dont need,,long,,double is enough,,
you must use fast exponentiations,,
@junkz

i have no idea on how to implement fast exponential.
Any sources for that?

@JLBorges
i included the necessary files into the compiler paths.
However the result is still the same, the same errors appear
Add the files to what your IDE calls 'the project'

Or if you are not crippled without an IDE, from the command prompt :

1. cd to the directory "MakeDesc"
2. Add the MinGw bin directory to the environment variable 'PATH'
2. g++ -Wall -pedantic-errors -std=C++11 -O3 -oMakeDesc.exe *.cpp
3. MakeDesc.exe
@JLBorges

thanks man, that did the job
try this source:
int fastExp(double x, double y, double z)
{
const int maks = 10000;
double pangkat,h,A,idx,biner[maks];

pangkat=y; idx=0;

if (y==0)
h=1;
else
{
while (!(pangkat==0)){
biner[idx]=pangkat%2;
pangkat=floor(float(pangkat)/2.0);
idx++;
}

for (int i=0;i<idx;i++)
{
if (i==0)
{
A=x; h=1;
}
else
{
A=(A*A)%z;
}
if(biner[i]==1)
h=(A*h)%z;
}
}
return h;
}
Topic archived. No new replies allowed.