FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-40286: Makes simpler the relation between randbytes() and getrandbits() by serhiy-storchaka · Pull Request #19574 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 20 additions & 4 deletions Lib/test/test_random.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ def test_randbytes(self):
# Mersenne Twister randbytes() is deterministic
# and does not depend on the endian and bitness.
seed = 8675309
expected = b'f\xf9\xa836\xd0\xa4\xf4\x82\x9f\x8f\x19\xf0eo\x02'
expected = b'3\xa8\xf9f\xf4\xa4\xd06\x19\x8f\x9f\x82\x02oe\xf0'

self.gen.seed(seed)
self.assertEqual(self.gen.randbytes(16), expected)
Expand All @@ -773,19 +773,35 @@ def test_randbytes(self):
self.assertEqual(b''.join([self.gen.randbytes(4) for _ in range(4)]),
expected)

# Each randbytes(2) or randbytes(3) call consumes 4 bytes of entropy
# Each randbytes(1), randbytes(2) or randbytes(3) call consumes
# 4 bytes of entropy
self.gen.seed(seed)
expected2 = b''.join(expected[i:i + 2]
expected1 = expected[3::4]
self.assertEqual(b''.join(self.gen.randbytes(1) for _ in range(4)),
expected1)

self.gen.seed(seed)
expected2 = b''.join(expected[i + 2: i + 4]
for i in range(0, len(expected), 4))
self.assertEqual(b''.join(self.gen.randbytes(2) for _ in range(4)),
expected2)

self.gen.seed(seed)
expected3 = b''.join(expected[i:i + 3]
expected3 = b''.join(expected[i + 1: i + 4]
for i in range(0, len(expected), 4))
self.assertEqual(b''.join(self.gen.randbytes(3) for _ in range(4)),
expected3)

def test_randbytes_getrandbits(self):
# There is a simple relation between randbytes() and getrandbits()
seed = 2849427419
gen2 = random.Random()
self.gen.seed(seed)
gen2.seed(seed)
for n in range(9):
self.assertEqual(self.gen.randbytes(n),
gen2.getrandbits(n * 8).to_bytes(n, 'little'))


def gamma(z, sqrt2pi=(2.0*pi)**0.5):
# Reflection to right half of complex plane
Expand Down
18 changes: 6 additions & 12 deletions Modules/_randommodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -534,31 +534,25 @@ _random_Random_randbytes_impl(RandomObject *self, Py_ssize_t n)
return NULL;
}

if (n == 0) {
/* Don't consume any entropy */
return PyBytes_FromStringAndSize(NULL, 0);
}

PyObject *bytes = PyBytes_FromStringAndSize(NULL, n);
if (bytes == NULL) {
return NULL;
}
uint8_t *ptr = (uint8_t *)PyBytes_AS_STRING(bytes);

do {
for (; n; ptr += 4, n -= 4) {
uint32_t word = genrand_uint32(self);
#if PY_LITTLE_ENDIAN
/* Convert to big endian */
#if PY_BIG_ENDIAN
/* Convert to little endian */
word = _Py_bswap32(word);
#endif
if (n < 4) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Just a matter of taste, but personally I would move this epilog out of the loop and change the loop condition to n >= 4.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Then you would need to repeat the following 5 lines:

        uint32_t word = genrand_uint32(self);
#if PY_BIG_ENDIAN
        /* Convert to little endian */
        word = _Py_bswap32(word);
#endif

memcpy(ptr, &word, n);
/* Drop least significant bits */
memcpy(ptr, (uint8_t *)&word + (4 - n), n);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It looks like this will give different results on big-endian and little-endian platforms, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

word was already converted to little endian.

break;
}
memcpy(ptr, &word, 4);
ptr += 4;
n -= 4;
} while (n);
}

return bytes;
}
Expand Down

Back | FazBrowse Home | New Git URL