Пример #1
0
class TC_():
    N = _sage_const_251
    NORM_BOUND = _sage_const_321
    q = _sage_const_128
    df = _sage_const_73
    dg = _sage_const_71
    t = _sage_const_2
    n = _sage_const_5
    T = 'transpose'
    p = -_sage_const_1
    H = None
    t_new = None

    members = []
    #users_count = [range(1, n+1)]
    active_users = {}

    def __init__(self):
        for i in range(self.n + _sage_const_1):
            print(i)
            self.add_member()
        self.share_secret()

    def get_free_id(self):
        for i in range(_sage_const_1, self.n + _sage_const_1):
            if i not in self.active_users.values():
                return i
        return -_sage_const_1

    def add_member(self):
        m = Member(self.N, self.q, self.df, self.dg, self.T)
        m.eval_k(self.q)
        self.members.append(m)

    def get_new_member_params(self, username):
        id = self.get_free_id()
        if id == -_sage_const_1:
            print('All clients already connected')
            return pickle.dumps(['server full'])

        f = self.members[id].f
        h = self.members[id].h
        fs = self.members[id].fs
        h0 = self.members[_sage_const_0].h
        shares = self.members[id].shares
        self.active_users.update({username: id})
        print(self.active_users)
        return pickle.dumps([f, fs, h, shares, h0, self.N, self.q])

    def delete_user(self, username):
        del self.active_users[username]

    def share_secret(self):
        self.k = _sage_const_1024
        self.shaTSS = Shamir(self.t, self.n)
        self.p, alpha = self.shaTSS.GC(self.k)
        self.T = self.members[_sage_const_0].T
        print('T: %d' % self.T)
        print('k ' + str(self.members[_sage_const_0].k))
        shares = self.shaTSS.DS(self.p, alpha, self.members[_sage_const_0].k)
        for i in range(_sage_const_1, self.n + _sage_const_1):
            self.members[i].shares = shares[i - _sage_const_1]
        print('secret generated')

    def change_t(self, t_new):
        if t_new > self.n or t_new <= self.t:
            return b'invalid new t'

        sigma_c = _sage_const_2**(-self.n)
        appr = ((self.t + t_new)**(_sage_const_1 / _sage_const_2) *
                _sage_const_2**((self.t + t_new) / _sage_const_2))
        g_cvp = log(appr, _sage_const_2)
        sigma_F = t_new / self.t / self.k * (
            log(sigma_c**(-_sage_const_1 / t_new) * self.n * self.t,
                _sage_const_2) + g_cvp + _sage_const_1)
        alpha_noise = (_sage_const_1 - (_sage_const_1 + sigma_F) /
                       (t_new / self.t))
        H = floor(self.p**alpha_noise / _sage_const_2)
        for i in range(_sage_const_1, self.n + _sage_const_1):
            alpha = self.members[i].shares[_sage_const_0]
            sigma = self.members[i].shares[_sage_const_1]
            self.members[i].shares[_sage_const_1] = self.shaTSS.SG(
                [alpha, sigma], self.p, H)
            #self.members[i].status = ['4', ]
        self.t_new = t_new
        self.H = H
        print(t_new)
        return b'ok'

    def get_info_client(self):
        print(self.active_users)
        return pickle.dumps(
            [self.t, self.n, ', '.join(self.active_users.keys())])
        #return pickle.dumps([self.q, self.members[0].h, self.p, self.t, self.n, self.NN])

    def get_info_dc(self):
        #print ([self.t, self.n, self.q, self.N, self.p, self.members[0].h, self.H, self.t_new])
        return pickle.dumps([
            self.t, self.n, self.N, self.q, self.T, self.p,
            self.members[_sage_const_0].h, self.H, self.t_new
        ])

    def get_user_status(self, username):
        id = self.active_users[username]
        return self.members[id].status

    def set_user_status(self, username, status):
        id = self.active_users[username]
        self.members[id].status = status

    def get_user_shares(self, username):
        id = self.active_users[username]
        return self.members[id].shares

    def gen_partial_sign(self, h, m, s, username):
        id = self.active_users[username]
        user = self.members[id]

        x = Rx(normilize_coeffs(
            -_sage_const_1 / self.q * m * user.fs))._polynomial % self.q
        y = Rx(normilize_coeffs(
            _sage_const_1 / self.q * m * user.f))._polynomial % self.q

        si = (Rx(x * user.f)._polynomial % self.q +
              Rx(y * user.fs)._polynomial % self.q) % self.q
        m = Rx(si * (user.h - h))._polynomial % self.q
        s = s + si
        return user.h, m, s

    def encode_msg(self, D, r):
        return H(D + str(r), self.N, self.q)
