|
Trying to build my own source filter (AVI format) I always get connection error with AVI Splitter.
Please take in mind that I am trying to open a fully functional avi video file, that works just fine with microsoft's async file and I that tried at the very begginng to change my mediatype declaration without success.
The error description in the graphEdit is "These filters cannot agree on connection. Verify type compatibility of input pin and output pin. Return code VFW_E_INVALID_FILE_FORMAT
0x8004022F".
DXerr.exe reports :
HRESULT: 0x80040207 (2147746311)
Name: DIERR_NOTBUFFERED
VFW_E_NO_ACCEPTABLE_TYPES
Description: Attempted to read buffered device data from a device that is not buffered. & There is no common media type between these pins.
Severity code: Failed
Facility Code: FACILITY_ITF (4)
Error Code: 0x0207 (519)
My mediatype is declared as :
const AMOVIESETUP_MEDIATYPE sudOpPinTypes =
{
&MEDIATYPE_Stream,
&MEDIASUBTYPE_NULL
};
I debugged the code and I found that somewhere in the
HRESULT CBasePin::AgreeMediaType(IPin *pReceivePin, const CMediaType *pmt) inside the amfilter.cpp is gettings first the AVI Splitter's MediaType list with the folowing code :
hr = pReceivePin->EnumMediaTypes(&pEnumMediaTypes);
Ok with that. Then it tries all mediatypes inside the method
HRESULT CBasePin::TryMediaTypes(IPin *pReceivePin, __in_opt const CMediaType *pmt, IEnumMediaTypes *pEnum)
by retriving them one by one with the following statement
hr = pEnum->Next(1, (AM_MEDIA_TYPE**)&pMediaType,&ulMediaCount);
At this point, the hr result returns S_FALSE and the pMediaType is undefined. The error code is
0x8004022f error code
VFW_E_INVALID_FILE_FORMAT
So the AVISplitter does not purpose any right ?
Now the code beggins to scan my own source filter mediatype list.
Despite the fact that I declared 2 or 3 different mediatypes it returns only one and it fails at
hr = AttemptConnection(pReceivePin, pMediaType); and specifically at
hr = pReceivePin->ReceiveConnection((IPin *)this, pmt);
What am I doing wrong ?
I suspect my MediaType declaration, but i dont see anything wrong. Below I attach the filter's template declarations with only one mediatype.
const WCHAR szAsyncFile[] = L"Async Source (File)";
const WCHAR szAsyncHttp[] = L"Async Source (Http)";
const AMOVIESETUP_MEDIATYPE sudOpPinTypes =
{
&MEDIATYPE_Stream,
&MEDIASUBTYPE_NULL
};
const AMOVIESETUP_PIN sudOpPin =
{
L"Output", FALSE, TRUE, FALSE, FALSE, &CLSID_NULL, L"Input", 1, &sudOpPinTypes
};
const AMOVIESETUP_FILTER sudAsyncFile =
{
&CLSID_PIAsyncFile, szAsyncFile, MERIT_UNLIKELY, 1, &sudOpPin };
const AMOVIESETUP_FILTER sudAsyncHttp =
{
&CLSID_PIAsyncHttp, szAsyncHttp, MERIT_UNLIKELY, 1, &sudOpPin };
CFactoryTemplate g_Templates[] =
{
{
szAsyncFile,
&CLSID_PIAsyncFile,
CAsyncFilter::CreateInstance,
NULL,
&sudAsyncFile
},
{
szAsyncHttp,
&CLSID_PIAsyncHttp,
CAsyncFilterHttp::CreateInstance,
NULL,
&sudAsyncHttp
}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(CFactoryTemplate);
sdancer75
modified 21-Jun-13 8:45am.
|
|
|
|
|
Hi, all
I create a new dialog based on MFC (VS2008) without border.
But i found that the dialog without shadow when executing.
how to add the shadow to it.
please help me. thanks
|
|
|
|
|
the style of the dialog is popup
the border is none
|
|
|
|
|
Im using MFC feature pack for my application to create menu bar looks like MS Office.
But is there any option to change the fonst size of text appearing in Ribbon Panel.
I used Add Category, Add Panel and CMFCRibbonButton for buttons in Panel. Just i want to increase the text size in each button.Now it appears very small.
Anu
|
|
|
|
|
Hi,
I am using a slider control on a dialog box. I have handled OnLButtonDown for Slider control. But I wonder its not getting called whenever I click on the slider thumb, instaed it gets called when I click anywhere on the dialog.
I want to handle OnLButtonDown when I click on the slider thumb.
Anybody have any idea where is the issue.?
Any help will be appreciated.
Regards,
Mbatra
|
|
|
|
|
Do you really need a OnLButtonDown on a Slider control? If you just want to handle whenever its value get changed, you can try OnHScroll or OnVScroll instead.
|
|
|
|
|
Hi,
Yes I do need to handle OnLButtonDown.
Scenario is I am using slider control to show the progress of the video. Slider will be incremented in steps in proportion to the time elapsed for the video. Now when I click on the slider and hold for the moment, I need to stop it, otherwise the slider keeps incrementing with the timer while still user is holding the mouse on clicking on its thumb.
So I need to handle this message handler. I have tried in OnHScroll also, it didn't work.
Regards,
Mbatra
|
|
|
|
|
In the OnHScroll, the UINT nSBCode parameter contains the code message. nSBCode == TB_THUMBTRACK when you use the mouse to drag it.
|
|
|
|
|
I had already tried this......NOT WORKING.
just check the same case.....Click on the slider thumb, but don't move the slider. Just click the left button and hold. Slider thumb keeps on moving with the timer and video progression.
I need to left click the mouse button on the slider thumb and handle that message.
Regards,
Mbatra
|
|
|
|
|
I just tried and I get notifications of each move. Code excerpt looks like:
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
ON_NOTIFY(TRBN_THUMBPOSCHANGING, IDC_SLIDER1, &CTestDlg::OnTRBNThumbPosChangingSlider1)
END_MESSAGE_MAP()
void CTestDlg::OnTRBNThumbPosChangingSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
NMTRBTHUMBPOSCHANGING *pNMTPC = reinterpret_cast<NMTRBTHUMBPOSCHANGING *>(pNMHDR);
TRACE(_T("Position = %lu\n"), pNMTPC->dwPos);
*pResult = 0;
}
Did you remember to turn on the "Notify Before Move" style?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Hi David,
Thanx for the reply.
Also plz help me to solve one more issue. I want to to move the thumb where user has clicked the mouse, not in ticks.
Slider thumb should move to the position in one shot, user clicked the mouse...
I tried this code.....but it didn't work for me.
CRect rc, trc;
m_Slider.GetChannelRect(rc);
m_Slider.GetThumbRect(trc);
rc.InflateRect(0, (trc.Height() - rc.Height())/2);
if (!PtInRect(rc, point))
return;
LONG range = m_Slider.GetRangeMax();
LONG pos = point.x - rc.left - trc.Width()/2;
LONG width = rc.Width() - trc.Width();
m_Slider.SetPos(int(DOUBLE(pos) * range / width + 0.5));
Please share any sample code if you have.
Regards,
Mbatra
|
|
|
|
|
Try something like:
void CSliderCtrlEx::OnLButtonDown(UINT nFlags, CPoint point)
{
CSliderCtrl::OnLButtonDown(nFlags, point);
CRect rectClient;
GetClientRect(rectClient);
CRect rectChannel;
GetChannelRect(rectChannel);
int nPos = (GetRangeMax() - GetRangeMin()) *
(point.x - rectClient.left - rectChannel.left) /
(rectChannel.right - rectChannel.left);
SetPos(nPos);
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
modified 3-Jul-13 10:50am.
|
|
|
|
|
Hi David,
I tried this one also....Still not working.
I had one issue, I don't know but the OnLButtonDown() handler is not getting called anymore. This is happening in case of mine.
That's why I was not able to set the position of the slider.
I have done like this:
void CCapturePage::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rectClient;
GetClientRect(rectClient);
CRect rectChannel;
m_CaptureSlider.GetChannelRect(rectChannel);
int nPos = (m_CaptureSlider.GetRangeMax() - m_CaptureSlider.GetRangeMin()) *
(point.x - rectClient.left - rectChannel.left) /
(rectChannel.right - rectChannel.left);
m_CaptureSlider.SetPos(nPos);
CPropertyPage::OnLButtonDown(nFlags, point);
}
This is a Propery page which is included in a property sheet. On that Dialog box, I am using a slider control.
Does SetCapture() and SetFocus() make any difference in OnLButtonDown().
I don't know why this handler is not getting called.?
Regards,
Mbatra
|
|
|
|
|
mbatra31 wrote: I had one issue, I don't know but the OnLButtonDown() handler is not getting called anymore. Because it is supposed to be in a CSliderCtrl -derived class.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Hi everyone.. I have written the code for encryption algorithm. It has one user function header file also. Sample code is as shown below... I want return {Pa,C1,C2} in the program to main function. Please help me..
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#include <math.h>
#include "FiniteFieldElement.hpp"
namespace Cryptography
{
template<int P>
typedef struct {
Cryptography::EllipticCurve<P>::Point Pa;
Cryptography::FiniteFieldElement<P> c1;
Cryptography::FiniteFieldElement<P> c2;
} encrypt_data_t;
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_;
}
.....
.....
....
...
};
typedef EllipticCurve<P> this_t;
typedef class EllipticCurve<P>::Point point_t;
namespace utils
{
}
using namespace Cryptography;
using namespace utils;
template <int P>
encrypt_data_t<P> my_encrypt()
{
typedef EllipticCurve<263> ec_t;
ec_t myEllipticCurve(1,1);
ec_t::Point Pa = a*G;
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";
encrypt_data_t <263> ret;
ret.Pa = Pa;
ret.c1 = c1;
ret.c2 = c2;
return ret;
}
main()
{
int result;
template <int P>
result = encrypt_data_t<P> my_encrypt();
}
"I want to return {Pa,C1,C2} to the main program using structures... I am getting so many errors"
"Please help me with this problem"
|
|
|
|
|
Quote: I am getting so many errors
I would start fixing them (or post them here if you are unable to).
Veni, vidi, vici.
|
|
|
|
|
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();
}
|
|
|
|
|
I think that confirms my comment to you about the use of namespaces and templates.
Use the best guess
|
|
|
|
|
Get rid of all your templates and namespaces, they serve no purpose.
Use the best guess
|
|
|
|
|
Templates and namespaces are very important for my program.
I have not posted the full code there...
|
|
|
|
|
Manoj7390 wrote: Templates and namespaces are very important for my program. That is most unlikely, and looking at what you have posted you are not using them for any good reason. You should get the code working first without the use of either, and then add them (if they are really necessary) at a later stage.
Use the best guess
|
|
|
|
|
Starting at the bottom, your main function doesn't make sense - it's one huge syntax error. Even with all the context and the realisation what this is about (encryption), it's almost impossible to guess what it should do.
Obviously you're not familiar with template syntax. Your attempt to invoke a templated function using a templated type is a syntactical mess. Your data structure definitions contain errors related to incomplete or improper template syntax. For that reason, and because you don't really use or need those template parameters, you should really forget about them!
Since you claim you do need the template, use a const instead: in most cases, where people think they need a template, a global/static definition works just as well:
const int P = 263;
Start with your struct definition:
struct encrypt_data_t {
EllipticCurve<P>::Point Pa;
FiniteFieldElement<P> c1;
FiniteFieldElement<P> c2;
};
There: no typedef required, no template parameters required. Also, since the definition is within your namespace, no need to explicitely add the prefix "Cryptography::".
Next, remove any remaining template parameter definition line in your code: if you always used P as the template parameter name, everything should compile after removing the template <argument list> .
Last: fix your main() . I have no idea what your intention is, but you do call a function that returns a struct, and apparently try to assign that struct to an int . That doesn't make sense. Fix it, or at least remove that assignment, so you can find out and fix any remaining syntax errors. Besides, you omitted the return type for main() (always int ), and your main didn't return a value. Both should be listed as errors or at the very least as warnings by your compiler. But it's possible your compiler didn't get that far due to it's inability to resolve previous spaghetti template syntax.
|
|
|
|
|
If I delete template parameter, then it is giving so many errors. I will post the full program to you please fix it.
I want to return {Pa,c1,c2} from this program to another main() function. 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;
}
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_;
FiniteFieldElement<P> a_;
FiniteFieldElement<P> b_;
bool table_filled_;
};
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";
int a = irand(1,myEllipticCurve.Degree()-1);
ec_t::Point Pa = a*G;
cout << "Alice' public key Pa = " << a << "*" << G << " = " << Pa << endl;
int b = irand(1,myEllipticCurve.Degree()-1);;
ec_t::Point Pb = b*G;
cout << "Bob's public key Pb = " << b << "*" << G << " = " << Pb << endl;
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;
}
|
|
|
|
|
Without the declaration of the class FiniteFieldElement, and without the error messages, I can't help you. I've successfully compiled that code by using the following dummy declaration:
template <int P>
class FiniteFieldElement {
public:
FiniteFieldElement(){}
FiniteFieldElement(int){}
int i() const {return 0;}
operator int() const {return 0;}
};
This means that either there is a problem with the actual class declaration, or you are not using it correctly.
|
|
|
|
|
The code is working fine. I need to return those three variables i.e. {Pa,c1,c2} from that program to another main program. What should i have to do. I have used structure for that but its not working. Please tell how to return a three user defined data type to the main function
|
|
|
|
|