#23061: crypto_rand_double() should produce all possible outputs on platforms 
with
32-bit int
-------------------------------------------------+-------------------------
 Reporter:  teor                                 |          Owner:  nickm
     Type:  defect                               |         Status:
                                                 |  needs_review
 Priority:  Medium                               |      Milestone:  Tor:
                                                 |  0.3.2.x-final
Component:  Core Tor/Tor                         |        Version:  Tor:
                                                 |  0.2.2.14-alpha
 Severity:  Normal                               |     Resolution:
 Keywords:  tor-relay, security-low, privcount,  |  Actual Points:  0.5
  031-backport, 030-backport, 029-backport, 028  |
  -backport-maybe, 027-backport-maybe, 026       |
  -backport-maybe                                |
Parent ID:                                       |         Points:  0.1
 Reviewer:                                       |        Sponsor:
                                                 |  SponsorQ
-------------------------------------------------+-------------------------

Comment (by yawning):

 Replying to [comment:18 nickm]:
 > That actually _does_ look fairly good.  Let's review that approach.

 As an incremental improvement:

 {{{
 double uint64_to_dbl_0_1(uint64_t x) {
   /* Can't merely check for __STDC_IEC_559__ because not every compiler we
 care about defines it. */
 #if FLT_RADIX != 2
 #error FLT_RADIX != 2, your system is the most special of them all.
 #endif

   uint64_t mask = ((uint64_t)1 << DBL_MANT_DIG) - 1;
   double half_epsilon = 1.0 / (mask + 1);

   x &= mask;
   return half_epsilon * x;
 }
 }}}

 While the assumption I made about how `DBL_EPSILON` is defined in relation
 to `FLT_RADIX` and `DBL_MANT_DIG` should hold everywhere that we care
 about, this is cleaner.

 If there is to be unit tests or build time tests in autoconf, the
 following invariants should hold:

  * `DBL_MANT_DIG > 0`
  * `half_epsilon == DBL_EPSILON/2`, `half_epsilon != DBL_EPSILON`
  * `uint64_to_dbl_0_1(UINT64_MAX) != 1.0`
  * `uint64_to_dbl_0_1(UINT64_MAX) + (DBL_EPSILON/2) == 1.0`
  * `uint64_to_dbl_0_1(UINT64_MAX) + DBL_EPSLION == 1.0`
  * `uint64_to_dbl_0_1(0) == 0`

--
Ticket URL: <https://trac.torproject.org/projects/tor/ticket/23061#comment:19>
Tor Bug Tracker & Wiki <https://trac.torproject.org/>
The Tor Project: anonymity online
_______________________________________________
tor-bugs mailing list
tor-bugs@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-bugs

Reply via email to