Next: ranges, Previous: random.sfc64, Up: Top [Contents]
%--------------------------------------------------% % vim: ft=mercury ts=4 sts=4 sw=4 et %--------------------------------------------------% % Copyright (C) 2021-2022, 2024 The Mercury team. % This file is distributed under the terms specified in COPYING.LIB. %--------------------------------------------------% % % File: random.system_rng.m. % Main author: Julien Fischer % % This module provides access to the system random number generator. % This is a platform specific cryptographically secure random number generator % that is seeded from the OS entropy pool. % % The intended use of this generator is to provide small amounts of % high-quality random material suitable, for example, for seeding other % random number generators. It is not intended for generating large amounts % of random material. % % On the C backends, the system RNG depends on the operating system. % For macOS, Cygwin, OpenBSD, NetBSD and versions of FreeBSD from 12 onwards, % we use the arc4random() family of functions. % For Windows, we use the rand_s() function. % For Linux, AIX, Solaris and versions of FreeBSD before 12, we read randomness % from /dev/urandom; on these system each open system RNG handle will require % an open file descriptor. % On other operating systems the system RNG is not available; on these systems % attempting to open a system RNG handle will throw an exception. % % On the C# backend, the system RNG is an instance of the % System.Security.Cryptography.RandomNumberGenerator class that uses the % default cryptographic random number generator implementation. % % On the Java backend, the system RNG is an instance of the % java.security.SecureRandom class using the default random number algorithm. % %--------------------------------------------------% %--------------------------------------------------% :- module random.system_rng. :- interface. :- import_module maybe. %--------------------------------------------------% % A handle through which the system RNG can be accessed. % % In general, it is *not* safe to share system RNG handles across % multiple threads. In circumstances where multiple threads require access % to the system RNG, each thread should open its own handle. % :- type system_rng. :- instance urandom(system_rng, io). %--------------------------------------------------% % True if this platform provides a system RNG. % :- pred have_system_rng is semidet. % open_system_rng(MaybeHandle, !IO): % % Returns a handle through which the system RNG can be accessed. % :- pred open_system_rng(maybe_error(system_rng)::out, io::di, io::uo) is det. % close_system_rng(Handle, !IO): % % Release the system RNG handle along with any OS resources associated % with it. Throws an exception if an error occurs. % :- pred close_system_rng(system_rng::in, io::di, io::uo) is det. %--------------------------------------------------% % Generate a uniformly distributed unsigned integer of 8, 16, 32 or % 64 bits respectively using the system RNG. % :- pred generate_uint8(system_rng::in, uint8::out, io::di, io::uo) is det. :- pred generate_uint16(system_rng::in, uint16::out, io::di, io::uo) is det. :- pred generate_uint32(system_rng::in, uint32::out, io::di, io::uo) is det. :- pred generate_uint64(system_rng::in, uint64::out, io::di, io::uo) is det. %--------------------------------------------------% %--------------------------------------------------%
Next: ranges, Previous: random.sfc64, Up: Top [Contents]