Programming Bitcoin is probably the best code exercise book for learning the fundamental math, cryptography, and data structures underpinning bitcoin.