コード例 #1
0
def setup():
    print("SETUP:")
    print("1. Select random l_p-bit primes and modulus n = pq")
    while True:
        p, q = randomPrime(l_p), randomPrime(l_p)
        if isPrime(p) and isPrime(q) and p != q:
            n = p * q
            phi_n = (p - 1) * (q - 1)
            break

    print("2. Choose random a, a_0, g, h in QR(n)")
    while True:
        g = random(n)
        if is_cyclic(g, n):
            g = g**2
            break
    a, a_0, h = [(g**random(n)) % n for _ in range(3)]

    print("3. Choose random secret element x and set public key y = g**x")
    x = random(phi_n)
    y = (g**x) % n

    print("4. The group public key Y: (n, a, a_0, y, g, h)")
    Y = {"n": n, "a": a, "a_0": a_0, "y": y, "g": g, "h": h}

    print("5. Corresponding secret key S: (p, q, phi_n, x)")
    S = {"p": p, "q": q, "phi_n": phi_n, "x": x}
    return (Y, S)
コード例 #2
0
def join_0(u):
    print("JOIN:")
    print("1. User generates x and r and sends C_1 with proof")
    u["x"] = random(u["n"]**2)
    u["r"] = random(2**(2 * l_p))

    u["C_1"] = (u['g']**u['x'] * u['h']**u['r']) % u['n']

    return {"C_1": u["C_1"], "status": "OK"}
コード例 #3
0
def join_1(gm):
    print("2. GM checks C1 and selects alpha and beta")
    if not is_cyclic(gm["C_1"], gm['n']):
        raise ValueError("FUCKUP")
        return {"status": "JOIN ERROR"}

    gm['alpha'], gm['beta'] = random(2**lambda_2), random(2**lambda_2)

    return {'alpha': gm['alpha'], 'beta': gm['beta'], 'status': 'OK'}
コード例 #4
0
ファイル: integergroup.py プロジェクト: MichaelONeil/charm
 def randomGen(self):
     while True:
         h = random(self.p)
         g = (h ** self.r) % self.p
         if not g == 1:
             break
     return g
コード例 #5
0
ファイル: commit_df02.py プロジェクト: teresatseng/pydemix
    def setup(self, secparam=None, N=0):
        Xz = integer(random(N))

        S = randomQR(N)
        Z = S**Xz

        return {'S': S, 'Z': Z, 'N': N}
コード例 #6
0
ファイル: commit_df02.py プロジェクト: adelapie/irma_phase_2
    def setup(self, secparam=None, N=0):
        Xz = integer(random(N))

        S = randomQR(N)
        Z = S ** Xz

        return { 'S': S, 'Z': Z, 'N': N }
コード例 #7
0
ファイル: ygc.py プロジェクト: Fadion96/Cloud
def main():
    d, N, e = generate_RSA(256)
    k_a = generate_keys(16)
    k_b = generate_keys(16)
    k_c = generate_keys(16)
    table = [
        enc(k_a[0], k_b[0], k_c[0]),
        enc(k_a[0], k_b[1], k_c[0]),
        enc(k_a[1], k_b[0], k_c[0]),
        enc(k_a[1], k_b[1], k_c[1])
    ]
    shuffle(table)
    key_a = secrets.choice(k_a)
    # OT
    xs = generate_random(N)
    bit = secrets.randbelow(2)
    k = random(N)
    v = choose(bit, k, xs, e)
    ks = mask_messages(v, xs, N, d, k_b)
    key_b = int(ks[bit] - k)
    #Bob has k_b_bit
    print(k_c)
    for ct in table:
        try:
            key_c = int.from_bytes(dec(key_a, key_b, ct), byteorder='big')
            for key in k_c:
                if key_c == key:
                    print(key_c)
                    break
        except:
            pass
コード例 #8
0
 def randomGen(self):
     while True:
         h = random(self.p)
         g = (h**self.r) % self.p
         if not g == 1:
             break
     return g
