-
-
Save gfredericks/af951e56680127f10eb8 to your computer and use it in GitHub Desktop.
(ns sneaky-NaNs | |
"Secret codez.") | |
(defn char->NaN | |
[c] | |
(-> c | |
(int) | |
(bit-or 0x7ff8000000000000) | |
(Double/longBitsToDouble))) | |
(defn NaN->char | |
[nan] | |
(-> nan | |
(Double/doubleToRawLongBits) | |
(bit-and 0xffffffffffff) | |
(char))) | |
(->> "¡Hello, world! ☺" (map char->NaN)) | |
=> (NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN) | |
(->> "¡Hello, world! ☺" (map char->NaN) (map NaN->char) (apply str)) | |
=> "¡Hello, world! ☺" |
Do the docs imply that there's a subset of NaNs that can safely be used for encoding things?
The Java docs don't imply much 'safety' of preserving bits when conversions between quiet and signaling NaNs are performed, and mention that it is processor specific.
The NaN definition on Wikipedia might provide such assurances. I just love that when discussing the bit-level format of IEEE 754 NaN values, they use the term 'payload' for most of the bits. Makes it sound like an IP packet :-) https://en.wikipedia.org/wiki/NaN
Obviously what we need is a table of test results for processor/OS/JVM version combinations indicating where this technique is safe vs. where it is not!
Does the internet yet have "run this code on fifty different processors as a service"?
That is an interesting question. I wouldn't be surprised if Amazon or Travis might offer something like that, but quick Google searches don't return positive results for me. I would expect companies like Google, Amazon, Facebook, Microsoft probably have internal tools to test software against their data center fleets of servers, with their variations in processors, but that isn't likely to be a public service.
Time to combine that with my other startup idea of mining and selling/trading testnet cryptocurrency.
stenanography.com is still available! :-)
I would like to file a bug against the entire existence of this gist, but it is just too fun to contemplate and pass on links to it.
I happened to read the last part of the documentation of Java's Double/longBitsToDouble method today, and see that depending upon the implementation, your encoding method might corrupt data! Interesting? You tell us!
https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#longBitsToDouble-long-