Wednesday, December 26, 2012

Nerding for no reason

I've recently started puttering a bit with Brainfuck as an amusingly nerdy way to encode little snips of text. I'd seen this done on mailing list signatures, and found it amusing.

I wrote the following by hand:

++++++++[>++++++++++<-]>.<++[>----<-]>-.<++[>++++<-]>+.<++[>-----------
<-]>.<++[>-------------<-]>.<++++[>++++<-]>.<++++++++[>+++++++++<-]>.<+
+++++++[>---------<-]>.<++[>+++++++++++<-]>.<+++[>-----<-]>--.<++[>++++
+++<-]>.<++[>-----<-]>.---.----.-.


After initially being quite satisfied with myself, I began to feel a bit sheepish.  What's the utility in obfuscating my PGP keyid in that way?  It certainly doesn't make discovering my keyid any easier than just expecting them to check the keyservers.

After that, I looked encoding my Geek Code block... but it's (a) too obscure, and (b) too verbose.  Encoded, it looks like:

+++++++[>>++++++++++<<-]>>+.<<++[>>--<<-]>>.<<++++[>>++++<<-]>>.<<++[>
>>+++++++++++++++++++++++<<<-]>>>+.<<<+++[>>----<<-]>>.--.>.<++.<<+++[
>>++++<<-]>>.<<++[>>>-------<<<-]>>>-.<<<++++++++++[>++++++++++<-]>.>>
.<<<++[>+++++++<-]>+.<++[>>>+++++<<<-]>>>+.<<<++[>>>+++++++<<<-]>>>+.<
<<++[>>>-------<<<-]>>>-.<<<++[>>>-----<<<-]>>>-.<<<+++[>------<-]>.>>
.<<<++++[>>----<<-]>>.<<++[>>>+++++<<<-]>>>+..<<<++[>>>---<<<-]>>>-.<<
<++[>>>--<<<-]>>>.<<<+++[>>++++++<<-]>>.<<+++[>>------<<-]>>-.<<++[>>>
+++++<<<-]>>>+..<<<++[>>>-----<<<-]>>>-.<<<++[>>+++++++<<-]>>.<<++[>>>
+++++<<<-]>>>+...<<<++[>>>-----<<<-]>>>-.+.<<<++[>>--<<-]>>.>-.<<<++[>
>---<<-]>>-.<<++[>>>+++++<<<-]>>>+..<<<++[>>>-----<<<-]>>>-.<<<+++[>>+
+++++<<-]>>.<<++[>>>+++++<<<-]>>>+...<<<++[>>>---<<<-]>>>-.<<<++[>>>--
<<<-]>>>.<<<++[>>----<<-]>>-.<<++[>>>+++++<<<-]>>>+..<<<++[>>>-----<<<
-]>>>-.<<<++[>+++++++<-]>.<+++++[>>>++++++<<<-]>>>+.<<<+++++[>>>------
<<<-]>>>-.<---.<<+++++[>>>++++++<<<-]>>>+.<<<+++++[>>>------<<<-]>>>-.
+.<<<++[>++++<-]>.>>-.+.<<<++[>>++<<-]>>.>-.<--.<<++[>>>+++++<<<-]>>>+
..<<<++[>>>-----<<<-]>>>-.<<<++[>>++++<<-]>>+.<<+++[>>>++++<<<-]>>>+.<
<<++[>>>-----------------<<<-]>>>-.<<<++[>>>+++++++++++<<<-]>>>.<<<++[
>>---<<-]>>.+++.<<++[>>>+++++<<<-]>>>+..<<<++[>>>-----<<<-]>>>-.<---.<
<++[>>-----<<-]>>-.<<+++[>>>++++<<<-]>>>+.<<<+++[>>>----<<<-]>>>-.<<<+
+++[>>+++++<<-]>>.<<++[>>>+++++<<<-]>>>+.<<<+++[>>>++++++<<<-]>>>+.<<<
+++[>>>------<<<-]>>>-.<<<++[>>>-----<<<-]>>>-.<<<++[>>----<<-]>>-.<<+
+[>>----<<-]>>-.<<++[>>++++<<-]>>+.<<++[>>>+++++<<<-]>>>+..<<<+++[>>>+
+++++<<<-]>>>+.<<<+++[>>>------<<<-]>>>-.<<<++[>>>-----<<<-]>>>-.<<---
.<++[>>>+++++<<<-]>>>+..<<<+++[>>>++++++<<<-]>>>+.<<<+++[>>>------<<<-
]>>>-.<<<++[>>>-----<<<-]>>>-.<<<++++[>>>+++++<<<-]>>>+.<<<++[>>>-----
<<<-]>>>..<<<++[>>>-----<<<-]>>>-.<<<++[>>++++<<-]>>.>.<<<++[>>---<<-]
>>.>.<<.++.<+++[>>>++++<<<-]>>>+.<<<+++[>>>----<<<-]>>>-.<<<++++[>----
-<-]>.<++[>>>+++++<<<-]>>>+..<<<++[>>>-----<<<-]>>>-.<<<++[>>-------<<
-]>>.<<++[>>++<<-]>>+.>.<<<++[>>--<<-]>>-.<<++[>>>+++++<<<-]>>>+..<<<+
+[>>>-----<<<-]>>>-.<+++.<<++[>>>+++++<<<-]>>>+..<<<++[>>>-----<<<-]>>
>-.<<+++.<++[>>>+++++<<<-]>>>+...---.+++.--.<<<++[>>>----<<<-]>>>-.<<+
++.<+++[>>>++++<<<-]>>>+....<<<++[>>>-----------------<<<-]>>>-.<<<++[
>>>+++++++++++<<<-]>>>.<<<++[>+++++<-]>.<++[>>>+++++<<<-]>>>+...<<<++[
>>>-----<<<-]>>>-.<<<++[>--<-]>-.<+++++[>>>++++++<<<-]>>>+.<<<+++++[>>
>------<<<-]>>>-.


