예제 #1
0
 def test_gram_schmidt(self):
     for i in range(_sage_const_20):
         dim = i
         A = random_matrix(QQ, i, algorithm='unimodular')
         oA1 = matrix_utils.gram_schmidt(A)
         oA2, _ = A.gram_schmidt(
         )  # compare results with sage's built in gram_schmidt
         assert oA1 == oA2
예제 #2
0
def random_even_symm_mat(n):
    while True:
        m = random_matrix(ZZ, n)
        m = m + m.transpose()
        for a in range(n):
            m[(a, a)] = m[(a, a)] * ZZ(2)
        if not m.is_singular():
            return m
예제 #3
0
 def time_search_loop(p):
     y = random_vector(F, n)
     g = random_matrix(F, p, n).rows()
     scalars = [  [ Fstar[randint(0,q-2)] for i in range(p) ]
                      for s in range(100) ]
     before = process_time()
     for m in scalars:
         e = y - sum(m[i]*g[i] for i in range(p))
     return (process_time() - before)/100.
예제 #4
0
def gen_instance(n, q, h, alpha=None, m=None, seed=None, s=None):
    """
    Generate FHE-style LWE instance

    :param n:     dimension
    :param q:     modulus
    :param alpha: noise rate (default: 8/q)
    :param h:     hamming weight of the secret (default: 2/3n)
    :param m:     number of samples (default: n)

    """
    if seed is not None:
        set_random_seed(seed)

    q = next_prime(ceil(q) - 1, proof=False)
    if alpha is None:
        stddev = 3.2
    else:
        stddev = alpha * q / sqrt(2 * pi)

    #RR = parent(alpha*q)
    #stddev = alpha*q/RR(sqrt(2*pi))

    if m is None:
        m = n
    K = GF(q, proof=False)

    while 1:
        A = random_matrix(K, m, n)
        if A.rank() == n:
            break

    if s is not None:
        c = A * s

    else:
        if h is None:
            s = random_vector(ZZ, n, x=-1, y=1)
        else:
            S = [-1, 1]
            s = [S[randint(0, 1)] for i in range(h)]
            s += [0 for _ in range(n - h)]
            shuffle(s)
            s = vector(ZZ, s)
        c = A * s

    D = DiscreteGaussian(stddev)

    for i in range(m):
        c[i] += D()

    u = random_vector(K, m)

    print '(A, c) is n-dim LWE samples (with secret s) / (A, u) is uniform samples'

    return A, c, u, s
예제 #5
0
 def time_search_loop(p):
     y = random_vector(F, n)
     g = random_matrix(F, p, n).rows()
     scalars = [[Fstar[randint(0, q - 2)] for i in range(p)]
                for s in range(100)]
     before = time.clock()
     for m in scalars:
         e = y - sum(m[i] * g[i] for i in range(p))
         errs = e.hamming_weight()
     return (time.clock() - before) / 100.
 def time_search_loop(p):
     y = random_vector(F, n)
     g = random_matrix(F, p, n).rows()
     scalars = [  [ Fstar[randint(0,q-2)] for i in range(p) ]
                      for s in range(100) ]
     before = time.clock()
     for m in scalars:
         e = y - sum(m[i]*g[i] for i in range(p))
         errs = e.hamming_weight()
     return (time.clock() - before)/100.
예제 #7
0
def gen_fhe_instance(n, q, alpha=None, h=None, m=None, seed=None):
    """
    Generate FHE-style LWE instance

    :param n:     dimension
    :param q:     modulus
    :param alpha: noise rate (default: 8/q)
    :param h:     hamming weight of the secret (default: 2/3n)
    :param m:     number of samples (default: n)

    """
    if seed is not None:
        set_random_seed(seed)

    q = next_prime(ceil(q)-1, proof=False)
    if alpha is None:
        alpha = ZZ(8)/q

    n, alpha, q = preprocess_params(n, alpha, q)

    stddev = stddevf(alpha*q)

    if m is None:
        m = n
    K = GF(q, proof=False)
    A = random_matrix(K, m, n)

    if h is None:
        s = random_vector(ZZ, n, x=-1, y=1)
    else:
        S = [-1, 1]
        s = [S[randint(0, 1)] for i in range(h)]
        s += [0 for _ in range(n-h)]
        shuffle(s)
        s = vector(ZZ, s)
    c = A*s

    D = DiscreteGaussian(stddev)

    for i in range(m):
        c[i] += D()

    return A, c
예제 #8
0
def gen_fhe_instance(n, q, alpha=None, h=None, m=None, seed=None):
    """
    Generate FHE-style LWE instance

    :param n:     dimension
    :param q:     modulus
    :param alpha: noise rate (default: 8/q)
    :param h:     hamming weight of the secret (default: 2/3n)
    :param m:     number of samples (default: n)

    """
    if seed is not None:
        set_random_seed(seed)

    q = next_prime(ceil(q) - 1, proof=False)
    if alpha is None:
        alpha = ZZ(8) / q

    n, alpha, q = preprocess_params(n, alpha, q)

    stddev = stddevf(alpha * q)

    if m is None:
        m = n
    K = GF(q, proof=False)
    A = random_matrix(K, m, n)

    if h is None:
        s = random_vector(ZZ, n, x=-1, y=1)
    else:
        S = [-1, 1]
        s = [S[randint(0, 1)] for i in range(h)]
        s += [0 for _ in range(n - h)]
        shuffle(s)
        s = vector(ZZ, s)
    c = A * s

    D = DiscreteGaussian(stddev)

    for i in range(m):
        c[i] += D()

    return A, c