From 6f10ac791552e39aaf8910d39ce70a8c67baa6b1 Mon Sep 17 00:00:00 2001 From: Ludo Pulles Date: Tue, 29 Oct 2024 11:51:42 +0100 Subject: [PATCH] Output sparse ternary differently. --- docs/schemes/nist-pqc-round-3.rst | 8 ++++---- estimator/nd.py | 23 ++++++++++++++++++++--- estimator/ntru_parameters.py | 9 ++++----- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/docs/schemes/nist-pqc-round-3.rst b/docs/schemes/nist-pqc-round-3.rst index eefde84..7a15397 100644 --- a/docs/schemes/nist-pqc-round-3.rst +++ b/docs/schemes/nist-pqc-round-3.rst @@ -60,7 +60,7 @@ 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 @@ -68,7 +68,7 @@ NIST PQC Round 3 Finalists >>> 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 @@ -76,7 +76,7 @@ NIST PQC Round 3 Finalists >>> 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 @@ -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 \ No newline at end of file + rop: ≈2^158.7, red: ≈2^157.7, svp: ≈2^157.7, β: 454, η: 489, d: 1306, tag: bdd diff --git a/estimator/nd.py b/estimator/nd.py index 10d74d0..f31ef6a 100644 --- a/estimator/nd.py +++ b/estimator/nd.py @@ -409,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 """ @@ -510,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): """ diff --git a/estimator/ntru_parameters.py b/estimator/ntru_parameters.py index c36f12b..d645efd 100644 --- a/estimator/ntru_parameters.py +++ b/estimator/ntru_parameters.py @@ -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 """