.svn folders cleaned
[libg2hec.git] / examples / dh.C
blob21fa8d5d16b471de754f1a76dd1a4738eccc8746
1 /* Example: local Diffie-Hellman key exchange */
2 /* Note that because we have a static g2hcurve member s_hcurve in the
3 divisor class and this memeber may result in chaos when we swith moduli, 
4 we require that there is only one modulus per process
5 */
7 #include <g2hec_nsfieldtype.h>
8 #include <assert.h>
9 #include <g2hec_Genus2_ops.h>
11 #undef MAX_STRING_LEN 
12 #define MAX_STRING_LEN 300
14 NS_G2_CLIENT
16 int main() 
18   /* Set PRNG seed */
19   SetSeed(to_ZZ(19800729));
21   char p[MAX_STRING_LEN];
23   cout << "Please choose your modulus p (up to " 
24        << MAX_STRING_LEN << " decimal digits):" << endl;
25   cout << "p = ";
26   cin.getline(p, MAX_STRING_LEN);
28   ZZ pZZ = to_ZZ(p);
30    field_t::init(pZZ); // define GF(p)
32    ZZ a, b;
34    g2hcurve curve;
36    divisor P, Q1, Q2;
38    curve.random();
40    P.set_curve(curve);
42    RandomBnd(a, pZZ*pZZ);
44    RandomBnd(b, pZZ*pZZ);
46    /* base point P */
47    P.random();
49    Q1 = a * P;
51    Q2 = b * P;
53    if ( b * Q1 == a * Q2 ) 
54      cout << "DH key exchange succeeded!" << endl;
55    else
56      cout << "DH key exchange failed!" << endl;
58    return 0;