Skip to content

Instantly share code, notes, and snippets.

@lmyyao
Last active December 1, 2023 03:33
Show Gist options
  • Save lmyyao/03aa50e092dd2424354c59a93fe8965f to your computer and use it in GitHub Desktop.
Save lmyyao/03aa50e092dd2424354c59a93fe8965f to your computer and use it in GitHub Desktop.
simple lwe demo
from sage.stats.distributions.discrete_gaussian_lattice import DiscreteGaussianDistributionLatticeSampler
def test_lwe(n, p, sigma, size=100):
F = GF(p)
a = random_vector(F, n)
HALF_P = int(p/2)
s = random_vector(F,n)
p41 = int(p/4)
p43 = int(p*3/4)
D = DiscreteGaussianDistributionLatticeSampler(F^1, sigma)
def decode(m):
if p41 < m < p43:
return 1
return 0
o = []
for i in range(size):
b = a*s + int(D()[0]) + HALF_P
m = (b- s*a) % p
o.append(decode(m))
print("n: ", n, "p: ", p, "sigma: ", sigma, sum(o))
if __name__ == "__main__":
p = [100 * i for i in range(1,10)]
for i in p:
test_lwe(256, next_prime(i), 3.3)
n = [2** i for i in range(3,10)]
for i in n:
test_lwe(i, next_prime(1000), 3.3)
sigma =[ i * 3.3 for i in range(1, 10)]
for i in sigma:
test_lwe(512, next_prime(8000), i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment