Skip to content

Instantly share code, notes, and snippets.

=begin
Author: Jabari Zakiya, Original: 2009-12-25
Revision-2: 2009-12-31
Revision-3: 2010-6-2
Revision-4: 2010-12-15
Revision-5: 2011-5-11
Revision-6: 2011-5-15
Module 'Roots' provides two methods 'root' and 'roots'
which will find all the nth roots of real and complex
@jzakiya
jzakiya / SHA-1.F
Created January 16, 2013 03:26
Secure Hash Algorithm SHA-1 implemented in ANS Forth
\ ANS Forth implementation of Secure Hash Algorithm SHA-1
\ FIPS 180-4 spec at: http://csrc.nist.gov/publications/PubsFIPS.html
\ Also HMAC for SHA-1 as specified in FIPS PUB 198-1
\ Code accommodates Big and Little Endian, byte addressable CPUs.
\
\ ------------ This implementation is for 32-bit systems ------------
\
\ DEPENDENCIES: CORE EXT WORDSET ; COMMON USAGE: ?DO CELL-
\ Use of this code is free subject to acknowledgment of copyright.
\ Copyright (c) 2001-2013 Jabari Zakiya - [email protected] 2013/1/15
@jzakiya
jzakiya / SHA-256_224.F
Last active February 3, 2022 01:57
Secure Hash Algorithms SHA-256 and SHA-224 implemented in ANS Forth
\ ANS Forth code for Secure Hash Algorithms SHA-256 and SHA-224
\ FIPS 180-4 specs at: http://csrc.nist.gov/publications/PubsFIPS.html
\ Code accommodates Big and Little Endian, byte addressable CPUs.
\
\ ------------ This implementation is for 32-bit systems ------------
\
\ DEPENDENCIES: CORE EXT WORDSET ; COMMON USAGE: ?DO CELL-
\ Use of this code is free subject to acknowledgment of copyright.
\ Copyright (c) 2001-2013 Jabari Zakiya - [email protected] 2013/1/15
\
@jzakiya
jzakiya / SHA-384_512xxx.F
Last active December 11, 2015 04:19
Secure Hash Algorithms SHA-384, SHA-512, SHA-512/256, SHA-512/224 implemented in ANS Forth
\ ANS Forth code for 1024-bit block Secure Hash Algorithms
\ SHA-384, SHA-512, SHA-512/256, and SHA-512/224
\ FIPS 180-4 specs at: http://csrc.nist.gov/publications/PubsFIPS.html
\ Code accommodates Big and Little Endian, byte addressable CPUs.
\
\ ------------ This implementation is for 64-bit systems ------------
\
\ DEPENDENCIES: CORE EXT WORDSET ; COMMON USAGE: ?DO CELL-
\ Use of this code is free subject to acknowledgment of copyright.
\ Copyright (c) 2012-2013 Jabari Zakiya - [email protected] 2013-1-15
@jzakiya
jzakiya / ssozp5cpp.cpp
Created June 6, 2014 17:25
Source code for Segmented Sieve of Zakiya (SSoZ) for P5.
/*
This C++ source file will compile to an executable program to
perform the Segmented Sieve of Zakiya (SSoZ) to find primes <= N.
It is based on the P5 Strictly Prime (SP) Prime Generator.
Prime Genrators have the form: mod*k + ri; ri -> {1,r1..mod-1}
The residues ri are integers coprime to mod, i.e. gcd(ri,mod) = 1
For P5, mod = 2*3*5 = 30 and the number of residues are
rescnt = (2-1)(3-1)(5-1) = 8, which are {1,7,11,13,17,19,23,29}.
@jzakiya
jzakiya / ssozp7cpp.cpp
Created June 6, 2014 17:28
Source code for Segmented Sieve of Zakiya (SSoZ) for P7.
/*
This C++ source file will compile to an executable program to
perform the Segmented Sieve of Zakiya (SSoZ) to find primes <= N.
It is based on the P7 Strictly Prime (SP) Prime Generator.
Prime Genrators have the form: mod*k + ri; ri -> {1,r1..mod-1}
The residues ri are integers coprime to mod, i.e. gcd(ri,mod) = 1
For P7, mod = 2*3*5*7 = 210 and the number of residues are
rescnt = (2-1)(3-1)(5-1)(7-1) = 48, which are {1,11,13,17..209}.
@jzakiya
jzakiya / ssozp7cppmp.cpp
Created June 6, 2014 17:30
Source code for Segmented Sieve of Zakiya (SSoZ) for P7 w/OpenMP.
/*
This C++ source file will compile to an executable program to
perform the Segmented Sieve of Zakiya (SSoZ) to find primes <= N.
It is based on the P7 Strictly Prime (SP) Prime Generator.
Prime Genrators have the form: mod*k + ri; ri -> {1,r1..mod-1}
The residues ri are integers coprime to mod, i.e. gcd(ri,mod) = 1
For P7, mod = 2*3*5*7 = 210 and the number of residues are
rescnt = (2-1)(3-1)(5-1)(7-1) = 48, which are {1,11,13,17..209}.
@jzakiya
jzakiya / nthprimessozp5cpp.cpp
Created July 14, 2014 14:54
Find the nth prime (64-bit input/output) using a modified Segmented Sieve of Zakiya (SSoZ).
/*
This C++ source file will compile to an executable program to
find the nth prime. It's foundation is the Sieve of Zakiya, and it
performs the Segmented Sieve of Zakiya (SSoZ) to find primes <= N.
This version is based on the P5 Strictly Prime (SP) Prime Generator.
Prime Genrators have the form: mod*k + ri; ri -> {1,r1..mod-1}
The residues ri are integers coprime to mod, i.e. gcd(ri,mod) = 1
For P5, mod = 2*3*5 = 30 and the number of residues are
rescnt = (2-1)(3-1)(5-1) = 8, which are {1,7,11,13,17,19,23,29}.
@jzakiya
jzakiya / twinprimesssozp5cpp.cpp
Last active March 5, 2018 14:14
Twin Primes Generator (extremely fast, 64-bit input/output) using single threaded Segmented Sieve of Zakiya (SSoZ)
/*
This C++ source file is a single threaded implementation to perform an
extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
It is based on the P5 Strictly Prime (SP) Prime Generator.
Prime Genrators have the form: mod*k + ri; ri -> {1,r1..mod-1}
The residues ri are integers coprime to mod, i.e. gcd(ri,mod) = 1
For P5, mod = 2*3*5 = 30 and the number of residues are
rescnt = (2-1)(3-1)(5-1) = 8, which are {1,7,11,13,17,19,23,29}.
For just Twin Primes, use generator: Pn = 30*k + {11,13,17,19,29,31}
@jzakiya
jzakiya / AES.F
Last active August 29, 2015 14:07
ANS Forth code for AES (Advanced Encryption Standard)
0 [IF] AES Code Tutotorial
ANS FORTH code to implement the Advanced Encryption Standard (AES).
The National Institute of Standards and Technology (NIST) announced
on October 2, 2000 that Rijndael, created by Joan Daemen and Vincent
Rijmen, was selected as the AES algorithm to replace the old (since 1976)
Data Encryption Standard (DES). The formal AES specification is codified
in NIST (Federal Information Processing Standard) FIPS-197, url below: