2020/04/30

Coquitos for quarantine

We are still in quarantine, but we had a bit of grated coconut and we made coquitos:
  • 200g grated coconut
  • 200g sugar
  • 2 eggs
  • I have no idea how hot the oven was.
We shaped them by hand, so they got with some very nice burnt stripes on the sides.

Coquitos: Some are already missing in the photo

(We tried a second time, but the mixture was very dry and after cooking they crumbled. It is very strange. Maybe it depends on the humidity of the grated coconut?)

2020/03/30

About the AR$5 bill in Argentina

[Quarantined]

[I should have written this a month ago, because with the Covid-19 quarantine the situation changed a lot, and most of this doesn't make sense now. Anyways, this seems to be a story that deserves to be told and I have some free time.] [We are fine for now. Thanks for asking.]

Once upon a time, during February ...

The effective exchange rate of the (US) dollar is approximately AR$80, so according to the estimate in the article on the AR$1 coin, the AR$2 coin should fall into disuse. (Surprisingly, it took less time than expected.)
This is not a strict rule, but it is a custom in Argentina. Recently (January-February), I was already seeing that we started to round the prices to pay to a multiple of AR$5. For example, if we must be pay AR$102, it was likely that no one would demand the AR$2 coin.


We had AR$5 bills and AR$5 coins in circulation, but the bills were old and they recently were taken out of circulation. They can be exchanged in a bank, but they cannot be used in a business.


The problem is that the amount of coins of AR$5 is approximately 1/4 or 1/5 of the amount of bills! So, when the AR$5 bills stop circulating, nobody had enough AR$5 coins.

If there are no coins ...

The solution is then to use the AR$2 coins and even rummage through the drawers and dust off the AR$1 coins that had already practically fallen into disuse. I think I have seen more coins of AR$1 in the last days (February) than in the last months. In general, when paying the coins are combined to form a multiple of AR$5 because many of the prices of discrete products are already rounded. (Although there are exceptions such as candies that are worth AR$3 and are a problem.)


My proposal (in February) is to stick with Superglue two coins of AR$2 and one coin of AR$1 and get a combo of AR$5. It would be much easier than searching and choosing the coins one by one (although perhaps illegal). However, after a few days, surely someone will replace the middle coin with a fake one to obtain a 40% profit.

[Current situation]

[Due to the current situation, we are trying to do only one or two weekly purchases at the supermarket. It is a large purchase and we pay with a credit card, so nobody cares if there are coins or not.]

2020/02/04

Quantumly Generated Random Bytes in Racket

Let’s get the bytes

Last week I read about the project Quantum RNG for OpenQu that is a “quantum random numbers as a service”. The idea is to use it to get some random numbers in Racket, using the net/hhp-client and json modules.

This function creates a bytestring of length n using the service.

#lang racket

(require net/http-client
         json)

(define (quantum-random-bytes n)
  (define-values (status headers content)
    (http-sendrecv "random.openqu.org"        
                   (string-append "/api/randint"
                                  "?size=" (number->string n)
                                  "&min=0"
                                  "&max=255")))
  (list->bytes (hash-ref (read-json content) 'result)))

(quantum-random-bytes 5) ; ==> #"\2\353<\223\346"
(quantum-random-bytes 5) ; ==> #"\240\200\242\364\25"
(quantum-random-bytes 5) ; ==> #"\201\370\367\32\25"

Other random-bytes functions

For comparison, we can use the crypto-random-bytes function and we also can write our own random-bytes function that uses the build-in pseudorandom number generator.

(require racket/random) ; for crypto-random-bytes
(define (random-bytes n)
  (list->bytes
   (for/list ([i (in-range n)])
    (random 256))))

(random-bytes 5)         ; ==> #"E\31\366\4\333"
(crypto-random-bytes 5)  ; ==> #"b\345\207\315\""
(quantum-random-bytes 5) ; ==> #"\30`\325\3377"

(require file/sha1) ; for bytes->hex-string

(bytes->hex-string (quantum-random-bytes 5)) ; ==> "00b7c4d6db"
(bytes->hex-string (crypto-random-bytes 5))  ; ==> "662b108fd2"
(bytes->hex-string (random-bytes 5))         ; ==> "da25419554"

As expected, the result of all of them look like similar random nonsense. But it looks nicer as hexadecimal random nonsense.

The first is deterministic, but it uses the initial time of the program as a seed, so you will get different results in each run. Its’s good enough to for a game or simulation, and you can use random-seed to get the same sequence.

The second use the operative system random number generator and is cryptographically secure. The operative system mix many sources of entropy, but all (most?) of them are classic source. (It’s like rolling a dice, it’s difficult to predict but it’s not truly random.)

The third uses a quantum system to produce the random numbers. These numbers are truly random (if quantum mechanics is correct). But remember that you can’t use them for a password and other security applications because the server owner may have a copy (or be lying and sending the digits ofpi). Or someone could be wiretapping your internet connection (this use http, not https!).

(Note that this are not qbits. The “q” part is killed in the generation, long before they are sent through the wire.)

I guess you can use it for a nice truly random quantum choice of numbers, like the starting position of a solitaire game. It’s “better” that just shuffling the cards, but probably undistinguishable for the player. For the secure version, you should buy the hardware version (and verify that it works as intended).

Testing the numbers

In a previous article, I used Random Sanity that is a service that does a minimal check of the random numbers. It may have false positives, and it may not detect fake random numbers, so do it’s only useful to detect very bad implementations of (pseudo) random numbers generators.

Using the functions defined in that article to contact the service, we get:

(test-random-bytes (random-bytes 64))         ; ==> #t ;probably
(test-random-bytes (crypto-random-bytes 64))  ; ==> #t
(test-random-bytes (quantum-random-bytes 64)) ; ==> #t
(with-random-seed 1234567890
  (test-random-bytes (random-bytes 64)))      ; ==> #f

The first is almost always true, but it may be false if we all keep trying and sending the numbers created by the pseudorandom number generator. Using the birthday paradox, with 38 persons starting the program in the same second, we have a 50% chance to get a collision. (Or melt the server, whatever happens first.) The last one is false because it is using a fixed seed, and I already tested it and the server detects the collision now.

Note that now that the bytes have been tested, they are even less secure because yet another person has a copy.

Some ideas to try

  • There is an API call to get some bytes in the base64 format. I tried to use it, but I get an error message. It may work later. It may be useful base64-decode.
  • Without any good reason I’m generating a random byte string instead of a single random number. With small modifications, it’s simple to make function that is a replacement of (random n). Note that the max value of the interval in the service may be included in the results, so remember to subtract 1 before sending the request. I don’t know how big can be max.
  • There is another API call to get floating point numbers, which can be used to write a function that is a replacement of (random). Just note that the roundtrip on the network may take longer than tolerable for an interactive program.

Bonus: Another similar service

While looking for more info I found a few similar services, but most of them require some kind of registration. One that is easy to use is the ANU Quantum Random Numbers Server. Let’s use it too:

(define (other-quantum-random-bytes n)
  (define-values (status headers content)
    (http-sendrecv "qrng.anu.edu.au"        
                   (string-append "/API/jsonI.php"
                                  "?length=" (number->string n)
                                  "&type=uint8")))
  (list->bytes (hash-ref (read-json content) 'data)))

(other-quantum-random-bytes 5) ; ==> #"\25\n1\236K"
(other-quantum-random-bytes 5) ; ==> #"\271\375'\272\205"
(other-quantum-random-bytes 5) ; ==> #"\23\362\324\271\\"
(bytes->hex-string (other-quantum-random-bytes 5)) ; ==> "9366b3f8d2"
(test-random-bytes (other-quantum-random-bytes 64)) ; ==> #t

The link to the API is difficult to find, but this server only support int8, int16 and bytes strings in some hexadecimal format. On the other hand, it supports https, but that part is left as an exercise for the reader.