commit cc4d92205ff60f9ec7d0cd5382cc0abdedd490d9 Author: Bashar DUDIN Date: Mon Nov 25 17:18:51 2019 +0100 Repository's initial state. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29f1024 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +*.annot +*.cmo +*.cma +*.cmi +*.a +*.o +*.cmx +*.cmxs +*.cmxa + +# ocamlbuild working directory +_build/ + +# ocamlbuild targets +*.byte +*.native + +# oasis generated files +setup.data +setup.log + +# Merlin configuring file for Vim and Emacs +.merlin + +# Dune generated files +*.install + +# Local OPAM switch +_opam/ + +# Local files +*~ \ No newline at end of file diff --git a/Source/builtin/basic_arithmetics.ml b/Source/builtin/basic_arithmetics.ml new file mode 100644 index 0000000..ec0fd46 --- /dev/null +++ b/Source/builtin/basic_arithmetics.ml @@ -0,0 +1,23 @@ +(** Basic arithmetics with built-in integers *) + +open Builtin + +(* Greater common divisor and smaller common multiple + implemetations. +*) + +(** Greater common (positive) divisor of two non-zero integers. + @param a non-zero integers + @param b non-zero integer +*) +let rec gcd a b = 0 + +(* Extended Euclidean algorithm. Computing Bezout Coefficients. *) + +(** Extended euclidean division of two integers NOT OCAML DEFAULT. + Given non-zero entries a b computes triple (u, v, d) such that + a*u + b*v = d and d is gcd of a and b. + @param a non-zero integer + @param b non-zero integer. +*) +let bezout a b = (0, 0, 0) diff --git a/Source/builtin/basic_arithmetics.mli b/Source/builtin/basic_arithmetics.mli new file mode 100644 index 0000000..e969bc2 --- /dev/null +++ b/Source/builtin/basic_arithmetics.mli @@ -0,0 +1,15 @@ +(** Basic arithmetics with built-in integers *) + +(** Greater common divisor (positive) of two non-zero integers. + @param a non-zero integers + @param b non-zero integer +*) +val gcd : int -> int -> int + +(** Extended euclidean division of two integers NOT OCAML DEFAULT. + Given non-zero entries a b computes triple (u, v, d) such that + a*u + b*v = d and d is gcd of a and b. + @param a non-zero integer + @param b non-zero integer. +*) +val bezout : int -> int -> (int * int * int) diff --git a/Source/builtin/break_ciphers.ml b/Source/builtin/break_ciphers.ml new file mode 100644 index 0000000..3688b1d --- /dev/null +++ b/Source/builtin/break_ciphers.ml @@ -0,0 +1,9 @@ +(** Factoring Built-In Int Primes *) + +open Builtin +open Basic_arithmetics + +(** Factors product of two primes. + @param key is public key of an RSA cryptosystem. + *) +let break key = (0, 0) diff --git a/Source/builtin/break_ciphers.mli b/Source/builtin/break_ciphers.mli new file mode 100644 index 0000000..61de776 --- /dev/null +++ b/Source/builtin/break_ciphers.mli @@ -0,0 +1,7 @@ +(** Factoring Built-In Int Primes *) + +(** Factors product of two primes. + @param key is public key of an RSA cryptosystem. + *) +val break : int * 'a -> (int * int) +;; diff --git a/Source/builtin/builtin.ml b/Source/builtin/builtin.ml new file mode 100644 index 0000000..e45a167 --- /dev/null +++ b/Source/builtin/builtin.ml @@ -0,0 +1,51 @@ +(** Tweaking OCaml built-in euclidean division + +The OCaml built-in euclidian divisions operations do not follow the +standard mathematical conventions. We adapt OCaml available +primitives to suit maths conventions. + + **) + +(** Sign function + @param x integer +*) +let sign x = 0 + +(* Integer quotient implementation ; main use is in case of quotient + of an integer by a natural number. + *) + +(** Quotient of an integer by a natural number. + This is the quotient in euclidiant division sense. + @param a dividend + @param b natural number you divide by. + *) +let quot a b = 0 + +(* Integer modulo implementations. Negative case need be taken into + account ; representant is expected non-negative. This is not OCAML + default. + *) + +(** Modulo of two integers. + Following Euclidean division. NOT OCAML DEFAULT. Positive integer + between 0 (included) and modulo (excluded) resulting from euclidian + division of entry by modulo. + + @param a input integer + @param b moduli a natural number. + *) +let modulo a b = 0 + +(* Integer modulo implementations. Negative case need be taken into + account ; representant is expected non-negative. This is not OCAML + default. + *) + +(** Division of an integer by a natural number. NOT OCAML DEFAULT. + Division of an integer by a non-zero integer b is the unique couple + of integers (q, r) such that a = b*q + r and r is in [0, abs b[. + @param a dividend + @param b integer you divide by. +*) +let div a b = (0, 0) diff --git a/Source/builtin/builtin.mli b/Source/builtin/builtin.mli new file mode 100644 index 0000000..e3ef94b --- /dev/null +++ b/Source/builtin/builtin.mli @@ -0,0 +1,38 @@ +(** Tweaking OCaml built-in euclidean division + +The OCaml built-in euclidian divisions operations do not follow the +standard mathematical conventions. We adapt OCaml available +primitives to suit maths conventions. + + **) + +(** Sign function + @param x integer +*) +val sign : int -> int + +(** Quotient of two natural numbers. + This is the quotient in euclidiant division sense. + @param a dividend + @param b natural number you divide by. + *) +val quot : int -> int -> int + +(** Modulo of two integers. + Following Euclidean division. NOT OCAML DEFAULT. Positive integer + between 0 (included) and modulo (excluded) resulting from euclidian + division of entry by modulo. + + @param a input integer + @param b moduli a natural number. + *) +val modulo : int -> int -> int + +(** Division of an integer by a natural number. NOT OCAML DEFAULT. + Division of an integer by a non-zero integer b is the unique couple + of integers (q, r) such that a = b*q + r and r is in interval 0, abs b + where left bound only is not included. + @param a dividend + @param b integer you divide by. +*) +val div : int -> int -> (int*int) diff --git a/Source/builtin/ciphers.ml b/Source/builtin/ciphers.ml new file mode 100644 index 0000000..38d363b --- /dev/null +++ b/Source/builtin/ciphers.ml @@ -0,0 +1,74 @@ +(** Ciphers + Built-in integer based ciphers. +*) + +open Builtin +open Basic_arithmetics +open Power + +(********** Cesar Cipher **********) + +(** Cesar's cipher encryption + @param k is an integer corresponding to key + @param m word to cipher. + @param b base ; for ASCII codes should be set to 255. + *) +let encrypt_cesar k m b = [] + +(** Cesar's cipher decryption + @param k is an integer corresponding to key + @param m encrypted word. + @param b base ; for ASCII code should be set to 255. + *) +let decrypt_cesar k m b = [] + +(********** RSA Cipher **********) + +(** Generate an RSA ciphering keys. + Involved prime numbers need to be distinct. Output is a couple + of public, private keys. + @param p prime number + @param q prime number +*) +let generate_keys_rsa p q = ((0,0), (0,0)) + + +(** Encryption using RSA cryptosystem. + @param m integer hash of message + @param pub_key a tuple (n, e) composing public key of RSA cryptosystem. + *) +let encrypt_rsa m (n, e) = 0 + +(** Decryption using RSA cryptosystem. + @param m integer hash of encrypter message. + @param pub_key a tuple (n, d) composing private key of RSA cryptosystem. + *) +let decrypt_rsa m (n , d) = 0 +;; + +(********** ElGamal Cipher **********) + +(** Generate ElGamal public data. Generates a couple (g, p) + where p is prime and g having high enough order modulo p. + @param p is prime having form 2*q + 1 for prime q. + *) +let rec public_data_g p = (0, 0) + +(** Generate ElGamal public data. + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +let generate_keys_g (g, p) = (0, 0) + +(** ElGamal encryption process. + @param msg message to be encrypted. + @param pub_data a tuple (g, p) of ElGamal public data. + @param kA ElGamal public key. + *) +let encrypt_g msg (g, p) kA = (0, 0) + +(** ElGamal decryption process. + @param msg a tuple (msgA, msgB) forming an encrypted ElGamal message. + @param a private key + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +let decrypt_g (msgA, msgB) a (g, p) = 0 diff --git a/Source/builtin/ciphers.mli b/Source/builtin/ciphers.mli new file mode 100644 index 0000000..0678126 --- /dev/null +++ b/Source/builtin/ciphers.mli @@ -0,0 +1,77 @@ +(** Ciphers + Built-in integer based ciphers. +*) + +(********** Cesar Cipher **********) + +(** Cesar's cipher encryption + @param k is an integer corresponding to key + @param m word to cipher. + @param b base ; for ASCII codes should be set to 255. + *) +val encrypt_cesar : int -> (int list) -> int -> (int list) +;; + +(** Cesar's cipher decryption + @param k is an integer corresponding to key + @param m encrypted word. + @param b base ; for ASCII code should be set to 255. + *) +val decrypt_cesar : int -> (int list) -> int -> (int list) +;; + +(********** RSA Cipher **********) + +(** Generate an RSA ciphering keys. + Involved prime numbers need to be distinct. Output is a couple + of public, private keys. + @param p prime number + @param q prime number +*) +val generate_keys_rsa : int -> int -> ((int * int) * (int * int)) +;; + +(** Encryption using RSA cryptosystem. + @param m integer hash of message + @param pub_key a tuple (n, e) composing public key of RSA cryptosystem. + *) +val encrypt_rsa : int -> (int * int) -> int +;; + +(** Decryption using RSA cryptosystem. + @param m integer hash of encrypter message. + @param pub_key a tuple (n, d) composing private key of RSA cryptosystem. + *) +val decrypt_rsa : int -> (int * int) -> int +;; + +(********** ElGamal Cipher **********) + +(** Generate ElGamal public data. Generates a couple (g, p) + where p is prime and g primitive root in F_p. + @param p is prime having form 2*q + 1 for prime q. + *) +val public_data_g : int -> (int * int) +;; + +(** Generate ElGamal public and private keys. + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +val generate_keys_g : (int * int) -> (int * int) +;; + +(** ElGamal encryption process. + @param msg message to be encrypted. + @param pub_data a tuple (g, p) of ElGamal public data. + @param kA ElGamal public key. + *) +val encrypt_g : int -> (int * int) -> int -> (int * int) +;; + +(** ElGamal decryption process. + @param msg a tuple (msgA, msgB) forming an encrypted ElGamal message. + @param a private key + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +val decrypt_g : (int * int) -> int -> (int * int) -> int +;; diff --git a/Source/builtin/encoding_msg.ml b/Source/builtin/encoding_msg.ml new file mode 100644 index 0000000..3ff3ad0 --- /dev/null +++ b/Source/builtin/encoding_msg.ml @@ -0,0 +1,19 @@ +(** Encoding Strings *) + +open Builtin +open Basic_arithmetics +open Power + +(** Encode a string containing ASCII characters. + @param str is a string representing message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +let encode str bits = 0 + +(** Decode a string containing ASCII characters. + @param msg is an integer representing an encoded message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +let decode msg bits = "" diff --git a/Source/builtin/encoding_msg.mli b/Source/builtin/encoding_msg.mli new file mode 100644 index 0000000..fb5b19d --- /dev/null +++ b/Source/builtin/encoding_msg.mli @@ -0,0 +1,15 @@ +(** Encoding Strings *) + +(** Encode a string containing ASCII characters. + @param str is a string representing message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +val encode : string -> int -> int + +(** Decode a string containing ASCII characters. + @param msg is an integer representing an encoded message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +val decode : int -> int -> string diff --git a/Source/builtin/generate_primes.ml b/Source/builtin/generate_primes.ml new file mode 100644 index 0000000..2d3895f --- /dev/null +++ b/Source/builtin/generate_primes.ml @@ -0,0 +1,90 @@ +(** Generating primes *) + +open Builtin +open Basic_arithmetics + +(* Initializing list of integers for eratosthenes's sieve. Naive + version. +*) + +(** List composed of 2 and then odd integers starting at 3. + @param n number of elements in the list of integers. + *) +let init_eratosthenes n = [] + +(* Eratosthenes sieve. *) + +(** Eratosthene sieve. + @param n limit of list of primes, starting at 2. +*) +let eratosthenes n = [] + +(* Write and read into file functions for lists. *) + +(** Write a list into a file. Element seperator is newline. + @param file path to write to. + *) +let write_list li file = () + +(** Write a list of prime numbers up to limit into a txt file. + @param n limit of prime numbers up to which to build up a list of primes. + @param file path to write to. +*) +let write_list_primes n file = () + +(** Read file safely ; catch End_of_file exception. + @param in_c input channel. + *) +let input_line_opt in_c = + try Some (input_line in_c) + with End_of_file -> None + +(** Create a list out of reading a line per line channel. + @param in_c input channel. + *) +let create_list in_c = () + +(** Load list of primes into OCaml environment. + @param file path to load from. + *) +let read_list_primes file = [] + +(* Auxiliary functions to extract big prime numbers for testing + purposes. + *) + +(** Get biggest prime. + @param l list of prime numbers. + *) +let rec last_element l = match l with + | [] -> failwith "Builtin.generate_primes.last_element: Your list \ + is empty. " + | e::[] -> e + | h::t -> last_element t + +(** Get two biggest primes. + @param l list of prime numbers. + *) +let rec last_two l = match l with + | [] | [_] -> failwith "Builtin.generate_primes.last_two: List has \ + to have at least two prime numbers." + | e::g::[] -> (e, g) + | h::t -> last_two t +;; + +(* Generating couples of prime numbers for specific or fun + purposes. + *) + +(** Finding couples of primes where second entry is twice the first + plus 1. + @param limit positive integer bounding searched for primes. + @param isprime function testing for (pseudo)primality. + *) +let double_primes limit isprime = [] + +(** Finding twin primes. + @param limit positive integer bounding searched for primes. + @param isprime function testing for (pseudo)primality. + *) +let twin_primes limit isprime = [] diff --git a/Source/builtin/generate_primes.mli b/Source/builtin/generate_primes.mli new file mode 100644 index 0000000..da8a7ef --- /dev/null +++ b/Source/builtin/generate_primes.mli @@ -0,0 +1,35 @@ +(** Generating primes *) + +(** List composed of 2 and then odd integers starting at 3. + @param n number of elements in the list of integers. + *) +val init_eratosthenes : int -> (int list) + +(** Eratosthene sieve. + @param n limit of list of primes, starting at 2. +*) +val eratosthenes : int -> (int list) + +(** Write a list of prime numbers up to limit into a txt file. + @param n limit of prime numbers up to which to build up a list of primes. + @param file path to write to. +*) +val write_list_primes : int -> string -> unit + +(** Load list of primes into OCaml environment. + @param file path to load from. + *) +val read_list_primes : string -> (int list) + +(** Finding couples of primes where second entry is twice the first + plus 1. + @param limit positive integer bounding searched for primes. + @param isprime function testing for (pseudo)primality. + *) +val double_primes : int -> (int -> bool) -> (int * int) list + +(** Finding twin primes. + @param limit positive integer bounding searched for primes. + @param isprime function testing for (pseudo)primality. + *) +val twin_primes : int -> (int -> bool) -> (int * int) list diff --git a/Source/builtin/power.ml b/Source/builtin/power.ml new file mode 100644 index 0000000..cbd3888 --- /dev/null +++ b/Source/builtin/power.ml @@ -0,0 +1,42 @@ +(** Power function implementations for built-in integers *) + +open Builtin +open Basic_arithmetics + +(* Naive and fast exponentiation ; already implemented in-class. + *) + +(** Naive power function. Linear complexity + @param x base + @param n exponent + *) +let pow x n = 0 + +(** Fast integer exponentiation function. Logarithmic complexity. + @param x base + @param n exponent + *) +let power x n = 0 + +(* Modular expnonentiation ; modulo a given natural number smaller + max_int we never have integer-overflows if implemented properly. + *) + +(** Fast modular exponentiation function. Logarithmic complexity. + @param x base + @param n exponent + @param m modular base + *) +let mod_power x n m = 0 + +(* Making use of Fermat Little Theorem for very quick exponentation + modulo prime number. + *) + +(** Fast modular exponentiation function mod prime. Logarithmic complexity. + It makes use of the Little Fermat Theorem. + @param x base + @param n exponent + @param p prime modular base + *) +let prime_mod_power x n p = 0 diff --git a/Source/builtin/power.mli b/Source/builtin/power.mli new file mode 100644 index 0000000..405885b --- /dev/null +++ b/Source/builtin/power.mli @@ -0,0 +1,28 @@ +(** Power function implementations for built-in integers *) + +(** Naive power function. Linear complexity + @param x base + @param n exponent + *) +val pow : int -> int -> int + +(** Fast integer exponentiation function. Logarithmic complexity. + @param x base + @param n exponent + *) +val power : int -> int -> int + +(** Fast modular exponentiation function. Logarithmic complexity. + @param x base + @param n exponent + @param m modular base + *) +val mod_power : int -> int -> int -> int + +(** Fast modular exponentiation function mod prime. Logarithmic complexity. + It makes use of the Little Fermat Theorem. + @param x base + @param n exponent + @param p prime modular base + *) +val prime_mod_power : int -> int -> int -> int diff --git a/Source/builtin/test_primes.ml b/Source/builtin/test_primes.ml new file mode 100644 index 0000000..9caa325 --- /dev/null +++ b/Source/builtin/test_primes.ml @@ -0,0 +1,14 @@ +(** Testing for primality *) + +open Builtin +open Basic_arithmetics +open Power + +(** Deterministic primality test *) +let is_prime n = true + +(** Pseudo-primality test based on Fermat's Little Theorem + @param p tested integer + @param testSeq sequence of integers againt which to test + *) +let is_pseudo_prime p test_seq = true diff --git a/Source/builtin/test_primes.mli b/Source/builtin/test_primes.mli new file mode 100644 index 0000000..e35da42 --- /dev/null +++ b/Source/builtin/test_primes.mli @@ -0,0 +1,14 @@ +(** Testing for primality *) + +(** Deterministic primality test + @param n integer bigger or equal to 2. +*) +val is_prime : int -> bool +;; + +(** Pseudo-primality test based on Fermat's Little Theorem + @param p tested integer + @param testSeq sequence of integers againt which to test + *) +val is_pseudo_prime : int -> (int list) -> bool +;; diff --git a/Source/builtin/tests/test_builtin.ml b/Source/builtin/tests/test_builtin.ml new file mode 100644 index 0000000..e4af3c7 --- /dev/null +++ b/Source/builtin/tests/test_builtin.ml @@ -0,0 +1,27 @@ +(** Test suites for builtin builtin ml file using oUnit. *) + +open OUnit2 +open Builtin +open Test_builtin_templates + +let () = let t_list = [(1, 1); (-1, -1); (0, 1)] in + run_test template_1_1 "Sign Function" sign t_list +;; + +let () = let t_list = [(10, 3), 3; (-10, 3), -4; + (10, 2), 5; (-10, 2), -5] + in + run_test template_2_1 "Quot Function" quot t_list +;; + +let () = let t_list = [(10, 3), 1; (10, 2), 0; + (10, 2), 0; (-10, 2), 0] + in + run_test template_2_1 "Modulo Function" modulo t_list +;; + +let () = let t_list = [(10, 3), (3, 1); (-10, 3), (-4, 2); + (10, 2), (5, 0); (-10, 2), (-5, 0)] + in + run_test template_2_2 "Div Function" div t_list +;; diff --git a/Source/builtin/tests/test_builtin_basic_arithmetics.ml b/Source/builtin/tests/test_builtin_basic_arithmetics.ml new file mode 100644 index 0000000..48437fd --- /dev/null +++ b/Source/builtin/tests/test_builtin_basic_arithmetics.ml @@ -0,0 +1,16 @@ +(** Test suites for builtin basic_arithmetic ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Test_builtin_templates + +let () = let t_list = [(32, 6), 2; (18, 12), 6; (-18, -12), 6] in + run_test template_2_1 "GCD Function" gcd t_list +;; + +let () = let t_list = [(18, 22), (5, -4, 2); (22, 18), (-4, 5, 2); + (17, 21), (5, -4, 1); (21, 17), (-4, 5, 1)] + in + run_test template_2_3 "Bezout Function" bezout t_list +;; diff --git a/Source/builtin/tests/test_builtin_break_ciphers.ml b/Source/builtin/tests/test_builtin_break_ciphers.ml new file mode 100644 index 0000000..8866aca --- /dev/null +++ b/Source/builtin/tests/test_builtin_break_ciphers.ml @@ -0,0 +1,14 @@ +(** Test suites for builtin break_cifers ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Break_ciphers +open Test_builtin_templates + +(* Only tests for RSA for now. *) + +let () = let t_list = [((99400891, 36199003), (9967, 9973))] + in + run_test template_cple_2 "Break Cifer RSA Test" break t_list +;; diff --git a/Source/builtin/tests/test_builtin_ciphers.ml b/Source/builtin/tests/test_builtin_ciphers.ml new file mode 100644 index 0000000..9a4f5e6 --- /dev/null +++ b/Source/builtin/tests/test_builtin_ciphers.ml @@ -0,0 +1,71 @@ +(** Test suites for builtin cifers ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Power +open Ciphers +open Test_builtin_templates + + +let str2list_f str f = + let rec __str2list i = + if i >= String.length str then + [] + else + (f str.[i])::__str2list (i + 1) + in + __str2list 0 + +;; + +let str2list str = + str2list_f str (function x -> Char.code x) +;; + +let () = let t_list = [((2, [2; 3; 6], 10), [4; 5; 8]); + ((0, str2list "hello", 255), str2list "hello"); + ((2, str2list "ABC", 255), str2list "CDE"); + ((-1, str2list "xyz", 255), str2list "wxy")] + in + run_test template_1L1_L "Encrypt Cesar Test" encrypt_cesar t_list +;; + +let () = let t_list = [((2, [4; 5; 8], 10), [2; 3; 6]); + ((0, str2list "hello", 255), str2list "hello"); + ((2, str2list "CDE", 255), str2list "ABC"); + ((-1, str2list "wxy", 255), str2list "xyz")] + in + run_test template_1L1_L "Decrypt Cesar Test" decrypt_cesar t_list +;; + +let p = 9967 and q = 9973 +let ((_, e), (n, d)) = generate_keys_rsa p q + +let phin = (p-1) * (q-1) +let is_inverse x y n = modulo ((modulo x n) * (modulo y n)) n = 1 +let () = let t_list = [(e, d, phin), true] + in + run_test template_3_b "Generate RSA Keys Test" is_inverse t_list +;; + +let () = let t_list = [((281237, (99400891, 36199003)), 70133953)] + in + run_test template_12_1 "Encrypt RSA Test" encrypt_rsa t_list +;; + +let () = let t_list = [((70133953, (99400891, 30869683)), 281237)] + in + run_test template_12_1 "Decrypt RSA Test" decrypt_rsa t_list +;; + +(* Test for ElGamal *) + +let (g, p) = public_data_g 100000007 ;; +let (pub, priv) = generate_keys_g (g, p) ;; +let (g_k, xA_k) = encrypt_g 42 (g, p) pub ;; + +let () = let t_list = [((g_k, xA_k), priv, (g, p)), 42]; + in + run_test template_212_1 "Decrypt ElGamal Keys" decrypt_g t_list +;; diff --git a/Source/builtin/tests/test_builtin_encoding_msg.ml b/Source/builtin/tests/test_builtin_encoding_msg.ml new file mode 100644 index 0000000..6a44c98 --- /dev/null +++ b/Source/builtin/tests/test_builtin_encoding_msg.ml @@ -0,0 +1,18 @@ +(** Test suites for builtin encoding_msg ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Power +open Encoding_msg +open Test_builtin_templates + +let () = let t_list = [(("Bashar", 7), 2294023860466)] + in + run_test template_s1_1 "Encode Function" encode t_list +;; + +let () = let t_list = [((2294023860466, 7), "Bashar")] + in + run_test template_2_s "Decode Function" decode t_list +;; diff --git a/Source/builtin/tests/test_builtin_generate_primes.ml b/Source/builtin/tests/test_builtin_generate_primes.ml new file mode 100644 index 0000000..6be78ae --- /dev/null +++ b/Source/builtin/tests/test_builtin_generate_primes.ml @@ -0,0 +1,32 @@ +(** Test suites for builtin power ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Test_primes +open Generate_primes +open Test_builtin_templates + + +let () = let t_list = [(2, [2]); (3, [2; 3]); (6, [2; 3; 5])] in + run_test template_1_L "Initialization list for eratosthenes" init_eratosthenes t_list +;; + +let () = let t_list = [(2, [2]); + (3, [2; 3]); + (6, [2; 3; 5]); + (25, [2; 3; 5; 7; 11; 13; 17; 19; 23]) + ] + in + run_test template_1_L "Erathosthenes Sieve" eratosthenes t_list +;; + +let () = let t_list = [((20, is_prime), [(2, 5); (3, 7); (5, 11); (11, 23)])] + in + run_test template_1f_L2 "Double Primes Generator" double_primes t_list +;; + +let () = let t_list = [((20, is_prime), [(2, 3); (3, 5); (5, 7); (11, 13); (17, 19)])] + in + run_test template_1f_L2 "Twin Primes Generator" twin_primes t_list +;; diff --git a/Source/builtin/tests/test_builtin_power.ml b/Source/builtin/tests/test_builtin_power.ml new file mode 100644 index 0000000..ee5000b --- /dev/null +++ b/Source/builtin/tests/test_builtin_power.ml @@ -0,0 +1,35 @@ +(** Test suites for builtin power ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Power +open Test_builtin_templates + +let () = let t_list = [((-1, 12), 1); ((-1, 11), -1); ((0, 2), 0); + ((3, 1), 3); ((5, 0), 1); ((-2, 2), 4); + ((-2, 3), -8); ((2, 5), 32); ((3, 3), 27)] + in + run_test template_2_1 "Pow Function" pow t_list +;; + +let () = let t_list = [((-1, 12), 1); ((-1, 11), -1); ((0, 2), 0); + ((3, 1), 3); ((5, 0), 1); ((-2, 2), 4); + ((-2, 3), -8); ((2, 5), 32); ((3, 3), 27)] + in + run_test template_2_1 "Power Function" power t_list +;; + +let () = let t_list = [((-1, 12, 10), 1); ((-1, 11, 11), 10); ((0, 2, 3), 0); + ((3, 1, 3), 0); ((5, 0, 2), 1); ((-2, 2, 5), 4); + ((-2, 3, 9), 1); ((2, 5, 17), 15); ((3, 3, 17), 10)] + in + run_test template_3_1 "Modular Power Function" mod_power t_list +;; + +let () = let t_list = [((-1, 12, 7), 1); ((-1, 11, 11), 10); ((0, 2, 3), 0); + ((3, 1, 3), 0); ((5, 0, 2), 1); ((-2, 2, 5), 4); + ((-2, 3, 5), 2); ((2, 5, 17), 15); ((3, 3, 17), 10)] + in + run_test template_3_1 "Power Modulo Prime Function" prime_mod_power t_list +;; diff --git a/Source/builtin/tests/test_builtin_templates.ml b/Source/builtin/tests/test_builtin_templates.ml new file mode 100644 index 0000000..3e29ee2 --- /dev/null +++ b/Source/builtin/tests/test_builtin_templates.ml @@ -0,0 +1,126 @@ +open OUnit2 + +(* Formatting combination of tuples of tuples. *) +let format_2 out_c (x, y) = + Printf.sprintf "(%i, %i)" x y +let format_3 out_c (x, y, z) = + Printf.sprintf "(%i, %i, %i)" x y z +let format_12 out_c (x, y_2) = + Printf.sprintf "(%i, %a)" x format_2 y_2 +let format_22 out_c (x_2, y_2) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_2 y_2 + +(* Formatting lists.*) +let format_L out_c li = + let rec __format li = + match li with + | [] -> "" + | [h] -> string_of_int h + | h::t -> (string_of_int h) ^ "; " ^ __format t + in + "[" ^ (__format li) ^ "]" +let format_L2 out_c li_2 = + let rec __format li_2 = + match li_2 with + | [] -> "" + | [(h1, h2)] -> Printf.sprintf "(%i, %i)" h1 h2 + | (h1, h2)::t -> (Printf.sprintf "(%i, %i)" h1 h2) ^ "; " ^ (__format t) + in + "[" ^ (__format li_2) ^ "]" + +(* Formatting input-output combinations of tested functions. + Essentially creating aliases. *) +let format_1_1 = format_2 +let format_2_1 out_c (x_2, t) = + Printf.sprintf "(%a, %i)" format_2 x_2 t +let format_2_2 = format_22 +let format_2_3 out_c (x_2, t_3) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_3 t_3 +let format_3_1 out_c (x_3, t) = + Printf.sprintf "(%a, %i)" format_3 x_3 t +let format_12_1 out_c (x_12, t) = + Printf.sprintf "(%a, %i)" format_12 x_12 t +let format_2_22 out_c (x_2, t_22) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_22 t_22 +let format_1_b out_c (x, b) = + Printf.sprintf "(%i -> %b)" x b +let format_3_b out_c (x_3, b) = + Printf.sprintf "(%a -> %b)" format_3 x_3 b +let format_1_L out_c (x, li) = + Printf.sprintf "%i -> %a" x format_L li +let format_1L_b out_c ((x, li), t) = + Printf.sprintf "(%i, %a) -> %b)" x format_L li t +let format_3_L out_c (x_3, li) = + Printf.sprintf "(%a, %a)" format_3 x_3 format_L li +let format_L_1 out_c (li, t) = + Printf.sprintf "(%a, %i)" format_L li t +let format_L_2 out_c (li, t_2) = + Printf.sprintf "(%a, %a)" format_L li format_2 t_2 +let format_1f_L2 out_c ((x, f), li_2) = + Printf.sprintf "%i -> %a)" x format_L2 li_2 +let format_s1_1 out_c ((s, x), y) = + Printf.sprintf "(%s, %i) -> %i" s x y +let format_2_s out_c (x_2, s) = + Printf.sprintf "%a -> %s" format_2 x_2 s +let format_1L1_L out_c ((x, li, y), lit) = + Printf.sprintf "(%i, %a, %i) -> %a" x format_L li y format_L lit +let format_212_1 out_c ((x_2, y, z_2), t) = + Printf.sprintf "(%a, %i, %a) -> %i)" format_2 x_2 y format_2 z_2 t + +(* Aliasing for output printers. *) +let o_printer_1 = Printf.sprintf "%i" +let o_printer_2 = Printf.sprintf "%a" format_2 +let o_printer_3 = Printf.sprintf "%a" format_3 +let o_printer_22 = Printf.sprintf "%a" format_22 +let o_printer_b = Printf.sprintf "%b" +let o_printer_L = Printf.sprintf "%a" format_L +let o_printer_L2 = Printf.sprintf "%a" format_L2 +let o_printer_s = Printf.sprintf "%s" + +(* To detuple functions. *) +let det_1 f = f +let det_2 f (x, y) = f x y +let det_3 f (x, y, z) = f x y z +let det_12 f (x, y_2) = f x y_2 +let det_1f (f: int -> (int -> bool) -> (int * int) list) (x, h) = f x h + +(* Templates. *) +let template format_ det o_printer t_name t_function t_list = + t_name >::: + (List.map + (fun (arg, res) -> + let title = Printf.sprintf "%a" format_ (arg, res) in + title >:: + (fun test_ctxt -> assert_equal + ~msg: t_name + ~printer: o_printer + res ((det t_function) arg)) + ) + t_list + ) + +let template_1_1 = template format_1_1 det_1 o_printer_1 +let template_2_1 = template format_2_1 det_2 o_printer_1 +let template_2_2 = template format_2_2 det_2 o_printer_2 +let template_2_3 = template format_2_3 det_2 o_printer_3 +let template_3_1 = template format_3_1 det_3 o_printer_1 +let template_1_b = template format_1_b det_1 o_printer_b +let template_3_b = template format_3_b det_3 o_printer_b +let template_1_L = template format_1_L det_1 o_printer_L +let template_1L_b = template format_1L_b det_2 o_printer_b +let template_3_L = template format_3_L det_3 o_printer_L +let template_L_1 = template format_L_1 det_1 o_printer_1 +let template_L_2 = template format_L_2 det_1 o_printer_2 +let template_1f_L2 = template format_1f_L2 det_1f o_printer_L2 +let template_s1_1 = template format_s1_1 det_2 o_printer_1 +let template_2_s = template format_2_s det_2 o_printer_s +let template_12_1 = template format_12_1 det_12 o_printer_1 +let template_2_22 = template format_2_22 det_2 o_printer_22 +let template_cple_2 = template format_2_2 det_1 o_printer_2 +let template_1L1_L = template format_1L1_L det_3 o_printer_L +let template_212_1 = template format_212_1 det_3 o_printer_1 + +(* Unit Test Wrapper. *) +let run_test template_ t_name t_function t_list = + let temp_test = template_ t_name t_function t_list in + run_test_tt_main temp_test ;; diff --git a/Source/builtin/tests/test_builtin_test_primes.ml b/Source/builtin/tests/test_builtin_test_primes.ml new file mode 100644 index 0000000..ffd3fb4 --- /dev/null +++ b/Source/builtin/tests/test_builtin_test_primes.ml @@ -0,0 +1,24 @@ +(** Test suites for builtin test_primes ml file using oUnit. *) + +open OUnit2 +open Builtin +open Basic_arithmetics +open Test_primes +open Test_builtin_templates + +let () = let t_list = [(2, true); (3, true); (5, true); + (7, true); (11, true); (13, true); + (4, false); (6, false); (12, false); + (45, false); (77, false); (63, false)] + in + run_test template_1_b "Is Prime Function" is_prime t_list +;; + +let () = let t_list = [((2, [2; 4; 8; 12]), true); ((11, [2; 4; 5; 20]), true); + ((23, [2; 9; 15; 18]), true); ((29,[30; 41; 52]), true); + ((4, [2; 9; 15; 18]), false); ((22,[30; 41; 52]), false); + ((15, [2; 9; 15; 18]), false); ((27,[30; 41; 52]), false) + ] + in + run_test template_1L_b "Is Pseudo Prime Function" is_pseudo_prime t_list +;; diff --git a/Source/multithreading/chinese_remaindert.ml b/Source/multithreading/chinese_remaindert.ml new file mode 100644 index 0000000..bc121a0 --- /dev/null +++ b/Source/multithreading/chinese_remaindert.ml @@ -0,0 +1,17 @@ +(** Chinese remainder theorem *) + +open Builtin +open Basic_arithmetics + +(** Image of the Chinese Remainder map + @param x positive integer of which you take image + @param l list of pairwise relatively prime positive integers. + *) +let crt_image x l = [] + +(** Inverse image of Chinese Remainder map + @para m a positive integer + @param l list of pairwise relatively prime factors of m + @param y list of remainders modulo pairwise relatively prime factors of m + *) +let crt_solver m l y = 0 diff --git a/Source/multithreading/chinese_remaindert.mli b/Source/multithreading/chinese_remaindert.mli new file mode 100644 index 0000000..3bb0296 --- /dev/null +++ b/Source/multithreading/chinese_remaindert.mli @@ -0,0 +1,16 @@ +(** Chinese remainder theorem *) + +(** Image of the Chinese Remainder map + @param x positive integer of which you take image + @param l list of pairwise relatively prime positive integers. + *) +val crt_image : int -> (int list) -> (int list) +;; + +(** Inverse image of Chinese Remainder map + @param m a positive integer + @param l list of pairwise relatively prime factors of m + @param y list of remainders modulo pairwise relatively prime factors of m + *) +val crt_solver : int -> (int list) -> (int list) -> int +;; diff --git a/Source/multithreading/multithreading.ml b/Source/multithreading/multithreading.ml new file mode 100644 index 0000000..e69de29 diff --git a/Source/multithreading/multithreading.mli b/Source/multithreading/multithreading.mli new file mode 100644 index 0000000..e69de29 diff --git a/Source/scalable/scalable.ml b/Source/scalable/scalable.ml new file mode 100644 index 0000000..522de05 --- /dev/null +++ b/Source/scalable/scalable.ml @@ -0,0 +1,198 @@ +(** A naive implementation of big integers + +This module aims at creating a set of big integers naively. Such data +types will be subsequently called bitarrays. A bitarray is a list of +zeros and ones ; first integer representing the sign bit. In this +context zero is reprensented by the empty list []. The list is to +be read from left to right ; this is the opposite convention to the +one you usually write binary decompositions with. After the sign bit +the first encountered bit is the coefficient in front of two to +the power zero. This convention has been chosen to ease writing +down code. A natural bitarray is understood as being a bitarray of +which you've taken out the sign bit, it is just the binary +decomposition of a non-negative integer. + + *) + +(** Creates a bitarray from a built-in integer. + @param x built-in integer. +*) +let from_int x = [] + +(** Transforms bitarray of built-in size to built-in integer. + UNSAFE: possible integer overflow. + @param bA bitarray object. + *) +let to_int bA = 0 + +(** Prints bitarray as binary number on standard output. + @param bA a bitarray. + *) +let print_b bA = () + +(** Toplevel directive to use print_b as bitarray printer. + CAREFUL: print_b is then list int printer. + UNCOMMENT FOR TOPLEVEL USE. +*) +(* #install_printer print_b *) + +(** Internal comparisons on bitarrays and naturals. Naturals in this + context are understood as bitarrays missing a bit sign and thus + assumed to be non-negative. +*) + +(** Comparing naturals. Output is 1 if first argument is bigger than + second -1 if it is smaller and 0 in case of equality. + @param nA A natural, a bitarray having no sign bit. + Assumed non-negative. + @param nB A natural. + *) +let rec compare_n nA nB = 0 + +(** Bigger inorder comparison operator on naturals. Returns true if + first argument is bigger than second and false otherwise. + @param nA natural. + @param nB natural. + *) +let (>>!) nA nB = true + +(** Smaller inorder comparison operator on naturals. Returns true if + first argument is smaller than second and false otherwise. + @param nA natural. + @param nB natural. + *) +let (<=!) nA nB = true + +(** Smaller or equal inorder comparison operator on naturals. Returns + true if first argument is smaller or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +let (<=!) nA nB = true + +(** Comparing two bitarrays. Output is 1 if first argument is bigger + than second -1 if it smaller and 0 in case of equality. + @param bA A bitarray. + @param bB A bitarray. +*) +let compare_b bA bB = 0 + +(** Bigger inorder comparison operator on bitarrays. Returns true if + first argument is bigger than second and false otherwise. + @param nA natural. + @param nB natural. + *) +let (<<) bA bB = true + +(** Smaller inorder comparison operator on bitarrays. Returns true if + first argument is smaller than second and false otherwise. + @param nA natural. + @param nB natural. + *) +let (>>) bA bB = true + +(** Bigger or equal inorder comparison operator on bitarrays. Returns + true if first argument is bigger or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +let (<<=) bA bB = true + +(** Smaller or equal inorder comparison operator on naturals. Returns + true if first argument is smaller or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +let (>>=) bA bB = true +;; + +(** Sign of a bitarray. + @param bA Bitarray. +*) +let sign_b bA = 0 + +(** Absolute value of bitarray. + @param bA Bitarray. +*) +let abs_b bA = [] + +(** Quotient of integers smaller than 4 by 2. + @param a Built-in integer smaller than 4. +*) +let _quot_t a = 0 + +(** Modulo of integer smaller than 4 by 2. + @param a Built-in integer smaller than 4. +*) +let _mod_t a = 0 + +(** Division of integer smaller than 4 by 2. + @param a Built-in integer smaller than 4. +*) +let _div_t a = (0, 0) + +(** Addition of two naturals. + @param nA Natural. + @param nB Natural. +*) +let add_n nA nB = [] + +(** Difference of two naturals. + UNSAFE: First entry is assumed to be bigger than second. + @param nA Natural. + @param nB Natural. +*) +let diff_n nA nB = [] + +(** Addition of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. + *) +let add_b bA bB = [] + +(** Difference of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. +*) +let diff_b bA bB = [] + +(** Shifts bitarray to the left by a given natural number. + @param bA Bitarray. + @param d Non-negative integer. +*) +let rec shift bA d = [] + +(** Multiplication of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. +*) +let mult_b bA bB = [] + +(** Quotient of two bitarrays. + @param bA Bitarray you want to divide by second argument. + @param bB Bitarray you divide by. Non-zero! +*) +let quot_b bA bB = [] + +(** Modulo of a bitarray against a positive one. + @param bA Bitarray the modulo of which you're computing. + @param bB Bitarray which is modular base. + *) +let mod_b bA bB = [] + +(** Integer division of two bitarrays. + @param bA Bitarray you want to divide. + @param bB Bitarray you wnat to divide by. +*) +let div_b bA bB = ([], []) diff --git a/Source/scalable/scalable.mli b/Source/scalable/scalable.mli new file mode 100644 index 0000000..8d032d3 --- /dev/null +++ b/Source/scalable/scalable.mli @@ -0,0 +1,178 @@ +(** A naive implementation of big integers + +This module aims at creating a set of big integers naively. Such data +types will be subsequently called bitarrays. A bitarray is a list of +zeros and ones ; first integer representing the sign bit. In this +context zero is reprensented by the empty list []. The list is to +be read from left to right ; this is the opposite convention to the +one you usually write binary decompositions with. After the sign bit +the first encountered bit is the coefficient in front of two to +the power zero. This convention has been chosen to ease writing +down code. A natural bitarray is understood as being a bitarray of +which you've taken out the sign bit, it is just the binary +decomposition of a non-negative integer. + + *) + +(** Creates a bitarray from a built-in integer. + @param x built-in integer. +*) +val from_int : int -> int list + +(** Transforms bitarray of built-in size to built-in integer. + UNSAFE: possible integer overflow. + @param bA bitarray object. + *) +val to_int : int list -> int + +(** Prints bitarray as binary number on standard output. + @param bA a bitarray. + *) +val print_b : int list -> unit + +(** Internal comparisons on bitarrays and naturals. Naturals in this + context are understood as bitarrays missing a bit sign and thus + assumed to be non-negative. +*) + + +(** Comparing naturals. Output is 1 if first argument is bigger than + second -1 if it is smaller and 0 in case of equality. + @param nA A natural, a bitarray having no sign bit. + Assumed non-negative. + @param nB A natural. + *) +val compare_n : int list -> int list -> int + +(** Bigger inorder comparison operator on naturals. Returns true if + first argument is bigger than second and false otherwise. + @param nA natural. + @param nB natural. + *) +val (>>!) : int list -> int list -> bool + +(** Smaller inorder comparison operator on naturals. Returns true if + first argument is smaller than second and false otherwise. + @param nA natural. + @param nB natural. + *) +val (< int list -> bool + +(** Bigger or equal inorder comparison operator on naturals. Returns + true if first argument is bigger or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +val (>=!) : int list -> int list -> bool + +(** Smaller or equal inorder comparison operator on naturals. Returns + true if first argument is smaller or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +val (<=!) : int list -> int list -> bool + + +(** Comparing two bitarrays. Output is 1 if first argument is bigger + than second -1 if it smaller and 0 in case of equality. + @param bA A bitarray. + @param bB A bitarray. +*) +val compare_b : int list -> int list -> int + +(** Bigger inorder comparison operator on bitarrays. Returns true if + first argument is bigger than second and false otherwise. + @param nA natural. + @param nB natural. + *) +val (<<) : int list -> int list -> bool + +(** Smaller inorder comparison operator on bitarrays. Returns true if + first argument is smaller than second and false otherwise. + @param nA natural. + @param nB natural. + *) +val (>>) : int list -> int list -> bool + +(** Bigger or equal inorder comparison operator on bitarrays. Returns + true if first argument is bigger or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +val (<<=) : int list -> int list -> bool + +(** Smaller or equal inorder comparison operator on naturals. Returns + true if first argument is smaller or equal to second and false + otherwise. + @param nA natural. + @param nB natural. + *) +val (>>=) : int list -> int list -> bool + +(** Sign of a bitarray. + @param bA Bitarray. +*) +val sign_b : int list -> int + +(** Absolute value of bitarray. + @param bA Bitarray. +*) +val abs_b : int list -> int list + +(** Addition of two naturals, output is a natural. + @param nA Natural. + @param nB Natural. +*) +val add_n : int list -> int list -> int list + +(** Difference of two naturals, output is a natural. + UNSAFE: First entry is assumed to be bigger than second. + @param nA Natural. + @param nB Natural. +*) +val diff_n : int list -> int list -> int list + +(** Addition of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. + *) +val add_b : int list -> int list -> int list + +(** Difference of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. +*) +val diff_b : int list -> int list -> int list + +(** Shifts bitarray to the left by a given natural number. + @param bA Bitarray. + @param d Non-negative integer. +*) +val shift : int list -> int -> int list + +(** Multiplication of two bitarrays. + @param bA Bitarray. + @param bB Bitarray. +*) +val mult_b : int list -> int list -> int list + +(** Quotient of two bitarrays. + @param bA Bitarray you want to divide by second argument. + @param bB Bitarray you divide by. Non-zero! +*) +val quot_b : int list -> int list -> int list + +(** Modulo of a bitarray against a positive one. + @param bA Bitarray the modulo of which you're computing. + @param bB Bitarray which is modular base. + *) +val mod_b : int list -> int list -> int list + +(** Integer division of two bitarrays. + @param bA Bitarray you want to divide. + @param bB Bitarray you wnat to divide by. +*) +val div_b : int list -> int list -> (int list * int list) diff --git a/Source/scalable/scalable_basic_arithmetics.ml b/Source/scalable/scalable_basic_arithmetics.ml new file mode 100644 index 0000000..83e1223 --- /dev/null +++ b/Source/scalable/scalable_basic_arithmetics.ml @@ -0,0 +1,17 @@ +(** Basic arithmetics for ordered euclidian ring. *) + +open Scalable + +(** Greater common (positive) divisor of two non-zero integers. + @param bA non-zero bitarray. + @param bB non-zero bitarray. +*) +let gcd_b bA bB = [] + +(** Extended euclidean division of two integers NOT OCAML DEFAULT. + Given non-zero entries a b computes triple (u, v, d) such that + a*u + b*v = d and d is gcd of a and b. + @param bA non-zero bitarray. + @param bB non-zero bitarray. +*) +let bezout_b bA bB = ([], [], []) diff --git a/Source/scalable/scalable_basic_arithmetics.mli b/Source/scalable/scalable_basic_arithmetics.mli new file mode 100644 index 0000000..21ccf76 --- /dev/null +++ b/Source/scalable/scalable_basic_arithmetics.mli @@ -0,0 +1,15 @@ +(** Basic arithmetics for ordered euclidian ring. *) + +(** Greater common (positive) divisor of two non-zero integers. + @param a non-zero bitarray. + @param b non-zero bitarray. +*) +val gcd_b : int list -> int list -> int list + +(** Extended euclidean division of two integers NOT OCAML DEFAULT. + Given non-zero entries a b computes triple (u, v, d) such that + a*u + b*v = d and d is gcd of a and b. + @param a non-zero bitarray. + @param b non-zero bitarray. +*) +val bezout_b : int list -> int list -> (int list * int list * int list) diff --git a/Source/scalable/scalable_break_ciphers.ml b/Source/scalable/scalable_break_ciphers.ml new file mode 100644 index 0000000..fc12b36 --- /dev/null +++ b/Source/scalable/scalable_break_ciphers.ml @@ -0,0 +1,9 @@ +(** Factoring bitarrays into primes *) + +open Scalable +open Scalable_basic_arithmetics + +(** Factors product of two prime bitarrays. + @param key is public key of an RSA cryptosystem. + *) +let break key = ([], []) diff --git a/Source/scalable/scalable_break_ciphers.mli b/Source/scalable/scalable_break_ciphers.mli new file mode 100644 index 0000000..31ee4ef --- /dev/null +++ b/Source/scalable/scalable_break_ciphers.mli @@ -0,0 +1,7 @@ +(** Factoring bitarrays into primes *) + +(** Factors product of two prime bitarrays. + @param key is public key of an RSA cryptosystem. + *) +val break : int list * 'a -> ((int list) * (int list)) +;; diff --git a/Source/scalable/scalable_ciphers.ml b/Source/scalable/scalable_ciphers.ml new file mode 100644 index 0000000..71d35b9 --- /dev/null +++ b/Source/scalable/scalable_ciphers.ml @@ -0,0 +1,56 @@ +(** Ciphers + bitarrays based ciphers. +*) + +open Scalable +open Scalable_basic_arithmetics +open Scalable_power + +(********** RSA Cipher **********) + +(** Generate an RSA ciphering key. + Involved prime bitarrays need to be distinct. Output is a couple + of public, private keys. + @param p prime bitarray + @param q prime bitarray +*) +let generate_keys_rsa p q = (([],[]), ([], [])) + +(** Encryption using RSA cryptosystem. + @param m bitarray hash of message + @param pub_key a tuple (n, e) composing public key of RSA cryptosystem. + *) +let encrypt_rsa m (n, e) = [] + +(** Decryption using RSA cryptosystem. + @param m bitarray hash of encrypted message. + @param pub_key a tuple (n, d) composing private key of RSA cryptosystem. + *) +let decrypt_rsa m (n , d) = [] + +(********** ElGamal Cipher **********) + +(** Generate ElGamal public data. Generates a couple (g, p) + where p is a prime bitarray and g a bitarray having high enough order modulo p. + @param p is a prime bitarray having form 2*q + 1 for prime bitarray q. + *) +let rec public_data_g p = ([], []) + +(** Generate ElGamal public data. + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +let generate_keys_g (g, p) = ([], []) + +(** ElGamal encryption process. + @param msg message to be encrypted. + @param pub_data a tuple (g, p) of ElGamal public data. + @param kA ElGamal public key. + *) +let encrypt_g msg (g, p) kA = ([], []) + +(** ElGamal decryption process. + @param msg a tuple (msgA, msgB) forming an encrypted ElGamal message. + @param a private key + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +let decrypt_g (msgA, msgB) a (g, p) = [] diff --git a/Source/scalable/scalable_ciphers.mli b/Source/scalable/scalable_ciphers.mli new file mode 100644 index 0000000..26e521b --- /dev/null +++ b/Source/scalable/scalable_ciphers.mli @@ -0,0 +1,53 @@ +(** Ciphers + bitarrays based ciphers. +*) + +(********** RSA Cipher **********) + +(** Generate an RSA ciphering key. + Involved prime bitarrays need to be distinct. Output is a couple + of public, private keys. + @param p prime bitarray + @param q prime bitarray +*) +val generate_keys_rsa : + int list -> int list -> ((int list) * (int list)) * ((int list) * (int list)) + +(** Encryption using RSA cryptosystem. + @param m bitarray hash of message + @param pub_key a tuple (n, e) composing public key of RSA cryptosystem. + *) +val encrypt_rsa : int list -> ((int list) * (int list)) -> int list + +(** Decryption using RSA cryptosystem. + @param m integer hash of encrypter message. + @param pub_key a tuple (n, d) composing private key of RSA cryptosystem. + *) +val decrypt_rsa : int list -> ((int list) * (int list)) -> int list + +(********** ElGamal Cipher **********) + +(** Generate ElGamal public data. Generates a couple (g, p) + where p is a prime bitarray and g a bitarray having high enough order modulo p. + @param p is a prime bitarray having form 2*q + 1 for prime bitarray q. + *) +val public_data_g : int list -> ((int list) * (int list)) + +(** Generate ElGamal public and private keys. + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +val generate_keys_g : ((int list) * (int list)) -> ((int list) * (int list)) + +(** ElGamal encryption process. + @param msg message to be encrypted. + @param pub_data a tuple (g, p) of ElGamal public data. + @param kA ElGamal public key. + *) +val encrypt_g : int list -> ((int list) * (int list)) -> int list -> ((int list) * (int list)) + +(** ElGamal decryption process. + @param msg a tuple (msgA, msgB) forming an encrypted ElGamal message. + @param a private key + @param pub_data a tuple (g, p) of public data for ElGamal cryptosystem. + *) +val decrypt_g : ((int list) * (int list)) -> int list -> ((int list) * (int list)) -> int list diff --git a/Source/scalable/scalable_encoding_msg.ml b/Source/scalable/scalable_encoding_msg.ml new file mode 100644 index 0000000..e544c72 --- /dev/null +++ b/Source/scalable/scalable_encoding_msg.ml @@ -0,0 +1,19 @@ +(** Encoding Strings *) + +open Scalable +open Scalable_basic_arithmetics +open Scalable_power + +(** Encode a string containing ASCII characters. + @param str is a string representing message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +let encode str bits = [] + +(** Decode a string containing ASCII characters. + @param msg is an integer representing an encoded message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +let decode msg bits = "" diff --git a/Source/scalable/scalable_encoding_msg.mli b/Source/scalable/scalable_encoding_msg.mli new file mode 100644 index 0000000..75b0d96 --- /dev/null +++ b/Source/scalable/scalable_encoding_msg.mli @@ -0,0 +1,15 @@ +(** Encoding Strings *) + +(** Encode a string containing ASCII characters. + @param str is a string representing message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +val encode : string -> int -> int list + +(** Decode a string containing ASCII characters. + @param msg is an integer representing an encoded message. + @param bits number of bits on which to store a character ; + alphanumeric ASCII is 7. + *) +val decode : int list -> int -> string diff --git a/Source/scalable/scalable_generate_primes.ml b/Source/scalable/scalable_generate_primes.ml new file mode 100644 index 0000000..4619553 --- /dev/null +++ b/Source/scalable/scalable_generate_primes.ml @@ -0,0 +1,90 @@ +(** Generating prime bitarrays *) + +open Scalable +open Scalable_basic_arithmetics + +(* Initializing list of bitarrays for eratosthenes's sieve. Naive + version. +*) + +(** List composed of 2 and then odd bitarrays starting at 3. + @param n upper bound to elements in the list of bitarrays. + *) +let init_eratosthenes n = [] + +(* Eratosthenes sieve. *) + +(** Eratosthene sieve. + @param n upper bound to elements in the list of primes, starting + at 2. +*) +let eratosthenes n = [] + +(* Write and read into file functions for lists. *) + +(** Write a list into a file. Element seperator is newline. Inner + seperator within elements of an element is ','. + @param file path to write to. +*) +let write_list li file = () + +(** Write a list of prime numbers up to limit into a txt file. + @param n limit of prime bitarrays up to which to build up a list of primes. + @param file path to write to. +*) +let write_list_primes n file = () + +(** Read file safely ; catch End_of_file exception. + @param in_c input channel. + *) +let input_line_opt in_c = + try Some (input_line in_c) + with End_of_file -> None + +(** Create a list of bitarrays out of reading a line per line channel. + @param in_c input channel. *) +let create_list in_c = () + +(** Load list of prime bitarrays into OCaml environment. + @param file path to load from. + *) +let read_list_primes file = [] + +(* Auxiliary functions to extract big prime numbers for testing + purposes. + *) + +(** Get last element of a list. + @param l list of prime bitarrays. + *) +let rec last_element l = match l with + | [] -> failwith "Scalable.generate_primes.last_element: Youre list \ + is empty. " + | e::[] -> e + | h::t -> last_element t + +(** Get two last elements. + @param l list of prime bitarrays. + *) +let rec last_two l = match l with + | [] | [_] -> failwith "Scalable.generate_primes.last_two: List has \ + to have at least two elements." + | e::g::[] -> (e, g) + | h::t -> last_two t +;; + +(* Generating couples of prime bitarrays for specific or fun + purposes. + *) + +(** Finding couples of prime bitarrays where second entry is twice the + first plus 1. + @param upper bound for searched for prime bitarrays, a built-in integer. + @param isprime function testing for (pseudo)primality. *) +let double_primes limit isprime = [] + +(** Finding twin primes. + @param upper bound for searched for prime bitarrays, a built-in integer.. + @param isprime function testing for (pseudo)primality. + *) +let twin_primes limit isprime = [] diff --git a/Source/scalable/scalable_generate_primes.mli b/Source/scalable/scalable_generate_primes.mli new file mode 100644 index 0000000..34e535f --- /dev/null +++ b/Source/scalable/scalable_generate_primes.mli @@ -0,0 +1,35 @@ +(** Generating prime bitarrays *) + +(** List composed of 2 and then odd bitarrays starting at 3. + @param n upper bound to elements in the list of bitarrays. + *) +val init_eratosthenes : int -> (int list) list + +(** Eratosthene sieve. + @param n upper bound to elements in the list of primes, starting + at 2. +*) +val eratosthenes : int -> (int list) list + +(** Write a list of prime numbers up to limit into a txt file. + @param n limit of prime bitarrays up to which to build up a list of primes. + @param file path to write to. +*) +val write_list_primes : int -> string -> unit + +(** Load list of primes into OCaml environment. + @param file path to load from. + *) +val read_list_primes : string -> (int list) list + +(** Finding couples of prime bitarrays where second entry is twice the + first plus 1. + @param upper bound for searched for prime bitarrays, a built-in integer. + @param isprime function testing for (pseudo)primality. *) +val double_primes : int -> (int list -> bool) -> ((int list) * (int list)) list + +(** Finding twin primes. + @param upper bound for searched for prime bitarrays, a built-in integer.. + @param isprime function testing for (pseudo)primality. + *) +val twin_primes : int -> (int list -> bool) -> ((int list) * (int list)) list diff --git a/Source/scalable/scalable_power.ml b/Source/scalable/scalable_power.ml new file mode 100644 index 0000000..5fed77a --- /dev/null +++ b/Source/scalable/scalable_power.ml @@ -0,0 +1,43 @@ +(** Power function implementations for bitarrays *) + +open Scalable +open Scalable_basic_arithmetics + +(* Naive and fast exponentiation ; already implemented in-class in the + built-in integer case. +*) + +(** Naive power function. Linear complexity + @param x base, a bitarray + @param n exponent, a non-negative bitarray + *) +let pow x n = [] + +(** Fast bitarray exponentiation function. Logarithmic complexity. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + *) +let power x n = [] + +(* Modular expnonentiation ; modulo a given natural (bitarray without + sign bits). +*) + +(** Fast modular exponentiation function. Logarithmic complexity. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + @param m modular base, a positive bitarray + *) +let mod_power x n m = [] + +(* Making use of Fermat Little Theorem for very quick exponentation + modulo prime number. + *) + +(** Fast modular exponentiation function mod prime. Logarithmic complexity. + It makes use of the Little Fermat Theorem. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + @param p prime modular base, a positive bitarray + *) +let prime_mod_power x n p = [] diff --git a/Source/scalable/scalable_power.mli b/Source/scalable/scalable_power.mli new file mode 100644 index 0000000..40ba19c --- /dev/null +++ b/Source/scalable/scalable_power.mli @@ -0,0 +1,28 @@ +(** Power function implementations for bitarrays *) + +(** Naive power function. Linear complexity + @param x base, a bitarray + @param n exponent, a non-negative bitarray + *) +val pow : int list -> int list -> int list + +(** Fast bitarray exponentiation function. Logarithmic complexity. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + *) +val power : int list -> int list -> int list + +(** Fast modular exponentiation function. Logarithmic complexity. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + @param m modular base, a positive bitarray + *) +val mod_power : int list -> int list -> int list -> int list + +(** Fast modular exponentiation function mod prime. Logarithmic complexity. + It makes use of the Little Fermat Theorem. + @param x base, a bitarray + @param n exponent, a non-negative bitarray + @param p prime modular base, a positive bitarray + *) +val prime_mod_power : int list -> int list -> int list -> int list diff --git a/Source/scalable/scalable_test_primes.ml b/Source/scalable/scalable_test_primes.ml new file mode 100644 index 0000000..9c2f4b7 --- /dev/null +++ b/Source/scalable/scalable_test_primes.ml @@ -0,0 +1,14 @@ +(** Testing for primality *) + +open Scalable +open Scalable_basic_arithmetics +open Scalable_power + +(** Deterministic primality test *) +let is_prime n = true + +(** Pseudo-primality test based on Fermat's Little Theorem + @param p tested bitarray + @param testSeq sequence of bitarrays againt which to test + *) +let is_pseudo_prime p test_seq = true diff --git a/Source/scalable/scalable_test_primes.mli b/Source/scalable/scalable_test_primes.mli new file mode 100644 index 0000000..3a4f4bd --- /dev/null +++ b/Source/scalable/scalable_test_primes.mli @@ -0,0 +1,12 @@ +(** Testing for primality *) + +(** Deterministic primality test +*) +val is_prime : int list -> bool + + +(** Pseudo-primality test based on Fermat's Little Theorem + @param p tested bitarray + @param testSeq sequence of bitarrays againt which to test + *) +val is_pseudo_prime : int list -> (int list) list -> bool diff --git a/Source/scalable/tests/test_scalable.ml b/Source/scalable/tests/test_scalable.ml new file mode 100644 index 0000000..e69de29 diff --git a/Source/scalable/tests/test_scalable_basic_arithmetics.ml b/Source/scalable/tests/test_scalable_basic_arithmetics.ml new file mode 100644 index 0000000..2cfc296 --- /dev/null +++ b/Source/scalable/tests/test_scalable_basic_arithmetics.ml @@ -0,0 +1,25 @@ +(** Test suites for builtin basic_arithmetic ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Test_scalable_templates + +let () = + let t_list = + [(from_int 32, from_int 6), from_int 2; + (from_int 18, from_int 12), from_int 6; + (from_int (-18), from_int (-12)), from_int 6] + in + run_test template_2_1 "GCD Function" gcd_b t_list +;; + +let () = + let t_list = + [(from_int 18, from_int 22), (from_int 5, from_int (-4), from_int 2); + (from_int 22, from_int 18), (from_int (-4), from_int 5, from_int 2); + (from_int 17, from_int 21), (from_int 5, from_int (-4), from_int 1); + (from_int 21, from_int 17), (from_int (-4), from_int 5, from_int 1)] + in + run_test template_2_3 "Bezout Function" bezout_b t_list +;; diff --git a/Source/scalable/tests/test_scalable_break_ciphers.ml b/Source/scalable/tests/test_scalable_break_ciphers.ml new file mode 100644 index 0000000..220b44e --- /dev/null +++ b/Source/scalable/tests/test_scalable_break_ciphers.ml @@ -0,0 +1,15 @@ +(** Test suites for scalable break_cifers ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_break_ciphers +open Test_scalable_templates + +(* Only tests for RSA for now. *) + +let () = let t_list = [((from_int 99400891, from_int 36199003), + (from_int 9967, from_int 9973))] + in + run_test template_cple_2 "Break Cifer RSA Test" break t_list +;; diff --git a/Source/scalable/tests/test_scalable_ciphers.ml b/Source/scalable/tests/test_scalable_ciphers.ml new file mode 100644 index 0000000..e6008ca --- /dev/null +++ b/Source/scalable/tests/test_scalable_ciphers.ml @@ -0,0 +1,44 @@ +(** Test suites for builtin cifers ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_power +open Scalable_ciphers +open Test_scalable_templates + + +let p = from_int 9967 and q = from_int 9973 +let ((_, e), (n, d)) = generate_keys_rsa p q + +let phin = mult_b (diff_b p [1;1]) (diff_b q [1;1]) +let is_inverse x y n = mod_b (mult_b (mod_b x n) (mod_b y n)) n = [1; 1] +let () = let t_list = [(e, d, phin), true] + in + run_test template_3_b "Generate RSA Keys Test" is_inverse t_list +;; + +let () = let t_list = [((from_int 281237, (from_int 99400891, from_int 36199003)), + from_int 70133953) + ] + in + run_test template_12_1 "Encrypt RSA Test" encrypt_rsa t_list +;; + +let () = let t_list = [((from_int 70133953, (from_int 99400891, from_int 30869683)), + from_int 281237) + ] + in + run_test template_12_1 "Decrypt RSA Test" decrypt_rsa t_list +;; + +(* Test for ElGamal *) + +let (g, p) = public_data_g (from_int 100000007) ;; +let (pub, priv) = generate_keys_g (g, p) ;; +let (g_k, xA_k) = encrypt_g (from_int 42) (g, p) pub ;; + +let () = let t_list = [((g_k, xA_k), priv, (g, p)), from_int 42]; + in + run_test template_212_1 "Decrypt ElGamal Keys" decrypt_g t_list +;; diff --git a/Source/scalable/tests/test_scalable_encoding_msg.ml b/Source/scalable/tests/test_scalable_encoding_msg.ml new file mode 100644 index 0000000..872100a --- /dev/null +++ b/Source/scalable/tests/test_scalable_encoding_msg.ml @@ -0,0 +1,18 @@ +(** Test suites for scalable encoding_msg ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_power +open Scalable_encoding_msg +open Test_scalable_templates + +let () = let t_list = [(("Bashar", 7), from_int 2294023860466)] + in + run_test template_s1i_1 "Encode Function" encode t_list +;; + +let () = let t_list = [((from_int 2294023860466, 7), "Bashar")] + in + run_test template_2i_s "Decode Function" decode t_list +;; diff --git a/Source/scalable/tests/test_scalable_generate_primes.ml b/Source/scalable/tests/test_scalable_generate_primes.ml new file mode 100644 index 0000000..fa8d690 --- /dev/null +++ b/Source/scalable/tests/test_scalable_generate_primes.ml @@ -0,0 +1,48 @@ +(** Test suites for scalable power ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_test_primes +open Scalable_generate_primes +open Test_scalable_templates + + +let () = let t_list = [(2, [from_int 2]); + (3, [from_int 2; from_int 3]); + (6, [from_int 2; from_int 3; from_int 5])] + in + run_test template_1i_L "Initialization list for eratosthenes" init_eratosthenes t_list +;; + +(* let () = let t_list = [(2, [from_int 2]); + * (3, [from_int 2; from_int 3]); + * (6, [from_int 2; from_int 3; from_int 5]); + * (25, [from_int 2; from_int 3; from_int 5; + * from_int 7; from_int 11; from_int 13; + * from_int 17; from_int 19; from_int 23]) + * ] + * in + * run_test template_1i_L "Erathosthenes Sieve" eratosthenes t_list + * ;; + * + * let () = let t_list = [((20, is_prime), + * [(from_int 2, from_int 5); + * (from_int 3, from_int 7); + * (from_int 5, from_int 11); + * (from_int 11, from_int 23)]) + * ] + * in + * run_test template_1f_L2 "Double Primes Generator" double_primes t_list + * ;; + * + * let () = let t_list = [((20, is_prime), + * [(from_int 2, from_int 3); + * (from_int 3, from_int 5); + * (from_int 5, from_int 7); + * (from_int 11, from_int 13); + * (from_int 17, from_int 19)]) + * ] + * in + * run_test template_1f_L2 "Twin Primes Generator" twin_primes t_list + * ;; *) diff --git a/Source/scalable/tests/test_scalable_power.ml b/Source/scalable/tests/test_scalable_power.ml new file mode 100644 index 0000000..6b06aa9 --- /dev/null +++ b/Source/scalable/tests/test_scalable_power.ml @@ -0,0 +1,59 @@ +(** Test suites for scalable power ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_power +open Test_scalable_templates + +let () = let t_list = [((from_int (-1), from_int 12), from_int 1); + ((from_int (-1), from_int 11), from_int (-1) ); + ((from_int 0, from_int 2), from_int 0); + ((from_int 3, from_int 1), from_int 3); + ((from_int 5, from_int 0), from_int 1); + ((from_int (-2), from_int 2), from_int 4); + ((from_int (-2), from_int 3), from_int (-8)); + ((from_int 2, from_int 5), from_int 32); + ((from_int 3, from_int 3), from_int 27)] + in + run_test template_2_1 "Pow Function" pow t_list +;; + +let () = let t_list = [((from_int (-1), from_int 12), from_int 1); + ((from_int (-1), from_int 11), from_int (-1)); + ((from_int 0, from_int 2), from_int 0); + ((from_int 3, from_int 1), from_int 3); + ((from_int 5, from_int 0), from_int 1); + ((from_int (-2), from_int 2), from_int 4); + ((from_int (-2), from_int 3), from_int (-8)); + ((from_int 2, from_int 5), from_int 32); + ((from_int 3, from_int 3), from_int 27)] + in + run_test template_2_1 "Power Function" power t_list +;; + +let () = let t_list = [((from_int (-1), from_int 12, from_int 10), from_int 1); + ((from_int (-1), from_int 11, from_int 11), from_int 10); + ((from_int 0, from_int 2, from_int 3), from_int 0); + ((from_int 3, from_int 1, from_int 3), from_int 0); + ((from_int 5, from_int 0, from_int 2), from_int 1); + ((from_int (-2), from_int 2, from_int 5), from_int 4); + ((from_int (-2), from_int 3, from_int 9), from_int 1); + ((from_int 2, from_int 5, from_int 17), from_int 15); + ((from_int 3, from_int 3, from_int 17), from_int 10)] + in + run_test template_3_1 "Modular Power Function" mod_power t_list +;; + +let () = let t_list = [((from_int (-1), from_int 12, from_int 7), from_int 1); + ((from_int (-1), from_int 11, from_int 11), from_int 10); + ((from_int 0, from_int 2, from_int 3), from_int 0); + ((from_int 3, from_int 1, from_int 3), from_int 0); + ((from_int 5, from_int 0, from_int 2), from_int 1); + ((from_int (-2), from_int 2, from_int 5), from_int 4); + ((from_int (-2), from_int 3, from_int 5), from_int 2); + ((from_int 2, from_int 5, from_int 17), from_int 15); + ((from_int 3, from_int 3, from_int 17), from_int 10)] + in + run_test template_3_1 "Power Modulo Prime Function" prime_mod_power t_list +;; diff --git a/Source/scalable/tests/test_scalable_templates.ml b/Source/scalable/tests/test_scalable_templates.ml new file mode 100644 index 0000000..42ea19c --- /dev/null +++ b/Source/scalable/tests/test_scalable_templates.ml @@ -0,0 +1,143 @@ +open OUnit2 +open Scalable + +(* Formatting combination of tuples of tuples. *) +let format_2 out_c (x, y) = + Printf.sprintf "(%i, %i)" (to_int x) (to_int y) +let format_3 out_c (x, y, z) = + Printf.sprintf "(%i, %i, %i)" (to_int x) (to_int y) (to_int z) +let format_12 out_c (x, y_2) = + Printf.sprintf "(%i, %a)" x format_2 y_2 +let format_1b2 out_c (x, y_2) = + Printf.sprintf "(%i, %a)" (to_int x) format_2 y_2 +let format_22 out_c (x_2, y_2) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_2 y_2 + +(* Formatting lists. *) +let format_L out_c li = + let rec __format li = + match li with + | [] -> "" + | [h] -> string_of_int (to_int h) + | h::t -> (string_of_int (to_int h)) ^ "; " ^ __format t + in + "[" ^ (__format li) ^ "]" + +let format_L2 out_c li_2 = + let rec __format li_2 = + match li_2 with + | [] -> "" + | [(h1, h2)] -> Printf.sprintf "(%i, %i)" (to_int h1) (to_int h2) + | (h1, h2)::t -> + (Printf.sprintf "(%i, %i)" (to_int h1) (to_int h2)) ^ "; " ^ (__format t) + in + "[" ^ (__format li_2) ^ "]" + +(* Formatting input-output combinations of tested functions. + Essentially creating aliases. *) +let format_1_1 = format_2 +let format_2_1 out_c (x_2, t) = + Printf.sprintf "(%a, %i)" format_2 x_2 (to_int t) +let format_2_2 = format_22 +let format_2_3 out_c (x_2, t_3) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_3 t_3 +let format_3_1 out_c (x_3, t) = + Printf.sprintf "(%a, %i)" format_3 x_3 (to_int t) +let format_12_1 out_c (x_12, t) = + Printf.sprintf "(%a, %i)" format_12 x_12 (to_int t) +let format_1b2_1 out_c (x_12, t) = + Printf.sprintf "(%a, %i)" format_1b2 x_12 (to_int t) +let format_2_22 out_c (x_2, t_22) = + Printf.sprintf "(%a, %a)" format_2 x_2 format_22 t_22 +let format_1_b out_c (x, b) = + Printf.sprintf "(%i -> %b)" (to_int x) b +let format_3_b out_c (x_3, b) = + Printf.sprintf "(%a -> %b)" format_3 x_3 b +let format_1i_L out_c (x, li) = + Printf.sprintf "%i -> %a" x format_L li +let format_1_L out_c (x, li) = + Printf.sprintf "%i -> %a" (to_int x) format_L li +let format_1L_b out_c ((x, li), t) = + Printf.sprintf "(%i, %a) -> %b)" (to_int x) format_L li t +let format_3_L out_c (x_3, li) = + Printf.sprintf "(%a, %a)" format_3 x_3 format_L li +let format_L_1 out_c (li, t) = + Printf.sprintf "(%a, %i)" format_L li (to_int t) +let format_L_2 out_c (li, t_2) = + Printf.sprintf "(%a, %a)" format_L li format_2 t_2 +let format_1f_L2 out_c ((x, f), li_2) = + Printf.sprintf "%i -> %a)" x format_L2 li_2 +let format_s1_1 out_c ((s, x), y) = + Printf.sprintf "(%s, %i) -> %i" s (to_int x) (to_int y) +let format_s1i_1 out_c ((s, x), y) = + Printf.sprintf "(%s, %i) -> %i" s x (to_int y) +let format_2_s out_c (x_2, s) = + Printf.sprintf "%a -> %s" format_2 x_2 s +let format_2i_s out_c ((x, y), s) = + Printf.sprintf "(%i, %i) -> %s" (to_int x) y s +let format_1L1_L out_c ((x, li, y), lit) = + Printf.sprintf "(%i, %a, %i) -> %a" (to_int x) format_L li (to_int y) format_L lit +let format_212_1 out_c ((x_2, y, z_2), t) = + Printf.sprintf "(%a, %i, %a) -> %i)" format_2 x_2 (to_int y) format_2 z_2 (to_int t) + +(* Aliasing for output printers. *) +let o_printer_1 bA = Printf.sprintf "%i" (to_int bA) +let o_printer_2 = Printf.sprintf "%a" format_2 +let o_printer_3 = Printf.sprintf "%a" format_3 +let o_printer_22 = Printf.sprintf "%a" format_22 +let o_printer_b = Printf.sprintf "%b" +let o_printer_L = Printf.sprintf "%a" format_L +let o_printer_L2 = Printf.sprintf "%a" format_L2 +let o_printer_s = Printf.sprintf "%s" + +(* To detuple functions. *) +let det_1 f = f +let det_2 f (x, y) = f x y +let det_3 f (x, y, z) = f x y z +let det_12 f (x, y_2) = f x y_2 +let det_1f f (x, h) = f x h + +(* Templates. *) +let template format_ det o_printer t_name t_function t_list = + t_name >::: + (List.map + (fun (arg, res) -> + let title = Printf.sprintf "%a" format_ (arg, res) in + title >:: + (fun test_ctxt -> assert_equal + ~msg: t_name + ~printer: o_printer + res ((det t_function) arg)) + ) + t_list + ) + +let template_1_1 = template format_1_1 det_1 o_printer_1 +let template_2_1 = template format_2_1 det_2 o_printer_1 +let template_2_2 = template format_2_2 det_2 o_printer_2 +let template_2_3 = template format_2_3 det_2 o_printer_3 +let template_3_1 = template format_3_1 det_3 o_printer_1 +let template_1_b = template format_1_b det_1 o_printer_b +let template_3_b = template format_3_b det_3 o_printer_b +let template_1_L = template format_1_L det_1 o_printer_L +let template_1i_L = template format_1i_L det_1 o_printer_L +let template_1L_b = template format_1L_b det_2 o_printer_b +let template_3_L = template format_3_L det_3 o_printer_L +let template_L_1 = template format_L_1 det_1 o_printer_1 +let template_L_2 = template format_L_2 det_1 o_printer_2 +(* FIXME *) +(* let template_1f_L2 = template format_1f_L2 det_1f o_printer_L2 *) +let template_s1_1 = template format_s1_1 det_2 o_printer_1 +let template_s1i_1 = template format_s1i_1 det_2 o_printer_1 +let template_2_s = template format_2_s det_2 o_printer_s +let template_2i_s = template format_2i_s det_2 o_printer_s +let template_12_1 = template format_1b2_1 det_12 o_printer_1 +let template_2_22 = template format_2_22 det_2 o_printer_22 +let template_cple_2 = template format_2_2 det_1 o_printer_2 +let template_1L1_L = template format_1L1_L det_3 o_printer_L +let template_212_1 = template format_212_1 det_3 o_printer_1 + +(* Unit Test Wrapper. *) +let run_test template_ t_name t_function t_list = + let temp_test = template_ t_name t_function t_list in + run_test_tt_main temp_test ;; diff --git a/Source/scalable/tests/test_scalable_test_primes.ml b/Source/scalable/tests/test_scalable_test_primes.ml new file mode 100644 index 0000000..9fe45c2 --- /dev/null +++ b/Source/scalable/tests/test_scalable_test_primes.ml @@ -0,0 +1,28 @@ +(** Test suites for builtin test_primes ml file using oUnit. *) + +open OUnit2 +open Scalable +open Scalable_basic_arithmetics +open Scalable_test_primes +open Test_scalable_templates + +let () = let t_list = [(from_int 2, true); (from_int 3, true); (from_int 5, true); + (from_int 7, true); (from_int 11, true); (from_int 13, true); + (from_int 4, false); (from_int 6, false); (from_int 12, false); + (from_int 45, false); (from_int 77, false); (from_int 63, false)] + in + run_test template_1_b "Is Prime Function" is_prime t_list +;; + +let () = let t_list = [((from_int 2, [from_int 2; from_int 4; from_int 8; from_int 12]), true); + ((from_int 11, [from_int 2; from_int 4; from_int 5; from_int 20]), true); + ((from_int 23, [from_int 2; from_int 9; from_int 15; from_int 18]), true); + ((from_int 29,[from_int 30; from_int 41; from_int 52]), true); + ((from_int 4, [from_int 2; from_int 9; from_int 15; from_int 18]), false); + ((from_int 22,[from_int 30; from_int 41; from_int 52]), false); + ((from_int 15, [from_int 2; from_int 9; from_int 15; from_int 18]), false); + ((from_int 27,[from_int 30; from_int 41; from_int 52]), false) + ] + in + run_test template_1L_b "Is Pseudo Prime Function" is_pseudo_prime t_list +;; diff --git a/_oasis b/_oasis new file mode 100644 index 0000000..b43e990 --- /dev/null +++ b/_oasis @@ -0,0 +1,363 @@ +OASISFormat: 0.4 +Name: ArithmeticsForIT +Version: 0.8 +License: GPL +LicenseFile: LICENSE +Authors: Bashar Dudin + +Synopsis: A library for education purposes on uses of arithmetics in IT. +Description: + Builds up a needed code and big int types to generate RSA and ElGamal + cryptosystem keys, test for primality, factor primes (within + unreasonable time), mutlithread using the chinese remainder theorem. + +Plugins: META (0.4), DevFiles (0.4) +BuildTools: ocamlbuild + +Library "builtin" + FindLibName: builtin + Path: Source/builtin + Modules: Builtin + +Library "builtin_basic_arithmetics" + FindLibName: basic_arithmetics + FindLibParent: builtin + Path: Source/builtin + Modules: Basic_arithmetics + +Library "builtin_power" + FindLibName: power + FindLibParent: builtin + Path: Source/builtin + Modules: Power + +Library "builtin_generate_primes" + FindLibName: generate_primes + FindLibParent: builtin + Path: Source/builtin + Modules: Generate_primes + +Library "builtin_test_primes" + FindLibName: test_primes + FindLibParent: builtin + Path: Source/builtin + Modules: Test_primes + +Library "builtin_encoding_msg" + FindLibName: encoding_msg + FindLibParent: builtin + Path: Source/builtin + Modules: Encoding_msg + +Library "builtin_ciphers" + FindLibName: ciphers + FindLibParent: builtin + Path: Source/builtin + Modules: Ciphers + +Library "builtin_break_ciphers" + FindLibName: break_ciphers + FindLibParent: builtin + Path: Source/builtin + Modules: Break_ciphers + +Library "test_builtin_templates" + Path: Source/builtin/tests + Modules: Test_builtin_templates + BuildDepends: oUnit + +Executable "test_builtin" + Path: Source/builtin/tests + MainIs: test_builtin.ml + Install: false + BuildDepends: oUnit, builtin, test_builtin_templates + +Test "test_builtin" + Run$: flag(tests) + Command: $test_builtin + TestTools: test_builtin + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_basic_arithmetics" + Path: Source/builtin/tests + MainIs: test_builtin_basic_arithmetics.ml + Install: false + BuildDepends: oUnit, builtin, test_builtin_templates + +Test "test_builtin_basic_arithmetics" + Run$: flag(tests) + Command: $test_builtin_basic_arithmetics + TestTools: test_builtin_basic_arithmetics + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_power" + Path: Source/builtin/tests + MainIs: test_builtin_power.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, + test_builtin_templates + +Test "test_builtin_power" + Run$: flag(tests) + Command: $test_builtin_power + TestTools: test_builtin_power + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_test_primes" + Path: Source/builtin/tests + MainIs: test_builtin_test_primes.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, builtin.power, + test_builtin_templates + +Test "test_builtin_test_primes" + Run$: flag(tests) + Command: $test_builtin_test_primes + TestTools: test_builtin_test_primes + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_generate_primes" + Path: Source/builtin/tests + MainIs: test_builtin_generate_primes.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, builtin.test_primes, + test_builtin_templates + +Test "test_builtin_generate_primes" + Run$: flag(tests) + Command: $test_builtin_generate_primes + TestTools: test_builtin_generate_primes + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_encoding_msg" + Path: Source/builtin/tests + MainIs: test_builtin_encoding_msg.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, builtin.power, + test_builtin_templates + +Test "test_builtin_encoding_msg" + Run$: flag(tests) + Command: $test_builtin_encoding_msg + TestTools: test_builtin_encoding_msg + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_ciphers" + Path: Source/builtin/tests + MainIs: test_builtin_ciphers.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, builtin.power, + test_builtin_templates + +Test "test_builtin_ciphers" + Run$: flag(tests) + Command: $test_builtin_ciphers + TestTools: test_builtin_ciphers + WorkingDirectory: Source/builtin/tests + +Executable "test_builtin_break_ciphers" + Path: Source/builtin/tests + MainIs: test_builtin_break_ciphers.ml + Install: false + BuildDepends: oUnit, + builtin, builtin.basic_arithmetics, + test_builtin_templates + +Test "test_builtin_break_ciphers" + Run$: flag(tests) + Command: $test_builtin_break_ciphers + TestTools: test_builtin_break_ciphers + WorkingDirectory: Source/builtin/tests + +Library "scalable" + FindLibName: scalable + Path: Source/scalable + Modules: Scalable + +Library "scalable_basic_arithmetics" + FindLibName: scalable_basic_arithmetics + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_basic_arithmetics + +Library "scalable_power" + FindLibName: scalable_power + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_power + +Library "scalable_generate_primes" + FindLibName: scalable_generate_primes + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_generate_primes + +Library "scalable_test_primes" + FindLibName: scalable_test_primes + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_test_primes + +Library "scalable_encoding_msg" + FindLibName: scalable_encoding_msg + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_encoding_msg + +Library "scalable_ciphers" + FindLibName: scalable_ciphers + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_ciphers + +Library "scalable_break_ciphers" + FindLibName: scalable_break_ciphers + FindLibParent: scalable + Path: Source/scalable + Modules: Scalable_break_ciphers + +Library "test_scalable_templates" + Path: Source/scalable/tests + Modules: Test_scalable_templates + BuildDepends: oUnit + +Executable "test_scalable" + Path: Source/scalable/tests + MainIs: test_scalable.ml + Install: false + BuildDepends: oUnit, scalable, test_scalable_templates + +Test "test_scalable" + Run$: flag(tests) + Command: $test_scalable + TestTools: test_scalable + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_basic_arithmetics" + Path: Source/scalable/tests + MainIs: test_scalable_basic_arithmetics.ml + Install: false + BuildDepends: oUnit, scalable, + scalable.scalable_basic_arithmetics, test_scalable_templates + +Test "test_scalable_basic_arithmetics" + Run$: flag(tests) + Command: $test_scalable_basic_arithmetics + TestTools: test_scalable_basic_arithmetics + WorkingDirectory: Source/scalable/tests + + +Executable "test_scalable_power" + Path: Source/scalable/tests + MainIs: test_scalable_power.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, + test_scalable_templates + +Test "test_scalable_power" + Run$: flag(tests) + Command: $test_scalable_power + TestTools: test_scalable_power + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_test_primes" + Path: Source/scalable/tests + MainIs: test_scalable_test_primes.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, scalable.scalable_power, + test_scalable_templates + +Test "test_scalable_test_primes" + Run$: flag(tests) + Command: $test_scalable_test_primes + TestTools: test_scalable_test_primes + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_generate_primes" + Path: Source/scalable/tests + MainIs: test_scalable_generate_primes.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, scalable.scalable_test_primes, + test_scalable_templates + +Test "test_scalable_generate_primes" + Run$: flag(tests) + Command: $test_scalable_generate_primes + TestTools: test_scalable_generate_primes + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_encoding_msg" + Path: Source/scalable/tests + MainIs: test_scalable_encoding_msg.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, scalable.scalable_power, + test_scalable_templates + +Test "test_scalable_encoding_msg" + Run$: flag(tests) + Command: $test_scalable_encoding_msg + TestTools: test_scalable_encoding_msg + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_ciphers" + Path: Source/scalable/tests + MainIs: test_scalable_ciphers.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, scalable.scalable_power, + test_scalable_templates + +Test "test_scalable_ciphers" + Run$: flag(tests) + Command: $test_scalable_ciphers + TestTools: test_scalable_ciphers + WorkingDirectory: Source/scalable/tests + +Executable "test_scalable_break_ciphers" + Path: Source/scalable/tests + MainIs: test_scalable_break_ciphers.ml + Install: false + BuildDepends: oUnit, + scalable, scalable.scalable_basic_arithmetics, + test_scalable_templates + +Test "test_scalable_break_ciphers" + Run$: flag(tests) + Command: $test_scalable_break_ciphers + TestTools: test_scalable_break_ciphers + WorkingDirectory: Source/scalable/tests + +Library "multithreading" + FindLibName: multithreading + Path: Source/multithreading + Modules: Multithreading + +Library "multithreading_chineses_remaindert" + FindLibName: chineses_remaindert + FindLibParent: multithreading + Path: Source/multithreading + Modules: Chinese_remaindert + BuildDepends: builtin, builtin.basic_arithmetics + +AlphaFeatures: ocamlbuild_more_args +Document "AFIT_Documentation" + Type: ocamlbuild (0.4) + BuildTools: ocamldoc + + Title: AFIT documentation + XOCamlbuildPath: . + XOCamlbuildExtraArgs: + "-docflags '-colorize-code -short-functors -charset utf-8'" + XOCamlbuildLibraries: builtin.basic_arithmetics, builtin.power, + builtin.generate_primes, + builtin.test_primes, builtin.encoding_msg, + builtin.ciphers, builtin.break_ciphers \ No newline at end of file diff --git a/afit_file_en.pdf b/afit_file_en.pdf new file mode 100644 index 0000000..963509a Binary files /dev/null and b/afit_file_en.pdf differ diff --git a/afit_file_fr.pdf b/afit_file_fr.pdf new file mode 100644 index 0000000..e2234ea Binary files /dev/null and b/afit_file_fr.pdf differ