In the process of trying to write that out, I became somewhat frustrated, and did what most programmers do.  I wrote a program to write my program:

do_factor = function(x){
  tmp = c()

  if (x>1){
    for (i in 1:x){
      if ( floor(x/i) == x/i){
        tmp = rbind( tmp, c(i, x/i, i+x/i))
      }
    }
    ix = head(which(tmp[,3] == min(tmp[,3])),1)
    sort(tmp[ ix, c(1,2) ])
  } else {
    c(1, 1)
  }
}

prep = function(x,n){ paste(rep(x,n), collapse='') }

x = as.numeric(charToRaw(
  "String to 'compile' goes here"))
cur = c(0,0,0)

output=""

for (i in 1:length(x)){
       if (x[i]>=97) { ctype=1 }
  else if (x[i]>=65) { ctype=2 }
  else               { ctype=3 }

  delta = x[i] - cur[ctype]

  R = paste(rep('>', ctype),collapse='')
  L = paste(rep('<', ctype),collapse='')

  if (delta==0){
    output=paste(output,R,'.',L,sep='')
  } else {
    if ( delta >= 0 ) { pch = '+' }
    else              { pch = '-'; delta = -delta }

    if (delta%%2==1 & delta >= 2){
      fx = do_factor(delta-1); add=pch
    } else {
      fx = do_factor(delta); add=''
    }

    if (fx[1]==1){
      output=paste(output, R, prep(pch,fx[2]),add,'.',L,sep='')
    } else {
      output=paste(output, prep('+',fx[1]),'[',R,prep(pch,fx[2]),L,'-]',R,add,'.',L,
                   sep='')
    }
  }
  cur[ctype] = x[i]
}

n_prev = nchar(output)
repeat {
  output = gsub('<>','',output)
  n_cur = nchar(output)
  if (n_cur == n_prev) break
  n_prev = n_cur
}



Now, I just need to find a sufficiently pithy quote such that the blob of BF is closer to the length of my PGP keyid, and farther from the insanely long geek code block.  The closest I've gotten thus far is:

+++++++[>>++++++++++++<<-]>>.<<++++++++[>+++++++++++++<-]>.---.<++++[>
>>++++++++<<<-]>>>.<<<++[>+++++++<-]>+.--.+++.-.<+++[>----<-]>.>>.<<+.
<++[>+++++<-]>.>>.<<-.<++++[>----<-]>-.<++++[>++++<-]>+.<+++[>----<-]>
-.<++[>+++<-]>+.<+++[>++++<-]>+.>>.<<<++[>----<-]>-.<++[>++<-]>+.---.<
+++[>----<-]>-.>>.<<<++[>--<-]>.<+++[>++++<-]>+.<++[>-----<-]>.>>.<<<+
+[>+++++<-]>.<++[>----<-]>-.<++++[>++++<-]>+.<++++[>----<-]>-.<+++[>++
++<-]>+.>>.<<+.<++[>-----<-]>.<++[>++<-]>.+++.<++[>--<-]>.<++[>---<-]>
-.<++[>>>+++++++<<<-]>>>.<<<++++++[>>>------<<<-]>>>.<<<++[>>>++++++++
+++<<<-]>>>..<<<+++[>>>++++<<<-]>>>+..<<<+++[>>>----<<<-]>>>-.<<<++[>>
--<<-]>>-.<<++[>+++++++<-]>.<++++[>----<-]>.--.<++++[>++++<-]>+.>>.<<<
++[>>++++<<-]>>.<<++[>----<-]>-.+++.<++[>----<-]>.+.<