Skip to content

Fixes UUIDv7 lack of randomness - #10865

Merged
thewilsonator merged 4 commits into
dlang:masterfrom
denizzzka:uuid_random_fail_demo
Sep 21, 2025
Merged

thewilsonator merged 4 commits into
dlang:masterfrom
denizzzka:uuid_random_fail_demo

Conversation

@denizzzka

@denizzzka denizzzka commented Sep 14, 2025 •

Copy link
Copy Markdown
Contributor

First commit: this test fails on my machine
Second commit fixes issue

I decided to use getEntropy directly instead as seed here because UUID requires a just a little more than 8 bytes of ulong needed for generate quality random sequence. So there is no point in calling Random for this.

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @denizzzka! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10865"

@denizzzka denizzzka changed the title UUIDv7 randomness check failure Fixes UUIDv7 randomness check failure Sep 15, 2025
@denizzzka
denizzzka marked this pull request as ready for review September 15, 2025 04:41
@denizzzka
denizzzka force-pushed the uuid_random_fail_demo branch 2 times, most recently from 3e23208 to 3ab5013 Compare September 15, 2025 04:45
@denizzzka denizzzka changed the title Fixes UUIDv7 randomness check failure Fixes UUIDv7 lack of randomness Sep 15, 2025
@denizzzka
denizzzka force-pushed the uuid_random_fail_demo branch from 3ab5013 to 3c2f87e Compare September 15, 2025 04:53
Comment thread std/uuid.d Resolved
@denizzzka
denizzzka force-pushed the uuid_random_fail_demo branch from 3c2f87e to 32956dd Compare September 16, 2025 20:46
@denizzzka

denizzzka commented Sep 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Changed to a less radical changes: Spending TRNG data to UUIDs seems too expensive

Looks like after merging unittest can be removed? Seems is very unlikely that this kind of error will happen again and will be caught by this test

@schveiguy

Copy link
Copy Markdown
Member

At first I was thinking, hm... what is this actually changing? Then I realized, it was only using a ubyte for seeding! So one of 256 seeds!

I don't think the unittest is acceptable. Yes, it's going to pass most of the time, but it's going to introduce a possibility of failures that a) have nothing to do with what is being tested and b) will not be reproducible. We do not want these kinds of tests in CI.

This might be something we don't need a unittest for. The cost of adding it is not worth it. And I doubt someone's going to come along and switch it back to a ubyte in this case.

@Herringway

Copy link
Copy Markdown
Contributor

A good test could do some statistical analysis and check for odd patterns in the results, but that might be a bit expensive.

@denizzzka

Copy link
Copy Markdown
Contributor Author

At first I was thinking, hm... what is this actually changing? Then I realized, it was only using a ubyte for seeding! So one of 256 seeds!

std.random is just bad. generateV7RandomData() function should never have existed.

@LightBender LightBender left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add a comment to the documentation for UUIDv7 noting that it does NOT use a CSPRNG in all cases?

After that I'll approve this.

@denizzzka

denizzzka commented Sep 17, 2025 •

Copy link
Copy Markdown
Contributor Author

Could you add a comment to the documentation for UUIDv7 noting that it does NOT use a CSPRNG in all cases?

Actually it uses CSPRNG now. And so it should be.

@LightBender

Copy link
Copy Markdown
Contributor

@denizzzka, unpredictableSeed has a note that it doesn't always use a CSPRNG, which means we need to either link to that note or note it directly.

@denizzzka

Copy link
Copy Markdown
Contributor Author

I don't know how to write this: I'm not a native speaker. Write something..

@LightBender

Copy link
Copy Markdown
Contributor

@denizzzka Something like:

This implementation is not guaranteed to use a cryptographically secure PRNG. For more information please see:

@denizzzka
denizzzka force-pushed the uuid_random_fail_demo branch from 4b997ef to e99cfd1 Compare September 21, 2025 11:05
@thewilsonator
thewilsonator merged commit aeaa5ed into dlang:master Sep 21, 2025
@denizzzka
denizzzka deleted the uuid_random_fail_demo branch September 21, 2025 12:21
@trikko

trikko commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Can't we use random data from /dev/urandom on linux?

@denizzzka

Copy link
Copy Markdown
Contributor Author

Can't we use random data from /dev/urandom on linux?

Strictly, /dev/urandom is poor choise because it is (surprisingly) can be unavailable on some cases (on booting, for example)

@trikko

trikko commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Actually, /dev/urandom never becomes "unavailable": its core design principle is to never block.

The issue during the very early boot phase (which is a highly specific edge case that most applications never hit) is about the quality of the entropy, not its availability. If you read from it before the kernel's entropy pool is fully initialized, it will still return data immediately, but that data will be cryptographically weak.

However, if our current implementation is already generating cryptographically weak data anyway, using /dev/urandom at early boot wouldn't put us in a worse position. The crucial difference is that for the other 99.9% of normal use cases, we would actually be getting cryptographically secure data.

@denizzzka

denizzzka commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Actually, /dev/urandom never becomes "unavailable": its core design principle is to never block.

Ok, another case: whole /dev/* pathnames will become invisible if chroot is used

@Herringway

Copy link
Copy Markdown
Contributor

What benefit does /dev/urandom provide here? It doesn't make the code any simpler, because other implementations will still be needed for other platforms.

@trikko

trikko commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

@Herringway /dev/urandom is cryptographically secure, that was my point. (also getrandom() syscall)

@Herringway

Copy link
Copy Markdown
Contributor

We'd still need cryptographically secure RNG implementations for other platforms for feature parity. Then there will be multiple implementations. If any of those other implementations can also function on linux, why use /dev/urandom?

@trikko

trikko commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

There's no other secure implementations for this. And I don't think "feature parity" is a thing.
It was a suggestion. Anyway I'm using my own UUIDv7 implementation, not really a problem.

Just in case: https://gist.github.com/trikko/31d12ecafc76c7eba02edd81e5d1e8e8

@0xEAB

0xEAB commented Apr 23, 2026

Copy link
Copy Markdown
Member

It doesn't make the code any simpler, because other implementations will still be needed for other platforms.

Use std.internal.entropy.

@0xEAB

0xEAB commented Apr 23, 2026

Copy link
Copy Markdown
Member

whole /dev/* pathnames will become invisible if chroot is used

That’s why std.internal.entropy defaults to syscalling getrandom on Linux.

@denizzzka

Copy link
Copy Markdown
Contributor Author

I don’t remember whether I wrote this link here or not

std.random suggestions

@0xEAB

0xEAB commented Apr 23, 2026

Copy link
Copy Markdown
Member

CS PRN generation in Phobos v2 is an unfortunate thing.
v3 will do better. Its project leader is well-known for his interest in cryptography.

You might remember my reply to the thread you’ve linked: https://forum.dlang.org/post/[email protected]

Phobos v2 has a backwards-compatibility policy. Given that we are shoehorning crypto security into APIs that weren’t designed with that in mind, one — unfortunately — has to lower one’s sights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants