Skip to content

Lattice

CVP_embedding(lattice_basis, v, M=1)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/embeddings.py
43
44
45
46
47
48
49
50
51
52
53
54
55
def CVP_embedding(lattice_basis: SquareMatrix, v: Vector, M = 1) -> SquareMatrix:
    r'''

    Args:

    Returns:

    '''
    assert v.shape[0] == lattice_basis.shape[0]
    n = lattice_basis.shape[0]


    return np.block([[lattice_basis, np.zeros((n, 1), dtype=lattice_basis.dtype)], [v, np.array([M])]])

bai_galbraith_embedding(A, b, q)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/embeddings.py
31
32
33
34
35
36
37
38
39
40
def bai_galbraith_embedding(A: MatrixInt, b: VectorInt, q: int) -> SquareMatrixInt:
    r'''

    Args:

    Returns:

    '''
    #B = np.block([np.identity(m, int), A, -b.reshape(-1,1)])
    pass

dual_q_ary_basis(A, q)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/embeddings.py
18
19
20
21
22
23
24
25
26
27
28
def dual_q_ary_basis(A: MatrixInt, q: int) -> MatrixModInt:
    r'''

    Args:

    Returns:

    '''
    m, n = A.shape
    Y = matrix.mod_left_kernel(A,q)
    return np.block([[Y], [ np.zeros((n, m - n), dtype=np.int64), q * np.identity(n, int)]])

q_ary_basis(A, q)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/embeddings.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def q_ary_basis(A: MatrixInt, q: int) -> MatrixModInt:
    r'''

    Args:

    Returns:

    '''
    m, n = A.shape
    B = np.block([[A.T], [q * np.identity(m, int)]])
    H, *_ = matrix.HNF(B)
    return H[:m, :m]

subset_sum_lattice(sequence, S)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/embeddings.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def subset_sum_lattice(sequence, S):
    r'''

    Args:

    Returns:

    '''
    n = len(sequence)
    M = np.identity(n + 1, dtype=float) * 2
    M[-1] = 1
    M[:-1, -1] = np.array(sequence, dtype=float)
    M[-1, -1] = S

