Skip to content

Instantly share code, notes, and snippets.

View Rexicon226's full-sized avatar
💭
optimizing

David Rubin Rexicon226

💭
optimizing
View GitHub Profile
@Rexicon226
Rexicon226 / impl.c
Last active October 21, 2026 01:23
Given 64x64 matrix, where row i and bit j represent a vertex i and vertex j connected, returns whether the graph is strongly connected. Fastest I could write, super tuned.
#include <immintrin.h>
#include <stdbool.h>
#include <stdint.h>
#define FN static __attribute__((noinline))
#define FN_INLINE static inline __attribute__((always_inline))
/* scalar stuff */
#define u64 uint64_t
@Rexicon226
Rexicon226 / factory.py
Last active August 18, 2026 22:21
Monifactory PSOC optimizer
from time import perf_counter
NAMES = ("inert", "blue", "green", "cyan", "red", "magenta", "yellow", "active")
INERT_NEXT = (0, 4, 1, 1, 2, 4, 2, 0)
T = tuple(
((f if x == 0 else x ^ f) | ((INERT_NEXT[f] if x == 0 else x) << 3))
if x != 7 and 1 <= f <= 6 else 0
for x in range(8)
for f in range(8)
@Rexicon226
Rexicon226 / hash.zig
Last active July 14, 2026 06:35
ripemd-160 implementation in Zig, um please don't use this, it's a terrible hash function
const std = @import("std");
const rotl = std.math.rotl;
// zig fmt: off
fn f1(x: u32, y: u32, z: u32) u32 { return x ^ y ^ z; }
fn f2(x: u32, y: u32, z: u32) u32 { return (x & y) | (~x & z); }
fn f3(x: u32, y: u32, z: u32) u32 { return (x | ~y) ^ z; }
fn f4(x: u32, y: u32, z: u32) u32 { return (x & z) | (y & ~z); }
fn f5(x: u32, y: u32, z: u32) u32 { return x ^ (y | ~z); }
// zig fmt: on
import Mathlib
open Finset
-- The number of prisoners, exluding ourselves.
variable {K : ℕ}
/-- For a given ℕ, what prisoner is being interrogated at that time. -/
abbrev Schedule (K : ℕ) : Type := ℕ → Fin (K+1)
-- A schedule chosen by the warden.
@Rexicon226
Rexicon226 / reedsol_recover.zig
Last active June 8, 2026 04:27
AVX{2,512}+{,GFNI} reedsol recovery for k=64
const std = @import("std");
const builtin = @import("builtin");
const L = if (builtin.cpu.has(.x86, .avx512f)) 64 else 32;
const V = @Vector(L, u8);
const pshufb = @extern(*const fn (V, V) callconv(.c) V, .{ .name = switch (L) {
64 => "llvm.x86.avx512.pshuf.b.512",
32 => "llvm.x86.avx2.pshuf.b",
else => unreachable,
} }).*;
@Rexicon226
Rexicon226 / reedsol_encode.zig
Last active April 12, 2026 18:30
AVX512+GFNI reedsol encoding, for k=64
//! Efficient computation of 32 parity shreds for 32 data shreds.
//!
//! Based on the O(n log n) algorithm described in:
//! zS. -J. Lin, T. Y. Al-Naffouri, Y. S. Han and W. -H. Chung, "Novel
//! Polynomial Basis With Fast Fourier Transform and Its Application to
//! Reed–Solomon Erasure Codes," in IEEE Transactions on Information Theory,
//! vol. 62, no. 11, pp. 6284-6299, Nov. 2016, doi: 10.1109/TIT.2016.2608892.
//!
//! Given 32 data shreds, we want to produce 32 parity shreds such that any
//! 32 of the 64 total shreds can reconstruct the original data. The standard
@Rexicon226
Rexicon226 / kt.c
Last active February 4, 2026 19:49
Dead simple KangarooTwelve-128 implementation
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
// Chunk size for tree hashing
#define CHUNK_SIZE 8192
#define ALIGN __attribute__ ((aligned (128)))
#define MODE 128
@Rexicon226
Rexicon226 / keccak.zig
Last active February 2, 2026 14:55
AVX-512 Keccak (mainly for parallel, not serial)
const V = @Vector(8, u64);
fn keccak(bytes: []const u8, comptime r: u32, comptime delim: u8, out: *[r]u8) void {
var state: [144]u8 align(32) = @splat(0);
var x: [5]V = @splat(@splat(0));
var input = bytes;
const rsize = 200 - 2 * r;
comptime std.debug.assert(rsize == 72); // TODO
@Rexicon226
Rexicon226 / r43x6.zig
Created January 19, 2026 00:48
6 x 43-bit curve25519 implementation
const std = @import("std");
const V = @Vector(8, u64);
const S = @Vector(8, i64);
const u64x4 = @Vector(4, u64);
const avx512 = @import("src/curves/ed25519/avx512.zig");
extern fn @"llvm.x86.avx512.vpmadd52l.uq.512"(V, V, V) V;
extern fn @"llvm.x86.avx512.vpmadd52h.uq.512"(V, V, V) V;
@Rexicon226
Rexicon226 / pht.zig
Created January 4, 2026 10:29
Compile-time perfect hash table. Based off of: https://cmph.sourceforge.net/papers/esa09.pdf. Meant for low key amounts (<= ~32).
const std = @import("std");
/// A perfect hash table for N keys of the same `length`. Useful for public key maps.
fn pht(K: type, V: type, entries: []const struct { K, V }) type {
const LAMBDA = 5;
const table_len = entries.len;
const bucket_len = (table_len + LAMBDA - 1) / LAMBDA;
const window = 4;
const length = @typeInfo(std.meta.Child(K)).array.len;