{{ message }}

Instantly share code, notes, and snippets.

🏠
Working from home

🏠
Working from home
Created Dec 22, 2016
Javascript in SVG example (prime circles)
View prime-sieve.svg
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Last active Dec 22, 2020
Combine two hashcodes
View HashCodeHelper.cs
 public static class HashCodeHelper { public static int Combine(int h1, int h2) { uint num = (uint)((h1 << 5) | (int)((uint)h1 >> 27)); return ((int)num + h1) ^ h2; } }
Last active Dec 22, 2020
BigInteger ExtensionMethods
View BigIntegerExtensionMethods.cs
 public static class BigIntegerExtensionMethods { public static BigInteger Sum(this IEnumerable source) { return source.Aggregate((accumulator, current) => accumulator + current); } public static BigInteger Product(this IEnumerable source) { return source.Aggregate((accumulator, current) => accumulator * current);
Last active Dec 22, 2020
Sieve of Eratosthenes.
View Eratosthenes.cs
 public static class Eratosthenes { public static IEnumerable Sieve(Int64 maxValue) { if (maxValue < 10) { if (maxValue == 9 || maxValue == 8 || maxValue == 7) { return new List() { 2, 3, 5, 7 }; }
Last active Dec 22, 2020
PrimeFactory class (with caching). Required by Factorization class.
View PrimeFactory.cs
 public static class PrimeFactory { private static BigInteger _cacheLargestPrimeCurrently; private static BigInteger _cacheCeiling; internal static BigInteger[] _primeCache; static PrimeFactory() { _cacheCeiling = BigInteger.Pow(11, 7); _primeCache = new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
Last active Dec 22, 2020
Factorization class. Provides prime factorization, primality testing, limitless prime enumeration. Requires PrimeFactory class.
View Factorization.cs
 public static class Factorization { private static BigInteger _cacheCeiling; private static List _primeCache; private static BigInteger _cacheLargestPrimeCurrently; private static BigInteger[] _probablePrimeCheckBases; static Factorization() { _cacheCeiling = BigInteger.Pow(11, 8);
Created Dec 22, 2020
ANS1 parser for a RSA PrivateKey. Can also create a private key from just a P and Q by calculating the D, DP, DQ, and InverseQ. Create real private keys from small primes for CTFs, factoring challenges or the lulz.
View ANS1PrivateKey.cs
 public class ANS1PrivateKey : IDisposable { public BigInteger Modulus = BigInteger.MinusOne; public BigInteger Exponent = BigInteger.MinusOne; public BigInteger P = BigInteger.MinusOne; public BigInteger Q = BigInteger.MinusOne; public BigInteger D = BigInteger.MinusOne; public BigInteger DP = BigInteger.MinusOne; public BigInteger DQ = BigInteger.MinusOne; public BigInteger InverseQ = BigInteger.MinusOne;
Last active Dec 22, 2020
Cryptographically secure random number generator. Safe, secure, & correct implementation. Numbers are evenly distributed across entire range; avoids modulo bias. Internal buffers always cleared after every value returned--leaves nothing in memory. Singleton intended to be instantiated only once. Remember to call Dispose.
View CryptoRandomSingleton.cs
 /// /// Cryptographically secure random number generator. Features a safe, secure, and correct implementation. /// Numbers are evenly distributed across the whole range; avoids modulo bias. /// Internal buffers always cleared after every value returned--leaves nothing in memory. /// Singleton intended to be instantiated only once for the lifetime of the application. /// Remember to call Dispose when no longer needed. /// public static class CryptoRandomSingleton {
Last active Dec 22, 2020
NumberTheory functions, including Legendre Symbol, Tonelli-Shanks, Chinese Remainder Theorem, Modular Multiplicative Inverse, Primitive Root, Multiplicative Order, Discrete Logarithm, Discrete Root, Eulers Totient Phi, Carmichael Lambda Function, LCM, and GCD.
View NumberTheory.Maths.cs
 public static class Maths { /// /// Legendre Symbol returns 1 for a (nonzero) quadratic residue mod p, -1 for a non-quadratic residue (non-residue), or 0 on zero. /// public static int LegendreSymbol(BigInteger a, BigInteger p) { if (p < 2) throw new ArgumentOutOfRangeException(nameof(p), \$"Parameter '{nameof(p)}' must not be < 2, but you have supplied: {p}"); if (a == 0) return 0; if (a == 1) return 1;
Created Dec 22, 2020
A fast factorial implementation using the divide and conquer method.
View FastFactorial.cs
 public static class FastFactorial { public static BigInteger Factorial(BigInteger n) { return MultiplyRange(2, n); } private static BigInteger MultiplyRange(BigInteger from, BigInteger to) { var diff = to - from;