Skip to content

Commit 047c4f7

Browse files
authored
Merge pull request #98 from haskellari/sys-random-always
Always sys/random.h, I don't like Apple nor Google equally now
2 parents 3584784 + cdddad7 commit 047c4f7

File tree

6 files changed

+58
-61
lines changed

6 files changed

+58
-61
lines changed

.github/workflows/haskell-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#
99
# For more information, see https://github.com/haskell-CI/haskell-ci
1010
#
11-
# version: 0.19.20250506
11+
# version: 0.19.20250605
1212
#
13-
# REGENDATA ("0.19.20250506",["github","cabal.project"])
13+
# REGENDATA ("0.19.20250605",["github","cabal.project"])
1414
#
1515
name: Haskell-CI
1616
on:
@@ -175,8 +175,8 @@ jobs:
175175
- name: install cabal-docspec
176176
run: |
177177
mkdir -p $HOME/.cabal/bin
178-
curl -sL https://github.com/phadej/cabal-extras/releases/download/cabal-docspec-0.0.0.20240703/cabal-docspec-0.0.0.20240703-x86_64-linux.xz > cabal-docspec.xz
179-
echo '48bf3b7fd2f7f0caa6162afee57a755be8523e7f467b694900eb420f5f9a7b76 cabal-docspec.xz' | sha256sum -c -
178+
curl -sL https://github.com/phadej/cabal-extras/releases/download/cabal-docspec-0.0.0.20250606/cabal-docspec-0.0.0.20250606-x86_64-linux.xz > cabal-docspec.xz
179+
echo 'cc20bb5c19501b42bde77556bc419c7c0a5c8d1eb65663024d8a4e4c868bef25 cabal-docspec.xz' | sha256sum -c -
180180
xz -d < cabal-docspec.xz > $HOME/.cabal/bin/cabal-docspec
181181
rm -f cabal-docspec.xz
182182
chmod a+x $HOME/.cabal/bin/cabal-docspec

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.1.3
2+
3+
- Use system specific entropy/randomess sources to initialise the default generator.
4+
Specifically `SecRandomCopyBytes` on Apple platforms and
5+
`RtlGenRandom` on Windows.
6+
17
# 0.1.2
28

39
- Use `getentropy` for initialisation on unix-like systems (i.e. not Windows).

cbits-apple/init.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdint.h>
2+
#include <Security/SecRandom.h>
3+
4+
uint64_t splitmix_init() {
5+
uint64_t result;
6+
int r = SecRandomCopyBytes(kSecRandomDefault, sizeof(uint64_t), &result);
7+
return r == errSecSuccess ? result : 0xfeed1000;
8+
}

cbits-unix/init.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include <stdint.h>
22
#include <unistd.h>
3-
4-
/* for macos */
5-
#ifdef __APPLE__
63
#include <sys/random.h>
7-
#endif
84

