Skip to content

Instantly share code, notes, and snippets.

View DavidBuchanan314's full-sized avatar
๐ŸŒ
Hack the planet!

David Buchanan DavidBuchanan314

๐ŸŒ
Hack the planet!
View GitHub Profile
@DavidBuchanan314
DavidBuchanan314 / Makefile
Last active January 16, 2024 12:40
simdutf incremental utf8 validation (proof-of-concept, not rigorously tested, see https://github.com/simdutf/simdutf/issues/361 )
CFLAGS := -Wall -Wextra -Wpedantic -O3
CXXFLAGS := ${CFLAGS}
LDFLAGS := -lsimdutf
main: main.o utf8_incremental.o
"""
This pure-python ChaCha20 implementation reaches 32MiB/sec on my machine (M1 Pro)
otoh, cryptography.io's impl reaches about 1700MiB/s. Way faster, of course, but only about 50x faster.
This is code is a proof-of-concept and should not be used in a security context.
"""
CONST_MAGIC = b"expand 32-byte k"
CONST_WORDS = [int.from_bytes(CONST_MAGIC[i:i+4], "little") for i in range(0, 16, 4)]
@DavidBuchanan314
DavidBuchanan314 / json_no_dupes.py
Last active August 18, 2024 03:00
How to ensure JSON has no duplicate map keys in Python 3.1+
from typing import List, Tuple, Any
import json
def ensure_no_duplicate_keys(object_pairs: List[Tuple[str, Any]]) -> dict:
value = dict(object_pairs)
if len(value) != len(object_pairs):
raise ValueError("Duplicate JSON map keys")
return value
if __name__ == "__main__":
"""
DISCLAIMER: This is a quick prototype, it's not at all tested, and may be deeply cryptographically flawed.
Normally, JSON canonicalization is at least O(nlogn), because you need to sort the map keys.
This approach avoids the need to do that, and in theory it's O(n), but in practice it's probably slower for most inputs... I have not benchmarked.
If you limit recursion depth, you could implement it as an Online Algorithm https://en.wikipedia.org/wiki/Online_algorithm
NB: Python's JSON parser allows duplicate map keys, which this impl will be oblivious to.
/*
gcc main.c -o main -O3 -lSDL2
This hits 240fps at 4K resolution, on my M1 Pro
*/
#include <SDL2/SDL.h>
#include <stdio.h>
import zlib
from io import BytesIO
# TODO: make this shorter
RLEFLATE_MAGIC = b'\xed\xe3\t\x90$I\x92$I\xcc\xff\xff\xff\xff\xff\xff\xff\xff'\
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'\
b'\xff\xff\xff\xff?3333\xe1\x0e\x00\x0e``\xa0\xaa*"\xc2\xdc]U\x04\x00\x80'
RLEFLATE_EOF = b'\x77'
RLEFLATE_REPEAT = [
None, b'W', b'7', b'G', b'g', b'#', b'3', b'+', b';', b'\x11', b'Q',
@DavidBuchanan314
DavidBuchanan314 / NWSL.TXT
Last active September 11, 2024 04:48
I think this is functionally MIT-equivalent but I am not a lawyer!
NUCLEAR WASTE SOFTWARE LICENSE V1.0
Copyright <YEAR> <OWNER>
This software license is a message... and part of a system of messages...
pay attention to it! Writing this software and associated documentation
files (the "Software") was important to us. We considered ourselves to be a
powerful culture. This Software is not a place of honor... no highly
esteemed deed is commemorated here... nothing valued is here. What is here was
dangerous and repulsive to us. This message is a warning about danger. The
@DavidBuchanan314
DavidBuchanan314 / bsky_firehose_standalone.py
Last active October 25, 2023 01:50
This is not production quality code, several corners have been cut, etc. etc.
import io
import base64
import struct
import hashlib
import asyncio
import websockets
from enum import Enum
# ground control to major type
import hashlib
from functools import reduce
inputs = """\
4194f2d3111228a07aeb0a54684fa4de21164109c4c789cd6b890771dbb3fff6
273853d452aec0d82d7599c043ec4bb55bf1f473a8d02302992f032e0804c02b
f4b264de9e84cd80c356712b686d9faff35dabb4221fd33966a18c9ddbe8a371
5f15dfdc4f1916427eb126f65d0b49ceba89db4653215fae7683b7c93ac733f7
78036761ae5ce55a3646baf06b0caf7301f7b060034c59bb097566c511f2c91d
7cb072d0b4be5eee1b115882d5655a588ad5ada350cfffb635216fa6cf871e91
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.