Order of operations for very very very very fast computers #1704

issue xanatos opened this issue on August 23, 2012
  1. xanatos commented at 9:44 AM on August 23, 2012: none

    In wallet.cpp, around lines 125, 129 and 237 there are some calculations to find the nDeriveIterations necessary for a given PC.

    pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
    
    pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
    
    kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
    

    Now, I'm not secure secure of it, but I'll say that the first has a better order of operations. Why?

    Now, nDeriveIterations is an uint. The first one multiplies it by a double number (100 / ...). There won't be an overflow at this point. Then the double is downcasted to uint (and here there could be an overflow). The second and third variants first multiply the uint by 100 and then (through the division with a double) convert it to a double. By first multiplying it by 100 you have limited the nDeriveIterations to around 42 million. Yeah, I do know that this isn't important with today computers :-)

  2. RayDillinger commented at 11:25 PM on December 2, 2013: none

    This was unlikely to ever result in a real bug, but I've fixed it and submitted a pull request anyway, 'cos it was very simple.

  3. laanwj closed this on Oct 27, 2015

  4. laanwj commented at 11:26 AM on October 27, 2015: member

    Not likely to ever really be a problem.

  5. suprnurd referenced this in commit d23adcc0f1 on Dec 5, 2017
  6. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-29 03:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me