Skip to content

Instantly share code, notes, and snippets.

View berenoguz's full-sized avatar

Beren Oguz berenoguz

  • Cambridge Mobile Telematics
  • Boston, MA
View GitHub Profile
@berenoguz
berenoguz / mersenne_twister.cpp
Last active April 10, 2016 20:44
Implementing Mersenne Twister 32 Algorithm for C++ templates
/**
* @challenge Implement Mersenne Twister 32 algorithm by only using C++ template variables
* @see https://gist.github.com/berenoguz/eae088a41f71696a7c11
* @usage You can use random integers for your templates (see above gist), so that behavior of templates will change every compilation
* @author Beren Oguz
* @copyright Copyright (c) 2015, Beren Oguz
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
@berenoguz
berenoguz / compilation_timestamp.hpp
Created January 7, 2015 18:12
Constexpr compilation UNIX timestamp
/**
* @challenge Implement a constexpr variable that has the Unix Timestamp of compilation time
* @see https://gist.github.com/berenoguz/a710b60f4a2fd2737fa3
* @usage Check above link: you can use constexpr Mersenne Twister 32 algorithm to have templates that change behavior in each compilation
* @author Beren Oguz
* @copyright Copyright (c) 2015, Beren Oguz
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
@berenoguz
berenoguz / wavelength_to_rgb.hpp
Last active July 30, 2020 15:53
Convert wavelength to RGB (red green blue) color
/**
* Wavelength to RGB color
* Algorithm from: http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
* @author Beren Oguz
* @copyright 2015, Beren Oguz
* @license This code is licensed with BSD-2 clause License
* Please see: http://opensource.org/licenses/BSD-2-Clause
*/
#include <cmath> // for std::trunc, std::round, std::pow
@berenoguz
berenoguz / ulam_spiral.cpp
Last active August 29, 2015 14:16
Ulam Spiral written to PNG
/**
* Ulam Spiral
* http://en.wikipedia.org/wiki/Ulam_spiral
* @author Beren Oguz
* @copyright 2015, Beren Oguz
* @license This code is licensed with BSD-2 clause License
* Please see: http://opensource.org/licenses/BSD-2-Clause
*/
#include <cstdint>
#include <cmath>
@berenoguz
berenoguz / multiport_represent_ports.py
Created January 24, 2017 00:27
A simple python function to return `iptables -m multiport` compatible shortest representation of a given list on integers.
# Author: Beren Oguz <beren@berkeley.edu>
# function: multiport_represent_ports
# input: arr, a list of port numbers, their type must be int
# output: shortest representation that can be passed to `iptables -m multiport`
def multiport_represent_ports(arr):
min_max = [[i, i] for i in range(len(arr))] # we keep track of min and max of ranges
port2id = dict() # port number -> array index
id2port = dict() # array index -> port number
@berenoguz
berenoguz / fft.py
Created February 18, 2017 08:15
The Fast Fourier Transform and The Fast Polynomial Multiplication Algorithms in Python 3
from cmath import exp
from math import pi
# A simple class to simulate n-th root of unity
# This class is by no means complete and is implemented
# merely for FFT and FPM algorithms
class NthRootOfUnity:
def __init__(self, n, k = 1):
self.k = k
self.n = n
@berenoguz
berenoguz / chacha20.c
Created July 29, 2017 22:16
A minimal, but functional implementation of ChaCha20 stream cipher (RFC 7539)
/**
* @author Beren Oguz <beren@berkeley.edu>
*
* @date 29 July 2017
*
* This is a minimal, but functional, implementation of ChaCha20 ciphering
* algorithm. Please read and understand RFC 7539 standard here:
* https://tools.ietf.org/html/rfc7539
* Please make sure you understand how the memory layout of input to ChaCha20
* works before using this function.
@berenoguz
berenoguz / risc.py
Created October 7, 2017 00:31
Super simple hex to bin utility
from sys import argv
l = 32
s = bin(int(argv[1], 16))[2:].zfill(l)
try:
i = 2
while argv[i]:
argv[i] = int(argv[i])
s = s[:l-argv[i]] + " " + s[l-argv[i]:]