babai_cvp(arbitrary_vector, lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
65
66
67
68
69
70
71
72
73
74
@enforce_type_check
def babai_cvp(arbitrary_vector: Vector, lattice_basis: SquareMatrix) -> VectorInt:
    r'''

    Args:

    Returns:

    '''
    return np.rint(arbitrary_vector @ np.linalg.inv(lattice_basis)).astype(int)

dual_basis(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
77
78
79
80
81
82
83
84
85
86
@enforce_type_check
def dual_basis(lattice_basis: SquareMatrix) -> SquareMatrix:
    r'''

    Args:

    Returns:

    '''
    return np.linalg.inv(lattice_basis.T)

gaussian_expected_shortest_length(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
40
41
42
43
44
45
46
47
48
49
50
@enforce_type_check
def gaussian_expected_shortest_length(lattice_basis: SquareMatrix) -> float:
    r'''

    Args:

    Returns:

    '''
    n = rank(lattice_basis)
    return np.sqrt(n / (2 * np.pi * np.e)) * (volume(lattice_basis) ** (1/n))

hadamard_ratio(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
28
29
30
31
32
33
34
35
36
37
@enforce_type_check
def hadamard_ratio(lattice_basis: SquareMatrix) -> float:
    r'''

    Args:

    Returns:

    '''
    return (volume(lattice_basis) / np.linalg.norm(lattice_basis, axis=1).prod()) ** (1/rank(lattice_basis))

rank(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
16
17
18
19
20
21
22
23
24
25
@enforce_type_check
def rank(lattice_basis: SquareMatrix) -> int:
    r'''

    Args:

    Returns:

    '''
    return lattice_basis.shape[0]

transition_matrix(from_basis, to_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
53
54
55
56
57
58
59
60
61
62
@enforce_type_check
def transition_matrix(from_basis: SquareMatrix, to_basis: SquareMatrix) -> SquareMatrixInt:
    r'''

    Args:

    Returns:

    '''
    return np.rint(to_basis @ np.linalg.inv(from_basis)).astype(int)

volume(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/fullrank.py
 4
 5
 6
 7
 8
 9
10
11
12
13
@enforce_type_check
def volume(lattice_basis: SquareMatrix) -> float:
    r'''

    Args:

    Returns:

    '''
    return abs(np.linalg.det(lattice_basis))

GLR_2dim(lattice_basis)

Gaussian Lattice reduction in dimension 2

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def GLR_2dim(lattice_basis: SquareMatrix) -> SquareMatrixFloat:
    '''
    Gaussian Lattice reduction in dimension 2

    Args:

    Returns:

    '''
    if lattice_basis.shape != (2,2):
        raise ValueError(f"Lattice has to have rank 2 for gaussian reduction")

    w1 = lattice_basis[0]
    w2 = lattice_basis[1]

    v1 = w1.astype(float)
    v2 = w2.astype(float)
    if np.linalg.norm(v1) > np.linalg.norm(v2):
        v1, v2 = v2, v1

    while np.linalg.norm(v2) > np.linalg.norm(v1):
        m = round(np.dot(v1, v2) / np.dot(v1, v1))
        if m == 0:
            return v1, v2
        v2 = v2 - m * v1
        if np.linalg.norm(v1) > np.linalg.norm(v2):
            v1, v2 = v2, v1

    return np.array([v1, v2])

GSO(B)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@enforce_type_check
def GSO(B: Matrix) -> Tuple[MatrixFloat, SquareMatrixFloat]:
    r'''

    Args:

    Returns:

    '''
    m, n = B.shape
    proj_coeff = lambda q, b: np.dot(b, q) / np.dot(q, q)
    B_star = B.astype(float)
    U = np.identity(m)

    for j in range(1, m):
        b = B_star[j].copy()
        for i in range(j):
            U[i,j] = proj_coeff(B_star[i], b)
            B_star[j] -= U[i][j] * B_star[i]

    # B = U.T @ B_star
    return B_star, U

LLL(lattice_basis, delta=0.75)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def LLL(lattice_basis: SquareMatrix, delta: float = 0.75) -> SquareMatrixFloat:
    r'''

    Args:

    Returns:

    '''
    n = lattice_basis.shape[0]
    B = lattice_basis.astype(float)
    while True:
        Bstar, _ = GSO(B)
        # Reduction Step
        for i in range(1, n):
            for j in range(i-1, -1, -1):
                cij = round(np.dot(B[i], Bstar[j]) / np.dot(Bstar[j], Bstar[j]))
                B[i] = B[i] - cij * B[j]
        # Swap step
        exists = False
        for i in range(n - 1):
            u = np.dot(B[i + 1], Bstar[i]) / np.dot(Bstar[i], Bstar[i])
            r = u * Bstar[i] + Bstar[i + 1]
            if delta * np.dot(Bstar[i], Bstar[i]) > np.dot(r, r):
                B[[i, i + 1]] = B[[i + 1, i]]
                exists = True
                break
        if not exists:
            break
    return B

babai_nearest_plane(lattice_basis, w)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def babai_nearest_plane(lattice_basis: SquareMatrix, w: VectorFloat):
    r'''

    Args:

    Returns:

    '''
    n = lattice_basis.shape[0]
    B = LLL(lattice_basis, 0.75)
    b = w.astype(float)
    for j in range(n - 1, -1, -1):
        Bstar, _ = GSO(B)
        cj = round(np.dot(b, Bstar[j]) / np.dot(Bstar[j], Bstar[j]))
        b = b - cj * B[j]
    return w - b

is_LLL_reduced(lattice_basis, delta)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
60
61
62
63
64
65
66
67
68
def is_LLL_reduced(lattice_basis: Matrix, delta: float):
    r'''

    Args:

    Returns:

    '''
    return is_size_reduced(lattice_basis) and lovasz_condition(lattice_basis, delta)

is_size_reduced(lattice_basis)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
28
29
30
31
32
33
34
35
36
37
def is_size_reduced(lattice_basis: Matrix) -> bool:
    r'''

    Args:

    Returns:

    '''
    _, U = GSO(lattice_basis)
    return np.all(np.abs(U[np.fromfunction(lambda i, j: i < j, U.shape).nonzero()]) <= 0.5)

lovasz_condition(lattice_basis, delta)

Args:

Returns:

Source code in src/lbpqc/primitives/lattice/reductions.py
45
46
47
48
49
50
51
52
53
54
55
56
57
def lovasz_condition(lattice_basis: Matrix, delta: float) -> bool:
    r'''

    Args:

    Returns:

    '''
    norm2 = lambda x: np.sum(x * x, axis=1)
    G, U = GSO(lattice_basis)
    lhs = delta * norm2(G[:-1])
    rhs = norm2(G[1:] + np.diag(U, 1)[:, np.newaxis] * G[:-1])
    return np.all(lhs <= rhs)