Пример #2
0
class TC_():
	NN = _sage_const_251 
	q = _sage_const_128 
	df = _sage_const_73 
	dg = _sage_const_71 
	t = _sage_const_2 
	n = _sage_const_5 
	T = 'transpose'
	p = -_sage_const_1 
	H = None
	t_new = None

	members = []
	#users_count = [range(1, n+1)]
	active_users = {}

	def __init__(self):
		for i in range(self.n + _sage_const_1 ):
			print (i)
			self.add_member() 
		f_bin  = ''.join(['0' * (self.NN - len(self.members[_sage_const_0 ].f.list()))]  + [bin(abs(x))[_sage_const_2 :] for x in  self.members[_sage_const_0 ].f.list()])
		fs_bin = ''.join(['0' * (self.NN - len(self.members[_sage_const_0 ].fs.list()))] + [bin(abs(x))[_sage_const_2 :] for x in self.members[_sage_const_0 ].fs.list()])
		self.share_secret()

	def get_free_id(self):
		for i in range(_sage_const_1 , self.n+_sage_const_1 ):
			if i not in self.active_users.values():
				return i
		return -_sage_const_1 

	def add_member(self):
		f,g,F,G = auxmath.gen_NTRU_fgFG(self.NN, self.q)
		m = Member(f,g,F,G, self.NN)
		m.calculate_params(self.NN, self.q, self.T)
		m.eval_k()
		self.members.append(m)

	def get_new_member_params(self, username):
		id = self.get_free_id()
		if id == -_sage_const_1 :
			print ('All clients already connected')
			return pickle.dumps(['server full'])

		f = self.members[id].f
		h = self.members[id].h
		fs = self.members[id].fs
		h0 = self.members[_sage_const_0 ].h
		shares = self.members[id].shares
		self.active_users.update({username : id})
		print (self.active_users)
		return pickle.dumps([f, fs, h, shares, h0, self.NN, self.q])

	def delete_user(self, username):
		del self.active_users[username]
		#for key, value in dict(self.active_users).items():
		#    if value == username:
		#        del self.active_users[key]

	def share_secret(self):
		self.k = _sage_const_2048 
		self.shaTSS = Shamir(self.t, self.n)
		self.p, alpha = self.shaTSS.GC(self.k)
		print ('k ' + str(self.members[_sage_const_0 ].k))
		shares = self.shaTSS.DS(self.p, alpha, self.members[_sage_const_0 ].k)
		for i in range(_sage_const_1 , self.n+_sage_const_1 ):
			self.members[i].shares = shares[i-_sage_const_1 ]
			self.members[i].alpha = shares[i-_sage_const_1 ][_sage_const_0 ]
		print ('secret generated')

	def change_t(self, t_new):
		if t_new > self.n or t_new <= self.t:
			return b'invalid new t'

		sigma_c = _sage_const_2 **(-self.n)
		appr = ((self.t+t_new)**(_sage_const_1 /_sage_const_2 )*_sage_const_2 **((self.t+t_new)/_sage_const_2 ))
		g_cvp = log(appr, _sage_const_2 )
		sigma_F = t_new/self.t/self.k*(log(sigma_c**(-_sage_const_1 /t_new)*self.n*self.t, _sage_const_2 ) + g_cvp + _sage_const_1 )
		alpha_noise = (_sage_const_1  - (_sage_const_1 +sigma_F) / (t_new/self.t))
		H = floor(self.p**alpha_noise / _sage_const_2 )
		for i in range(_sage_const_1 , self.n+_sage_const_1 ):
			alpha = self.members[i].shares[_sage_const_0 ]
			sigma = self.members[i].shares[_sage_const_1 ]
			self.members[i].shares[_sage_const_1 ] = self.shaTSS.SG([alpha, sigma], self.p, H)
			#self.members[i].status = ['4', ]
		self.t_new = t_new
		self.H = H
		print (t_new)
		return b'ok'


	def get_info(self):
		print (self.active_users)
		return pickle.dumps([self.t, self.n, self.q, self.p, self.members[_sage_const_0 ].h, ', '.join(self.active_users.keys())])
		#return pickle.dumps([self.q, self.members[0].h, self.p, self.t, self.n, self.NN])

	def get_info_dc(self):
		print ([self.t, self.n, self.q, self.p, self.members[_sage_const_0 ].h, self.H, self.t_new])
		return pickle.dumps([self.t, self.n, self.q, self.p, self.members[_sage_const_0 ].h, self.H, self.t_new])

	def get_user_status(self, username):
		id = self.active_users[username]
		return self.members[id].status

	def set_user_status(self, username, status):
		id = self.active_users[username]
		self.members[id].status = status

	def get_user_shares(self, username):
		id = self.active_users[username]
		return self.members[id].shares


	def gen_partial_sign(self, h0, m0, s0, username):
		id = self.active_users[username]
		user = self.members[id]
		x = auxmath.normilize_coeffs(-(_sage_const_1 /self.q)*m0*user.fs)
		y = auxmath.normilize_coeffs(-(_sage_const_1 /self.q)*m0*user.f)
		s = x*user.f + y*user.fs
		
		m = auxmath.normilize_coeffs2(s * (user.h - h0), self.q)
		s = s + s0
		h = user.h
		return h, m, s