コード例 #9
0
def sign(u, m):
    print("SIGN:")
    print("1. Generate random value w")
    w = random(2**l_p)
    T_1 = (u['A'] * u['y']**w) % u['n']
    T_2 = (u['g']**w) % u['n']
    T_3 = ((u['g']**u['e']) * (u['h']**w)) % u['n']

    print("2. Randomly choose r_1, r_2, r_3, and r_4")
    r_1 = random(2**(eps * (gamma_2 + k)))
    r_2 = random(2**(eps * (lambda_2 + k)))
    r_3 = random(2**(eps * (gamma_1 + 2 * l_p + k + 1)))
    r_4 = random(2**(eps * (2 * l_p + k)))

    print("(a) d_1, d_2, d_3, d_4")
    d_1 = ((T_1**r_1) / ((u['a']**r_2) * (u['y']**r_3))) % u['n']
    d_2 = ((T_2**r_1) / (u['g']**r_3)) % u['n']
    d_3 = (u['g']**r_4) % u['n']
    d_4 = ((u['g']**r_1) * (u['h']**r_4)) % u['n']

    print("(b) c = HASH()")
    c = integer(
        int(
            hashlib.sha1(
                serialize(u['g']) + serialize(u['h']) + serialize(u['y']) +
                serialize(u['a_0']) + serialize(u['a']) + serialize(T_1) +
                serialize(T_2) + serialize(T_3) + serialize(d_1) +
                serialize(d_2) + serialize(d_3) + serialize(d_4) +
                m.encode()).hexdigest(), 16))

    print("(c) s_1, s_2, s_3, s_4")
    s_1 = integer(r_1) - c * (integer(u['e']) - 2**gamma_1)
    s_2 = integer(r_2) - c * (integer(u['x']) - 2**lambda_1)
    s_3 = integer(r_3) - c * integer(u['e']) * integer(w)
    s_4 = integer(r_4) - c * integer(w)

    print("3. Output signature")
    return {
        "c": c,
        "s_1": s_1,
        "s_2": s_2,
        "s_3": s_3,
        "s_4": s_4,
        "T_1": T_1,
        "T_2": T_2,
        "T_3": T_3
    }
コード例 #10
0
 def Encrypt(self, pk, plaintext):  #公钥加密
     r = random(self.N / 4) % self.N2
     A = (self.g**r) % self.N2
     B1 = (self.N * plaintext + 1) % (self.N2)
     B2 = (pk**r) % (self.N2)
     B = B1 * B2 % self.N2
     ciphertext = {"A": A, "B": B}
     return ciphertext
コード例 #11
0
ファイル: integergroup.py プロジェクト: MichaelONeil/charm
 def randomGen(self):
     while True:
         h = random(self.p)
         g = (h ** self.r) % self.p
         if not g == 1:
             #print "g => %s" % g 
             break
     return g
コード例 #12
0
 def randomGen(self):
     while True:
         h = random(self.p)
         g = (h**self.r) % self.p
         if not g == 1:
             #print "g => %s" % g
             break
     return g
コード例 #13
0
ファイル: dupless.py プロジェクト: syoungjoo/fogpaper
	def generate_key (self, pk,h,ks):
		BCStart(group)
		r=random(pk['N'])
		#x=h*(r**pk['e'])
		x=1
		y=ks.sign(x)
		#z=y*(r**-1)
		BCEnd (group, benchmarkResult,  "KS")
		# we should add network delay for interaction with KS
		benchmarkResult['KS']=benchmarkResult['KS']+options.KS_delay1/1000
コード例 #14
0
 def getV(self, x0, x1):
     # self.b = 0
     self.b = 1
     if self.b == 0:
         self.xb = x0
     else:
         self.xb = x1
     self.k = random(self.pk['N'])
     self.v = (self.xb + self.k**self.pk['e']) % self.pk['N']
     return self.v
コード例 #15
0
ファイル: commit_df02.py プロジェクト: adelapie/irma_phase_2
    def setupBlock(self, secparam=None, N=0, l=16):
        Xr = {}

        for i in range(1, l + 1): 
            Xr[str(i)] = integer(random(N))

        S = randomQR(N)
        R = {}

        for i in range(1, l + 1): 
            R[str(i)] = S ** Xr[str(i)]

        return { 'S': S, 'R': R, 'N': N }
コード例 #16
0
ファイル: commit_df02.py プロジェクト: teresatseng/pydemix
    def setupBlock(self, secparam=None, N=0, l=16):
        Xr = {}

        for i in range(1, l + 1):
            Xr[str(i)] = integer(random(N))

        S = randomQR(N)
        R = {}

        for i in range(1, l + 1):
            R[str(i)] = S**Xr[str(i)]

        return {'S': S, 'R': R, 'N': N}
