Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
make crypto fuzz ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Kliem committed Jul 21, 2020
1 parent 1d99129 commit 0b128d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/sage/crypto/boolean_function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1433,9 +1433,8 @@ def random_boolean_function(n):
sage: B = random_boolean_function(9)
sage: B.nvariables()
9
sage: B.nonlinearity()
217 # 32-bit
222 # 64-bit
sage: B.nonlinearity() # random
222
"""
from sage.misc.randstate import current_randstate
r = current_randstate().python_random()
Expand Down
47 changes: 32 additions & 15 deletions src/sage/crypto/lwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
chosen as in [Reg09]_::
sage: from sage.crypto.lwe import samples
sage: samples(30, 20, 'Regev')
sage: samples(30, 20, 'Regev') # random
[((360, 264, 123, 368, 398, 392, 41, 84, 25, 389, 311, 68, 322, 41, 161, 372, 222, 153, 243, 381), 122),
...
((155, 22, 357, 312, 87, 298, 182, 163, 296, 181, 219, 135, 164, 308, 248, 320, 64, 166, 214, 104), 152)]
Expand All @@ -30,7 +30,7 @@
implementing their own oracles::
sage: from sage.crypto.lwe import samples, LindnerPeikert
sage: samples(30, 20, LindnerPeikert)
sage: samples(30, 20, LindnerPeikert) # random
[((1275, 168, 1529, 2024, 1874, 1309, 16, 1869, 1114, 1696, 1645, 618, 1372, 1273, 683, 237, 1526, 879, 1305, 1355), 950),
...
((1787, 2033, 1677, 331, 1562, 49, 796, 1002, 627, 98, 91, 711, 1712, 418, 2024, 163, 1773, 184, 1548, 3), 1815)]
Expand All @@ -39,7 +39,7 @@
sage: from sage.crypto.lwe import LindnerPeikert
sage: lwe = LindnerPeikert(20)
sage: samples(30, 20, lwe)
sage: samples(30, 20, lwe) # random
[((465, 180, 440, 706, 1367, 106, 1380, 614, 1162, 1354, 1098, 2036, 1974, 1417, 1502, 1431, 863, 1894, 1368, 1771), 618),
...
((1050, 1017, 1314, 1310, 1941, 2041, 484, 104, 1199, 1744, 161, 1905, 679, 1663, 531, 1630, 168, 1559, 1040, 1719), 1006)]
Expand All @@ -50,7 +50,7 @@
sage: from sage.stats.distributions.discrete_gaussian_polynomial import DiscreteGaussianDistributionPolynomialSampler
sage: D = DiscreteGaussianDistributionPolynomialSampler(ZZ['x'], euler_phi(16), 5)
sage: ringlwe = RingLWE(16, 257, D, secret_dist='uniform')
sage: samples(30, euler_phi(16), ringlwe)
sage: samples(30, euler_phi(16), ringlwe) # random
[((232, 79, 223, 85, 26, 68, 60, 72), (72, 158, 117, 166, 140, 103, 142, 223)),
...
((27, 191, 241, 179, 246, 204, 36, 72), (207, 158, 127, 240, 225, 141, 156, 201))]
Expand All @@ -63,7 +63,7 @@
scalars over/in the integers are returned::
sage: from sage.crypto.lwe import samples
sage: samples(30, 20, 'Regev', balanced=True)
sage: samples(30, 20, 'Regev', balanced=True) # random
[((-46, -84, 21, -72, -47, -162, -40, -31, -9, -131, 74, 183, 62, -83, -135, 164, -33, -109, -127, -124), 96),
...
((-48, 185, 118, 69, 57, 109, 109, 138, -42, -45, -16, 180, 34, 178, 20, -119, -58, -136, -46, 169), -72)]
Expand Down Expand Up @@ -114,7 +114,7 @@ class UniformSampler(SageObject):
sage: from sage.crypto.lwe import UniformSampler
sage: sampler = UniformSampler(-2, 2); sampler
UniformSampler(-2, 2)
sage: sampler()
sage: sampler() # random
-2
.. automethod:: __init__
Expand Down Expand Up @@ -149,8 +149,10 @@ def __call__(self):
sage: from sage.crypto.lwe import UniformSampler
sage: sampler = UniformSampler(-12, 12)
sage: sampler()
sage: s = sampler(); s # random
-10
sage: s in range(-12, 13)
True
"""
return randint(self.lower_bound, self.upper_bound)

