Skip to content

Instantly share code, notes, and snippets.

@ilknarf
ilknarf / 01 patience.py
Last active November 8, 2020 00:13
Patience is a Virtue: Python implementations of sort and merge algorithms
"""
Rough implementations of patience sorts (p3 sort) as shown in
https://www.microsoft.com/en-us/research/publication/patience-is-a-virtue-revisiting-merge-and-sort-on-modern-processors/.
Note that performance benchmarks won't be valid, since the data structures used (e.g. deque) don't take advantage of memory locality
in the same way that an implementation of cache-local linked memory blocks would.
"""
from collections import deque
from binary_search import binary_search
# Merkle Tree implementation in Python
from collections import deque
from hashlib import sha3_224
class MerkleNode:
def __init__(self, children: list = None, value: str = None):
assert not (children and value is not None) and (children or value is not None), 'requires either children or value'
@ilknarf
ilknarf / randomHashGenerator.js
Last active August 10, 2020 15:32
generate a random byte string with node's crypto.randomBytes
const crypto = require('crypto');
let bytes = crypto.randomBytes(16);
// using bit operations (unnecessary) to get individual digits
console.log([...bytes].map(v => (v >> 4).toString(16) + (v & 15).toString(16)).join(''));
// you can directly convert the byte to hexadecimal instead, making sure to pad digits.
console.log([...bytes].map(v => v.toString(16).padStart(2, '0')).join(''));
// even better, directly convert the buffer to hex
console.log(bytes.toString('hex'));
@ilknarf
ilknarf / rsa.js
Created August 7, 2020 00:45
Toy RSA Cryptosystem
/*
* The goal of this gist is to demonstrate how the RSA cryptosystem works
* with a toy implementation.
*/
// must have both primes to efficiently compute the private key.
function computePrivateKey(p1, p2, pub) {
// Use Euler's theorem to calculate phi(p1p2) => phi(p1) * phi(p2).
/*