def compute_key(self): n_pw = r.fast_exp_w_mod(self.N, self.pw, self.p) gcd, x, y = r.egcd(n_pw, self.p) n_pw_inverse = x tmp = (self.y_star*n_pw_inverse)%self.p the_key = r.fast_exp_w_mod(tmp, self.x, self.p) self.key = str(the_key)
def compute_key(self): g_to_x2x4s = r.fast_exp_w_mod(self.gx4, self.x2_times_s, self.q) gcd, x, y = r.egcd(g_to_x2x4s, self.q) g_to_x2x4s_inverse = x quotient = (self.B*g_to_x2x4s_inverse)%self.q self.key = r.fast_exp_w_mod(quotient, self.x_2, self.q)
def zkp_for(self, g, x_val): gx = r.fast_exp_w_mod(g, x_val, self.q) v = self.get_rand_val_mod_q() gv = r.fast_exp_w_mod(g, v, self.q) # I am having problems with this hashing # I don't know what type of hashing they are expecting in the paper h = self.my_hash(gv, gx, self.signerID) r_val = v-(x_val*h) # print(str(self.clientId) + "'s r value:", r_val) return (gv, r_val)
def verify_zkp(self, pf_val, some_gx, g): gv = pf_val[0] r_val = pf_val[1] h = self.my_hash(gv, some_gx, self.otherSignerID) if r_val >= 0: gr = r.fast_exp_w_mod(g, r_val, self.q) else: gr_inverse = r.fast_exp_w_mod(g, -r_val, self.q) gcd, x, y =r.egcd(gr_inverse, self.q) gr = x gxh = r.fast_exp_w_mod(some_gx, h, self.q) grxh = (gr*gxh)%self.q # print(gv == grxh) return gv == grxh
def my_hash(self, gv, gx_i, signerID): """ signerID is a string """ g_product = (self.g*gv*gx_i)%self.q int_signerID = int(signerID) int_hash = r.fast_exp_w_mod(g_product, int_signerID, self.q) # Adding this to decrease the size of the number to be hashed # otherwise the to_bytes function does not work int_hash = int_hash%10000 sha_hashing = H(int_hash.to_bytes(2, "big")) sha_hashing_int = int(sha_hashing.hexdigest(), 16) num = sha_hashing_int while num > 10: num = r.sum_digits(num) # print("The h value is", num) return num
def compute_x_star(self): self.x_star = (self.x_upper * r.fast_exp_w_mod(self.M, self.pw, self.p))%self.p
def compute_x_upper(self): self.x_upper = r.fast_exp_w_mod(self.g, self.x, self.p)
def computeA(self): self.own_g_product = (((self.gx1 * self.gx3)%self.q)*self.gx4)%self.q self.x2_times_s = self.x_2 * self.pw self.A = r.fast_exp_w_mod(self.own_g_product, self.x2_times_s, self.q)
def compute_gx2(self): self.gx2 = r.fast_exp_w_mod(self.g, self.x_2, self.q)
def compute_gx1(self): self.gx1 = r.fast_exp_w_mod(self.g, self.x_1, self.q)
def getPrime(self): return self.dsa.p def getGenerator(self): return self.dsa.g if __name__ == "__main__": genprime = RandPrime(1024) p = genprime.getPrime() g = genprime.getGenerator() print(p) print(number.isPrime(p)) print() print(g) print(r.fast_exp_w_mod(g, p - 1, p)) primes_dict = {} for n in range(512, 1088, 64): dsa = DSA.generate(n) p_g_tuple = (dsa.p, dsa.g) primes_dict[str(n)] = p_g_tuple print(primes_dict) PRIMES = {'512': (6703908381269271699777665515301486430940325495831751083983518348587277080009934768580100257389271901528250429809191279701925857791761852446537307403456203,\ 5721480122062316331125709584465055560618495927894896084390606834116211818007745766538076816524758380737967282371126664597515477290641747599979368470175097),\ '576': (123665200736552267030251260510364015521863621517431341114426147177870319948507570823709422622948813447314753334731538379421353297356744047365846994446970986611462773076558369,\ 123643701351673164158969069052229845882018250960577850531814098868614347087053717709773541237943545539794711429801011478458273060246949784005227781709276359434234453482140252),\ '640': (2281220308811097609320585802850145662446614253667333794597844707240082901381232985004786499261417537739203946118631850827212415849223502470730561142807661757450036356732395938316598564252937161,\ 1665079381523358815543971116141715104871765470427853559433602906399908404942513357646064772951117151122494484081758613939413481383894284545402174430466122643854872381936274422141840632075792638),\