Click here to Skip to main content
15,915,845 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: OnLButtonDown not getting called for Slider Control in MFC.? Pin
David Crow21-Jun-13 10:12
David Crow21-Jun-13 10:12 
GeneralRe: OnLButtonDown not getting called for Slider Control in MFC.? Pin
mbatra3121-Jun-13 20:16
mbatra3121-Jun-13 20:16 
AnswerRe: OnLButtonDown not getting called for Slider Control in MFC.? Pin
David Crow24-Jun-13 5:29
David Crow24-Jun-13 5:29 
GeneralRe: OnLButtonDown not getting called for Slider Control in MFC.? Pin
mbatra3124-Jun-13 22:40
mbatra3124-Jun-13 22:40 
AnswerRe: OnLButtonDown not getting called for Slider Control in MFC.? Pin
David Crow25-Jun-13 2:57
David Crow25-Jun-13 2:57 
QuestionHow to retrieve the user defined data type into main function Pin
Manoj739019-Jun-13 22:54
Manoj739019-Jun-13 22:54 
AnswerRe: How to retrieve the user defined data type into main function Pin
CPallini20-Jun-13 1:57
mveCPallini20-Jun-13 1:57 
GeneralRe: How to retrieve the user defined data type into main function Pin
Manoj739020-Jun-13 18:36
Manoj739020-Jun-13 18:36 
These are the errors i am getting for the program

