Skip to content

Instantly share code, notes, and snippets.

@eu90h
eu90h / pyo3_bounded_mem.rs
Created March 15, 2024 15:52
A silly PyO3 example to demonstrate memory allocation.
#[pyfunction]
fn process_file(file: &PyAny, callback: &PyAny) {
assert!(callback.is_callable());
let file = file.to_string();
if let Ok(lines) = read_lines(file) {
for line in lines {
Python::with_gil(|py| {
let pool = unsafe { py.new_pool() };
let py = pool.python();
for msg in line.unwrap().replace("}{", "}\n{").split("\n") {
@eu90h
eu90h / low-ram.diff
Created March 9, 2024 16:35
This patch, intended for Mastodon v4.2 releases, changes some webpack settings so that I can actually build Mastodon on a VPS with low amounts of RAM.
diff --git a/config/webpack/production.js b/config/webpack/production.js
index cec810184..a5807830a 100644
--- a/config/webpack/production.js
+++ b/config/webpack/production.js
@@ -15,8 +15,9 @@ const sharedConfig = require('./shared');
const root = resolve(__dirname, '..', '..');
module.exports = merge(sharedConfig, {
+ parallelism: 1,
mode: 'production',
@eu90h
eu90h / ls-usb.fish
Created February 13, 2024 14:37
A fish shell script to list USB devices. (Yes, you can just use `lsusb`)
for f in (ls /sys/bus/usb/devices/*/product)
echo $f && cat $f
end
@eu90h
eu90h / string.cpp
Last active April 9, 2017 04:27
Example program used to examine string layouts for this article: https://eu90h.github.io/cpp-strings.html
// Compile with -O3
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
string greeting("Hello, ");
string name = argv[1];
cout << greeting.append(name) << endl;
return 0;
@eu90h
eu90h / rand.rkt
Last active September 17, 2015 02:07
OS-based rng access
#lang racket/base
(require racket/contract/base "unix_rand.rkt" "windows_rand.rkt" (only-in file/sha1 bytes->hex-string))
(provide (contract-out [crypto-random-bytes (-> exact-positive-integer? (or/c bytes? string?))]
[crypto-random (-> exact-positive-integer? exact-positive-integer?)]))
(define bytes->number (compose (lambda (s) (string->number s 16)) bytes->hex-string))
; (: crypto-random-bytes (-> Positive-Integer Bytes))
; returns n random bytes from the os.
(define (crypto-random-bytes n)
@eu90h
eu90h / openssl-gen-prime.rkt
Last active September 15, 2015 21:08
Generating primes with OpenSSL & Racket
#lang racket/base
(require ffi/unsafe
racket/runtime-path
(for-syntax racket/base)
"libcrypto.rkt")
(define _BIGNUM-ptr (_cpointer/null 'BIGNUM))
(define _BN_CTX-ptr (_cpointer/null 'BN_CTX))
(define _BN_GENCB-ptr (_cpointer/null 'BN_GENCB))
@eu90h
eu90h / fns
Created September 13, 2015 23:48
returning named functions
(define (make-adder increment)
(define (adder x)
(+ x increment))
adder)
(define adder (make-adder 2))
(adder 1)
@eu90h
eu90h / neumann_extractor.rkt
Last active June 26, 2023 14:13
von Neumann extractor
#lang racket
; An implementation of the von Neumann unbiasing algorithm.
; The unbias procedure takes a pseudo-random bit generator and extracts entropy from the sequence of generated bits
; thereby creating a "more random" sequence.
; The catch here is that the pairs 01 and 10 have to be equally likely, and there can't be correlation between successive bits.
(define (extract-entropy bit-generator)
(let ([x (bit-generator)]
[y (bit-generator)])
(cond [(or (and (= x 1) (= y 1))
(and (= x 0) (= y 0)))
@eu90h
eu90h / merkle-tree.rkt
Created September 2, 2015 00:34
Merkle tree implementation in Racket
#lang racket
(require openssl/sha1)
(struct node (value left right) #:transparent)
(define (hash s)
(sha1 (open-input-string s)))
(define (build-foundation l [hash hash])
(let loop ([l l])
@eu90h
eu90h / diffie-hellman.rkt
Last active September 1, 2015 22:17
Diffie-Hellman key exchange in Racket
#lang racket
; groups.rkt is located in the repository https://github.com/eu90h/elgamal-signature
(require math "groups.rkt")
(define (diffie-hellman in out [p ike-2048] [g 2])
; Alice uniformly chooses a random integer a s.t. 1 <= a < p - 1
; and then computes A := g^a mod p
(let* ([a (random-integer 1 (- p 1))]
[A (modular-expt g a p)])
; Alice then sends A to her interlocutor Bob.