Skip to content

Instantly share code, notes, and snippets.

View redwrasse's full-sized avatar

redwrasse

View GitHub Profile
@redwrasse
redwrasse / mc1.py
Created July 2, 2020 01:34
monte carlo integration of functions on subset of the real line
"""
Monte Carlo integration of functions on subsets of the real line,
using uniform probability distributions
"""
import math
import random
def montecarlo(f, g, a, b):
"""
@redwrasse
redwrasse / mock_refmon.go
Last active February 19, 2022 06:01
A mock reference monitor for accessing a top secret recipe
// A mock reference monitor design for a mock security system, enforcing the following access control policy:
// Only the user 'Alice' who identifies herself on requested inputs
// with her name and her birthday of '01.01.1980' is granted access to
// the top secret recipe file, stored on disk.
// This reference monitor design provides (some) security towards the given access policy
// by a) storing a hash rather than the values of the user inputs of (name, birthday) to compare against,
// and b) using a salt of the valid inputs as a symmetric key for AES encrypting the top secret recipe file beforehand and
// for decrypting before returning to Alice.
//
// Of course these comments would not be kept in the source code.
@redwrasse
redwrasse / insecure_deserialization.py
Created June 17, 2020 18:41
example deserialization attack
# insecure_deserialization.py
"""
as stated in the docs (https://docs.python.org/3/library/pickle.html):
'Warning The pickle module is not secure. Only unpickle data you trust.'
"""
import os
@redwrasse
redwrasse / fd_harmonic.py
Created June 12, 2020 23:07
finite difference for harmonic oscillator
import matplotlib.pyplot as plt
def hom1(f, x0, dx0, delta, n):
sol = [x0]
x1, dx = x0 + dx0 * delta, dx0
for _ in range(n-1):
x0, x1, dx = x1, f(x0) * delta + x0, 1.0 * (x1 - x0) / delta
sol.append(x0)
return sol
@redwrasse
redwrasse / redundant_enc.py
Created June 4, 2020 02:01
a redundant bits encoder
"""
a basic redundant bits encoder-decoder
"""
import json
import random
import sys
SAMPLE_PAYLOAD = {"foo": 1, "bar": 2}
FLIP_PROB = 0.05
@redwrasse
redwrasse / linear_ae_pca.py
Created May 28, 2020 19:23
A single-layer linear autoencoder is equivalent to principal component projection
"""
Program demonstration that a single-layer linear autoencoder
is equivalent to principal component projection
"""
import numpy as np
import tensorflow as tf
class A1(object):
@redwrasse
redwrasse / tictactoe.c
Created May 26, 2020 19:22
tictactoe minimax in c
#include <iostream>
#define SIZE 3
#define EMPTY 'e'
#define BIGNUM 100
#define MAXNUMSTATES 10000
enum Player {Player1 = 'x', Player2 = 'o'};
struct state {
@redwrasse
redwrasse / tictactoe_minimax.py
Last active May 23, 2020 02:35
tictactoe with minimax algorithm
import random
EMPTY = ' '
PLAYER1 = 'x'
PLAYER2 = 'o'
DIM = 3
def player_wins(state, player):
anycol = any(all(state[i][j] == player for i in range(DIM)) for j in range(DIM))
anyrow = any(all(state[j][i] == player for i in range(DIM)) for j in range(DIM))
@redwrasse
redwrasse / minimax.c
Created May 20, 2020 23:30
example minimax implementation
/*---------------------------------
minimax example
/ | \
/ \ / \ / | \
/\ /\ /\ /\ /\ /\ /\
2 7 8 1 4 5 9 0 3 6 5 1 2 1
becomes
7
@redwrasse
redwrasse / stack_computations.py
Created May 19, 2020 19:36
simple stack-based computations
# stack_computations.py
"""
Simple example stack computations
"""
"""
Stack diagram
f(n)
f(n-1)