Manoj1.cc:25:17: error: template class without a name
Manoj1.cc:25:9: error: template declaration of ‘typedef’
Manoj1.cc:39:44: error: ‘P’ was not declared in this scope
Manoj1.cc:39:45: error: template argument 1 is invalid
Manoj1.cc:47:37: error: ‘EllipticCurve’ is not a template
Manoj1.cc:47:51: error: ‘P’ was not declared in this scope
Manoj1.cc:49:48: error: ‘P’ was not declared in this scope
Manoj1.cc:49:49: error: template argument 1 is invalid
Manoj1.cc:231:41: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc:231:55: error: ‘P’ was not declared in this scope
Manoj1.cc:243:59: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc:243:73: error: ‘P’ was not declared in this scope
Manoj1.cc:418:25: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc:418:39: error: ‘P’ was not declared in this scope
Manoj1.cc:420:31: error: ‘EllipticCurve’ is not a template
Manoj1.cc:420:45: error: ‘P’ was not declared in this scope
Manoj1.cc:521:36: error: ‘P’ was not declared in this scope
Manoj1.cc:521:37: error: template argument 1 is invalid
Manoj1.cc:525:36: error: ‘P’ was not declared in this scope
Manoj1.cc:525:37: error: template argument 1 is invalid
Manoj1.cc:533:64: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc:549:40: error: ‘P’ was not declared in this scope
Manoj1.cc:549:41: error: template argument 1 is invalid
Manoj1.cc:551:40: error: ‘P’ was not declared in this scope
Manoj1.cc:551:41: error: template argument 1 is invalid
Manoj1.cc: In member function ‘Cryptography::EllipticCurve::Point Cryptography::EllipticCurve::Point::scalarMultiply(int, const Cryptography::EllipticCurve::Point&)’:
Manoj1.cc:90:51: error: call of overloaded ‘Point(int, int, Cryptography::EllipticCurve&)’ is ambiguous
Manoj1.cc:90:51: note: candidates are:
Manoj1.cc:243:21: note: Cryptography::EllipticCurve::Point::Point(const ffe_t&, const ffe_t&, Cryptography::EllipticCurve&)
Manoj1.cc:231:21: note: Cryptography::EllipticCurve::Point::Point(int, int, Cryptography::EllipticCurve&)
Manoj1.cc: In member function ‘void Cryptography::EllipticCurve::Point::add(Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t&, Cryptography::EllipticCurve::Point::ffe_t&) const’:
Manoj1.cc:179:40: error: request for member ‘i’ in ‘x1’, which is of non-class type ‘Cryptography::EllipticCurve::Point::ffe_t {aka int}’
Manoj1.cc:179:47: error: request for member ‘i’ in ‘x1’, which is of non-class type ‘Cryptography::EllipticCurve::Point::ffe_t {aka int}’
Manoj1.cc: In function ‘Cryptography::EllipticCurve::Point Cryptography::operator+(const Cryptography::EllipticCurve::Point&, const Cryptography::EllipticCurve::Point&)’:
Manoj1.cc:364:52: error: call of overloaded ‘Point(Cryptography::EllipticCurve::Point::ffe_t&, Cryptography::EllipticCurve::Point::ffe_t&, Cryptography::EllipticCurve&)’ is ambiguous
Manoj1.cc:364:52: note: candidates are:
Manoj1.cc:243:21: note: Cryptography::EllipticCurve::Point::Point(const ffe_t&, const ffe_t&, Cryptography::EllipticCurve&)
Manoj1.cc:231:21: note: Cryptography::EllipticCurve::Point::Point(int, int, Cryptography::EllipticCurve&)
Manoj1.cc: In member function ‘void Cryptography::EllipticCurve::CalculatePoints()’:
Manoj1.cc:447:31: error: ‘P’ was not declared in this scope
Manoj1.cc:457:25: error: ‘x_val’ was not declared in this scope
Manoj1.cc:457:50: error: request for member ‘i’ in ‘((Cryptography::EllipticCurve*)this)->Cryptography::EllipticCurve::a_’, which is of non-class type ‘int’
Manoj1.cc:457:63: error: request for member ‘i’ in ‘((Cryptography::EllipticCurve*)this)->Cryptography::EllipticCurve::b_’, which is of non-class type ‘int’
Manoj1.cc:459:25: error: ‘y_val’ was not declared in this scope
Manoj1.cc:473:34: error: ‘x_val’ was not declared in this scope
Manoj1.cc:473:46: error: ‘y_val’ was not declared in this scope
Manoj1.cc:477:67: error: call of overloaded ‘Point(int&, int&, Cryptography::EllipticCurve&)’ is ambiguous
Manoj1.cc:477:67: note: candidates are:
Manoj1.cc:243:21: note: Cryptography::EllipticCurve::Point::Point(const ffe_t&, const ffe_t&, Cryptography::EllipticCurve&)
Manoj1.cc:231:21: note: Cryptography::EllipticCurve::Point::Point(int, int, Cryptography::EllipticCurve&)
Manoj1.cc: In member function ‘int Cryptography::EllipticCurve::Degree() const’:
Manoj1.cc:517:49: error: ‘P’ was not declared in this scope
Manoj1.cc: At global scope:
Manoj1.cc:561:22: error: expected nested-name-specifier before ‘EllipticCurve’
Manoj1.cc:561:35: error: expected initializer before ‘<’ token
Manoj1.cc:567:49: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc: In function ‘std::ostream& Cryptography::operator<<(std::ostream&, const Cryptography::EllipticCurve&)’:
Manoj1.cc:583:35: error: request for member ‘i’ in ‘EllipticCurve.b_’, which is of non-class type ‘const int’
Manoj1.cc: At global scope:
Manoj1.cc:603:34: error: expected initializer before ‘<’ token
Manoj1.cc:685:1: error: ‘Cryptography::EllipticCurve’ is not a template
Manoj1.cc:685:23: error: expected initializer before ‘my_encrypt’
Manoj1.cc:856:11: error: ‘::main’ must return ‘int’
Manoj1.cc: In function ‘int main()’:
Manoj1.cc:859:3: error: a template declaration cannot appear at block scope
Manoj1.cc:861:2: error: expected ‘;’ before ‘}’ token


The complete program is


#include <cstdlib>

#include <iostream>

#include <vector>

using namespace std;

#include <math.h>

#include "FiniteFieldElement.hpp"

namespace Cryptography

