Sign in to follow this  
Defeatist

C++ value template questions.

Recommended Posts

Hey! I have a few questions regarding C++ templates that take a value.

What I want to be able to do in a declaration is:


encryption::rsa<1024> my_1024bit_rsa;
encryption::rsa<2048> my_2048bit_rsa;

math::unsigned_integer<128> my_128bit_unsigned_integer;
math::integer<4096> my_4096bit_signed_integer;
//etc



mainly because I want to force people to think about what size they pick, and secondly because i've read it allows for extra optimization by the compiler.

What I have so far is

header rsa.hpp

template<unsigned int size = 1024>class rsa
{
public:
// constructs a new pair of keys
rsa();

math::integer encrypt(const math::integer& message) const;
math::integer decrypt(const math::integer& message) const;

// defines a single key
struct key
{
math::integer exponent;
math::integer modulus;
};

protected:

private:
rsa(const rsa&);
rsa& operator= (const rsa&);

bool valid_;

key key_encryption;
key key_decryption;
};





source rsa.cpp

rsa::rsa() // <------ error 1
: valid_(false)
{
while(!valid_)
{
// we want p and q to be random primes and p != q
math::integer p(math::integer::random(size));
math::integer q(math::integer::random(size));

while(!math::prime::isprime(p)){
p = math::integer::random(size);
}
while(!math::prime::isprime(q) || q == p){
q = math::integer::random(size);
}

// n is our modulus
math::integer n(p*q);
// it must be able to take the modulus of our largest chunk, I picked the size of a char
if(n < 256) continue;

// phi is our totient
math::integer phi((p - 1) * (q - 1));

// e is our encryption exponent
math::integer e = rand() % (65537);

// e must be coprime with phi
// e can not be even
// e can not be a factor of phi
// 5 <= e < 65537
while(math::greatestcommondivisor(phi, e) != 1
|| e % 2 == 0
|| phi % e == 0){
e = (rand() % (65537 - 5) + 5;
}

// d is decryption exponent
try
{
math::integer d(math::modularlineequation(e, 1, phi));
}
// the equation is not always solvable
catch(...)
{
continue;
}

// we need a positive exponent
if(d <= 5){
continue
}else{
valid_ = true;
}
}

// asign the keys
key_encryption.modulus = n;
key_encryption.exponent = e;

key_decryption.modulus = n;
key_decryption.exponent = d;
}

integer rsa::encrypt(const integer& message) const
{
return math::integer::power(message, key_encryption.exponent) % key_encryption.modulus;
}

integer rsa::decrypt(const integer& message) const
{
return math::integer::power(message, key_decryption.exponent) % key_decryption.modulus;
}





compiling results in (at least one) error. I've marked the line it mentions in the source file
/core/encryption/rsa.cpp|8|error: ‘template<unsigned int size> class core::encryption::rsa’ used without template parameters|

/core/encryption/rsa.cpp|8|error: ISO C++ forbids declaration of ‘rsa’ with no type|
/core/encryption/rsa.cpp|8|error: ‘int core::encryption::rsa()’ redeclared as different kind of symbol|
/core/encryption/rsa.hpp|10|error: previous declaration of ‘template<unsigned int size> class core::encryption::rsa’|




Does this mean I can only declare the constructor in the header?
Can I use the
unsigned int size
as an rvalue in my code?

How would I define a template operator that can handle the following operation (regardless of the actual comparing data part)?
math::integer<1024> == math::integer<256>

Share this post


Link to post
Share on other sites
When implementing template functions outside the header, you must use the template keyword:

template<unsigned int size>
rsa::rsa()
{
// ...
}


Of course, a template must be implemented in a header file (or some non source file that gets included) to avoid linker errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Defeatist

How would I define a template operator that can handle the following operation (regardless of the actual comparing data part)?
math::integer<1024> == math::integer<256>


template <size_t X, size_t Y>
void foo(integer<X> & a, integer<Y> & b);

Share this post


Link to post
Share on other sites
Quote:
Original post by Defeatist
source rsa.cpp


Also note that templates should be defined either in header files, or in translation units but then using explicit instantiation (which should be a valid option in your case). Otherwise, there's only exported templates, which were never really supported (apart from one commercial compiler frontend) and which are deprecated as of C++0x.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this