コード例 #17
0
    def keygen(self, p, q):

        N = p * q

        Xz = integer(random(N))
        Xr = {}

        for i in range(1, l + 1):
            Xr[str(i)] = integer(random(N))

        S = randomQR(N)
        Z = S**Xz

        R = {}

        for i in range(1, l + 1):
            R[str(i)] = S**Xr[str(i)]

        pk = {'N': N, 'R': R, 'S': S, 'Z': Z}
        sk = {'p': p, 'q': q}

        return (pk, sk)
コード例 #18
0
    def keygen(self, p, q):

        N = p * q

        Xz = integer(random(N))
        Xr = {}

        for i in range(1, l + 1): 
            Xr[str(i)] = integer(random(N))

        S = randomQR(N)
        Z = S ** Xz

        R = {}

        for i in range(1, l + 1): 
            R[str(i)] = S ** Xr[str(i)]

        pk = { 'N':N, 'R':R,'S':S, 'Z':Z }
        sk = { 'p':p, 'q':q }

        return (pk, sk)
コード例 #19
0
def key_gen(key_size, message_elem_bit_len, vector_len):
    """
    Generate keys for vector commitment
    :param key_size: security key size (k in the paper)
    :param message_elem_bit_len: length of message in bits (l in the paper)
    :param vector_len: length of commitment vector (q in the paper)
    :return:
    """
    k = key_size
    l = message_elem_bit_len
    q = vector_len

    private_key = rsa.generate_private_key(public_exponent=65537,
                                           key_size=k,
                                           backend=default_backend())
    private_numbers = private_key.private_numbers()

    n = private_numbers.public_numbers.n
    p_1 = private_numbers.p
    p_2 = private_numbers.q

    # logger.info(p_1)
    # logger.info(p_2)

    phi_n = (p_1 - 1) * (p_2 - 1)

    # generate list of primes [e_1,...,e_q], each has length (l+1)
    e = list()
    while True:
        tmp_prime = reduce(randomPrime(l + 1) % n)
        if isPrime(tmp_prime) and phi_n % int(tmp_prime) != 0:
            e.append(int(tmp_prime))
            if len(e) == q:
                break

    a = random(n)

    s = list()
    for i in range(q):
        tmp_exponent = integer(1) % n
        for j in range(q):
            if j != i:
                tmp_exponent = reduce(tmp_exponent * (integer(e[j]) % n))
        s_i = reduce(a**tmp_exponent)
        s.append(int(s_i))

    return n, int(a), s, e, p_1, p_2
コード例 #20
0
    def gen_key_pair(self):

        self.pk_i = {}
        self.sk_i = {}

        self.pksig = Sig_CL03_Idmx(lin=self.l)
        (self.pk_i, self.sk_i) = self.pksig.keygen(self.p, self.q)

        self.S = self.pk_i['S']
        self.Z = self.pk_i['Z']
        self.R = self.pk_i['R']
        self.N = self.pk_i['N']

        self.Ro = self.pk_i['S'] ** integer(random(self.pk_i['N']))
        self.pk_i['Ro'] = self.Ro

        return (self.pk_i, self.sk_i)
コード例 #21
0
  def genKeyPair(self):
  
    self.pk_i = {}
    self.sk_i = {}
  
    self.pksig = Sig_CL03_Idmx(lin = self.l)
    (self.pk_i, self.sk_i) = self.pksig.keygen(self.p, self.q)

    self.S = self.pk_i['S']
    self.Z = self.pk_i['Z']
    self.R = self.pk_i['R']
    self.N = self.pk_i['N']
    
    self.Ro = self.pk_i['S'] ** integer(random(self.pk_i['N'])) 
    self.pk_i['Ro'] = self.Ro
    
    return (self.pk_i, self.sk_i)
コード例 #22
0
    def keygen(self, secparam=1024, params=None):
        if params:
            (N, e, d, p, q) = self.convert(params)
            phi_N = (p - 1) * (q - 1)
            pk = {'N': N, 'e': e}
            sk = {'phi_N': phi_N, 'd': d, 'N': N}
            return (pk, sk)

        (p, q, N, phi_N) = self.paramgen(secparam)

        while True:
            e = random(phi_N)
            if not gcd(e, phi_N) == 1:
                continue
            d = e**-1
            break
        pk = {'N': N, 'e': toInt(e)}  # strip off \phi
        sk = {'phi_N': phi_N, 'd': d, 'N': N}

        return (pk, sk)
