Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ludopulles committed Sep 19, 2024
1 parent d4cad09 commit f7a9fc4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
12 changes: 6 additions & 6 deletions estimator/lwe_dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .io import Logging
from .conf import red_cost_model as red_cost_model_default, mitm_opt as mitm_opt_default
from .errors import OutOfBoundsError, InsufficientSamplesError
from .nd import NoiseDistribution
from .nd import DiscreteGaussian, SparseTernary
from .lwe_guess import exhaustive_search, mitm, distinguish


Expand Down Expand Up @@ -66,17 +66,17 @@ def dual_reduce(
raise OutOfBoundsError(f"Splitting weight {h1} must be between 0 and h={h}.")
# assuming the non-zero entries are uniform
p = h1 / 2
red_Xs = NoiseDistribution.SparseTernary(params.n - zeta, h / 2 - p)
slv_Xs = NoiseDistribution.SparseTernary(zeta, p)
red_Xs = SparseTernary(params.n - zeta, h / 2 - p)
slv_Xs = SparseTernary(zeta, p)

if h1 == h:
# no reason to do lattice reduction if we assume
# that the hw on the reduction part is 0
return replace(params, Xs=slv_Xs, m=oo), 1
else:
# distribution is i.i.d. for each coordinate
red_Xs = replace(params.Xs, n=params.n - zeta)
slv_Xs = replace(params.Xs, n=zeta)
red_Xs = params.Xs.resize(params.n - zeta)
slv_Xs = params.Xs.resize(zeta)

c = red_Xs.stddev * params.q / params.Xe.stddev

Expand All @@ -91,7 +91,7 @@ def dual_reduce(
# Compute new noise as in [INDOCRYPT:EspJouKha20]
# ~ sigma_ = rho * red_Xs.stddev * delta ** (m_ + red_Xs.n) / c ** (m_ / (m_ + red_Xs.n))
sigma_ = rho * red_Xs.stddev * delta**d / c ** (m_ / d)
slv_Xe = NoiseDistribution.DiscreteGaussian(params.q * sigma_)
slv_Xe = DiscreteGaussian(params.q * sigma_)

slv_params = LWEParameters(
n=zeta,
Expand Down
6 changes: 3 additions & 3 deletions estimator/lwe_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from sage.all import oo, binomial, log, sqrt, ceil

from .nd import NoiseDistribution
from .nd import NoiseDistribution, DiscreteGaussian
from .errors import InsufficientSamplesError


Expand Down Expand Up @@ -120,7 +120,7 @@ def amplify_m(self, m):
# -two signs per position (+1,-1)
# - all "-" and all "+" are the same
if binomial(self.m, k) * 2**k - 1 >= m:
Xe = NoiseDistribution.DiscreteGaussian(float(sqrt(k) * self.Xe.stddev))
Xe = DiscreteGaussian(float(sqrt(k) * self.Xe.stddev))
d["Xe"] = Xe
d["m"] = ceil(m)
return LWEParameters(**d)
Expand Down Expand Up @@ -157,7 +157,7 @@ def switch_modulus(self):
self.n,
p,
Xs=self.Xs,
Xe=NoiseDistribution.DiscreteGaussian(sqrt(2) * self.Xe.stddev * scale),
Xe=DiscreteGaussian(sqrt(2) * self.Xe.stddev * scale),
m=self.m,
tag=f"{self.tag},scaled" if self.tag else None,
)
Expand Down
75 changes: 39 additions & 36 deletions estimator/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def stddevf(sigma):
EXAMPLE::
>>> from estimator.nd import stddevf
>>> stddevf(64.0)
>>> from estimator import *
>>> ND.stddevf(64.0)
25.532...
>>> stddevf(64)
>>> ND.stddevf(64)
25.532...
>>> stddevf(RealField(256)(64)).prec()
>>> ND.stddevf(RealField(256)(64)).prec()
256
"""
try:
Expand All @@ -43,16 +43,16 @@ def sigmaf(stddev):
EXAMPLE::
>>> from estimator.nd import stddevf, sigmaf
>>> from estimator import *
>>> n = 64.0
>>> sigmaf(stddevf(n))
>>> ND.sigmaf(ND.stddevf(n))
64.000...
>>> sigmaf(RealField(128)(1.0))
>>> ND.sigmaf(RealField(128)(1.0))
2.5066282746310005024157652848110452530
>>> sigmaf(1.0)
>>> ND.sigmaf(1.0)
2.506628274631...
>>> sigmaf(1)
>>> ND.sigmaf(1)
2.506628274631...
"""
RR = parent(stddev)
Expand All @@ -75,6 +75,7 @@ class NoiseDistribution:
n: int = None
bounds: tuple = (-oo, oo)
density: float = 1.0 # hamming_weight() / n.
is_Gaussian_like: bool = False

def __lt__(self, other):
"""
Expand Down Expand Up @@ -215,8 +216,8 @@ class DiscreteGaussian(NoiseDistribution):
EXAMPLE::
>>> from estimator.nd import DiscreteGaussian
>>> DiscreteGaussian(3.0, 1.0)
>>> from estimator import *
>>> ND.DiscreteGaussian(3.0, 1.0)
D(σ=3.00, μ=1.00)
"""
# cut-off for Gaussian distributions
Expand All @@ -225,25 +226,21 @@ class DiscreteGaussian(NoiseDistribution):
gaussian_tail_prob: float = 1 - 2 * exp(-4 * pi)

def __init__(self, stddev, mean=0, n=None):
super().__init__(stddev=stddev, mean=mean, n=n)
super().__init__(stddev=stddev, mean=mean, n=n, is_Gaussian_like=True)

b_val = oo if n is None else ceil(log(n, 2) * stddev)
self.bounds = (-b_val, b_val)
self.density = max(0.0, 1 - RR(1 / sigmaf(stddev)))

@property
def is_Gaussian_like(self):
return True

def support_size(self, fraction=1.0):
"""
Compute the size of the support covering the probability given as fraction.
EXAMPLE::
>>> from estimator.nd import DiscreteGaussian
>>> DiscreteGaussian(1.0, n=128).support_size(0.99)
???
>>> from estimator import *
>>> ND.DiscreteGaussian(1.0, n=128).support_size(0.99)
2.686...e+174
"""
# We will treat this noise distribution as bounded with failure probability `1 - fraction`.
n = len(self)
Expand All @@ -259,18 +256,20 @@ def support_size(self, fraction=1.0):
return (2 * b + 1)**n


class DiscreteGaussianAlpha(DiscreteGaussian):
def DiscreteGaussianAlpha(alpha, q, mean=0, n=None):
"""
A discrete Gaussian distribution with standard deviation α⋅q/√(2π) per component.
EXAMPLE::
>>> from estimator.nd import DiscreteGaussianAlpha
>>> DiscreteGaussianAlpha(0.001, 2048)
>>> from estimator import *
>>> alpha, q = 0.001, 2048
>>> ND.DiscreteGaussianAlpha(alpha, q)
D(σ=0.82)
>>> ND.DiscreteGaussianAlpha(alpha, q) == ND.DiscreteGaussian(ND.stddevf(alpha * q))
True
"""
def __init__(self, alpha, q, mean=0, n=None):
super().__init__(RR(stddevf(alpha * q)), RR(mean), n)
return DiscreteGaussian(RR(stddevf(alpha * q)), RR(mean), n)


class CenteredBinomial(NoiseDistribution):
Expand All @@ -279,30 +278,27 @@ class CenteredBinomial(NoiseDistribution):
EXAMPLE::
>>> import estimator.ND
>>> from estimator import *
>>> ND.CenteredBinomial(8)
D(σ=2.00)
"""
def __init__(self, eta, n=None):
super().__init__(
density=1 - binomial(2 * eta, eta) * 2 ** (-2 * eta),
stddev=RR(sqrt(eta / 2.0)),
is_Gaussian_like=True,
bounds=(-eta, eta),
n=n,
)

@property
def is_Gaussian_like(self):
return True

def support_size(self, fraction=1.0):
"""
Compute the size of the support covering the probability given as fraction.
EXAMPLE::
>>> from estimator import *
>>> CenteredBinomial(3, 10).support_size()
>>> ND.CenteredBinomial(3, 10).support_size()
282475249
>>> ND.CenteredBinomial(3, 10).support_size(0.99)
279650497
Expand Down Expand Up @@ -352,7 +348,7 @@ def support_size(self, fraction=1.0):
return ceil(RR(fraction) * (b - a + 1)**len(self))


class UniformMod(Uniform):
def UniformMod(q, n=None):
"""
Uniform mod ``q``, with balanced representation.
Expand All @@ -363,10 +359,11 @@ class UniformMod(Uniform):
D(σ=2.00)
>>> ND.UniformMod(8)
D(σ=2.29, μ=0.50)
>>> ND.UniformMod(2) == ND.Uniform(0, 1)
True
"""
def __init__(self, q, n=None):
half_q = q // 2
super().__init__(half_q - q + 1, half_q, n=n)
half_q = q // 2
return Uniform(half_q - q + 1, half_q, n=n)


class SparseTernary(NoiseDistribution):
Expand All @@ -383,8 +380,7 @@ class SparseTernary(NoiseDistribution):
D(σ=0.42, μ=0.02)
"""
def __init__(self, n, p, m=None):
if m is None:
m = p
p, m = int(p), int(p if m is None else m)
self.p, self.m = p, m

# Yes, n=0 might happen when estimating the cost of the dual attack!
Expand All @@ -400,6 +396,13 @@ def __init__(self, n, p, m=None):
n=n
)

def resize(self, new_n):
"""
Return an altered distribution having a dimension `new_n`.
Assumes `p` and `m` stay the same.
"""
return SparseTernary(new_n, self.p, self.m)

@property
def hamming_weight(self):
return self.p + self.m
Expand Down
7 changes: 3 additions & 4 deletions param_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import numpy as np
from matplotlib import pyplot as plt

from estimator import LWE
from estimator import ND, LWE
from estimator.io import Logging
from estimator.nd import NoiseDistribution as ND


class ParameterSweep:
Expand Down Expand Up @@ -71,7 +70,7 @@ def parameter_sweep(
EXAMPLE ::
>>> from estimator import LWE, nd
>>> from estimator import LWE, ND
>>> from param_sweep import ParameterSweep as PS
>>> n_list = [600, 900]
>>> e_list = [7, 9]
Expand All @@ -81,7 +80,7 @@ def parameter_sweep(
e=e_list,\
s=2,\
s_log=False,\
Xs=nd.NoiseDistribution.UniformMod,\
Xs=ND.UniformMod,\
f=LWE.estimate.rough,\
tag='test',\
log_level=2,\
Expand Down

0 comments on commit f7a9fc4

Please sign in to comment.