Sunday, August 20, 2017

Units of Measure in F#

An interesting (and seemingly very useful feature) of F#: units-of-measure types, which help the compiler reason about correctness. 

https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure

> [<Measure>] type cm;;
 [<Measure>]
 type cm

> [<Measure>] type mile;;
 [<Measure>]
 type mile

> [<Measure>] type hour;;
 [<Measure>]
 type hour

> let distance = 20.0<mile>;;
 val distance : float<mile> = 20.0

> let duration = 12.0<hour>;;
 val duration : float<hour> = 12.0

> let speed = distance / duration;;
 val speed : float<mile/hour> = 1.666666667

> speed + 2<cm>;;
 Stopped due to error
 System.Exception: Operation could not be completed due to earlier error
 The type 'int<cm>' does not match the type 'float<mile/hour>' at 3,8
 The type 'int<cm>' does not match the type 'float<mile/hour>' at 3,6


> speed + 1.0<mile/hour>;;
 val it : float<mile/hour> = 2.666666667

Sunday, May 28, 2017

Cryptopals Set 1 -- F#


Completed with word/line count comparisons stats between Python and F# here: https://github.com/tkuriyama/cryptopals

F# is about 1000 lines total vs 1180 lines in Python. Although F# is overall more concise, there is considerably more boilerplate in calling .NET libraries such as cryptography functions.

Since the problem space is relatively pure computation, I've found writing functionally in F# to be more pleasurable. On the other hand, the MSDN documentation seems rather unfriendly compared to what I've gotten used to in Python... (fortunately, the C# examples are pretty readable and I found someone else's example of using the AES decryption API).

Thursday, April 27, 2017

Cryptopals Set 2


http://cryptopals.com/sets/2
https://github.com/tkuriyama/cryptopals/tree/master/set2

Notes
  • Challenge 9 and 15 PKCS#7 padding -- the challenges do not make clear that that if the final data block fits the data block perfectly (e.g. there are 16 bytes of data remaining in the final 16-byte block), an additional block should be appended as padding.
  • Challenge 14 Byte-at-a-time ECB decryption -- this is indeed a harder variant of challenge 12, since a random number of bytes being prefixed to the attacker-controlled text means that the oracle is not always reliable. From previous challenges, however, we know that repeating blocks are encrypted identically in ECB mode. We can therefore prepend the attacker-controlled text with two repeating blocks, i.e. have the oracle encrypt (random prefix || repeating blocks || attacker-controlled text || target-bytes). If we observe repeating blocks, we know where our attacker-controlled text begins (as a wrapper around the oracle function, call this "smart oracle"). Assuming that the random number of bytes prefixed is uniformly distributed over integers with some upper boundary N, it will take roughly N times more calls to the oracle to achieve a desirable result... which is reasonable as it scales linearly. I'm still missing something as the smart oracle sometimes fails, but decryption generally works after a few tries.

Saturday, April 22, 2017

Cryptopals Set 1

I have started working through the Cryptopals Challenges after hearing about it on a Podcast.__init__ episode. It seems like a good and practical refresher to Dan Boneh's Crytopgraphy I class on Coursera (incidentally, I've been waiting for Crytography II for many years but the course seems to be constantly pushed back). It should also be a good exercise to work through in F# -- as a means to learn more of the language -- after completing a first pass in Python.

Set 1 is fairly straightforward, though it took me a while to workout how to operate consistently on bytearrays (per the Cryptopals rules) and convert between different encodings (hex, base64, bytes represented as ints vs chars...). It seems that Python 3 makes the experience of working with bytes a little easier.

Notes

  • Challenge 3 Single-Byte XOR Cipher: most frequency distribution of characters in the English language that I found did not include non-alphabet characters, such as space. My solution did not work until space was included in the scoring of plaintext (using chi-squared). 

https://cryptopals.com/sets/1
https://github.com/tkuriyama/cryptopals/tree/master/set1

Wednesday, December 14, 2016

Hiatus, JSON

After another long hiatus due to work travel, I've started looking at content on Coursera and Udacity again. It seems like the breadth and depth of classes has expanded significantly in the past year -- with a much stronger monetization push in the "modular credential" model. Are people actually paying? It would appear that recent valuation puts Udacity's value at $1BN... which factors in significant growth expectation based on a rumored run rate of around $25MM.

Meanwhile, I worked on a small command-line utility for browsing JSON files recently, after getting frustrated by large JSON files that have no line breaks (taking a peek by calling head is not at all useful!).

https://github.com/tkuriyama/jsonutils/blob/master/docs/jbro.md


Tuesday, May 24, 2016

New Coders Survey 2016


Brief analysis of the dataset from the Free Code Camp's New Coders Survey 2016:

https://github.com/tkuriyama/notebooks/blob/master/ipython/New_Coders_Survey_2016.ipynb

TLDR; coding schools / bootcamps yield mixed results, but there are lots of folks interested in coding and lots of resources available beyond Udacity and Coursera.

Wednesday, March 30, 2016

SICP


I've decided to work through SICP. So far, its reputation as a classic text that has withstood the test of time seems well deserved. I've been targeting one sub-chapter a week, so factoring in an extra 50% for distractions and presumed difficulty of later chapters, it will take me the remainder of the year to finish.

This post was incredibly useful in terms of setting up a good vim / tmux working environment with Racket as the language of choice: http://crash.net.nz/posts/2014/08/configuring-vim-for-sicp/. It seems like emacs may have better indentation support, but learning a new editor still escapes me.