github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/G_q.cpp (about) 1 /* 2 * G_q.cpp 3 * 4 * Created on: 30.09.2010 5 * Author: stephaniebayer 6 * 7 * 8 */ 9 10 #include "G_q.h" 11 12 #include "FakeZZ.h" 13 #include "CurvePoint.h" 14 NTL_CLIENT 15 16 #include <assert.h> 17 18 #include <time.h> 19 #include <sstream> 20 void G_q::print() const { 21 cout << generator << endl; 22 cout << order << endl; 23 cout << mod << endl; 24 } 25 26 G_q::G_q() {} 27 28 //Constructor creates an instance of G_q subset Z_p with order o and generator value val 29 G_q::G_q(CurvePoint val, ZZ o, ZZ p){ 30 generator = Mod_p(val, p); 31 order = o; 32 mod = p; 33 } 34 35 G_q::~G_q() {} 36 37 38 //return the generator 39 Mod_p G_q::get_gen()const{ 40 41 return generator; 42 } 43 44 //return the order o 45 ZZ G_q::get_ord()const{ 46 47 return order; 48 } 49 50 //return the modular value mod 51 ZZ G_q::get_mod()const{ 52 53 return mod; 54 } 55 56 //Checks if an element is a generator of the group G 57 bool G_q::is_generator(const Mod_p& el){ 58 #if USE_REAL_POINTS 59 return true; // TODO replace with not_identity 60 #else 61 CurvePoint pow; 62 bool b; 63 b=false; 64 pow = PowerMod(el.get_val(),order,mod); 65 if((pow == curve_zeropoint()) & (el.get_val()!=curve_zeropoint())) 66 { 67 b=true; 68 } 69 return b; 70 #endif 71 } 72 73 //returns the identity of the group 74 Mod_p G_q::identity(){ 75 // TODO this is never called, right? 76 assert(false); 77 return Mod_p(curve_zeropoint(), mod); 78 } 79 80 Mod_p G_q::map_to_group_element(ZZ& m) { 81 #if USE_REAL_POINTS 82 CurvePoint x; 83 basepoint_scalarmult(x, m); 84 return Mod_p(x, mod); 85 #else 86 return generator.expo(m); 87 #endif 88 } 89 90 91 //returns an element of the group with value v 92 Mod_p G_q::element(CurvePoint v){ 93 94 95 return Mod_p(v,mod); 96 97 } 98 99 100 //returns an element of the group with value v 101 Mod_p G_q::element(long v){ 102 // TODO this is never called, right? 103 assert(false); 104 105 106 // return Mod_p(to_curve_pt(v),mod); 107 return Mod_p(curve_zeropoint(),mod); 108 109 } 110 111 112 void G_q::operator =(const G_q& H){ 113 114 generator = H.get_gen(); 115 order = H.get_ord(); 116 mod = H.get_mod(); 117 } 118 119 //Output operator, output format is (generator value, order, modular value) 120 ostream& operator<<(ostream& os, const G_q G){ 121 return os<< "("; 122 os << G.get_gen().get_val(); 123 return os << ", " << G.get_ord() <<", " << G.get_mod()<<")"; 124 } 125