Mercurial > dropbear
comparison libtommath/bn_s_mp_rand_jenkins.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1691:2d3745d58843 | 1692:1051e4eea25a |
---|---|
1 #include "tommath_private.h" | |
2 #ifdef BN_S_MP_RAND_JENKINS_C | |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | |
4 /* SPDX-License-Identifier: Unlicense */ | |
5 | |
6 /* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */ | |
7 /* Chosen for speed and a good "mix" */ | |
8 typedef struct { | |
9 uint64_t a; | |
10 uint64_t b; | |
11 uint64_t c; | |
12 uint64_t d; | |
13 } ranctx; | |
14 | |
15 static ranctx jenkins_x; | |
16 | |
17 #define rot(x,k) (((x)<<(k))|((x)>>(64-(k)))) | |
18 static uint64_t s_rand_jenkins_val(void) | |
19 { | |
20 uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7); | |
21 jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13); | |
22 jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37); | |
23 jenkins_x.c = jenkins_x.d + e; | |
24 jenkins_x.d = e + jenkins_x.a; | |
25 return jenkins_x.d; | |
26 } | |
27 | |
28 void s_mp_rand_jenkins_init(uint64_t seed) | |
29 { | |
30 uint64_t i; | |
31 jenkins_x.a = 0xf1ea5eedULL; | |
32 jenkins_x.b = jenkins_x.c = jenkins_x.d = seed; | |
33 for (i = 0uLL; i < 20uLL; ++i) { | |
34 (void)s_rand_jenkins_val(); | |
35 } | |
36 } | |
37 | |
38 mp_err s_mp_rand_jenkins(void *p, size_t n) | |
39 { | |
40 char *q = (char *)p; | |
41 while (n > 0u) { | |
42 int i; | |
43 uint64_t x = s_rand_jenkins_val(); | |
44 for (i = 0; (i < 8) && (n > 0u); ++i, --n) { | |
45 *q++ = (char)(x & 0xFFuLL); | |
46 x >>= 8; | |
47 } | |
48 } | |
49 return MP_OKAY; | |
50 } | |
51 | |
52 #endif |