コード例 #23
0
ファイル: pkenc_rsa.py プロジェクト: FinalF/charm
    def keygen(self, secparam=1024, params=None):
        if params: 
            (N, e, d, p, q) = self.convert(params)
            phi_N = (p - 1) * (q - 1)
            pk = { 'N':N, 'e':e }
            sk = { 'phi_N':phi_N, 'd':d , 'N':N}
            return (pk, sk)

        (p, q, N, phi_N) = self.paramgen(secparam)
        
        while True:
            e = random(phi_N)
            if not gcd(e, phi_N) == 1:
                continue
            d = e ** -1
            break
        pk = { 'N':N, 'e':toInt(e) } # strip off \phi
        sk = { 'phi_N':phi_N, 'd':d , 'N':N}

        return (pk, sk)
コード例 #24
0
    def __init__(self, secparam):

        # generate p,q
        while True:
            p, q = randomPrime(secparam), randomPrime(secparam)
            if isPrime(p) and isPrime(q) and p != q:
                N = p * q
                phi = (p - 1) * (q - 1)
                break

        # calculate private key and public key
        while True:
            e = random(phi)
            if not gcd(e, phi) == 1:
                continue
            d = e**-1
            break

        # prepare public key
        self.pk = {'N': N, 'e': toInt(e)}

        # prepare private key
        self.sk = {'phi': phi, 'd': d, 'N': N}
コード例 #25
0
'''
@Descripttion: 
@version: 
@Author: HuiKwok
@Date: 2019-10-10 06:53:46
@LastEditors: HuiKwok
@LastEditTime: 2019-10-10 07:01:56
'''
from charm.toolbox.integergroup import IntegerGroup
from charm.schemes.pkenc.pkenc_rsa import RSA_Enc, RSA_Sig
from charm.core.math.integer import integer, randomBits, random, randomPrime, isPrime, encode, decode, hashInt, bitsize, legendre, gcd, lcm, serialize, deserialize, int2Bytes, toInt

secparam = 1024
p, q = randomPrime(int(secparam / 2),
                   True), randomPrime(int(secparam / 2), True)
N = p * q
N2 = N * N

g = random(N2)
print(type(g))
int(g) - 1
コード例 #26
0
ファイル: commit_df02.py プロジェクト: adelapie/irma_phase_2
def randomQR(n):
    return random(n) ** 2
コード例 #27
0
while True:
    q = randomPrime(prime_bits, 1)
    q_prime = (q - 1) / 2
    if (isPrime(q) and isPrime(q_prime)):
        break
N = p * q
group_order = 2 * p_prime * q_prime  # order of L_DCR
N_square = N * N

print('p = ', p)
print('q = ', q)
# search the generator for L_DCR
tmp = 0
while True:
    tmp = random(N_square)
    if (gcd(tmp, N_square) == 1):
        break

val1 = (tmp**(2 * N)) % N_square
print('val1 = ', val1)
print('toInt(val1) = ', toInt(val1))
g = N_square - toInt(val1)
g = g % N_square
print('g = ', g)

alpha_value = random(N_square / 2)  # of length bits
beta_value = random(N_square / 2)
hp_1_value = (g**alpha_value) % N_square
hp_2_value = (g**beta_value) % N_square
コード例 #28
0
 def __init__(self, sk):
     self.sk = sk
     self.m0 = random(sk['N'])
     self.m1 = random(sk['N'])
コード例 #29
0
ファイル: commit_df02.py プロジェクト: teresatseng/pydemix
def randomQR(n):
    return random(n)**2
コード例 #30
0
ファイル: ot.py プロジェクト: Fadion96/Cloud
 def generate_messages(self, nr_of_messages):
     self.messages = [random(self.N) for _ in range(nr_of_messages)]
コード例 #31
0
 def random(self, max=0):
     if max == 0:
         return random(self.p)
     else:
         return random(max)
コード例 #32
0
 def KeyGen(self):  #公私钥生成
     tmp = self.N2 / 2
     sk = random(tmp) % self.N2
     pk = (self.g**sk) % self.N2
     return pk, sk
