Click here to Skip to main content
15,885,790 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Where does the m_pMainWnd delete? Pin
Jochen Arndt2-Jul-13 8:14
professionalJochen Arndt2-Jul-13 8:14 
Questionzero copy in windows Pin
Satya1441-Jul-13 20:20
professionalSatya1441-Jul-13 20:20 
AnswerRe: zero copy in windows Pin
enhzflep2-Jul-13 12:08
enhzflep2-Jul-13 12:08 
AnswerRe: zero copy in windows Pin
Erudite_Eric4-Jul-13 2:23
Erudite_Eric4-Jul-13 2:23 
GeneralRe: zero copy in windows Pin
Satya1447-Jul-13 23:44
professionalSatya1447-Jul-13 23:44 
GeneralRe: zero copy in windows Pin
Erudite_Eric11-Jul-13 6:12
Erudite_Eric11-Jul-13 6:12 
QuestionProblem in returning the used defined data type using structure Pin
Manoj73901-Jul-13 18:48
Manoj73901-Jul-13 18:48 
QuestionProblem in returning the used defined data type using structure Pin
Manoj73901-Jul-13 18:47
Manoj73901-Jul-13 18:47 
hi everyone.. I have written a program where I want to return three user defined data types to the another main() function (here i have not mentioned that main). I want to return {Pa,c1,c2} from this program. Please help me out.

#include <cstdlib>
#include <iostream>
#include <vector>
using std:: cout;
using std:: cin;
using std:: endl;
using namespace std;
#include <math.h>
#include "FiniteFieldElement.hpp"

namespace Cryptography
{           
        template<int P>
        class   EllipticCurve
        {
            public:
                
                typedef FiniteFieldElement<P> ffe_t;
                 
                class   Point
                {
                    friend  class   EllipticCurve<P>;
                    typedef FiniteFieldElement<P> 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;
                        }
                                                
                        ffe_t s;                                                
                        if ( x1 == x2 && y1 == y2 )
                        {                     
                            s = (3*(x1.i()*x1.i()) + ec_->a()) / (2*y1);
                            xR = ((s*s) - 2*x1);                            
                        }
                        else
                        {                                                     
                            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<P> & EllipticCurve)
                     : x_(x),
                       y_(y),
                       ec_(&EllipticCurve)
                    {}
                    
                    Point(const ffe_t& x, const ffe_t& y, EllipticCurve<P> & EllipticCurve)
                     : x_(x),
                       y_(y),
                       ec_(&EllipticCurve)
                    {}
                    
                public:                    
                    static  Point   ONE;
                    
                    Point(const Point& rhs)
                    {
                        x_ = rhs.x_;
                        y_ = rhs.y_;
                        ec_ = rhs.ec_;
                    }
                    
                    Point& operator=(const Point& rhs)
                    {
                        x_ = rhs.x_;
                        y_ = rhs.y_;
                        ec_ = rhs.ec_;
                        return *this;
                    }
                    