{

template<int p="">

typedef struct {
Cryptography::EllipticCurve

::Point Pa;
Cryptography::FiniteFieldElement

c1;
Cryptography::FiniteFieldElement

c2;
} encrypt_data_t;

class EllipticCurve

{
public:

typedef FiniteFieldElement

ffe_t;

class Point

{

friend class EllipticCurve

;

typedef FiniteFieldElement

ffe_t;

ffe_t x_;

ffe_t y_;

EllipticCurve *ec_;


void addDouble(int m, Point& acc)

{

if ( m > 0 )

{

Point r = acc;

for ( int n=0; n < m; ++n )

{

r += r; // doubling step

}

acc = r;

}

}


Point scalarMultiply(int k, const Point& a)

{

Point acc = a;

Point res = Point(0,0,*ec_);

int i = 0, j = 0;

int b = k;

while( b )

{

if ( b & 1 )

{

addDouble(i-j,acc);

res += acc;

j = i;
}

b >>= 1;

++i;

}

return res;

}



void add(ffe_t x1, ffe_t y1, ffe_t x2, ffe_t y2, ffe_t & xR, ffe_t & yR) const

{

if ( x1 == 0 && y1 == 0 )

{

xR = x2;

yR = y2;

return;

}

if ( x2 == 0 && y2 == 0 )

{

xR = x1;

yR = y1;

return;

}

if ( y1 == -y2 )

{

xR = yR = 0;

return;

}

// the additions

ffe_t s;

if ( x1 == x2 && y1 == y2 )

{

//2P

s = (3*(x1.i()*x1.i()) + ec_->a()) / (2*y1);

xR = ((s*s) - 2*x1);

}

else

{
//P+Q

s = (y1 - y2) / (x1 - x2);

xR = ((s*s) - x1 - x2);

}



if ( s != 0 )

{

yR = (-y1 + s*(x1 - xR));

}

else

{

xR = yR = 0;

}

}



Point(int x, int y)

: x_(x),

y_(y),

ec_(0)

{}



Point(int x, int y, EllipticCurve

& EllipticCurve)

: x_(x),

y_(y),

ec_(&EllipticCurve)

{}



Point(const ffe_t& x, const ffe_t& y, EllipticCurve

& EllipticCurve)

: x_(x),

y_(y),

ec_(&EllipticCurve)

{}



public:

static Point ONE;



// copy ctor

Point(const Point& rhs)

{

x_ = rhs.x_;

y_ = rhs.y_;

ec_ = rhs.ec_;

}

// assignment

Point& operator=(const Point& rhs)

{

x_ = rhs.x_;

y_ = rhs.y_;

ec_ = rhs.ec_;

return *this;

}

// access x component as element of Fp

ffe_t x() const { return x_; }

// access y component as element of Fp

ffe_t y() const { return y_; }


unsigned int Order(unsigned int maxPeriod = ~0) const

{

Point r = *this;

unsigned int n = 0;

while( r.x_ != 0 && r.y_ != 0 )

{

++n;

r += *this;

if ( n > maxPeriod ) break;

}

return n;

}

// negate

Point operator-()

{

return Point(x_,-y_);

}

// ==

friend bool operator==(const Point& lhs, const Point& rhs)

{

return (lhs.ec_ == rhs.ec_) && (lhs.x_ == rhs.x_) && (lhs.y_ == rhs.y_);

}

// !=

friend bool operator!=(const Point& lhs, const Point& rhs)

{

return (lhs.ec_ != rhs.ec_) || (lhs.x_ != rhs.x_) || (lhs.y_ != rhs.y_);

}

// a + b

friend Point operator+(const Point& lhs, const Point& rhs)

{

ffe_t xR, yR;

lhs.add(lhs.x_,lhs.y_,rhs.x_,rhs.y_,xR,yR);

return Point(xR,yR,*lhs.ec_);

}

// a * int

friend Point operator*(int k, const Point& rhs)

{

return Point(rhs).operator*=(k);

}

// +=

Point& operator+=(const Point& rhs)

{

add(x_,y_,rhs.x_,rhs.y_,x_,y_);

return *this;

}

// a *= int

Point& operator*=(int k)

{

return (*this = scalarMultiply(k,*this));

}

// ostream handler: print this point

friend ostream& operator <<(ostream& os, const Point& p)

{

return (os << "(" << p.x_ << ", " << p.y_ << ")");

}

};



// ==================================================== EllipticCurve impl



typedef EllipticCurve

this_t;

typedef class EllipticCurve

::Point point_t;



// ctor

// Initialize EC as y^2 = x^3 + ax + b

EllipticCurve(int a, int b)

: a_(a),

b_(b),

m_table_(),

table_filled_(false)

{

}


void CalculatePoints()

{

int x_val[P];

int y_val[P];

for ( int n = 0; n < P; ++n )

{

int nsq = n*n;

x_val[n] = ((n*nsq) + a_.i() * n + b_.i()) % P;

y_val[n] = nsq % P;

}



for ( int n = 0; n < P; ++n )

{

for ( int m = 0; m < P; ++m )

{

if ( x_val[n] == y_val[m] )

{

m_table_.push_back(Point(n,m,*this));

}

}

}



table_filled_ = true;

}

// get a point (group element) on the curve

Point operator[](int n)

{

if ( !table_filled_ )

{

CalculatePoints();

}



return m_table_[n];

}

// number of elements in this group

size_t Size() const { return m_table_.size(); }

// the degree P of this EC

int Degree() const { return P; }

// the parameter a (as an element of Fp)

FiniteFieldElement

a() const { return a_; }

// the paramter b (as an element of Fp)

FiniteFieldElement

b() const { return b_; }



// ostream handler: print this curve in human readable form

template<int t="">

friend ostream& operator <<(ostream& os, const EllipticCurve<t>& EllipticCurve);

// print all the elements of the EC group

ostream& PrintTable(ostream &os, int columns=4);



private:

typedef std::vector<point> m_table_t;



m_table_t m_table_; // table of points

FiniteFieldElement

a_; // paramter a of the EC equation

FiniteFieldElement

b_; // parameter b of the EC equation

bool table_filled_; // true if the table has been calculated

};



template<int t="">

typename EllipticCurve<t>::Point EllipticCurve<t>::Point::ONE(0,0);



template<int t="">

ostream& operator <<(ostream& os, const EllipticCurve<t>& EllipticCurve)

{

os << "y^2 mod " << T << " = (x^3" << showpos;

if ( EllipticCurve.a_ != 0 )

{

os << EllipticCurve.a_ << "x";

}



if ( EllipticCurve.b_.i() != 0 )

{

os << EllipticCurve.b_;

}



os << noshowpos << ") mod " << T;

return os;

}



template<int p="">

ostream& EllipticCurve

::PrintTable(ostream &os, int columns)

{

if ( table_filled_ )

{

int col = 0;

typename EllipticCurve

::m_table_t::iterator iter = m_table_.begin();

for ( ; iter!=m_table_.end(); ++iter )

{

os << "(" << (*iter).x_.i() << ", " << (*iter).y_.i() << ") ";

if ( ++col > columns )

{

os << "\n";

col = 0;

}

}

}

else

{

os << "EllipticCurve, F_" << P;

}

return os;

}

}



