Finite Fields - Bitcoin & Haskell

in #programming22 days ago


Bitcoin's price has gone crazy up, and now I'm really responsible for learning it. I found the book Programming Bitcoin by Jimmy Song, it looks good enough to give it a try. It uses Python to teach, which is uninteresting and I end up only skimming over it, which leads to low retention. Learning necessitates inefficiencies, trial and error, solving problems and sharing. Thus to learn I need to throw an extra challenge. I'm going to learn another programming language(Haskell) to solve the book's problems.

The first chapter in Programming Bitcoin deals with finite fields. In Python a class is implemented which holds 2 values and then methods are attached to that class. In Haskell, I use the same idea, only that the procedure is different.

Define a data type, using the record syntax you name the fields number and prime, which gives you the getters automatically. There are no setters, as in Haskell you work with values and those are immutable. deriving (Eq) is really nice, as you get the most evident equality defined for free. In this case, it is the want we need.

data FieldElement =
    { number :: Int
    , prime  :: Int
  deriving (Eq)

The level of conciseness for this small definition is great compared to Python. I also get the type safety, that ensures this two fields are integers. However, my current ignorance forbids me to implement the check that `number