                    ffe_t x() const { return x_; }
                    
                    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;
                    }
                    
                    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_);
                    }                    
                             
                    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_);    
                    }
                    
                    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;  
                    }
                    
                    Point& operator*=(int k)
                    {
                        return (*this = scalarMultiply(k,*this));
                    }                    
                    
                    friend ostream& operator <<(ostream& os, const Point& p)
                    {
                        return (os << "(" << p.x_ << ", " << p.y_ << ")");
                    }
                };
                
                typedef EllipticCurve<P> this_t;
                typedef class EllipticCurve<P>::Point point_t;
                
                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;
                }
                
                Point   operator[](int n)
                {
                    if ( !table_filled_ )
                    {
                        CalculatePoints();
                    }
                    
                    return m_table_[n];
                }
                size_t  Size() const { return m_table_.size(); }
                int     Degree() const { return P; }
                FiniteFieldElement<P>  a() const { return a_; }
                FiniteFieldElement<P>  b() const { return b_; }
                template<int T>
                friend ostream& operator <<(ostream& os, const EllipticCurve<T>& EllipticCurve);                       
                ostream&    PrintTable(ostream &os, int columns=4);
                
                private:
                    typedef std::vector<Point>  m_table_t;
                    
                    m_table_t                   m_table_;   // table of points
                    FiniteFieldElement<P>       a_;         // paramter a of the EC equation
                    FiniteFieldElement<P>       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<P>::PrintTable(ostream &os, int columns)
        {
            if ( table_filled_ )
            {
                int col = 0;
                typename EllipticCurve<P>::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;

int main(int argc, char *argv[])
{
    typedef EllipticCurve<263> ec_t;
    ec_t   myEllipticCurve(1,1);
    
    cout << "A little Elliptic Curve cryptography example\nby Jarl Ostensen, 2007\n\n";
    
    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,m2;
   cout << "Enter two message\n"<< endl;
   cin >>  m1;  
   cin >>  m2;

    cout << "Plain text message from Alice to Bob: (" << m1 << ", " << m2 << ")\n";
    
    ec_t::Point Pk = a*Pb;
    ec_t::ffe_t c1( m1*Pk.x() );
    ec_t::ffe_t c2( m2*Pk.y() );
    
    cout << "Encrypted message from Alice to Bob = {Pa,c1,c2} = {" << Pa << ", " << c1 << ", " << c2 << "}\n\n";
    
    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;
    
    Pk = j*Pa;
    m1d = c1/Pk.x();
    m2d = c2/Pk.y();

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

QuestionRe: Problem in returning the used defined data type using structure Pin
Richard MacCutchan1-Jul-13 20:42
mveRichard MacCutchan1-Jul-13 20:42 
AnswerRe: Problem in returning the used defined data type using structure Pin
Stefan_Lang3-Jul-13 0:33
Stefan_Lang3-Jul-13 0:33 
QuestionHow to get the process's DLL module address? Pin
SetHookCurrent1-Jul-13 18:45
SetHookCurrent1-Jul-13 18:45 
AnswerRe: How to get the process's DLL module address? Pin
Richard MacCutchan1-Jul-13 22:08
mveRichard MacCutchan1-Jul-13 22:08 
AnswerRe: How to get the process's DLL module address? Pin
Santhosh G_2-Jul-13 5:37
Santhosh G_2-Jul-13 5:37 
AnswerRe: How to get the process's DLL module address? Pin
Ghosuwa Wogomon2-Jul-13 7:30
Ghosuwa Wogomon2-Jul-13 7:30 
Questionis it possible to update a specific column in listview? Pin
JoneLe861-Jul-13 16:37
JoneLe861-Jul-13 16:37 
SuggestionRe: is it possible to update a specific column in listview? Pin
David Crow2-Jul-13 2:49
David Crow2-Jul-13 2:49 
QuestionCFile reads comma delimited text automatically? Pin
Vaclav_1-Jul-13 13:03
Vaclav_1-Jul-13 13:03 
AnswerRe: CFile reads comma delimited text automatically? Pin
SoMad1-Jul-13 14:47
professionalSoMad1-Jul-13 14:47 
QuestionRe: CFile reads comma delimited text automatically? Pin
Vaclav_2-Jul-13 4:04
Vaclav_2-Jul-13 4:04 
SuggestionRe: CFile reads comma delimited text automatically? Pin
David Crow2-Jul-13 4:09
David Crow2-Jul-13 4:09 
AnswerRe: CFile reads comma delimited text automatically? Pin
SoMad2-Jul-13 20:44
professionalSoMad2-Jul-13 20:44 
QuestionGetting the Command Line of Another Process Pin
Richard Andrew x641-Jul-13 10:58
professionalRichard Andrew x641-Jul-13 10:58 
AnswerRe: Getting the Command Line of Another Process Pin
Garth J Lancaster1-Jul-13 12:47
professionalGarth J Lancaster1-Jul-13 12:47 
GeneralRe: Getting the Command Line of Another Process Pin
Richard Andrew x641-Jul-13 13:14
professionalRichard Andrew x641-Jul-13 13:14 
GeneralRe: Getting the Command Line of Another Process Pin
Garth J Lancaster1-Jul-13 13:18
professionalGarth J Lancaster1-Jul-13 13:18 

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.