namespace utils

{

float frand()

{

static float norm = 1.0f / (float)RAND_MAX;

return (float)rand()*norm;

}



int irand(int min, int max)

{

return min+(int)(frand()*(float)(max-min));

}

}



using namespace Cryptography;

using namespace utils;


template <int p="">
EllipticCurve

ec_t my_encrypt()

{
// template <int p="">
//encrypt_data_t

my_encrypt();

typedef EllipticCurve<263> ec_t;

ec_t myEllipticCurve(1,1);



cout << "A little Elliptic Curve cryptography example\nby Jarl Ostensen, 2007\n\n";



// print out a little info and test some properties

cout << "The elliptic curve: " << myEllipticCurve << "\n";


myEllipticCurve.CalculatePoints();



cout << "\nPoints on the curve (i.e. the group elements):\n";

myEllipticCurve.PrintTable(cout,5);

cout << "\n\n";



ec_t::Point P = myEllipticCurve[2];

cout << "some point P = " << P << ", 2P = " << (P+P) << "\n";

ec_t::Point Q = myEllipticCurve[3];

cout << "some point Q = " << Q << ", P+Q = " << (P+Q) << "\n";

ec_t::Point R = P;

R += Q;

cout << "P += Q = " << R << "\n";

R = P;

R += R;

cout << "P += P = 2P = " << R << "\n";



cout << "\nEC message encryption example\n===============================================\n\n";


ec_t::Point G = myEllipticCurve[0];

while( (G.y() == 0 || G.x() == 0) || (G.Order()<2) )

{

int n = (int)(frand()*myEllipticCurve.Size());

G = myEllipticCurve[n];

}



cout << "G = " << G << ", order(G) is " << G.Order() << "\n";



// Alice

int a = irand(1,myEllipticCurve.Degree()-1);

ec_t::Point Pa = a*G; // public key

cout << "Alice' public key Pa = " << a << "*" << G << " = " << Pa << endl;



// Bob

int b = irand(1,myEllipticCurve.Degree()-1);;

ec_t::Point Pb = b*G; // public key

cout << "Bob's public key Pb = " << b << "*" << G << " = " << Pb << endl;



// Jane, the eavesdropper

int j = irand(1,myEllipticCurve.Degree()-1);;

ec_t::Point Pj = j*G;

cout << "Jane's public key Pj = " << j << "*" << G << " = " << Pj << endl;



cout << "\n\n";

int m1 = 19;

int m2 = 72;



cout << "Plain text message from Alice to Bob: (" << m1 << ", " << m2 << ")\n";



// encrypt using Bob`s key

ec_t::Point Pk = a*Pb;

ec_t::ffe_t c1( m1*Pk.x() );

ec_t::ffe_t c2( m2*Pk.y() );



// encrypted message is: Pa,c1,c2

cout << "Encrypted message from Alice to Bob = {Pa,c1,c2} = {" << Pa << ", " << c1 << ", " << c2 << "}\n\n";

encrypt_data_t <263> ret;
ret.Pa = Pa;
ret.c1 = c1;
ret.c2 = c2;

return ret;

// Bob now decrypts Alice`s message, using her public key and his session integer "b" which was also used to generate his public key

Pk = b*Pa;

ec_t::ffe_t m1d = c1/Pk.x();

ec_t::ffe_t m2d = c2/Pk.y();



cout << "\tBob's decrypted message from Alice = (" << m1d << ", " << m2d << ")" << endl;



// Jane intercepts the message and tries to decrypt it using her key

Pk = j*Pa;

m1d = c1/Pk.x();

m2d = c2/Pk.y();



cout << "\nJane's decrypted message from Alice = (" << m1d << ", " << m2d << ")" << endl;



cout << endl;

}

