Can anyone share an example of RabinMillerTest() that works? My googlefu is sadly lacking.
Here is my test code:
#include "integer.h"
#include "nbtheory.h"
#include "cryptlib.h"
#include "osrng.h"
#include <iostream>
int main(int argc,char *argv[])
{
CryptoPP::RandomNumberGenerator rng;
CryptoPP::Integer a("123456789");
CryptoPP::Integer b;
std::cout << a << std::endl;
std::cout << "is prime: " << IsPrime(a) << std::endl;
b=a+CryptoPP::Integer::Two();
std::cout << b << std::endl;
std::cout << "is prime: " << IsPrime(b) << std::endl;
int r=RabinMillerTest(rng,&b,2);
std::cout << "RabinMiller: " << r <<std::endl;
}
Here is the build command:
g++ bignum.cpp -I. -fpermissive libcryptopp.a
The only example I could find is here:
http://www.cryptopp.com/wiki/Diffie-Hellman
My code crashes every time I run it, and one crash was so bad I had to cold boot to recover. If I remove the call to RabinMillerTest() then it runs fine. I suspect a problem with C++ syntax is the culprit, but I just don't see it. Since I can't use it properly, I am currently stripping the guts of the RabinMillerTest() function to use separately.
You're using the wrong signature for MillerRabinTest. Here's what it is (from nbtheory.h):
CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng,
const Integer &w, unsigned int rounds);
Instead of:
int r=RabinMillerTest(rng,&b,2);
Try:
bool b=RabinMillerTest(rng, b, 2);
There's also a discussion of Miller-Rabin round counts on the Cryptography Stack Exchange at Trial divisions before Miller-Rabin checks?. It was written with OpenSSL and Crypto++ in mind.
This was the answer edited into the question. Adding as a separate answer block. Credit goes to user3920315.
UPDATE: I got it to work. Here is the working code:
#include "integer.h"
#include "nbtheory.h"
#include "cryptlib.h"
#include "osrng.h"
#include <iostream>
int main(int argc,char *argv[])
{
//CryptoPP::RandomNumberGenerator rng;
CryptoPP::NonblockingRng rng;
CryptoPP::Integer a("123456789");
CryptoPP::Integer b;
std::cout << a << std::endl;
std::cout << "is prime: " << IsPrime(a) << std::endl;
b=a+CryptoPP::Integer::Two();
std::cout << b << std::endl;
std::cout << "is prime: " << IsPrime(b) << std::endl;
bool r=RabinMillerTest(rng,b,2);
std::cout << "RabinMiller: " << r <<std::endl;
}
I changed the call to RabinMillerTest() but the resulting executable would still crash.
When I changed the random number generator to a class that inherits from RandomNumberGenerator, then it works.
Related
I'm self learning C++ and for some reason "double" doesn't print more than 6 significant digits even after std::setprecision. Do I need to do something else? Most recent version of codeblocks if that helps. This is all the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
std::setprecision(9);
double A = 654321.987;
cout << A << endl;
return 0;
}
You need to feed the result of std::setprecision(9) to std::cout. Otherwise it has no way of knowing what output stream it applies to (and so it won't apply to anything).
std::cout << std::setprecision(9) << A << std::endl;
Or if you prefer you can do it separately:
std::cout << std::setprecision(9);
std::cout << A << std::endl;
Using the following bit of code compiled against boost 1.62:
#include <boost/rational.hpp>
#include <iostream>
int main() {
auto val = boost::rational<int64_t>(499999, 2);
std::cout << val << std::endl;
std::cout << boost::rational_cast<double>(val) << std::endl;
}
I get the following output:
499999/2
250000
I would expect rational_cast to output 249999.5
Can anyone explain what I am doing wrong?
Modify the default formatting for floating-point input/output:
std::cout << std::fixed << boost::rational_cast<double>(v) << std::endl; add std::fixed to it.
I am trying to load the pixel rgb/ga information of a png image into a matrix, using the library png++, to do some computations with them.
My Code (which does not work at the moment):
#include <iostream>
#include <png++/image.hpp>
#include <png++/rgb_pixel.hpp>
int main(int argc, const char * argv[]) {
const std::string path="img_03.png";
png::image< png::basic_rgb_pixel <unsigned char> > pic(path);
pixel=pic.get_pixel(0, 0);
pixelp = &pixel;
std::cout << "value=" << pic[10][10].red << std::endl; //output: '?'
std::cout << "value=" << pixel.red << std::endl; //nothing
std::cout << "pointer=" << pixelp << std::endl; //delivers adress
pic.read(path);
std::cout << "value=" << pic[10][10].red << std::endl; //nothing
pic.write("picOutput.png"); //same picture
return 0;
}
However, none of those methods work to get the rgb values of each pixel.
If there is another way to get rgb/ga information of each pixel, please mention it.
The line pic.write("picOutput.png"); delivers the same png i loaded in the line pic.read(path). This is a personal exercise for me to get more used to C++, criticise my code as much as you can.
Thanks!
Here comes the solution:
change line:
std::cout << "value=" << pic[10][10].red << std::endl; //nothing
with:
std::cout << "value=" << (int) pic[10][10].red << std::endl; //nothing
because std::cout can't output types of unsigned char.
Thanks to Alex!
For in-depth explanation, look here:
cout not printing unsigned char
I am learning how to use boost coroutines2 library. I have read some tutorials and started experimenting with it. But then I found something very confusing. Please take a look at this basic example.
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
source();
}
The result is, naturally, this:
First time.
Second time.
But, to my surprise, when I remove the 'source' call in the main function, the result is just the same! (According to tutorials, the coroutine is called first time at construction time, so it is ok that it is called, but should be now called only once!)
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
}
The result is still:
First time.
Second time.
When I remove the second 'yield' in the coroutine, the result is also the same:
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
}
int main()
{
cr::pull_type source{creator};
}
Result:
First time.
Second time.
How is that possible? How does it work? I expected that when I don't call the coroutine, then even if there is another 'yield' waiting, nothing will happen.
And I find also strange this behaviour:
When I add another 'source' statements in the main, the code still prints the same, as at the beginning!
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
source();
source();
}
Result:
First time.
Second time.
No error, even when sourcing more times than there are 'yield's.
Only after adding one more 'source' in the main function do I receive a runtime error (This application has requested the Runtime to terminate it in an unusual way...)
int main()
{
cr::pull_type source{creator};
source();
source();
source();
}
Could someone help me please with understanding this behaviour?
The reason lies in a bug in Boost. I checked that in Boost 1.65.1 everything works fine. Here is the proof: https://wandbox.org/permlink/pRuSgnwa3VPdqNUk
I just want to iterate through the members of an unordered map.
There are many simple examples on the web, including on this site, and yet none of them will compile. Apparently some examples are from a previous non-standard STL version, some are just old, and some are so new that my gcc 4.7.2 can't handle them. Please do not suggest the new auto iterator from C++11. I will get there some day when all my libraries are validated for that. Until then, I just want the old one to work. (see below for what I have tried)
Here is my test code:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[]) {
boost::unordered::unordered_map<std::string,int> umap;
//can't get gcc to accept the value_type()...
//umap.insert(boost::unordered_map::value_type("alpha",1));
//umap.insert(boost::unordered_map::value_type("beta",2));
//umap.insert(boost::unordered_map::value_type("gamma",3));
umap["alpha"]=1; //this works
umap["beta"]=2;
umap["gamma"]=3;
//can't get gcc to compile the iterator
//for (boost::unordered_map::iterator it=umap.begin();it!=umap.end();++it)
// std::cout << it->first <<", " << it->second << std::endl;
//gcc does not like it this way either
//for (int x=0;x<umap.size();x++)
// std::cout << x << " : " << umap[x].first << " = " << umap[x].second << std::endl;
//will gcc take this? No it does not
//for (int x=0;x<umap.size();x++)
// std::cout << x << " : " << umap[x] << std::endl;
//this does not work
//boost::unordered::unordered_map::iterator<std::string,int> it;
//this does not work
//boost::unordered::unordered_map::iterator it;
//for (it=umap.begin();it!=umap.end();++it)
// std::cout << it->first <<", " << it->second << std::endl;
//this does not work
//BOOST_FOREACH(boost::unordered_map::value_type value, umap) {
// std::cout << value.second;
// }
//std::cout << std::endl;
//this does not work either
//BOOST_FOREACH(boost::unordered_map::value_type<std::string,int> value, umap) {
// std::cout << value.second;
// }
//std::cout << std::endl;
std::cout << "umap size: " << umap.size() << std::endl;
std::cout << "umap max size: " << umap.max_size() << std::endl;
std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;
std::cout << "count beta: " << umap.count("beta") << std::endl;
}
Most of the errors are a variation of this:
error: 'template<class K, class T, class H, class P, class A> class boost::unordered::unordered_map' used without template parameters
Here is my build command:
g++ -I..\boost umap.cpp
I should be embarrassed for getting stuck on such a beginner's question, but from the volume of similar questions I am finding, this is just hard enough to stop a lot of people in their tracks. I have written hash containers before (back when it was recommended NOT to use STL) and I am very tempted to just write my own... but the right thing to do is learn to use as many existing tools as possible... help!
I've looked at the following questions on stackoverflow where I haven't found an answer:
iterate through unordered_map using boost_foreach
I tried:
BOOST_FOREACH(boost::unordered_map::value_type& value, umap) {
but it gives the same error I show below.
Unordered_map iterator invalidation
This one is close, but not quite my issue:
Iterator invalidation in boost::unordered_map
This one uses auto
and I can't switch compilers at this time.
C++ some questions on boost::unordered_map & boost::hash
This one is mostly about the theory of maps:
how to use boost::unordered_map
This is a rather complicated example, but you will see in my code I am already trying to declare iterators... they just won't compile.
How to use BOOST_FOREACH with an Unordered_map?
This is a nice example, but
it just does not compile. I tried a version of this in my code.
IT WORKS !
Here is the working code:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[]) {
boost::unordered::unordered_map<std::string,int> umap;
umap["alpha"]=1;
umap["beta"]=2;
umap["gamma"]=3;
boost::unordered::unordered_map<std::string,int>::iterator it;
for (it=umap.begin();it!=umap.end();++it)
std::cout << it->first <<", " << it->second << std::endl;
std::cout << "umap size: " << umap.size() << std::endl;
std::cout << "umap max size: " << umap.max_size() << std::endl;
std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;
std::cout << "count beta: " << umap.count("beta") << std::endl;
}
It was a syntax error. I was putting the type in the wrong place when declaring the iterator.
Thanks everyone for your responses.
try changing boost::unordered::unordered_map::iterator it; it to boost::unordered::unordered_map<std::string,int>::iterator it;
NOTE:
It is also possible, and a good idea in more complex situations, to create a typedef of it, such as typedef boost::unordered::unordered_map<std::string,int>::iterator UMapStringIntIt;, or whatever you may call it.
The answer is in the question, but the simple solution is here for your convenience:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[])
{
boost::unordered::unordered_map<std::string,int> umap;
umap["alpha"]=1;
umap["beta"]=2;
umap["gamma"]=3;
for ( auto it= umap.begin(); it != umap.end(); ++it )
std::cout << it->first <<", " << it->second << std::endl;
}