github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/ElGammal.cpp (about) 1 /* 2 * ElGammal.cpp 3 * 4 * Created on: 03.10.2010 5 * Author: stephaniebayer 6 */ 7 8 #include "ElGammal.h" 9 #include "G_q.h" 10 11 #include "FakeZZ.h" 12 #include "CurvePoint.h" 13 NTL_CLIENT 14 15 #include "Mod_p.h" 16 #include <stdio.h> 17 #include <time.h> 18 #include <vector> 19 #include <fstream> 20 21 void ElGammal::print() const { 22 cout <<"Elgamal Keys:" << endl; 23 G.print(); 24 cout << "secret:" << endl; 25 cout << sk << endl; 26 cout << "public:" << endl; 27 cout << pk << endl; 28 } 29 30 ElGammal::ElGammal() {} 31 32 ElGammal::ElGammal(const ElGammal &other) { 33 G = other.G; 34 pk = other.pk; 35 sk = other.sk; 36 } 37 38 ElGammal::~ElGammal() {} 39 40 //Access to the parameters 41 G_q ElGammal::get_group()const{ 42 return G; 43 } 44 45 Mod_p ElGammal::get_pk() const{ 46 return pk; 47 } 48 49 ZZ ElGammal::get_sk()const{ 50 return sk; 51 } 52 53 //functions to change parameters 54 void ElGammal::set_group(G_q H){ 55 G = H; 56 } 57 58 void ElGammal::set_sk(long s){ 59 sk = to_ZZ(s); 60 // pk = G.get_gen().expo( s); // TODO remove containing stack -- this is never called 61 } 62 63 void ElGammal::set_pk(Mod_p& pk_) { 64 pk = pk_; 65 } 66 67 void ElGammal::set_sk(ZZ s){ 68 sk = s; 69 #if USE_REAL_POINTS 70 ZZ mod = G.get_mod(); 71 CurvePoint x; 72 basepoint_scalarmult(x, s); 73 pk = Mod_p(x, mod); 74 #else 75 pk = G.get_gen().expo(s); 76 #endif 77 } 78 79 Cipher_elg ElGammal::encrypt(Mod_p el, ZZ ran){ 80 // yossigi: this is the function that is called 81 //cout << "called " << el << " and " << ran <<endl; 82 Cipher_elg c; 83 Mod_p temp_1, temp_2; 84 #if USE_REAL_POINTS 85 ZZ mod = G.get_mod(); 86 CurvePoint x; 87 basepoint_scalarmult(x, ran); 88 temp_1 = Mod_p(x, mod); 89 #else 90 temp_1 = G.get_gen().expo(ran); 91 #endif 92 temp_2 = pk.expo(ran)*el; 93 c = Cipher_elg(temp_1); 94 95 return c; 96 97 } 98 99 Cipher_elg ElGammal::encrypt(CurvePoint m, ZZ ran){ 100 Cipher_elg c; 101 Mod_p temp_1, temp_2; 102 #if USE_REAL_POINTS 103 ZZ mod = G.get_mod(); 104 CurvePoint x; 105 basepoint_scalarmult(x, ran); 106 temp_1 = Mod_p(x, mod); 107 #else 108 temp_1 = G.get_gen().expo(ran); 109 #endif 110 temp_2 = pk.expo(ran)*Mod_p(m,G.get_mod()); 111 c = Cipher_elg(temp_1); 112 //modified 113 return c; 114 } 115 116 //Decrypts the ciphertext c 117 Mod_p ElGammal::decrypt(Cipher_elg c){ 118 CurvePoint temp; 119 ZZ mod = G.get_mod(); 120 temp = InvMod(c.get_u(),mod); 121 temp = PowerMod(temp,sk, mod); 122 // temp = MulMod(temp,c.get_v(),mod); 123 return Mod_p(temp, mod); 124 } 125 126 //Assigment operator 127 void ElGammal::operator=(const ElGammal& el){ 128 129 G = el.get_group(); 130 sk = el.get_sk(); 131 pk = el.get_pk(); 132 } 133