70 points · by waffle_ss · 9y ago · 60 comments
Audits matter. Your books should always sum zero. Time and time again, we've seen bugs like this across the space: there was the infamous Bitcoinica "infinite leverage" bug [1], a rounding error in another's order sizes, and it's theorized that a similar bug was leveraged in the downfall of MtGox to create virtually infinite balances and use it to buy Bitcoin up to $1000 in 2013.
Whether it's in your engine or outside of it, whether it's a Bitcoin product or any other financial product, make sure there is something - somewhere - that is regularly summing up all accounts and ensuring that every cent or satoshi is accounted for.
We run such a system at every execution. If the system doesn't sum zero, we shut down. It's happened three times in two and a half years, each time due to tiny mathematical errors in our liquidation process, each time for less than 10,000 satoshis (~$0.10). We sleep much better at night knowing it's there.
Audits would catch the error caused by float math. And of course, don't do float math.
1. I can't find the original reference to it, but it was originally possible to simply edit the DOM to submit any leverage amount you pleased (such as 100,000x; never trust the client!) and then run your account massively into profit - or bankruptcy.
That's like the industry that epitomizes the usecase for fixed point!
The snippet of what I presume is their API response is interesting:
{"currencyCode":"XBT","balance":100000.00000008,"available":100000.00000008,"onDeposit":0}
The amount fields are JSON numbers. That's fine for integers up to 2^52[1] but can have issues with floating point math. They'd be better off representing everything as satoshis and doing integer math on everything.I bet their entire system is based on floating point math. Tisk tisk ...
[1]: Yes probably not an issue unless your Bill Gates and convert your networth to Zimbabwean dollars...
You will, of course, run into huge trouble if you perform more complex operations. All bets are off once you start doing things like trigonometry or logarithms, unless you implement it yourself.
Also note the the way you round usually depends on the context, for some calculations you want to round up, for some down, and for some in yet another way. So you perform explicit rounding with an explicit rounding mode everywhere it is required and don't assume that implicit rounding or the default rounding mode will do the right thing because they won't in general.
EDIT: To expand a bit on the context. If you, for example, charge a percentage fee you can choose between rounding up getting at least the fee you are asking for or rounding down not charging the customer more than you are asking for. Or you can try to be less biased and round either up or down depending on the fractional part. This may still require tie-breaking, either in a direction of your choice, or randomized, or by some rule like round half to even. Different choices will obviously have different biases.
While this may almost seem somewhat pedantic, such things have been exploited, see for example »Computer Capers: Tales of Electronic Thievery, Embezzlement & Fraud«. [1][2] Also note that there may be even legal regulations prescribing how you can and can not round, for example between the Euro and the replaced national currencies. [3] I also have some faint memories of a case against someone systematically exploiting rounding errors but I can not come up with a reference and may even misremember this.
[1] http://www.snopes.com/business/bank/salami.asp
[2] https://www.amazon.com/Computer-Capers-Electronic-Thievery-E...
fee = accountX * 0.01
accountY = accountY + fee
accountX = accountX - fee
vs accountY = accountY + accountX * 0.01
accountX = accountX * 0.99
I've seen a fair bit of (non financial) accounting go wrong like this, where fractions just slip through the cracks.No floats. Not even once.
My order of preference:
* Fixed-point arithmetic: All-around safe, though you'll probably need to take a maintenance window later if you need extra precision.
* Floating point arithmetic: Sounds dangerous, but you can eliminate most of the risk using database checks.
* Arbitrary-precision arithmetic: Almost any place they're used opens you up to DoS attacks; it's very difficult to detect the problem. I'd ensure my database and application server have debugging symbols installed, replace libgmp with something that alerts by generating core dumps when a timing threshold is passed, use a language where backtraces are easy to read in gdb, and ensure that all arbitrary-precision arithmetic is written against gmp. I'm not sure what I'd do when I detected the problem though: If I truncated 1 + 1/(10^10)! to 1 to resolve the issue, it's equivalent to using floating point in the first place.
http://speleotrove.com/decimal/ etc.
https://hn.algolia.com/?query=speleotrove&sort=byDate&dateRa...
For Bitcoin applications, I think you'd want to work in integer satoshis.
Basically I think the idea with 4 digits after the decimal is that the smallest denomination you can have is 1 penny, and that allows you to calculate percentages of pennies accurately to a hundredth of a percentage point.
11:34:40.979 SecurityError: The operation is insecure. 1 frontend.fe083880.js:15
e.exports<.initialize https://hackerone.com/assets/frontend.fe083880.js:15:31757
t.Model https://hackerone.com/assets/vendor.b247925b.js:31:4648
e.exports<.constructor https://hackerone.com/assets/frontend.fe083880.js:2:20109
q/r< https://hackerone.com/assets/vendor.b247925b.js:31:22455
<anonymous> https://hackerone.com/assets/frontend.fe083880.js:19:31012
t https://hackerone.com/assets/vendor.b247925b.js:1:96
n https://hackerone.com/assets/frontend.fe083880.js:26:14283
forEach self-hosted
<anonymous> https://hackerone.com/assets/frontend.fe083880.js:96:23586
t https://hackerone.com/assets/vendor.b247925b.js:1:96
window.webpackJsonp https://hackerone.com/assets/vendor.b247925b.js:1:418
<anonymous> https://hackerone.com/assets/frontend.fe083880.js:1:1
Fail, Hacker One.Apparently Fintech startups have no need for the wisdom built up over the course of 500 years of modern banking, because this stuff is Banking 101 for any engineer at a bank.