void main()
{
int result;
template <int p="">
result = encrypt_data_t

my_encrypt();
}


GeneralRe: How to retrieve the user defined data type into main function Pin
Richard MacCutchan20-Jun-13 20:53
mveRichard MacCutchan20-Jun-13 20:53 
AnswerRe: How to retrieve the user defined data type into main function Pin
Richard MacCutchan20-Jun-13 2:09
mveRichard MacCutchan20-Jun-13 2:09 
GeneralRe: How to retrieve the user defined data type into main function Pin
Manoj739020-Jun-13 18:13
Manoj739020-Jun-13 18:13 
GeneralRe: How to retrieve the user defined data type into main function Pin
Richard MacCutchan20-Jun-13 20:38
mveRichard MacCutchan20-Jun-13 20:38 
AnswerRe: How to retrieve the user defined data type into main function Pin
Stefan_Lang24-Jun-13 0:41
Stefan_Lang24-Jun-13 0:41 
GeneralRe: How to retrieve the user defined data type into main function Pin
Manoj73901-Jul-13 18:42
Manoj73901-Jul-13 18:42 
GeneralRe: How to retrieve the user defined data type into main function Pin
Stefan_Lang2-Jul-13 0:22
Stefan_Lang2-Jul-13 0:22 
GeneralRe: How to retrieve the user defined data type into main function Pin
Manoj73902-Jul-13 23:47
Manoj73902-Jul-13 23:47 
GeneralRe: How to retrieve the user defined data type into main function Pin
Stefan_Lang3-Jul-13 0:28
Stefan_Lang3-Jul-13 0:28 
GeneralRe: How to retrieve the user defined data type into main function Pin
Stefan_Lang3-Jul-13 0:36
Stefan_Lang3-Jul-13 0:36 
GeneralRe: How to retrieve the user defined data type into main function Pin
Manoj73903-Jul-13 20:53
Manoj73903-Jul-13 20:53 
GeneralRe: How to retrieve the user defined data type into main function Pin
Stefan_Lang4-Jul-13 1:54
Stefan_Lang4-Jul-13 1:54 
Questionhow to start Pin
john563219-Jun-13 19:04
john563219-Jun-13 19:04 
AnswerRe: how to start Pin
thanh_bkhn19-Jun-13 20:18
professionalthanh_bkhn19-Jun-13 20:18 
AnswerRe: how to start Pin
Richard MacCutchan19-Jun-13 20:29
mveRichard MacCutchan19-Jun-13 20:29 
AnswerRe: how to start Pin
_AnsHUMAN_ 20-Jun-13 23:04
_AnsHUMAN_ 20-Jun-13 23:04 
Questionfatal error C1083: Cannot open source file: ' ..\filea.cpp Pin
ForNow19-Jun-13 5:53
ForNow19-Jun-13 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.