aboutsummaryrefslogtreecommitdiff
path: root/lib/std/rand.zig
AgeCommit message (Collapse)Author
2022-08-30std.rand: make weightedIndex proportions param a const sliceVeikka Tuominen
The function does not mutate the proportions and the signature should reflect that.
2022-08-28std.random: add weightedIndex functionJustin Whear
`weightedIndex` picks from a selection of weighted indices.
2022-08-22stage2+stage1: remove type parameter from bit builtinsVeikka Tuominen
Closes #12529 Closes #12511 Closes #6835
2022-07-01crypto: add the Xoodoo permutation, prepare for Gimli deprecation (#11866)Frank Denis
Gimli was a game changer. A permutation that is large enough to be used in sponge-like constructions, yet small enough to be compact to implement and fast on a wide range of platforms. And Gimli being part of the Zig standard library was awesome. But since then, Gimli entered the NIST Lightweight Cryptography Competition, competing againt other candidates sharing a similar set of properties. Unfortunately, Gimli didn't pass the 3rd round. There are no practical attacks against Gimli when used correctly, but NIST's decision means that Gimli is unlikely to ever get any traction. So, maybe the time has come to move Gimli from the standard library to another repository. We shouldn't do it without providing an alternative, though. And the best candidate for this is probably Xoodoo. Xoodoo is the core function of Xoodyak, one of the finalists of the NIST LWC competition, and the most direct competitor to Gimli. It is also a 384-bit permutation, so it can easily be used everywhere Gimli was used with no parameter changes. It is the building block of Xoodyak (for actual encryption and hashing) as well as Charm, that some Zig applications are already using. Like Gimli that it was heavily inspired from, it is compact and suitable for constrained environments. This change adds the Xoodoo permutation to std.crypto.core. The set of public functions includes everything required to later implement existing Xoodoo-based constructions. In order to prepare for the Gimli deprecation, the default CSPRNG was changed to a Xoodoo-based that works exactly the same way.
2022-05-24Add std.rand.RomuTrioFrancesco Alemanno
Co-authored-by: ominitay <37453713+ominitay@users.noreply.github.com>
2022-05-12std.rand.float: simplify leading zero calculationsErik Arvstedt
This saves a `bitwise or` operation in the common case and removes the (slightly magic) mask constants.
2022-05-12std.rand: fixup 'improve random float generation'Erik Arvstedt
- Test: Fix bucket counting. Previously, the first hit was not counted. This off-by-one error slightly increased the mean of `*_total_variance`, which decreased the acceptance rate for a particular random seed from 95% to 92.6%. (Irrelevant for test failure because the seed is fixed.) - Improve comments
2022-05-10std.rand: move tests to a separate test fileAndrew Kelley
2022-05-10std: improve random float generationMahdi Rakhshandehroo
2022-04-14stage2: progress towards stage3Andrew Kelley
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly doing element casting for slice element types. This required an enhancement both to stage1 and stage2. * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that abused this is fixed. * Make some parameters comptime to support functions in switch expressions (as opposed to making them function pointers). * Avoid relying on local temporaries being mutable. * Workarounds for when stage1 and stage2 disagree on function pointer types. * Workaround recursive formatting bug with a `@panic("TODO")`. * Remove unreachable `else` prongs for some inferred error sets. All in effort towards #89.
2022-03-11Sema: improve detection of generic parametersVeikka Tuominen
2021-12-19stage1, stage2: rename c_void to anyopaque (#10316)Isaac Freund
zig fmt now replaces c_void with anyopaque to make updating code easy.
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-10-29Add argument for `fillFn` to `Random.init`ominitay
As suggested by @leecannon, this provides more flexibility to the `Random` interface. For exmaple, this allows for an implementation to provide multiple different fill functions.
2021-10-27std.rand: Refactor `Random` interfaceOminitay
These changes have been made to resolve issue #10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
2021-10-25Fix documentation for Random.intominitay
Documentation incorrectly stated that Random.int 'Returns a random int `i` such that `0 <= i <= maxInt(T)`.' This commit amends this.
2021-10-10rand: remove workaround for issue #1770Sizhe Zhao
2021-10-04migrate from `std.Target.current` to `@import("builtin").target`Andrew Kelley
closes #9388 closes #9321
2021-08-24remove redundant license headers from zig standard libraryAndrew Kelley
We already have a LICENSE file that covers the Zig Standard Library. We no longer need to remind everyone that the license is MIT in every single file. Previously this was introduced to clarify the situation for a fork of Zig that made Zig's LICENSE file harder to find, and replaced it with their own license that required annual payments to their company. However that fork now appears to be dead. So there is no need to reinforce the copyright notice in every single file.
2021-08-19std.rand.Random: add enumValue() (#9583)Justin Whear
* add Random.enumValue() * edits suggested by review * applied zig fmt * Rewrite to use std.enums.values Implemented pfgithub's suggestion to rewrite against this function, greatly simplifying the implementation. Co-authored-by: Justin Whear <justin@economicmodeling.com>
2021-07-08Switch rand.DefaultPrng to Xoshiro256++ (#9301)Frank Denis
Xoroshiro128+ is the current default non-cryptographic random number generator. This algorithm was designed to generate floating-point numbers, by only using the top 53 bits. Lower bits have a significant bias, that contradicts the documented properties for `rand.DefaultPrng`. This also has implications on everything using `Random.fill()`, including the way we generate random floating-point numbers. In addition, Xoroshiro128+ has known issues. See for example: - https://lemire.me/blog/2017/08/22/cracking-random-number-generators-xoroshiro128/ - https://www.pcg-random.org/posts/xoroshiro-fails-truncated.html Xoshiro256++ addresses these issues, while remaining very fast.
2021-07-04implement xoshiro256++ (#9298)leesongun
Implement xoshiro256++
2021-05-08Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgenAndrew Kelley
Conflicts: * doc/langref.html.in * lib/std/enums.zig * lib/std/fmt.zig * lib/std/hash/auto_hash.zig * lib/std/math.zig * lib/std/mem.zig * lib/std/meta.zig * test/behavior/alignof.zig * test/behavior/bitcast.zig * test/behavior/bugs/1421.zig * test/behavior/cast.zig * test/behavior/ptrcast.zig * test/behavior/type_info.zig * test/behavior/vector.zig Master branch added `try` to a bunch of testing function calls, and some lines also had changed how to refer to the native architecture and other `@import("builtin")` stuff.
2021-05-08std: update usage of std.testingVeikka Tuominen
2021-04-15std: change `@import("builtin")` to `std.builtin`Andrew Kelley
2021-01-22std: Update `test ""` to `test` where it makes senseLemonBoy
2020-12-31Year++Frank Denis
2020-12-18std: introduce a thread-local CSPRNG for general useAndrew Kelley
std.crypto.random * cross platform, even freestanding * can't fail. on initialization for some systems requires calling os.getrandom(), in which case there are rare but theoretically possible errors. The code panics in these cases, however the application may choose to override the default seed function and then handle the failure another way. * thread-safe * supports the full Random interface * cryptographically secure * no syscall required to initialize on Linux (AT_RANDOM) * calls arc4random on systems that support it `std.crypto.randomBytes` is removed in favor of `std.crypto.random.bytes`. I moved some of the Random implementations into their own files in the interest of organization. stage2 no longer requires passing a RNG; instead it uses this API. Closes #6704
2020-11-19Add builtin.Signedness, use it instead of is_signedTadeo Kondrak
2020-11-06change debug.assert to testing.expect in testsxackus
2020-10-17Make std.meta.Int accept a signedness parameterJan Prudil
2020-10-15std.rand: set DefaultCsprng to Gimli, and require a larger seedFrank Denis
`DefaultCsprng` is documented as a cryptographically secure RNG. While `ISAAC` is a CSPRNG, the variant we have, `ISAAC64` is not. A 64 bit seed is a bit small to satisfy that claim. We also saw it being used with the current date as a seed, that also defeats the point of a CSPRNG. Set `DefaultCsprng` to `Gimli` instead of `ISAAC64`, rename the parameter from `init_s` to `secret_seed` + add a comment to clarify what kind of seed is expected here. Instead of directly touching the internals of the Gimli implementation (which can change/be architecture-specific), add an `init()` function to the state. Our Gimli-based CSPRNG was also not backtracking resistant. Gimli is a permutation; it can be reverted. So, if the state was ever leaked, future secrets, but also all the previously generated ones could be recovered. Clear the rate after a squeeze in order to prevent this. Finally, a dumb test was added just to exercise `DefaultCsprng` since we don't use it anywhere.
2020-10-15std: move std.meta.refAllDecls to std.testingTadeo Kondrak
2020-09-03update uses of deprecated type field accessVexu
2020-08-25std.rand: promote normal comments to doc commentsAndrew Kelley
2020-08-20Breaking: sort std/crypto functions into categoriesFrank Denis
Instead of having all primitives and constructions share the same namespace, they are now organized by category and function family. Types within the same category are expected to share the exact same API.
2020-08-20add license header to all std lib filesAndrew Kelley
add SPDX license identifier copyright ownership is zig contributors
2020-05-20make rand function public, fixes crypto benchmarkAndrew Kelley
2020-04-28std.meta.IntType -> std.meta.IntTadeo Kondrak
2020-04-18rand: ref the decls so they get testedAndrew Kelley
2020-03-30std lib API deprecations for the upcoming 0.6.0 releaseAndrew Kelley
See #3811
2020-03-19update std lib to take advantage of slicing with comptime indexesAndrew Kelley
2020-02-24remove uses of `@ArgType` and `@IntType`Vexu
2020-02-06std: add Gimli based PRNG to std.randdaurnimator
2019-11-27remove type coercion from array values to referencesAndrew Kelley
* Implements #3768. This is a sweeping breaking change that requires many (trivial) edits to Zig source code. Array values no longer coerced to slices; however one may use `&` to obtain a reference to an array value, which may then be coerced to a slice. * Adds `IrInstruction::dump`, for debugging purposes. It's useful to call to inspect the instruction when debugging Zig IR. * Fixes bugs with result location semantics. See the new behavior test cases, and compile error test cases. * Fixes bugs with `@typeInfo` not properly resolving const values. * Behavior tests are passing but std lib tests are not yet. There is more work to do before merging this branch.
2019-11-08update more of the std lib to use `@as`Andrew Kelley
2019-11-08update the codebase to use `@as`Andrew Kelley
2019-09-25mv std/ lib/Andrew Kelley
that's all this commit does. further commits will fix cli flags and such. see #2221