Skip to content

Instantly share code, notes, and snippets.

@karlgluck
karlgluck / Hash Ladders for Shorter Lamport Signatures.md
Last active March 31, 2024 17:53
I describe a method for making Lamport signatures take up less space. I haven't seen anyone use hash chains this way before, so I think it's pretty cool.

What's this all about?

Digital cryptography! This is a subject I've been interested in since taking a class with Prof. Fred Schneider back in college. Articles pop up on Hacker News fairly often that pique my interest and this technique is the result of one of them.

Specifically, this is about Lamport signatures. There are many signature algorithms (ECDSA and RSA are the most commonly used) but Lamport signatures are unique because they are formed using a hash function. Many cryptographers believe that this makes them resistant to attacks made possible by quantum computers.

How does a Lamport Signature work?

@karlgluck
karlgluck / How to Access Data in the Backbuffer in Direct3D 9.cpp
Created January 17, 2014 03:35
This is the code for accessing pixel data from the backbuffer in a D3D9 application. Keywords: LPDIRECT3DSURFACE9 read backbuffer copy back buffer directly access back buffer Direct3D device DirectX 9
void demoExtractBackBufferPixels(LPDIRECT3DDEVICE9 d3d_device) {
// TODO: In your app, add FAILED() macros to check the HRESULTs passed back
// by each of the API calls. I leave these out for clarity.
// Grab the backbuffer from the Direct3D device
LPDIRECT3DSURFACE9 back_buffer = NULL;
d3d_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer);
// Get the buffer's description and make an offscreen surface in system memory.
// this lets you write bits to a buffer and scan them back:
//
// char buffer[3];
// bitfield writer(buffer);
// writer.write(2,6); // write "2" as a 6-bit uint
// writer.write(9,4); // write "9" as a 4-bit uint
// writer.write(0,1); // etc
// writer.write(1,2);
// size_t bytes = writer.bytes(buffer); // bytes == 2
@karlgluck
karlgluck / Extensible-Merkle-Trees.md
Last active November 26, 2018 08:05
I present a simple algorithm that allows billions of one-time signatures to be used for the same public key with today's technology

Extensible Merkle Trees

I present a simple algorithm that lets one use an undetermined number of OTS's for the same public key at the expense of a larger signature. My scheme would allow at least 4.2 billion one-time signatures to be used with a single public key using today's technology.

Background

The Merkle signature scheme (MSS) is a well-known way to use a one-time signature (OTS) like the Lamport-Diffie OTS to create a public key cryptosystem. Briefly, one creates a hash tree of height h from 2^h OTS public keys leading to a root public key. To sign a message, one then simply creates a signature from one of the leaf OTS's as usual and provides evidence of its presence in the tree by giving the sequence of hashes that lead from it to the root public key of the MSS.

Keybase proof

I hereby claim:

  • I am karlgluck on github.
  • I am karlgluck (https://keybase.io/karlgluck) on keybase.
  • I have a public key whose fingerprint is 6797 F0BC 1431 E8CB 5E34 D924 B4F7 4EB0 FF8B 9035

To claim this, I am signing this object:

@karlgluck
karlgluck / CollectTwitchGameStats.py
Created April 18, 2015 15:59
Script that can be run with crontab to collect viewer & channel statistics about the top games on Twitch.TV into CSV files
import requests, json
import os
import re
import time
import datetime
data_dir = os.path.dirname(os.path.realpath(__file__)) + '/data'
api_url = 'https://api.twitch.tv/kraken/games/top?limit=100'
@karlgluck
karlgluck / Binder.cs
Last active March 7, 2017 22:55
Super simple predeclaration framework for Unity that I'm toying with! Basically you can just declare any delegate field with [Bind] on it and this will look around for a static function or single member on another class that implements it, and hook that up on launch.
using System;
using System.Collections;
using System.Reflection;
using UnityEngine.Scripting;
// [Bind] is used to annotate a static field or property of type Func or Action. It causes
// the value of that member to be mapped to a function somewhere else in the program with the
// same name as that field. The target function can be a static function or an instance
// function whose class has a static property/field of its own type named "Singleton".
//
@karlgluck
karlgluck / Inverted-Program-Flow-By-Abusing-IEnumerator.md
Last active December 4, 2016 23:56
A novel control flow pattern for easily writing a game's primary state machine

I'm working on some side projects and have been toying with ideas for more efficient ways to write games in Unity with C#. One of the major hangups for some of my larger side projects is that once it reaches sufficient complexity, it's hard to tell exactly what state the app is in. Even the simple wallet app I built has this issue. I've come up with the beginnings of a solution so I wanted to write it down before it gets lost.

The key problem is that you want your program to respond to inputs that require waiting for a response from something (a webserver, a user, etc). There are a number of options in Unity with C#.

These first 4 options are variations on a theme of control "elsewhere" in the program letting your code know that something has happened:

  • Virtual Callback E.g. if you derive from a class, and override a method which gets called in response to an event
  • Reflection Callback E.g. if you declare OnMouseMove on a class and that function gets called whenever the mouse moves
  • **Callb
@karlgluck
karlgluck / google-spreadsheet-as-database.gs
Last active March 7, 2017 22:54
Easy way to make a key-value store in a Google Spreadsheet. Just publish as a webapp and use jsonp.
function doGet(request)
{
var lock = LockService.getPublicLock();
lock.waitLock(15000);
var ss = SpreadsheetApp.openById(request.parameter.id);
var sheet = ss.getSheetByName('Sheet1');
var obj;
var dataRange = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var dataValues = dataRange.getValues();
if (request.parameter.k && request.parameter.v) {
@karlgluck
karlgluck / IsPointInside.cs
Last active July 13, 2017 21:06
Winding method for finding whether a point is contained by a 2D polygon defined by a vertex loop (C#/Unity)
public static bool IsPointInside (Vector2 point, Vector2[] polygonBoundary)
{
int i = 0, j = 1;
int retval = 0;
while (j < polygonBoundary.Length)
{
var pointI = polygonBoundary[i];
var pointJ = polygonBoundary[j];
int side = (pointJ.x - pointI.x) * (point.y - pointI.y) - (point.x - pointI.x) * (pointJ.y - pointI.y);
int upward = ((side > 0) & (pointI.y <= point.y) & (pointJ.y > point.y)) ? +1 : 0;