Esempio n. 1
0
def handle_query_0(query, path_, file_e_):
    in_=open(path_+file_e_, 'r')
    n, m =in_.readline().split()
    n=int(n)
    m=int(m)/2 
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
#    buf=open(path_+"priv.txt",'rb')
#    priv=pickle.load(buf)
#    buf.close()
    res=[0]*n
    significant_time_=[0]*n
    query.sort(key=lambda x: x[1])
    i=0
    for seq in range(n):
        acc_0=1
        acc_1=1 
        t=0
        old_pos=0
        
        for (letter, pos) in query:
#            print letter, pos
#            print decrypt(priv, pub, seq[2*pos]), decrypt(priv, pub, seq[2*pos+1])
#            break
            for j in range(old_pos, pos): 
                in_.readline()
                in_.readline()
            old_pos=pos+1
            
            if letter=='A': 
                st_=time.time()
                acc_0=e_add(pub, acc_0, mpz(in_.readline()))
                acc_0=e_add(pub, acc_0, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
            elif letter=='C':
                st_=time.time()
                acc_0=e_add(pub, acc_0, mpz(in_.readline()))
                acc_1=e_add(pub, acc_1, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
                t+=1
            elif letter=='G':
                st_=time.time()
                acc_1=e_add(pub, acc_1, mpz(in_.readline()))
                acc_0=e_add(pub, acc_0, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_ 
                t+=1
            elif letter=='T': 
                st_=time.time()
                acc_1=e_add(pub, acc_1, mpz(in_.readline()))
                acc_1=e_add(pub, acc_1, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
                t+=2
        st_=time.time()
        res[i]=e_add(pub, e_mul_const(pub, acc_0, random.randrange(pub.n)),
                         e_mul_const(pub, 
                                     e_add_const(pub, acc_1,  pub.n-t), 
                                     random.randrange(pub.n)))
        significant_time_[i]+=time.time()-st_
        i+=1
        for j in range(2*pos+2, 2*m):
            in_.readline()
    
    in_.close()
    print "sum significant query time = %f s" % (sum(significant_time_))
    return decrypt_query_res(res, path_)
def handle_query_1(query, path_, file_e_):
    in_=open(path_+file_e_, 'r')
    n, m =in_.readline().split()
    n=int(n)
    m=int(m)/3
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
#    buf=open(path_+"priv.txt",'rb')
#    priv=pickle.load(buf)
#    buf.close()
#    print query 
    query.sort(key=lambda x: x[1])
#    print query
    res=[0]*n #equal to the number of seq 
    significant_time_=[0]*n
#    counter=0
    cnst_1_= encrypt(pub, 1)
    with open(path_+file_pre, 'r') as pre_: 
            cnst= mpz(pre_.readlines()[len(query)-1])
            pre_.close()
    for i in range(n):
        acc=1
        old_pos=0
        for (letter, pos) in query:
            for j in range(old_pos, pos):
                in_.readline()
                in_.readline()
                in_.readline()
            
#                counter+=4
#            print counter
            old_pos=pos+1
            s1_=mpz(in_.readline())
            s2_=mpz(in_.readline())
            s3_=mpz(in_.readline())
            if letter=='A': 
                st_=time.time()
                acc= e_add(pub, acc, s1_)
                significant_time_[i]+=time.time()-st_
            elif letter=='C':
                st_=time.time()
                acc= e_add(pub, acc, s2_)
                significant_time_[i]+=time.time()-st_
            elif letter=='G':
                st_=time.time()
                acc= e_add(pub, acc, s3_)
                significant_time_[i]+=time.time()-st_
            elif letter=='T':
                st_=time.time()
                acc= e_add(pub, acc, e_add(pub, cnst_1_, invert(e_add(pub,e_add(pub, s1_, s2_),s3_), pub.n_sq)))
                significant_time_[i]+=time.time()-st_
#            counter+=4
#            print counter 
        for j in range(3*pos+3, 3*m):
            in_.readline()
#            counter+=1
#        print counter
        st_=time.time()
        res[i]=e_mul_const(pub, e_add(pub, acc, cnst), random.randrange(pub.n))
        significant_time_[i]+=time.time()-st_
        #print significant_time_[i]
    in_.close()
    sg=sum(significant_time_)
    print "sum significant query time = %f s" % (sg)
    return decrypt_query_res(res, path_), sg
Esempio n. 3
0
def handle_query_1(query, path_, file_e_):
    in_=open(path_+file_e_, 'r')
    n, m =in_.readline().split()
    n=int(n)
    m=int(m)/4
    buf=open(path_+"pub.txt",'rb')
    pub=pickle.load(buf)
    buf.close()
#    buf=open(path_+"priv.txt",'rb')
#    priv=pickle.load(buf)
#    buf.close()
#    print query 
    query.sort(key=lambda x: x[1])
#    print query
    res=[0]*n #equal to the number of seq 
    significant_time_=[0]*n
#    counter=0
    for i in range(n):
        acc=1
        old_pos=0
        for (letter, pos) in query:
            for j in range(old_pos, pos):
                in_.readline()
                in_.readline()
                in_.readline()
                in_.readline()
#                counter+=4
#            print counter
            old_pos=pos+1
            if letter=='A': 
                st_=time.time()
                acc= e_add(pub, acc, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
                in_.readline()
                in_.readline()
                in_.readline()
            elif letter=='C':
                in_.readline()
                st_=time.time()
                acc= e_add(pub, acc, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
                in_.readline()
                in_.readline()
            elif letter=='G':
                in_.readline()
                in_.readline()
                st_=time.time()
                acc= e_add(pub, acc, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
                in_.readline()
            elif letter=='T':
                in_.readline()
                in_.readline()
                in_.readline()
                st_=time.time()
                acc= e_add(pub, acc, mpz(in_.readline()))
                significant_time_[i]+=time.time()-st_
#            counter+=4
#            print counter 
        for j in range(4*pos+4, 4*m):
            in_.readline()
#            counter+=1
#        print counter
        st_=time.time()
        res[i]=e_mul_const(pub, e_add_const(pub, acc, pub.n-len(query)), random.randrange(pub.n))
        significant_time_[i]+=time.time()-st_
        #print significant_time_[i]
    in_.close()
    print "sum significant query time = %f s" % (sum(significant_time_))
    return decrypt_query_res(res, path_)
def handle_query_0(query, path_, file_e_):
    in_ = open(path_ + file_e_, "r")
    n, m = in_.readline().split()
    n = int(n)
    m = int(m) / 2
    buf = open(path_ + "pub.txt", "rb")
    pub = pickle.load(buf)
    buf.close()
    #    buf=open(path_+"priv.txt",'rb')
    #    priv=pickle.load(buf)
    #    buf.close()
    res = [0] * n
    significant_time_ = [0] * n
    query.sort(key=lambda x: x[1])
    i = 0
    ltrs = map(lambda x: x[0], query)
    t = ltrs.count("T") * 2 + ltrs.count("C") + ltrs.count("G")
    with open(path_ + file_pre, "r") as pre_:
        cnst = mpz(pre_.readlines()[t - 1])
        pre_.close()
    for seq in range(n):
        acc_0 = 1
        acc_1 = 1
        old_pos = 0

        for (letter, pos) in query:
            #            print letter, pos
            #            print decrypt(priv, pub, seq[2*pos]), decrypt(priv, pub, seq[2*pos+1])
            #            break
            for j in range(old_pos, pos):
                in_.readline()
                in_.readline()
            old_pos = pos + 1

            if letter == "A":
                st_ = time.time()
                acc_0 = e_add(pub, acc_0, mpz(in_.readline()))
                acc_0 = e_add(pub, acc_0, mpz(in_.readline()))
                significant_time_[i] += time.time() - st_
            elif letter == "C":
                st_ = time.time()
                acc_0 = e_add(pub, acc_0, mpz(in_.readline()))
                acc_1 = e_add(pub, acc_1, mpz(in_.readline()))
                significant_time_[i] += time.time() - st_
            elif letter == "G":
                st_ = time.time()
                acc_1 = e_add(pub, acc_1, mpz(in_.readline()))
                acc_0 = e_add(pub, acc_0, mpz(in_.readline()))
                significant_time_[i] += time.time() - st_
            elif letter == "T":
                st_ = time.time()
                acc_1 = e_add(pub, acc_1, mpz(in_.readline()))
                acc_1 = e_add(pub, acc_1, mpz(in_.readline()))
                significant_time_[i] += time.time() - st_
        st_ = time.time()
        res[i] = e_add(
            pub,
            e_mul_const(pub, acc_0, random.randrange(pub.n)),
            e_mul_const(pub, e_add(pub, acc_1, cnst), random.randrange(pub.n)),
        )
        significant_time_[i] += time.time() - st_
        i += 1
        for j in range(2 * pos + 2, 2 * m):
            in_.readline()

    in_.close()
    sg = sum(significant_time_)
    print "sum significant query time = %f s" % (sg)
    return decrypt_query_res(res, path_), sg