Skip to content

Instantly share code, notes, and snippets.

@eu90h
eu90h / quicksort.rs
Created August 31, 2014 01:54
an implementation of quicksort in rustlang
fn choose_pivot(left: uint, right: uint) -> uint {
if left < right+1 {
std::rand::task_rng().gen_range(left, right+1)
} else {
left
}
}
fn partition(data: &mut[int], left: uint, right: uint) -> uint {
let pivotIndex: uint = choose_pivot(left, right);
@eu90h
eu90h / gist:d3f1de3f72ad03f1be56
Last active August 29, 2015 14:26
the value of values
(define (f x y)
(list (add1 x) (add1 y)))
(define (g x y)
(values (add1 x) (add1 y)))
(define x 1)
(define y -1)
(let* ([values (f x y)]
[new-x (first values)]
[new-y (second values)])
@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.
@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 / 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 / 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 / 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 / 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 / bst.rkt
Last active December 4, 2017 01:16
A binary search tree implementation in Racket
(struct bst-node (val left right) #:transparent #:mutable)
(define (bst-add tree value)
(if (null? tree) (bst-node value null null)
(let ([x (bst-node-val tree)])
(cond
[(= value x) tree]
[(< value x) (if (null? (bst-node-left tree))
(set-bst-node-left! tree (bst-node value null null))
(bst-add (bst-node-left tree) value))]
@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)))