示例#1
0
def random_x_values(intercept, field_base=8):
    """
    :param intercept: number of shares, n
    :param field_base: the base of finite field
    :return: n X_i's we need
    """
    upper_bound = 2**field_base - 1
    x_values = []
    for i in range(intercept):
        value_x = randint(0, upper_bound)
        while value_x in x_values:
            value_x = randint(0, upper_bound)
        x_values.append(value_x)
    return x_values
示例#2
0
def share_generation(k, m, n, l=None, pub_inf=None):
    #k degree, m members, n identities, n >=m, modulus is 2l bits long
    if l == None:
        s = safePrime()
    else:
        s = safePrime(l)

    mod = s.get_modulus()
    A = random_polynomial(k, randint(0, mod - 1),
                          mod)  #the second parameter means secret
    #A = [985, 254, 1957, 312]
    #print "f_i" + str(A)
    P = Polynomial(A, mod, n)
    '''
    for i in range(1,n+1):
        print P.value(i)
    '''
    S = []
    if pub_inf == None:
        pub_inf = range(1, n + 1)
    inverseOfnminus1 = mod_inverse(n - 1, s.get_eular())
    for i in range(n):
        c = s.sqaure_and_multiply(P.value(pub_inf[i]), inverseOfnminus1)
        S.append(share_Polynomial(P.get_mul_value(c), mod, n, i))
    return A, S
示例#3
0
def random_polynomial(degree, intercept, upper_bound):
    """ Generates a random polynomial with positive coefficients.
    """
    if degree < 0:
        raise ValueError('Degree must be a non-negative number.')
    coefficients = [randint(0, upper_bound - 1) for x in range(degree)]
    coefficients.insert(0, intercept)
    return coefficients
示例#4
0
def random_polynomial(degree, intercept, upper_bound):
    """ Generates a random polynomial with positive coefficients.
    """
    if degree < 0:
        raise ValueError('Degree must be a non-negative number.')
    coefficients = [intercept]
    for _ in range(degree):
        random_coeff = randint(0, upper_bound - 1)
        coefficients.append(random_coeff)
    return coefficients
示例#5
0
def random_polynomial(degree, intercept, upper_bound):
    """ Generates a random polynomial with positive coefficients.
    """
    if degree < 0:
        raise ValueError("Degree must be a non-negative number.")
    coefficients = [intercept]
    for i in range(degree):
        random_coeff = randint(0, upper_bound - 1)
        coefficients.append(random_coeff)
    return coefficients
示例#6
0
def random_polynomials_coeff(degree, field_base=8):
    """
    :param degree: degree of the parameter, which is k-1
    :param field_base: the base of the finite field
    :return: return the coefficients
    """
    if degree < 0:
        raise ValueError("Degree cannot be a negative number.")
    coefficients = []
    upper_bound = 2**field_base - 1
    for i in range(degree):
        random_coeff = randint(0, upper_bound)
        coefficients.append(random_coeff)
    return coefficients
def generate_g_polynomial(degree, field_base, f_coefficients, secret):
    '''
    To detect cheating, we generate another polynomial g(x) = a0 + a1x + ... + ak-1x^(k-1), which 
    satisfies a0 + s0*r = 0, a1 + s1*r = 0, r belongs to GF(2^8). 
    Here we denote f(x) = s0 + s1x + ... + sk-1*x^(k-1)
    This function returns [a0, a1, ... , ak-1]
    
    '''
    F = ffield.FField(field_base)
    if degree < 0:
        raise ValueError("Degree cannot be a negative number.")
    g_coefficients = []
    upper_bound = 2**field_base - 1
    random_r = randint(0, upper_bound)
    s0 = ord(secret)
    a0 = F.Subtract(0, F.Multiply(random_r, s0))
    g_coefficients.append(a0)
    a1 = F.Subtract(0, F.Multiply(random_r, f_coefficients[0]))
    g_coefficients.append(a1)
    for i in range(1, degree):
        random_coeff = randint(0, upper_bound)
        g_coefficients.append(random_coeff)
    return g_coefficients