コード例 #33
0
 def __init__(self, secparam=1024, param=None):
     if param:
         self.N2 = param.N2
         self.N = param.N
         self.g = param.g
         self.k = param.k
     else:
         self.p, self.q = randomPrime(int(secparam / 2), True), randomPrime(
             int(secparam / 2), True)
         self.pp = (self.p - 1) / 2
         self.qq = (self.q - 1) / 2
         self.N = self.p * self.q
         while True:  # choose a good N
             if bitsize(self.N) == secparam and len(int2Bytes(
                     self.N)) == int(
                         secparam / 8) and int2Bytes(self.N)[0] & 128 != 0:
                 break
             self.p, self.q = randomPrime(int(secparam / 2),
                                          True), randomPrime(
                                              int(secparam / 2), True)
             self.pp = (self.p - 1) / 2
             self.qq = (self.q - 1) / 2
             self.N = self.p * self.q
         self.N2 = self.N**2
         self.g = random(self.N2)
         one = integer(1) % self.N2
         while True:  #choose a good g
             self.g = random(self.N2)
             self.g = integer(
                 (int(self.g) - 1) * (int(self.g) - 1)) % self.N2
             if self.g == one:
                 continue
             tmp = self.g**self.p % self.N2
             if tmp == one:
                 continue
             tmp = self.g**self.pp % self.N2
             if tmp == one:
                 continue
             tmp = self.g**self.q % self.N2
             if tmp == one:
                 continue
             tmp = self.g**self.qq % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.pp) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.q) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.pp * self.q) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.pp * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.q * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.q * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.pp * self.q) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.pp * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.p * self.q * self.qq) % self.N2
             if tmp == one:
                 continue
             tmp = self.g**(self.pp * self.q * self.qq) % self.N2
             if tmp == one:
                 continue
             break
         self.k = integer(
             (int(self.g**(self.pp * self.qq)) - 1)) / self.N % self.N
         self.MK = {"pp": self.pp, "qq": self.qq}
コード例 #34
0
ファイル: ot.py プロジェクト: Fadion96/Cloud
 def generate_random(self, nr_of_messages):
     self.xs = [random(self.N) for _ in range(nr_of_messages)]
     return self.xs
コード例 #35
0
ファイル: ot.py プロジェクト: Fadion96/Cloud
 def choose(self, xs, nr_of_messages):
     self.xs = xs
     self.b = secrets.randbelow(nr_of_messages)
     self.k = random(self.N)
     self.v = self.xs[self.b] + (self.k ** self.e)
     return self.v
コード例 #36
0
ファイル: party_A.py プロジェクト: eldron/AKE_chen
group_order_bits = 2 * security_level
group_p = 0  # the prime number for mod calculation

group_order = randomPrime(group_order_bits)
#print('group_order = ', group_order)

while True:
    random_k_value = randomBits(bits - group_order_bits)
    group_p = random_k_value * group_order + 1
    if isPrime(group_p):
        break
#print('group_p = ', group_p)

group_g = 0
while True:
    i = random(group_p)
    tmp1 = (group_p - 1) / group_order
    tmp2 = (i**tmp1) % group_p
    if tmp2 != 1 and i > 1:
        group_g = tmp2
        break

#print('group_g = ', group_g)
time1 = time.time()

alpha_1_value = randomBits(group_order_bits) % group_order
alpha_2_value = randomBits(group_order_bits) % group_order
beta_1_value = randomBits(group_order_bits) % group_order
beta_2_value = randomBits(group_order_bits) % group_order
alpha_1_bytes = Conversion.IP2OS(alpha_1_value, group_order_bits // 8)
alpha_2_bytes = Conversion.IP2OS(alpha_2_value, group_order_bits // 8)
コード例 #37
0
ファイル: ygc.py プロジェクト: Fadion96/Cloud
def generate_random(value):
    xs = [random(value) for _ in range(2)]
    return xs
コード例 #38
0
ファイル: integergroup.py プロジェクト: MichaelONeil/charm
 def random(self, max=0):
     if max == 0:
         return random(self.p)        
     else:
         return random(max)
コード例 #39
0
 def getV(self, A, index):
     self.index = index
     self.k = random(self.pk['N'])
     v = (A[index] + self.k**self.pk['e']) % self.pk['N']
     return v
コード例 #40
0
 def getRandomMessages(self):
     self.x0 = random(self.sk['N'])
     self.x1 = random(self.sk['N'])
     return self.x0, self.x1