Skip to content

Instantly share code, notes, and snippets.

@0xZDH
Created July 17, 2019 15:03
Show Gist options
  • Save 0xZDH/3fa6c3ba5c44e525f7f0d39fb06d1dfb to your computer and use it in GitHub Desktop.
Save 0xZDH/3fa6c3ba5c44e525f7f0d39fb06d1dfb to your computer and use it in GitHub Desktop.
Silicon Valley Season 3 Episode 1 Code

wb-combined.c

Original C Code

#include <stdio.h>
#include <stdlib.h>

typedef unsigned long u64;

/* Start here */
typedef void enc_cfg_t;
typedef int enc_cfg2_t;
typedef __int128_t dcf_t;

enc_cfg_t _ctx_iface(dcf_t s, enc_cfg2_t i){
  int c = (((s & ((dcf_t)0x1FULL << i * 5)) >> i * 5) + 65);
  printf("%c", c); }
  enc_cfg2_t main() {
  for (int i=0; i<17; i++){
    _ctx_iface(0x79481E6BBCC01223 + ((dcf_t)0x1222DC << 64), i);
  }
}
/* End here */

//TOneverDO change name
u64 HammingCtr(u64 a, u64 b) {
  u64 c = a ^ b;

  /*for (d = 0; c>0; c >>= 1)
  {
    d += c & 1;
  }*/
  // O(m) lol no thanks

  //TODO wrap into loop
  c = c - ((c >> 1) & ~0UL/3);
  c = (c & ~0UL/5) + ((c >> 2) & ~0UL/5);
  c = (c & ~0UL/0x11) + ((c >> 4) & ~0UL/0x11);
  c = (c & ~0UL/0x101) + ((c >> 8) & ~0UL/0x101);
  c = (c & ~0UL/0x10001)+((c>>16)&~0UL/0x10001);
  c = (c & ~0UL/0x100000001)+((c>>32)&~0UL/0x100000001);
  
  //TODO throw away intermediates... but could be useful later (see seander)
  return c;
}

// TODO transform + multiply spectra + transform back. faster? lossy?

u64 * ConvolutedMagic(u64 *x, u64 y, u64 *z, u64 n, u64 n_y) {
  //z is arrray of offsets in BITS.
  //y is left-aligned of length n_y
  //TODO function is ridic fragile. e.g. if len z > len x/y...

  u64 * a = malloc(sizeof(u64)*n);
  u64 b,o_64,o_bit;

  for (int i=0; i<n; i++) {
    o_64 = z[i] >> 6;
    o_bit= z[i] - ((z[i]>>6) << 6);
    b = *(x+o_64) << o_bit;
    if (o_bit > 0) {
      b += x[o_64+1] >> (64-o_bit);
    }
    b = (b >> (64-n_y))<<(64-n_y);
    y = (y >> (64-n_y))<<(64-n_y); //not necessary, just in case
    a[i] = HammingCtr(b,y);
  }

  return a;
}

int main() {
  //test hamconv
  u64 x[] = {1,2,3,4,5,6,7,8};
  u64 y = 15;
  u64 z[] = {0,64,64*2,64*3,64*4,64*5,64*6,64*7};

  u64 n_samples = sizeof(z)/sizeof(u64);
  u64 *out = ConvolutedMagic(x,y,z,n_samples,64);

  for (int i=0; i<n_samples;i++) {
  
  }

  return 0;
}



_ctx_iface Function in Python3

#!/usr/bin/env python3

def _ctx_iface(s, i):
  c = (((s & (0x1F << i * 5)) >> i * 5) + 65)
  print("%s" % chr(c), end='')

for i in range(17):
  _ctx_iface(0x79481E6BBCC01223 + (0x1222DC << 64), i)



_ctx_iface Function Explained

Parameter 1, 's', of _ctx_iface function call:

s = 0x79481E6BBCC01223 + (0x1222DC << 64)
#    0x1222DC79481E6BBCC01223
#    1001000100010110111000111100101001000000111100110101110111100110000000001001000100011

Main calculation of _ctx_iface:

((s & (0x1F << i * 5)) >> i * 5) + 65

Working from the inside out:

x = 0x1F << i * 5
#    0x1F == 0b11111
#    i * 5 == Char Position (out of 17) * 5-bits

# Left shift to pad the binary value (11111) with 0's on the right
# so that 0x1F aligns with each 5-bits in 's' (each character made up of 5-bits)

# At 23:51 in Season 3 Episode 1, the whiteboard displays that there is a '5-bit mask'
s = s & x
# s & (0x1F << i * 5)

# Bitwise AND is performed against 's' to extract the 5-bit character that 'x' is padded to
s = s >> i * 5
# (s & (0x1F << i * 5)) >> i * 5

# Right shift to remove the 0 padding that was added to 'x'
s = s + 65
# ((s & (0x1F << i * 5)) >> i * 5) + 65

# Add 65 to the value (landing in 0b11111 > 0x1F > 31) to make it a valid uppercase character

Output of _ctx_iface

DREAM_ON_ASSHOLES



_ctx_iface Function Reworked

Parameter 1, 's', of _ctx_iface function call:

s = 0x79481E6BBCC01223 + (0x1222DC << 64)
#    0x1222DC79481E6BBCC01223

Parameter 1, 's', converted to binary:

s = bin(s)[2:]
#    1001000100010110111000111100101001000000111100110101110111100110000000001001000100011

Parameter 1, 's', split into 5-bit objects

bin_arr = [s[i:i+5] for i in range(0, len(s), 5)]
# >>> bin_arr
# ['10010', '00100', '01011', '01110', '00111', '10010', '10010', '00000', '11110', '01101', '01110', '11110', '01100', '00000', '00100', '10001', '00011']
# >>> len(bin_arr)
# 17

5-bit binary objects converted to integers and then converted to characters

str_ = ""
for i in bin_arr:
  str_ += "%s" % chr(int(i, 2) + 65)

print(str_[::-1])
# DREAM_ON_ASSHOLES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment