Skip to content

Instantly share code, notes, and snippets.

@tscholl2
tscholl2 / aes.go
Last active March 29, 2024 11:06
simple AES encryption/decryption example with PBKDF2 key derivation in Go, Javascript, and Python
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
@tscholl2
tscholl2 / ec.svg
Last active July 13, 2023 11:15
plot of adding points on an elliptic curve
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tscholl2
tscholl2 / importCycles.js
Created March 20, 2017 18:07
find some cycles in typescript imports
const fs = require('fs');
const path = require('path');
const entry = path.resolve(process.argv.slice(2)[0]) || './index.ts';
const entryPath = path.dirname(entry);
/**
* Tries to find all imported file names in given file.
* @param {string} fileName
* @returns {string[]} importedFiles
*/
@tscholl2
tscholl2 / LZW.js
Last active August 19, 2020 14:38
Some types of compression in js
(() => {
const fcp = String.fromCodePoint;
const L = "length";
/**
* @param {Uint8Array} input
* @returns {Uint8Array}
*/
function compress(input) {
const codewords = [];
const dictionary = new Map();
@tscholl2
tscholl2 / main.go
Created August 17, 2020 16:05
AWS lambda example in Go
// Testrun with:
// go build *.go && docker run --rm -v "$PWD":/var/task lambci/lambda:go1.x main '{"prefix":"23727483927892"}'
// Deploy by:
// zip main.zip main
// upload main.zip to https://console.aws.amazon.com/lambda/home
package main
import (
"context"
def cm_method(q,t):
"""
Given a prime power q and integer t with |t| <= 2sqrt(q),
returns an Elliptic curve over GF(q) with q + 1 - t points.
"""
n = q + 1 - t
d = t^2 - 4*q
K = QuadraticField(d)
j = K.hilbert_class_polynomial().any_root()
E = EllipticCurve_from_j(GF(q)(j))
/**
* Given a sting s, return the SHA-256 digest of s
* (encoded as a UTF-16-LE byte array) in hex form.
* @param {string} s
* @returns {string}
*
* python -c 'import hashlib; print(hashlib.sha256("a🐦".encode("utf-16-le")).hexdigest())'
* 49f1ecba591ec4ae7a049570bd21b97dc77d42659f3fbe70a34a8f02ebacee17
*
* python -c 'import hashlib; print(hashlib.sha256("$€𐐷𤭢".encode("utf-16-le")).hexdigest())'
#include "linear-algebra.h"
#define mat_val(i, j) A[i * m + j]
#define swap_values(a, b) t = b, b = a, a = t
#define swap_rows(_i1, _i2) \
for (int k = 0; k < m; k++) \
swap_values(mat_val(_i1, k), mat_val(_i2, k));
int solve(Element *A, Element *b, int n, int m, Element *x)
{
const enum Direction {
L = 0,
R = 1,
}
const enum Alphabet {
A = 0,
B = 1,
}
type StateOutput = [
Alphabet, // write
@tscholl2
tscholl2 / Curve25519.sage
Last active October 27, 2019 13:20
list of Certicom curves
# Curve25519
p = 2^255 - 19
N = 8*(2^252 + 27742317777372353535851937790883648493)
E = EllipticCurve(GF(p),[0,486662,0,1,0])
G = E.lift_x(9)
assert E.count_points() == N
assert G.order() == N/8