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
"""
31-round sha256 collision.
Not my research, just a PoC script I put together with numbers plugged in from the slide at
https://twitter.com/jedisct1/status/1772647350554464448 from FSE2024
SHA256 impl follows FIPS 180-4
https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
"""

This is my best attempt at generating complete test vectors for the ML-KEM compress() and decompress() functions described by the FIPS 203 IPD.

This is has not (yet) been subject to cross-referencing against any other implementation. It may be incorrect!!!

If your implementation doesn't match up, please let me know.

["compress"][d][x] holds the result of compress_d(x)

["decompress"][d][y] holds the result of decompress_d(y)

@DavidBuchanan314
DavidBuchanan314 / Makefile
Last active January 9, 2024 22:19
A second attempt at 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
@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 February 23, 2024 11:03
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__":
@DavidBuchanan314
DavidBuchanan314 / hash_json.py
Last active December 27, 2023 18:09
I wrote about this code in more detail here: https://www.da.vidbuchanan.co.uk/blog/signing-json.html
"""
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 1, 2023 14:11
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