Initial GIT commit
[libg2hec.git] / examples / .svn / text-base / elgamal_enc.C.svn-base
blob01ae9f66d0b51a98573df6f009b48a272cb29afc
1 /* Example: local ElGamal encryption */
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 x, k;
34   g2hcurve curve;
36   divisor m, g, h, a, b;
38   curve.random();
40    /* private key x */
41   RandomBnd(x, pZZ*pZZ);
42    /* random number k */
43   RandomBnd(k, pZZ*pZZ);
45   m.set_curve(curve);
47    /* random message m as divisor */
48   m.random();
50    /* random base point */
51   g.random();
53    /* public key h */
54   h = x * g;
56    /* cipher text (a, b) */
57   a = k * g;
58   b = k * h + m;
60   /* message decryption  */
62   if ( b - x * a == m )
63     cout << "ElGamal decryption succeeded!" << endl;
64   else
65     cout << "ElGamal decryption failed!" << endl;
67    return 0;