95
uint64_t splitmix_init() {
106
uint64_t result;

cbits-win/init.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
#include <stdint.h>
2-
32
#include <windows.h>
3+
#include <ntsecapi.h>
44

55
uint64_t splitmix_init() {
6-
/* Handy list at https://stackoverflow.com/a/3487338/1308058 */
7-
8-
uint64_t a = GetCurrentProcessId(); /* DWORD */
9-
uint64_t b = GetCurrentThreadId(); /* DWORD */
10-
uint64_t c = GetTickCount(); /* DWORD */
11-
12-
SYSTEMTIME t = {0,0,0,0,0,0,0,0};
13-
GetSystemTime(&t);
14-
15-
LARGE_INTEGER i;
16-
QueryPerformanceCounter(&i);
17-
18-
return a ^ (b << 32) ^ (c << 16)
19-
^ ((uint64_t) t.wYear << 56)
20-
^ ((uint64_t) t.wMonth << 48)
21-
^ ((uint64_t) t.wDayOfWeek << 40)
22-
^ ((uint64_t) t.wDay << 32)
23-
^ ((uint64_t) t.wHour << 24)
24-
^ ((uint64_t) t.wMinute << 16)
25-
^ ((uint64_t) t.wSecond << 8)
26-
^ ((uint64_t) t.wMilliseconds << 0)
27-
^ ((uint64_t) i.QuadPart);
6+
uint64_t result;
7+
int r = RtlGenRandom(&result, sizeof(uint64_t));
8+
return r ? result : 0xfeed1000;
289
}

splitmix.cabal

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
cabal-version: >=1.10
1+
cabal-version: 2.4
22
name: splitmix
3-
version: 0.1.2
3+
version: 0.1.3
44
synopsis: Fast Splittable PRNG
55
description:
66
Pure Haskell implementation of SplitMix described in
@@ -26,28 +26,30 @@ description:
2626
(the mixing functions are easily inverted, and two successive outputs
2727
suffice to reconstruct the internal state).
2828

29-
license: BSD3
29+
license: BSD-3-Clause
3030
license-file: LICENSE
3131
maintainer: Oleg Grenrus <oleg.grenrus@iki.fi>
3232
bug-reports: https://github.com/haskellari/splitmix/issues
3333
category: System, Random
3434
build-type: Simple
3535
tested-with:
36-
GHC ==8.6.5
37-
|| ==8.8.4
38-
|| ==8.10.4
39-
|| ==9.0.2
40-
|| ==9.2.8
41-
|| ==9.4.8
42-
|| ==9.6.7
43-
|| ==9.8.4
44-
|| ==9.10.2
45-
|| ==9.12.2
36+
GHC ==8.6.5
37+
|| ==8.8.4
38+
|| ==8.10.4
39+
|| ==9.0.2
40+
|| ==9.2.8
41+
|| ==9.4.8
42+
|| ==9.6.7
43+
|| ==9.8.4
44+
|| ==9.10.2
45+
|| ==9.12.2
46+
47+
extra-doc-files:
48+
Changelog.md
49+
README.md
4650

4751
extra-source-files:
48-
Changelog.md
4952
make-hugs.sh
50-
README.md
5153
test-hugs.sh
5254

5355
flag optimised-mixer
@@ -72,7 +74,7 @@ library
7274
-- ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html
7375

7476
build-depends:
75-
base >=4.12.0.0 && <4.22
77+
, base >=4.12.0.0 && <4.22
7678
, deepseq >=1.4.4.0 && <1.6
7779

7880
if flag(optimised-mixer)
@@ -92,6 +94,10 @@ library
9294
if os(windows)
9395
c-sources: cbits-win/init.c
9496

97+
elif (os(osx) || os(ios))
98+
c-sources: cbits-apple/init.c
99+
ld-options: -framework Security
100+
95101
else
96102
c-sources: cbits-unix/init.c
97103

@@ -110,7 +116,7 @@ benchmark comparison
110116
hs-source-dirs: bench
111117
main-is: Bench.hs
112118
build-depends:
113-
base
119+
, base
114120
, containers >=0.6.0.1 && <0.8
115121
, criterion >=1.6.0.0 && <1.7
116122
, random
@@ -124,7 +130,7 @@ benchmark simple-sum
124130
hs-source-dirs: bench
125131
main-is: SimpleSum.hs
126132
build-depends:
127-
base
133+
, base
128134
, random
129135
, splitmix
130136

@@ -136,7 +142,7 @@ benchmark range
136142
main-is: Range.hs
137143
other-modules: Data.Bits.Compat
138144
build-depends:
139-
base
145+
, base
140146
, random
141147
, splitmix
142148

@@ -147,7 +153,7 @@ test-suite examples
147153
hs-source-dirs: tests
148154
main-is: Examples.hs
149155
build-depends:
150-
base
156+
, base
151157
, HUnit >=1.6.0.0 && <1.7
152158
, splitmix
153159

@@ -162,7 +168,7 @@ test-suite splitmix-tests
162168
Uniformity
163169

164170
build-depends:
165-
base
171+
, base
166172
, containers >=0.4.0.0 && <0.8
167173
, HUnit >=1.6.0.0 && <1.7
168174
, math-functions >=0.3.3.0 && <0.4
@@ -177,7 +183,7 @@ test-suite montecarlo-pi
177183
hs-source-dirs: tests
178184
main-is: SplitMixPi.hs
179185
build-depends:
180-
base
186+
, base
181187
, splitmix
182188

183189
test-suite montecarlo-pi-32
@@ -187,7 +193,7 @@ test-suite montecarlo-pi-32
187193
hs-source-dirs: tests
188194
main-is: SplitMixPi32.hs
189195
build-depends:
190-
base
196+
, base
191197
, splitmix
192198

193199
test-suite splitmix-dieharder
@@ -197,15 +203,15 @@ test-suite splitmix-dieharder
197203
hs-source-dirs: tests
198204
main-is: Dieharder.hs
199205
build-depends:
200-
async >=2.2.1 && <2.3
206+
, async >=2.2.1 && <2.3
201207
, base
202-
, bytestring >=0.10.8.2 && <0.13
208+
, bytestring >=0.10.8.2 && <0.13
203209
, deepseq
204-
, process >=1.6.0.0 && <1.7
210+
, process >=1.6.0.0 && <1.7
205211
, random
206212
, splitmix
207-
, tf-random >=0.5 && <0.6
208-
, vector >=0.13.0.0 && <0.14
213+
, tf-random >=0.5 && <0.6
214+
, vector >=0.13.0.0 && <0.14
209215

210216
test-suite splitmix-testu01
211217
if !os(linux)
@@ -219,7 +225,7 @@ test-suite splitmix-testu01
219225
c-sources: tests/cbits/testu01.c
220226
extra-libraries: testu01
221227
build-depends:
222-
base
228+
, base
223229
, base-compat-batteries >=0.10.5 && <0.15
224230
, splitmix
225231

@@ -230,6 +236,6 @@ test-suite initialization
230236
hs-source-dirs: tests
231237
main-is: Initialization.hs
232238
build-depends:
233-
base
239+
, base
234240
, HUnit >=1.6.0.0 && <1.7
235241
, splitmix

0 commit comments

Comments
 (0)