Expand All @@ -172,8 +174,17 @@ class UniformPolynomialSampler(SageObject):
EXAMPLES::
sage: from sage.crypto.lwe import UniformPolynomialSampler
sage: UniformPolynomialSampler(ZZ['x'], 8, -2, 2)()
sage: p = UniformPolynomialSampler(ZZ['x'], 8, -2, 2)(); p # random
-2*x^7 + x^6 - 2*x^5 - x^3 - 2*x^2 - 2
sage: p.degree() < 8
True
sage: all(k in range(-2, 3) for k in p.coefficients())
True
sage: p = UniformPolynomialSampler(ZZ['x'], 8, 2, 5)(); p # random
2*x^7 + 2*x^6 + 2*x^5 + 4*x^4 + 2*x^3 + 4*x^2 + 2*x + 4
sage: p.degree()
7
.. automethod:: __init__
.. automethod:: __call__
Expand Down Expand Up @@ -213,8 +224,12 @@ def __call__(self):
sage: from sage.crypto.lwe import UniformPolynomialSampler
sage: sampler = UniformPolynomialSampler(ZZ['x'], 8, -12, 12)
sage: sampler()
sage: p = sampler(); p # random
-10*x^7 + 5*x^6 - 8*x^5 + x^4 - 4*x^3 - 11*x^2 - 10
sage: p.degree() < 8
True
sage: all(k in range(-12, 13) for k in p.coefficients())
True
"""
coeff = [self.D() for _ in range(self.n)]
f = self.P(coeff)
Expand Down Expand Up @@ -285,8 +300,8 @@ def __init__(self, n, q, D, secret_dist='uniform', m=None):
fix the representation and recover the correct standard deviation of the
noise::
sage: sqrt(variance([e if e <= 200 else e-401 for e in S]).n())
3.0...
sage: sqrt(variance([e if e <= 200 else e-401 for e in S]).n()) # abs tol 2e-1
3
If ``m`` is not ``None`` the number of available samples is restricted::
Expand Down Expand Up @@ -341,7 +356,7 @@ def __call__(self):
EXAMPLES::
sage: from sage.crypto.lwe import DiscreteGaussianDistributionIntegerSampler, LWE
sage: LWE(10, 401, DiscreteGaussianDistributionIntegerSampler(3))()
sage: LWE(10, 401, DiscreteGaussianDistributionIntegerSampler(3))() # random
((309, 347, 198, 194, 336, 360, 264, 123, 368, 398), 198)
"""
if self.m is not None:
Expand Down Expand Up @@ -579,7 +594,7 @@ def __call__(self):
sage: n = euler_phi(N)
sage: D = DiscreteGaussianDistributionPolynomialSampler(ZZ['x'], n, 5)
sage: ringlwe = RingLWE(N, 257, D, secret_dist='uniform')
sage: ringlwe()
sage: ringlwe() # random
((226, 198, 38, 222, 222, 127, 194, 124), (11, 191, 177, 59, 105, 203, 108, 42))
"""
if self.m is not None:
Expand Down Expand Up @@ -648,6 +663,7 @@ def __init__(self, ringlwe):
EXAMPLES::
sage: set_random_seed(0)
sage: from sage.crypto.lwe import DiscreteGaussianDistributionPolynomialSampler, RingLWE, RingLWEConverter
sage: D = DiscreteGaussianDistributionPolynomialSampler(ZZ['x'], euler_phi(16), 5)
sage: lwe = RingLWEConverter(RingLWE(16, 257, D, secret_dist='uniform'))
Expand All @@ -664,6 +680,7 @@ def __call__(self):
"""
EXAMPLES::
sage: set_random_seed(0)
sage: from sage.crypto.lwe import DiscreteGaussianDistributionPolynomialSampler, RingLWE, RingLWEConverter
sage: D = DiscreteGaussianDistributionPolynomialSampler(ZZ['x'], euler_phi(16), 5)
sage: lwe = RingLWEConverter(RingLWE(16, 257, D, secret_dist='uniform'))
Expand Down Expand Up @@ -769,7 +786,7 @@ def balance_sample(s, q=None):
EXAMPLES::
sage: from sage.crypto.lwe import balance_sample, samples, Regev
sage: [balance_sample(s) for s in samples(10, 5, Regev)]
sage: [balance_sample(s) for s in samples(10, 5, Regev)] # random
[((-9, -4, -4, 4, -4), 4), ((-8, 11, 12, -11, -11), -7),
...
((-11, 12, 0, -6, -3), 7), ((-7, 14, 8, 11, -8), -12)]
Expand All @@ -778,7 +795,7 @@ def balance_sample(s, q=None):
sage: from sage.crypto.lwe import balance_sample, DiscreteGaussianDistributionPolynomialSampler, RingLWE, samples
sage: D = DiscreteGaussianDistributionPolynomialSampler(ZZ['x'], 8, 5)
sage: rlwe = RingLWE(20, 257, D)
sage: [balance_sample(s) for s in samples(10, 8, rlwe)]
sage: [balance_sample(s) for s in samples(10, 8, rlwe)] # random
[((-64, 107, -91, -24, 120, 54, 38, -35), (-84, 121, 28, -99, 91, 54, -60, 11)),
...
((-40, -117, 35, -69, -11, 10, 122, 48), (-80, -2, 119, -91, 27, 66, 121, -1))]
Expand Down
19 changes: 12 additions & 7 deletions src/sage/crypto/mq/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,9 +1071,11 @@ def random_state_array(self, *args, **kwds):
EXAMPLES::
sage: sr = mq.SR(2, 2, 2, 4)
sage: sr.random_state_array()
sage: M = sr.random_state_array(); M # random
[ a^2 a^3 + a + 1]
[a^3 + a^2 + a + 1 a + 1]
sage: M in MatrixSpace(sr.base_ring(), sr.r, sr.c)
True
"""
return random_matrix(self.base_ring(), self._r, self._c, *args, **kwds)

Expand All @@ -1085,7 +1087,7 @@ def random_vector(self, *args, **kwds):
EXAMPLES::
sage: sr = mq.SR(2, 2, 2, 4)
sage: sr.random_vector()
sage: v = sr.random_vector(); v # random
[ a^2]
[ a + 1]
[ a^2 + 1]
Expand All @@ -1102,6 +1104,8 @@ def random_vector(self, *args, **kwds):
[ a^2 + 1]
[ a]
[ a^2]
sage: v in MatrixSpace(sr.base_ring(), sr.r*sr.c*sr.e, 1)
True
.. note::
Expand All @@ -1124,12 +1128,12 @@ def random_element(self, elem_type = "vector", *args, **kwds):
EXAMPLES::
sage: sr = mq.SR()
sage: sr.random_element()
sage: sr.random_element() # random
[ a^2]
[ a + 1]
[a^2 + 1]
[ a]
sage: sr.random_element('state_array')
sage: sr.random_element('state_array') # random
[a^3 + a + 1]
Passes extra positional or keyword arguments through::
Expand Down Expand Up @@ -1861,6 +1865,7 @@ def round_polynomials(self, i, plaintext=None, ciphertext=None):
EXAMPLES::
sage: set_random_seed(0)
sage: sr = mq.SR(1, 1, 1, 4)
sage: k = sr.base_ring()
sage: p = [k.random_element() for _ in range(sr.r*sr.c)]
Expand Down Expand Up @@ -2077,7 +2082,7 @@ def polynomial_system(self, P=None, K=None, C=None):
(C000, C001, C002, C003)
sage: P = sr.vars("P",0)
sage: F,s = sr.polynomial_system(P=P,C=C)
sage: [(k,v) for k,v in sorted(s.items())] # this can be ignored
sage: [(k,v) for k,v in sorted(s.items())] # random
[(k003, 1), (k002, 1), (k001, 0), (k000, 1)]
sage: F
Polynomial Sequence with 36 Polynomials in 28 Variables
Expand Down Expand Up @@ -2261,7 +2266,7 @@ def antiphi(self, l):
sage: sr = mq.SR()
sage: A = sr.random_state_array()
sage: A
sage: A # random
[a^2]
sage: sr.antiphi(sr.phi(A)) == A
True
Expand Down Expand Up @@ -2666,7 +2671,7 @@ def antiphi(self, l):
sage: sr = mq.SR(gf2=True)
sage: A = sr.random_state_array()
sage: A
sage: A # random
[a^2]
sage: sr.antiphi(sr.phi(A)) == A
True
Expand Down

0 comments on commit 0b128d8

Please sign in to comment.