Skip to content

Commit

Permalink
Merge pull request #133 from ludopulles/main
Browse files Browse the repository at this point in the history
Cast DiscreteGaussian args to RR
  • Loading branch information
malb authored Oct 29, 2024
2 parents baf9382 + 6f10ac7 commit 848cc1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
8 changes: 4 additions & 4 deletions docs/schemes/nist-pqc-round-3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ NIST PQC Round 3 Finalists

>>> from estimator import *
>>> schemes.NTRUHPS2048509Enc
NTRUParameters(n=508, q=2048, Xs=D(σ=0.82), Xe=D(σ=0.71), m=508, tag='NTRUHPS2048509Enc', ntru_type='matrix')
NTRUParameters(n=508, q=2048, Xs=D(σ=0.82), Xe=T(p=127, m=127, n=508), m=508, tag='NTRUHPS2048509Enc', ntru_type='matrix')
>>> NTRU.primal_bdd(schemes.NTRUHPS2048509Enc)
rop: ≈2^131.1, red: ≈2^130.1, svp: ≈2^130.2, β: 357, η: 390, d: 916, tag: bdd

::

>>> from estimator import *
>>> schemes.NTRUHPS2048677Enc
NTRUParameters(n=676, q=2048, Xs=D(σ=0.82), Xe=D(σ=0.61), m=676, tag='NTRUHPS2048677Enc', ntru_type='matrix')
NTRUParameters(n=676, q=2048, Xs=D(σ=0.82), Xe=T(p=127, m=127, n=676), m=676, tag='NTRUHPS2048677Enc', ntru_type='matrix')
>>> NTRU.primal_bdd(schemes.NTRUHPS2048677Enc)
rop: ≈2^170.8, red: ≈2^169.6, svp: ≈2^169.9, β: 498, η: 533, d: 1179, tag: bdd

::

>>> from estimator import *
>>> schemes.NTRUHPS4096821Enc
NTRUParameters(n=820, q=4096, Xs=D(σ=0.82), Xe=D(σ=0.79), m=820, tag='NTRUHPS4096821Enc', ntru_type='matrix')
NTRUParameters(n=820, q=4096, Xs=D(σ=0.82), Xe=T(p=255, m=255, n=820), m=820, tag='NTRUHPS4096821Enc', ntru_type='matrix')
>>> NTRU.primal_bdd(schemes.NTRUHPS4096821Enc)
rop: ≈2^199.7, red: ≈2^198.7, svp: ≈2^198.6, β: 601, η: 636, d: 1485, tag: bdd

Expand All @@ -86,4 +86,4 @@ NIST PQC Round 3 Finalists
>>> schemes.NTRUHRSS701Enc
NTRUParameters(n=700, q=8192, Xs=D(σ=0.82), Xe=D(σ=0.82), m=700, tag='NTRUHRSS701', ntru_type='matrix')
>>> NTRU.primal_bdd(schemes.NTRUHRSS701Enc)
rop: ≈2^158.7, red: ≈2^157.7, svp: ≈2^157.7, β: 454, η: 489, d: 1306, tag: bdd
rop: ≈2^158.7, red: ≈2^157.7, svp: ≈2^157.7, β: 454, η: 489, d: 1306, tag: bdd
26 changes: 22 additions & 4 deletions estimator/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class DiscreteGaussian(NoiseDistribution):
gaussian_tail_prob: float = 1 - 2 * exp(-4 * pi)

def __init__(self, stddev, mean=0, n=None):
stddev, mean = RR(stddev), RR(mean)
b_val = oo if n is None else ceil(log(n, 2) * stddev)
density = max(0.0, 1 - RR(1 / sigmaf(stddev))) # NOTE: approximation that is accurate for large stddev.

Expand Down Expand Up @@ -288,7 +289,7 @@ def DiscreteGaussianAlpha(alpha, q, mean=0, n=None):
>>> ND.DiscreteGaussianAlpha(alpha, q) == ND.DiscreteGaussian(ND.stddevf(alpha * q))
True
"""
return DiscreteGaussian(RR(stddevf(alpha * q)), RR(mean), n)
return DiscreteGaussian(stddevf(alpha * q), mean, n)


class CenteredBinomial(NoiseDistribution):
Expand Down Expand Up @@ -408,11 +409,11 @@ class SparseTernary(NoiseDistribution):
>>> from estimator import *
>>> ND.SparseTernary(10, n=100)
D(σ=0.45)
T(p=10, m=10, n=100)
>>> ND.SparseTernary(10, 10, 100)
D(σ=0.45)
T(p=10, m=10, n=100)
>>> ND.SparseTernary(10, 8, 100)
D(σ=0.42, μ=0.02)
T(p=10, m=8, n=100)
>>> ND.SparseTernary(0, 0, 0).support_size()
1
"""
Expand Down Expand Up @@ -509,6 +510,23 @@ def support_size(self, fraction=1.0):
n, p, m = len(self), self.p, self.m
return ceil(binomial(n, p) * binomial(n - p, m) * RR(fraction))

def __str__(self):
"""
EXAMPLE::
>>> from estimator import *
>>> ND.SparseTernary(20, 20, n=100)
T(p=20, m=20, n=100)
"""
if self.n:
return f"T(p={self.p}, m={self.m}, n={int(self.n)})"
else:
return f"T(p={int(self.p)}, m={int(self.m)})"

def __repr__(self):
return str(self)


def SparseBinary(hw, n=None):
"""
Expand Down
9 changes: 4 additions & 5 deletions estimator/ntru_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ def updated(self, **kwds):
EXAMPLE::
>>> from estimator import *
>>> schemes.NTRUHPS2048509Enc #doctest: +ELLIPSIS
NTRUParameters(n=508, q=2048, Xs=D(σ=0.82), Xe=D(σ=0.71), m=508, tag='NTRUHPS2048509Enc', ntru_type='ma...
>>> schemes.NTRUHPS2048509Enc
NTRUParameters(n=508, q=2048, Xs=D(σ=0.82), Xe=T(p=127, m=127, n=508), m=508, ...
>>> schemes.NTRUHPS2048509Enc.possibly_overstretched
False
>>> schemes.NTRUHPS2048509Enc.updated(q=16536) #doctest: +ELLIPSIS
NTRUParameters(n=508, q=16536, Xs=D(σ=0.82), Xe=D(σ=0.71), ..., tag='NTRUHPS2048509Enc', ntru_type='matrix')
>>> schemes.NTRUHPS2048509Enc.updated(q=16536)
NTRUParameters(n=508, q=16536, Xs=D(σ=0.82), Xe=T(p=127, m=127, n=508), m=508, ...
>>> schemes.NTRUHPS2048509Enc.updated(q=16536).possibly_overstretched
True
"""
Expand Down

0 comments on commit 848cc1e

Please sign in to comment.