3 ### Testing random-generator functionality
5 ### (c) 2019 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to Catacomb.
12 ### Catacomb/Python is free software: you can redistribute it and/or
13 ### modify it under the terms of the GNU General Public License as
14 ### published by the Free Software Foundation; either version 2 of the
15 ### License, or (at your option) any later version.
17 ### Catacomb/Python is distributed in the hope that it will be useful, but
18 ### WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ### General Public License for more details.
22 ### You should have received a copy of the GNU General Public License
23 ### along with Catacomb/Python. If not, write to the Free Software
24 ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 ###--------------------------------------------------------------------------
34 ###--------------------------------------------------------------------------
35 class TestRandomGenerator (U.TestCase):
37 def check_rand(me, rng):
41 me.assertEqual(type(x), int)
42 me.assertTrue(0 <= x < 256)
45 me.assertTrue(0 <= x <= 0xffffffff)
49 me.assertEqual(type(x), int)
50 me.assertTrue(0 <= x < 10)
51 x = rng.range(T.MAXFIXNUM + 1)
52 me.assertEqual(type(x), C.MP)
56 me.assertEqual(type(x), C.MP)
57 me.assertEqual(x.nbits, 123)
58 me.assertEqual(x&7, 7)
59 me.assertRaises((OverflowError, ValueError), rng.mp, 128, -1)
60 me.assertRaises((OverflowError, ValueError), rng.mp, 128, C.MPW_MAX + 1)
63 me.assertEqual(type(x), C.ByteString)
64 me.assertEqual(len(x), 17)
68 me.assertFalse(rng.cryptop)
71 me.assertEqual(rng.word(), w0)
75 me.assertFalse(rng.cryptop)
78 me.assertEqual(rng.word(), w0)
80 def test_truerand(me):
82 me.assertTrue(rng.cryptop)
86 me.assertRaises(ValueError, rng.seed, C.RAND_IBITS + 1)
88 rng.seedword(0x12345678)
89 rng.seedrand(T.detrand("seed-truerand"))
90 rng.seedblock(T.span(123))
91 rng.add(T.span(123), 978)
97 def test_cryptorand(me):
98 for r, kw in [("rijndael-counter", {}),
100 ("xchacha20", { "nonce": T.span(24) }),
101 ("seal", { "i": 12345678 }),
102 ("shake128", { "func": T.bin("TEST"),
103 "perso": T.bin("Catacomb/Python test") }),
104 ("kmac256", { "perso": T.bin("Catacomb/Python test") })]:
106 rng = rcls(T.span(rcls.keysz.default), **kw)
107 me.assertTrue(rng.cryptop)
109 def test_sslrand(me):
110 rng = C.SSLRand(T.span(16), T.span(32), C.md5, C.sha)
113 rng = C.TLSDataExpansion(T.span(16), T.span(32), C.sha256_hmac)
116 rng = C.TLSPRF(T.span(16), T.span(32), C.md5_hmac, C.sha_hmac)
119 def test_dsarand(me):
122 rng = C.DSARand(seed)
124 me.assertEqual(rng.seed, (n + 153 + 3).storeb(16))
127 ev = T.EventRecorder()
128 drng = T.detrand("bbs")
129 rngpriv = C.BBSPriv.generate(1536, event = ev, rng = drng)
130 me.assertEqual(rngpriv.n.nbits, 1536)
131 me.assertEqual(rngpriv.p&3, 3)
132 me.assertEqual(rngpriv.q&3, 3)
133 me.assertTrue(rngpriv.p.primep())
134 me.assertTrue(rngpriv.q.primep())
135 me.assertEqual(rngpriv.n, rngpriv.p*rngpriv.q)
136 me.assertEqual(ev.events,
146 x0 = drng.range(rngpriv.n)
148 rng = C.BlumBlumShub(rngpriv.n, x0)
149 me.check_rand(rngpriv)
151 me.assertEqual(rngpriv.x, rng.x)
159 nsteps = (123*8 + 7)//(rng.stepsz)
160 rngpriv.rew(nsteps + 1)
161 me.assertEqual(rngpriv.mask(ct), msg)
163 ###----- That's all, folks --------------------------------------------------
165 if __name__ == "__main__": U.main()