示例#8
0
def combine_action():
    x_values = []
    file = request.files['file0']
    filename = secure_filename(file.filename)
    x_values.append(int(filename.split('.')[0]))
    fp0 = os.path.join(app.config['COMBINE_FOLDER'], filename)
    file.save(fp0)

    file = request.files['file1']
    filename = secure_filename(file.filename)
    x_values.append(int(filename.split('.')[0]))
    fp1 = os.path.join(app.config['COMBINE_FOLDER'], filename)
    file.save(fp1)

    file = request.files['file2']
    filename = secure_filename(file.filename)
    x_values.append(int(filename.split('.')[0]))
    fp2 = os.path.join(app.config['COMBINE_FOLDER'], filename)
    file.save(fp2)
    print(x_values)
    y_values = []
    y_values.append(read_img(fp0))
    y_values.append(read_img(fp1))
    y_values.append(read_img(fp2))

    pixel_list = fetch_secret_px(x_values, y_values, 2)
    random_int = randint(1, UPPER_BOUND)
    fp = resume_img_secret(pixel_list, random_int)

    # shares = []
    # shares.append(read_file(fp0))
    # shares.append(read_file(fp1))
    # shares.append(read_file(fp2))
    #
    # secret = fetch_secret(shares, 2)
    # random_int = randint(1, UPPER_BOUND)
    # fp = os.path.join(app.config['COMBINE_FOLDER'], str(random_int) + ".jpg")
    # if os.path.exists(fp):
    #     os.remove(fp)
    #     random_int = randint(1, UPPER_BOUND)
    #     fp = os.path.join(app.config['COMBINE_FOLDER'], str(random_int) + ".jpg")
    # fh = open(fp, "wb")
    # fh.write(base64.b64decode(secret))
    # fh.close()
    # print(fp)
    # return jsonify(fp)
    return jsonify(fp)
示例#9
0
n = int(sys.argv[1])
rn = n/2
# 1024bit modulus
p1 = 11514844562790516655282382339997901326205477956091910904703943499562165719386197597458797308772109262770530717689695340334206066966408416627740404681562663L
q1 = 10423868126996985387214298003163473887816545231233184168499707786423333353741569421872579600137156466386073703548533187413949131260907402186949276882953347L
n1 = 120029221225396603943216312047289862327499010103056102571360609037851894896802135599029426055406735973259252625983764193927877972873712575004271317839369300415405820754446598418407393743146043397841334533819748796599919740150430457027942915144241523764241525063139352964442411349882905130519757202210486083061L
phi_1 = 120029221225396603943216312047289862327499010103056102571360609037851894896802135599029426055406735973259252625983764193927877972873712575004271317839369278476693130966944555921727050581770829375818147208724675592948633754651357329260923583767332614498512368458718114735914663194684677814700942512528921567052L

# 2048bit modulus
p2 = 146577968092877312553398069696998164521291778517747266285620770923741199895672989329883393034806862195786134935131447729635292436654606931321259940448635910284739497814443242901860431973473904516100420002377014760516087876319694046674932514757720182024711379435769206957579891080424488883021398865152668185847L
q2 = 161185860087032198171591177899415457502154280429768268816998037977934434736927998922989563504350082388505854576461166890757554351306828172915196408722596424537550936512619197351577432179777058245856717154742653464973379928171789861715315739636999452888860044631026786739283446194194236313836676520593401112159L
n2 = 23626295856859992265056730641465568551098718630084909446692697033225009031418301544480771860359655448664564181287192033292039242567946888484015603287220606316235151997738808126001142006753732243117524136214693962867755236545218137604793811858657384961745470253756673427655525701533591709750228473515744735089371318394149314335225824881013690795368106910575340795775903156498142994768069714898602269014705183848012394862675132316756664263850553979274626204746281189677294090916601447299267285290025687498569239951469483691647850557764983064367446907502177190801149385157844508468862171881233487337714983247906703413673L
phi_2 = 23626295856859992265056730641465568551098718630084909446692697033225009031418301544480771860359655448664564181287192033292039242567946888484015603287220606316235151997738808126001142006753732243117524136214693962867755236545218137604793811858657384961745470253756673427655525701533591709750228473515744735089063554565969404824500835633417277173344660851627825260673284347596467360135468726645729312475548239263720405351082517696363817475889118875038169855575048854855003656589539007045829421136774724736612102794349815466158382753273499155977198653107457555887577961091048514771998834606614762140856907862160634115668L

# degree of polynomial f11 is 10, and modulus is 1024 bits
f11 =  random_polynomial(10, randint(0, n1-1), n1)
P11 = Polynomial(f11, n1, n)
# degree of polynomial f12 is 10, and modulus is 2048 bits
f12 =  random_polynomial(10, randint(0, n2-1), n2)
P12 = Polynomial(f12, n2, n)

# degree of polynomial f11 is 20, and modulus is 1024 bits
f21 =  random_polynomial(20, randint(0, n1-1), n1)
P21 = Polynomial(f21, n1, n)

# degree of polynomial f12 is 20, and modulus is 2048 bits
f22 =  random_polynomial(20, randint(0, n2-1), n2)
P22 = Polynomial(f22, n2, n)

pub_inf = range(1,n+1)
f = open('results.txt','a')