├── README-Dinghy.md ├── README-GCE.md ├── README.md ├── c-libpaillier-optimised ├── .gitignore ├── README.md └── bench.c ├── c-libpaillier ├── README.md ├── bench.c └── libpaillier-0.8 │ ├── AUTHORS │ ├── COPYING │ ├── INSTALL │ ├── Makefile.in │ ├── NEWS │ ├── README │ ├── aclocal.m4 │ ├── configure │ ├── configure.ac │ ├── install-sh │ ├── m4 │ └── gmp.m4 │ ├── missing │ ├── mkinstalldirs │ ├── paillier.c │ └── paillier.h ├── go-go-gadget-paillier ├── .gitignore ├── LICENSE ├── README.md ├── example_test.go ├── imgs │ └── Inspector-gadget.jpg └── paillier_test.go ├── java-javallier ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── ai │ └── snips │ ├── Decrypt.java │ ├── Encrypt.java │ ├── Keygen.java │ └── Ops.java ├── julia-sketch ├── README.md ├── addition.jl ├── decryption.jl ├── encryption.jl ├── paillier.jl ├── plaintexts.jl └── primes.jl ├── python-paillier ├── .gitignore ├── README.md ├── addition.py ├── decryption.py ├── encryption.py ├── keygen.py └── paillier │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── demo.py │ ├── paillier │ ├── __init__.py │ ├── paillier.py │ └── primes.py │ ├── requirements.txt │ ├── setup.py │ └── tests │ ├── test_paillier.py │ └── test_primes.py ├── python-phe ├── .gitignore ├── README.md ├── addition.py ├── decryption.py ├── encryption.py └── keygen.py ├── rust-paillier-gmp ├── .gitignore ├── Cargo.toml ├── README.md ├── benches │ ├── addition.rs │ ├── constants.rs │ ├── decryption.rs │ ├── encryption.rs │ └── keygen.rs └── src │ └── lib.rs └── rust-paillier-ramp ├── .gitignore ├── Cargo.toml ├── README.md ├── benches ├── addition.rs ├── constants.rs ├── decryption.rs ├── encryption.rs └── keygen.rs └── src └── lib.rs /README-Dinghy.md: -------------------------------------------------------------------------------- 1 | # Running the Rust-RAMP benches on Phones 2 | 3 | [rust-paillier-ramp](/rust-paillier-ramp) can be relatively easily run on iOS and Android devices. 4 | 5 | If you're already an iOS and Android developer, and the bench is already 6 | running on your computer, then all you need is to install the phone toolchain and [Dinghy](https://medium.com/p/c9f94f81d305/edit), a tool we are 7 | developing to spawn Rust test and benches on iOS and Android devices. 8 | 9 | For relatively recent Apple devices: 10 | 11 | ``` 12 | rustup target install aarch64-apple-ios 13 | ``` 14 | 15 | For Android 16 | 17 | ``` 18 | rustup target install arm-linux-androideabi 19 | ``` 20 | 21 | Then 22 | 23 | ``` 24 | cargo install dinghy 25 | ``` 26 | 27 | Plug-in your phone, unlock it... 28 | 29 | ``` 30 | cd rust-paillier-ramp 31 | cargo dinghy bench 32 | ``` 33 | 34 | If you're not a registered iOS or Android user, then you will have some setup to 35 | do, which is detailed in [Dinghy Readme](https://github.com/snipsco/dinghy). 36 | -------------------------------------------------------------------------------- /README-GCE.md: -------------------------------------------------------------------------------- 1 | # Installation on Google GCE 2 | 3 | ## Basic dependencies 4 | 5 | Compilers etc.: 6 | ``` 7 | sudo apt-get install build-essential 8 | ``` 9 | 10 | GMP: 11 | ``` 12 | sudo apt-get install libgmp10 libgmp-dev libmpfr-dev libmpc-dev 13 | ``` 14 | 15 | ## Rust and nightly toolchain 16 | 17 | From https://rustup.rs/: 18 | ``` 19 | curl https://sh.rustup.rs -sSf | sh 20 | ``` 21 | 22 | Then add `~/.cargo/bin` to `PATH`: 23 | ``` 24 | echo "PATH=\$PATH:~/.cargo/bin" >> ~/.bashrc 25 | source ~/.bashrc 26 | ``` 27 | 28 | And finally install the nightly toolchain: 29 | ``` 30 | rustup toolchain install nightly 31 | rustup default nightly 32 | ``` 33 | 34 | ## Python and dependencies 35 | 36 | Python3: 37 | ``` 38 | sudo apt-get install python3 python3-dev 39 | ``` 40 | 41 | `virtualenv` (optional): 42 | ``` 43 | sudo apt-get install virtualenv 44 | echo "export LC_ALL=C" >> ~/.bashrc 45 | source ~/.bashrc 46 | ``` 47 | 48 | 49 | ## Java 50 | 51 | Add Oracle repository: 52 | ``` 53 | sudo apt-get install software-properties-common 54 | sudo add-apt-repository ppa:webupd8team/java 55 | sudo apt-get update 56 | ``` 57 | 58 | Install Oracle Java and set as default: 59 | ``` 60 | sudo apt-get install oracle-java8-installer 61 | sudo apt install oracle-java8-set-default 62 | ``` 63 | 64 | Install Maven 65 | ``` 66 | sudo apt-get install maven 67 | ``` 68 | 69 | 70 | ## Julia 71 | 72 | ``` 73 | sudo apt-get install julia 74 | ``` 75 | 76 | 77 | ## Go 78 | ``` 79 | sudo apt-get install golang 80 | echo "export GOPATH=~/.go" >> ~/.bashrc 81 | ``` 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Benching Paillier libraries 2 | 3 | Benchmarks of libraries implementing Paillier encryption in various languages as part of a [blog post](https://medium.com/snips-ai/benchmarking-paillier-encryption-15631a0b5ad8) on the concrete performance of [our Rust library](https://github.com/snipsco/rust-paillier). 4 | 5 | All code has been tested on macOS and Google GCE (Ubuntu), some also on Raspberry Pis and mobile phones. 6 | 7 | Each subdirectory contains notes regarding specific installation and running. [Setup instructions for a blank Google GCE instance](README-GCE.md) and [Running Rust+Ramp test on phones](README-Dinghy.md) are also available. 8 | -------------------------------------------------------------------------------- /c-libpaillier-optimised/.gitignore: -------------------------------------------------------------------------------- 1 | libpaillier/ 2 | -------------------------------------------------------------------------------- /c-libpaillier-optimised/README.md: -------------------------------------------------------------------------------- 1 | # libpaillier 2 | 3 | This is the bench of [libpaillier](http://acsc.cs.utexas.edu/libpaillier/) written in C, with optimised encryption. 4 | 5 | ## Prerequisites 6 | 7 | Requires GMP to be installed. 8 | 9 | 10 | ## Installation 11 | 12 | Run the following to build the libpaillier library: 13 | ``` 14 | git clone https://github.com/mcornejo/libpaillier.git 15 | cd libpaillier 16 | ./configure 17 | make 18 | cd .. 19 | ``` 20 | which on AWS must be done with the path to GMP explicitly specified: 21 | ``` 22 | git clone https://github.com/mcornejo/libpaillier.git 23 | cd libpaillier 24 | ./configure --with-gmp-include=/usr/include/x86_64-linux-gnu/ --with-gmp-lib=/usr/lib/x86_64-linux-gnu/ 25 | make 26 | cd .. 27 | ``` 28 | 29 | 30 | ## Benching 31 | 32 | Building the bench: 33 | ``` 34 | gcc -O3 bench.c libpaillier/paillier.o -Ilibpaillier -lgmp -o bench -std=gnu11 35 | ``` 36 | 37 | Running the bench 38 | ``` 39 | ./bench 40 | ``` 41 | -------------------------------------------------------------------------------- /c-libpaillier-optimised/bench.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "paillier.h" 6 | 7 | #ifdef CLOCK_PROCESS_CPUTIME_ID 8 | /* cpu time in the current process */ 9 | #define CLOCKTYPE CLOCK_PROCESS_CPUTIME_ID 10 | #else 11 | /* this one should be appropriate to avoid errors on multiprocessors systems */ 12 | #define CLOCKTYPE CLOCK_MONOTONIC 13 | #endif 14 | 15 | #define ROUNDS 10 16 | 17 | 18 | typedef struct PARAMS_BENCH { 19 | int modulusbits; 20 | paillier_pubkey_t* pub; 21 | paillier_prvkey_t* prv; 22 | paillier_plaintext_t *plaintext; 23 | paillier_ciphertext_t *ciphertext1; 24 | paillier_ciphertext_t *ciphertext2; 25 | paillier_ciphertext_t *ciphertext_result; 26 | } paillier_params_t; 27 | 28 | /* Function to time other functions */ 29 | /* eg: void *identity(void *) { } */ 30 | 31 | double time_it(void (*action)(void *arg), void* arg) 32 | { 33 | struct timespec tsi, tsf; 34 | clock_gettime(CLOCKTYPE, &tsi); 35 | action(arg); 36 | clock_gettime(CLOCKTYPE, &tsf); 37 | 38 | double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec); 39 | long elaps_ns = tsf.tv_nsec - tsi.tv_nsec; 40 | return elaps_s + ((double)elaps_ns) / 1.0e9; 41 | } 42 | 43 | 44 | double avg_time(double in_arr[]) { 45 | double sum = 0; 46 | for(int i = 0; imodulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 57 | } 58 | 59 | void bench_paillier_encryption(void *params) { 60 | paillier_params_t *my_params = (paillier_params_t *)params; 61 | paillier_enc(&(my_params->ciphertext1), my_params->pub, my_params->plaintext, paillier_get_rand_devurandom); 62 | } 63 | 64 | void bench_paillier_decryption(void *params) { 65 | paillier_params_t *my_params = (paillier_params_t *)params; 66 | paillier_dec(&(my_params->plaintext), my_params->pub, my_params->prv, my_params->ciphertext1); 67 | } 68 | 69 | void bench_paillier_multiplication(void *params) { 70 | paillier_params_t *my_params = (paillier_params_t *)params; 71 | paillier_mul(&(my_params->ciphertext_result), my_params->pub, my_params->ciphertext1, my_params->ciphertext2); 72 | } 73 | 74 | 75 | 76 | 77 | 78 | double test_keygen(int bitlength) { 79 | paillier_params_t *params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 80 | params->modulusbits = bitlength; 81 | 82 | double keygen_times[ROUNDS]; 83 | for(int i = 0; ipub); 86 | paillier_freeprvkey(params->prv); 87 | } 88 | free(params); 89 | return avg_time(keygen_times); 90 | } 91 | 92 | 93 | double test_encryption_small(int bitlength){ 94 | double small_encryption_times[ROUNDS]; 95 | 96 | paillier_params_t *my_params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 97 | my_params->modulusbits = bitlength; 98 | paillier_keygen(my_params->modulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 99 | my_params->plaintext = paillier_plaintext_from_ui(42); 100 | 101 | for(int i = 0; iciphertext1); 104 | } 105 | paillier_freeplaintext(my_params->plaintext); 106 | paillier_freepubkey(my_params->pub); 107 | paillier_freeprvkey(my_params->prv); 108 | free(my_params); 109 | return avg_time(small_encryption_times); 110 | } 111 | 112 | double test_encryption_large(int bitlength){ 113 | double encryption_times[ROUNDS]; 114 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 115 | 116 | paillier_params_t *my_params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 117 | my_params->modulusbits = bitlength; 118 | paillier_keygen(my_params->modulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 119 | my_params->plaintext = paillier_plaintext_from_str(magic_number_str, 10); 120 | 121 | for(int i = 0; iciphertext1); 124 | } 125 | paillier_freeplaintext(my_params->plaintext); 126 | paillier_freepubkey(my_params->pub); 127 | paillier_freeprvkey(my_params->prv); 128 | free(my_params); 129 | return avg_time(encryption_times); 130 | } 131 | 132 | double test_decryption_small(int bitlength){ 133 | double small_decryption_times[ROUNDS]; 134 | 135 | paillier_params_t *my_params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 136 | my_params->modulusbits = bitlength; 137 | paillier_keygen(my_params->modulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 138 | my_params->plaintext = paillier_plaintext_from_ui(42); 139 | paillier_enc(&(my_params->ciphertext1), my_params->pub, my_params->plaintext, paillier_get_rand_devurandom); 140 | 141 | 142 | for(int i = 0; iplaintext); 144 | small_decryption_times[i] = time_it(bench_paillier_decryption, (void*)my_params); 145 | } 146 | paillier_freeplaintext(my_params->plaintext); 147 | paillier_freeciphertext(my_params->ciphertext1); 148 | paillier_freepubkey(my_params->pub); 149 | paillier_freeprvkey(my_params->prv); 150 | free(my_params); 151 | return avg_time(small_decryption_times); 152 | } 153 | 154 | double test_decryption_large(int bitlength){ 155 | double large_decryption_times[ROUNDS]; 156 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 157 | 158 | paillier_params_t *my_params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 159 | my_params->modulusbits = bitlength; 160 | paillier_keygen(my_params->modulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 161 | my_params->plaintext = paillier_plaintext_from_str(magic_number_str, 10); 162 | paillier_enc(&(my_params->ciphertext1), my_params->pub, my_params->plaintext, paillier_get_rand_devurandom); 163 | 164 | 165 | for(int i = 0; iplaintext); 167 | large_decryption_times[i] = time_it(bench_paillier_decryption, (void*)my_params); 168 | } 169 | paillier_freeplaintext(my_params->plaintext); 170 | paillier_freeciphertext(my_params->ciphertext1); 171 | paillier_freepubkey(my_params->pub); 172 | paillier_freeprvkey(my_params->prv); 173 | free(my_params); 174 | return avg_time(large_decryption_times); 175 | } 176 | 177 | 178 | double test_multiplication(int bitlength){ 179 | double multiplication_times[ROUNDS]; 180 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 181 | 182 | paillier_params_t *my_params = (paillier_params_t *)malloc(sizeof(paillier_params_t)); 183 | my_params->modulusbits = bitlength; 184 | paillier_keygen(my_params->modulusbits, &(my_params->pub), &(my_params->prv), paillier_get_rand_devurandom); 185 | 186 | my_params->plaintext = paillier_plaintext_from_ui(42); 187 | paillier_enc(&(my_params->ciphertext1), my_params->pub, my_params->plaintext, paillier_get_rand_devurandom); 188 | 189 | paillier_freeplaintext(my_params->plaintext); 190 | 191 | my_params->plaintext = paillier_plaintext_from_str(magic_number_str, 10); 192 | paillier_enc(&(my_params->ciphertext2), my_params->pub, my_params->plaintext, paillier_get_rand_devurandom); 193 | 194 | for(int i = 0; iciphertext_result); 197 | } 198 | paillier_freeplaintext(my_params->plaintext); 199 | paillier_freeciphertext(my_params->ciphertext1); 200 | paillier_freeciphertext(my_params->ciphertext2); 201 | paillier_freepubkey(my_params->pub); 202 | paillier_freeprvkey(my_params->prv); 203 | free(my_params); 204 | return avg_time(multiplication_times); 205 | } 206 | 207 | void run_keygen_tests() { 208 | printf("KEYGEN(%d bits) AVG TIME: %lf ms\n", 1024, test_keygen(1024)*1000); 209 | printf("KEYGEN(%d bits) AVG TIME: %lf ms\n", 2048, test_keygen(2048)*1000); 210 | printf("KEYGEN(%d bits) AVG TIME: %lf ms\n", 3072, test_keygen(3072)*1000); 211 | printf("KEYGEN(%d bits) AVG TIME: %lf ms\n", 4096, test_keygen(4096)*1000); 212 | } 213 | 214 | void run_encryption_small_tests(){ 215 | printf("ENCRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 1024, test_encryption_small(1024)*1000); 216 | printf("ENCRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 2048, test_encryption_small(2048)*1000); 217 | printf("ENCRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 3072, test_encryption_small(3072)*1000); 218 | printf("ENCRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 4096, test_encryption_small(4096)*1000); 219 | } 220 | 221 | void run_encryption_large_tests(){ 222 | printf("ENCRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 1024, test_encryption_large(1024)*1000); 223 | printf("ENCRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 2048, test_encryption_large(2048)*1000); 224 | printf("ENCRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 3072, test_encryption_large(3072)*1000); 225 | printf("ENCRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 4096, test_encryption_large(4096)*1000); 226 | } 227 | 228 | void run_decryption_small_tests(){ 229 | printf("DECRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 1024, test_decryption_small(1024)*1000); 230 | printf("DECRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 2048, test_decryption_small(2048)*1000); 231 | printf("DECRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 3072, test_decryption_small(3072)*1000); 232 | printf("DECRYPTION SMALL (%d bits) AVG TIME: %lf ms\n", 4096, test_decryption_small(4096)*1000); 233 | } 234 | 235 | void run_decryption_large_tests(){ 236 | printf("DECRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 1024, test_decryption_large(1024)*1000); 237 | printf("DECRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 2048, test_decryption_large(2048)*1000); 238 | printf("DECRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 3072, test_decryption_large(3072)*1000); 239 | printf("DECRYPTION LARGE (%d bits) AVG TIME: %lf ms\n", 4096, test_decryption_large(4096)*1000); 240 | } 241 | 242 | void run_multiplication_tests(){ 243 | printf("MULTIPLICATION (%d bits) AVG TIME: %lf ms\n", 1024, test_multiplication(1024)*1000); 244 | printf("MULTIPLICATION (%d bits) AVG TIME: %lf ms\n", 2048, test_multiplication(2048)*1000); 245 | printf("MULTIPLICATION (%d bits) AVG TIME: %lf ms\n", 3072, test_multiplication(3072)*1000); 246 | printf("MULTIPLICATION (%d bits) AVG TIME: %lf ms\n", 4096, test_multiplication(4096)*1000); 247 | } 248 | 249 | 250 | int main(int argc, char **argv){ 251 | 252 | run_keygen_tests(); 253 | run_encryption_small_tests(); 254 | run_encryption_large_tests(); 255 | run_decryption_small_tests(); 256 | run_decryption_large_tests(); 257 | run_multiplication_tests(); 258 | 259 | return 0; 260 | } -------------------------------------------------------------------------------- /c-libpaillier/README.md: -------------------------------------------------------------------------------- 1 | # libpaillier 2 | 3 | This is the bench of [libpaillier](http://acsc.cs.utexas.edu/libpaillier/) written in C. 4 | 5 | ## Prerequisites 6 | 7 | Requires GMP to be installed. 8 | 9 | 10 | ## Installation 11 | 12 | Run the following to build the libpaillier library: 13 | ``` 14 | cd libpaillier-0.8 15 | ./configure 16 | make 17 | cd .. 18 | ``` 19 | which on Google GCE must be done with the path to GMP explicitly specified: 20 | ``` 21 | cd libpaillier-0.8 22 | ./configure --with-gmp-include=/usr/include/x86_64-linux-gnu/ --with-gmp-lib=/usr/lib/x86_64-linux-gnu/ 23 | make 24 | cd .. 25 | ``` 26 | 27 | 28 | ## Benching 29 | 30 | Building the bench: 31 | ``` 32 | gcc -O3 bench.c libpaillier-0.8/paillier.o -Ilibpaillier-0.8 -lgmp -o bench -std=gnu11 33 | ``` 34 | 35 | Running the bench 36 | ``` 37 | ./bench 38 | ``` 39 | -------------------------------------------------------------------------------- /c-libpaillier/bench.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "paillier.h" 6 | 7 | #ifdef CLOCK_PROCESS_CPUTIME_ID 8 | /* cpu time in the current process */ 9 | #define CLOCKTYPE CLOCK_PROCESS_CPUTIME_ID 10 | #else 11 | /* this one should be appropriate to avoid errors on multiprocessors systems */ 12 | #define CLOCKTYPE CLOCK_MONOTONIC 13 | #endif 14 | 15 | #define ROUNDS 10 16 | 17 | 18 | typedef struct KEYS { 19 | int modulusbits; 20 | paillier_pubkey_t* pub; 21 | paillier_prvkey_t* prv; 22 | } paillier_keys_t; 23 | 24 | typedef struct PARAM { 25 | paillier_keys_t *keys; 26 | paillier_ciphertext_t *ciphertext; 27 | paillier_plaintext_t *plaintext; 28 | } paillier_t; 29 | 30 | typedef struct PARAM_MUL { 31 | paillier_keys_t *keys; 32 | paillier_ciphertext_t *ciphertext1; 33 | paillier_ciphertext_t *ciphertext2; 34 | paillier_ciphertext_t *ciphertext_result; 35 | } paillier_mul_t; 36 | 37 | 38 | 39 | void test_paillier_keygen(void *keys) { 40 | paillier_keys_t *my_keys = (paillier_keys_t *)keys; 41 | paillier_keygen(my_keys->modulusbits, &(my_keys->pub), &(my_keys->prv), paillier_get_rand_devurandom); 42 | } 43 | 44 | void test_paillier_encryption_small(void *keys) { 45 | paillier_keys_t *my_keys = (paillier_keys_t *)keys; 46 | paillier_plaintext_t* magic_number; 47 | paillier_ciphertext_t* ciphertext; 48 | magic_number = paillier_plaintext_from_ui(45); 49 | paillier_enc(0, my_keys->pub, magic_number, paillier_get_rand_devurandom); 50 | } 51 | 52 | void test_paillier_encryption_random(void *keys) { 53 | paillier_keys_t *my_keys = (paillier_keys_t *)keys; 54 | paillier_plaintext_t* magic_number; 55 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 56 | magic_number = paillier_plaintext_from_str(magic_number_str); 57 | paillier_enc(0, my_keys->pub, magic_number, paillier_get_rand_devurandom); 58 | } 59 | 60 | void test_paillier_decryption(void *params) { 61 | paillier_t *my_params = (paillier_t *)params; 62 | paillier_plaintext_t* sum; 63 | paillier_dec(my_params->plaintext, my_params->keys->pub, my_params->keys->prv, my_params->ciphertext); 64 | } 65 | 66 | void test_paillier_multiplication(void *params) { 67 | paillier_mul_t *my_params = (paillier_mul_t *)params; 68 | paillier_mul(my_params->keys->pub, my_params->ciphertext_result, my_params->ciphertext1, my_params->ciphertext2); 69 | } 70 | 71 | 72 | 73 | 74 | /* Function to time other functions */ 75 | /* eg: 76 | void *identity(void *) { } 77 | */ 78 | double time_it(void (*action)(void *arg), void* arg) 79 | { 80 | struct timespec tsi, tsf; 81 | clock_gettime(CLOCKTYPE, &tsi); 82 | action(arg); 83 | clock_gettime(CLOCKTYPE, &tsf); 84 | 85 | double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec); 86 | long elaps_ns = tsf.tv_nsec - tsi.tv_nsec; 87 | return elaps_s + ((double)elaps_ns) / 1.0e9; 88 | } 89 | 90 | 91 | double avg_time(double in_arr[]) { 92 | double sum = 0; 93 | for(int i = 0; imodulusbits = bitlength; 104 | double keygen_times[ROUNDS]; 105 | for(int i = 0; imodulusbits = bitlength; 115 | paillier_keygen(keys->modulusbits, &(keys->pub), &(keys->prv), paillier_get_rand_devurandom); 116 | double small_encryption_times[ROUNDS]; 117 | 118 | for(int i = 0; imodulusbits = bitlength; 128 | paillier_keygen(keys->modulusbits, &(keys->pub), &(keys->prv), paillier_get_rand_devurandom); 129 | double random_encryption_times[ROUNDS]; 130 | 131 | for(int i = 0; imodulusbits = bitlength; 141 | paillier_keygen(keys->modulusbits, &(keys->pub), &(keys->prv), paillier_get_rand_devurandom); 142 | paillier_plaintext_t* magic_number; 143 | paillier_ciphertext_t* ciphertext; 144 | magic_number = paillier_plaintext_from_ui(42); 145 | ciphertext = paillier_enc(0, keys->pub, magic_number, paillier_get_rand_devurandom); 146 | 147 | paillier_t *params = (paillier_t *)malloc(sizeof(paillier_t)); 148 | params->keys = keys; 149 | params->ciphertext = ciphertext; 150 | params->plaintext = magic_number; 151 | 152 | double small_decryption_times[ROUNDS]; 153 | 154 | for(int i = 0; imodulusbits = bitlength; 165 | paillier_keygen(keys->modulusbits, &(keys->pub), &(keys->prv), paillier_get_rand_devurandom); 166 | paillier_plaintext_t* magic_number; 167 | paillier_ciphertext_t* ciphertext; 168 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 169 | magic_number = paillier_plaintext_from_str(magic_number_str); 170 | ciphertext = paillier_enc(0, keys->pub, magic_number, paillier_get_rand_devurandom); 171 | 172 | paillier_t *params = (paillier_t *)malloc(sizeof(paillier_t)); 173 | params->keys = keys; 174 | params->ciphertext = ciphertext; 175 | params->plaintext = magic_number; 176 | 177 | double small_decryption_times[ROUNDS]; 178 | 179 | for(int i = 0; imodulusbits = bitlength; 191 | paillier_keygen(keys->modulusbits, &(keys->pub), &(keys->prv), paillier_get_rand_devurandom); 192 | paillier_plaintext_t* magic_number; 193 | paillier_plaintext_t* magic_number_random; 194 | paillier_ciphertext_t* ciphertext1; 195 | paillier_ciphertext_t* ciphertext2; 196 | magic_number = paillier_plaintext_from_ui(42); 197 | ciphertext1 = paillier_enc(0, keys->pub, magic_number, paillier_get_rand_devurandom); 198 | 199 | char *magic_number_str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 200 | magic_number_random = paillier_plaintext_from_str(magic_number_str); 201 | ciphertext2 = paillier_enc(0, keys->pub, magic_number_random, paillier_get_rand_devurandom); 202 | 203 | paillier_mul_t *params = (paillier_mul_t *)malloc(sizeof(paillier_mul_t)); 204 | paillier_ciphertext_t *result = (paillier_ciphertext_t *) malloc (sizeof(paillier_ciphertext_t)); 205 | params->keys = keys; 206 | params->ciphertext1 = ciphertext1; 207 | params->ciphertext2 = ciphertext2; 208 | params->ciphertext_result = ciphertext2; // change this! allocate space 209 | 210 | double multiplication_times[ROUNDS]; 211 | 212 | for(int i = 0; ipub->n); 278 | 279 | return 0; 280 | } -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/AUTHORS: -------------------------------------------------------------------------------- 1 | John Bethencourt 2 | http://www.cs.berkeley.edu/~bethenco 3 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/INSTALL: -------------------------------------------------------------------------------- 1 | Basic Installation 2 | ================== 3 | 4 | These are generic installation instructions. 5 | 6 | The `configure' shell script attempts to guess correct values for 7 | various system-dependent variables used during compilation. It uses 8 | those values to create a `Makefile' in each directory of the package. 9 | It may also create one or more `.h' files containing system-dependent 10 | definitions. Finally, it creates a shell script `config.status' that 11 | you can run in the future to recreate the current configuration, and a 12 | file `config.log' containing compiler output (useful mainly for 13 | debugging `configure'). 14 | 15 | It can also use an optional file (typically called `config.cache' 16 | and enabled with `--cache-file=config.cache' or simply `-C') that saves 17 | the results of its tests to speed up reconfiguring. (Caching is 18 | disabled by default to prevent problems with accidental use of stale 19 | cache files.) 20 | 21 | If you need to do unusual things to compile the package, please try 22 | to figure out how `configure' could check whether to do them, and mail 23 | diffs or instructions to the address given in the `README' so they can 24 | be considered for the next release. If you are using the cache, and at 25 | some point `config.cache' contains results you don't want to keep, you 26 | may remove or edit it. 27 | 28 | The file `configure.ac' (or `configure.in') is used to create 29 | `configure' by a program called `autoconf'. You only need 30 | `configure.ac' if you want to change it or regenerate `configure' using 31 | a newer version of `autoconf'. 32 | 33 | The simplest way to compile this package is: 34 | 35 | 1. `cd' to the directory containing the package's source code and type 36 | `./configure' to configure the package for your system. If you're 37 | using `csh' on an old version of System V, you might need to type 38 | `sh ./configure' instead to prevent `csh' from trying to execute 39 | `configure' itself. 40 | 41 | Running `configure' takes awhile. While running, it prints some 42 | messages telling which features it is checking for. 43 | 44 | 2. Type `make' to compile the package. 45 | 46 | 3. Optionally, type `make check' to run any self-tests that come with 47 | the package. 48 | 49 | 4. Type `make install' to install the programs and any data files and 50 | documentation. 51 | 52 | 5. You can remove the program binaries and object files from the 53 | source code directory by typing `make clean'. To also remove the 54 | files that `configure' created (so you can compile the package for 55 | a different kind of computer), type `make distclean'. There is 56 | also a `make maintainer-clean' target, but that is intended mainly 57 | for the package's developers. If you use it, you may have to get 58 | all sorts of other programs in order to regenerate files that came 59 | with the distribution. 60 | 61 | Compilers and Options 62 | ===================== 63 | 64 | Some systems require unusual options for compilation or linking that the 65 | `configure' script does not know about. Run `./configure --help' for 66 | details on some of the pertinent environment variables. 67 | 68 | You can give `configure' initial values for configuration parameters 69 | by setting variables in the command line or in the environment. Here 70 | is an example: 71 | 72 | ./configure CC=c89 CFLAGS=-O2 LIBS=-lposix 73 | 74 | *Note Defining Variables::, for more details. 75 | 76 | Compiling For Multiple Architectures 77 | ==================================== 78 | 79 | You can compile the package for more than one kind of computer at the 80 | same time, by placing the object files for each architecture in their 81 | own directory. To do this, you must use a version of `make' that 82 | supports the `VPATH' variable, such as GNU `make'. `cd' to the 83 | directory where you want the object files and executables to go and run 84 | the `configure' script. `configure' automatically checks for the 85 | source code in the directory that `configure' is in and in `..'. 86 | 87 | If you have to use a `make' that does not support the `VPATH' 88 | variable, you have to compile the package for one architecture at a 89 | time in the source code directory. After you have installed the 90 | package for one architecture, use `make distclean' before reconfiguring 91 | for another architecture. 92 | 93 | Installation Names 94 | ================== 95 | 96 | By default, `make install' will install the package's files in 97 | `/usr/local/bin', `/usr/local/man', etc. You can specify an 98 | installation prefix other than `/usr/local' by giving `configure' the 99 | option `--prefix=PREFIX'. 100 | 101 | You can specify separate installation prefixes for 102 | architecture-specific files and architecture-independent files. If you 103 | give `configure' the option `--exec-prefix=PREFIX', the package will 104 | use PREFIX as the prefix for installing programs and libraries. 105 | Documentation and other data files will still use the regular prefix. 106 | 107 | In addition, if you use an unusual directory layout you can give 108 | options like `--bindir=DIR' to specify different values for particular 109 | kinds of files. Run `configure --help' for a list of the directories 110 | you can set and what kinds of files go in them. 111 | 112 | If the package supports it, you can cause programs to be installed 113 | with an extra prefix or suffix on their names by giving `configure' the 114 | option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. 115 | 116 | Optional Features 117 | ================= 118 | 119 | Some packages pay attention to `--enable-FEATURE' options to 120 | `configure', where FEATURE indicates an optional part of the package. 121 | They may also pay attention to `--with-PACKAGE' options, where PACKAGE 122 | is something like `gnu-as' or `x' (for the X Window System). The 123 | `README' should mention any `--enable-' and `--with-' options that the 124 | package recognizes. 125 | 126 | For packages that use the X Window System, `configure' can usually 127 | find the X include and library files automatically, but if it doesn't, 128 | you can use the `configure' options `--x-includes=DIR' and 129 | `--x-libraries=DIR' to specify their locations. 130 | 131 | Specifying the System Type 132 | ========================== 133 | 134 | There may be some features `configure' cannot figure out automatically, 135 | but needs to determine by the type of machine the package will run on. 136 | Usually, assuming the package is built to be run on the _same_ 137 | architectures, `configure' can figure that out, but if it prints a 138 | message saying it cannot guess the machine type, give it the 139 | `--build=TYPE' option. TYPE can either be a short name for the system 140 | type, such as `sun4', or a canonical name which has the form: 141 | 142 | CPU-COMPANY-SYSTEM 143 | 144 | where SYSTEM can have one of these forms: 145 | 146 | OS KERNEL-OS 147 | 148 | See the file `config.sub' for the possible values of each field. If 149 | `config.sub' isn't included in this package, then this package doesn't 150 | need to know the machine type. 151 | 152 | If you are _building_ compiler tools for cross-compiling, you should 153 | use the `--target=TYPE' option to select the type of system they will 154 | produce code for. 155 | 156 | If you want to _use_ a cross compiler, that generates code for a 157 | platform different from the build platform, you should specify the 158 | "host" platform (i.e., that on which the generated programs will 159 | eventually be run) with `--host=TYPE'. 160 | 161 | Sharing Defaults 162 | ================ 163 | 164 | If you want to set default values for `configure' scripts to share, you 165 | can create a site shell script called `config.site' that gives default 166 | values for variables like `CC', `cache_file', and `prefix'. 167 | `configure' looks for `PREFIX/share/config.site' if it exists, then 168 | `PREFIX/etc/config.site' if it exists. Or, you can set the 169 | `CONFIG_SITE' environment variable to the location of the site script. 170 | A warning: not all `configure' scripts look for a site script. 171 | 172 | Defining Variables 173 | ================== 174 | 175 | Variables not defined in a site shell script can be set in the 176 | environment passed to `configure'. However, some packages may run 177 | configure again during the build, and the customized values of these 178 | variables may be lost. In order to avoid this problem, you should set 179 | them in the `configure' command line, using `VAR=value'. For example: 180 | 181 | ./configure CC=/usr/local2/bin/gcc 182 | 183 | will cause the specified gcc to be used as the C compiler (unless it is 184 | overridden in the site shell script). 185 | 186 | `configure' Invocation 187 | ====================== 188 | 189 | `configure' recognizes the following options to control how it operates. 190 | 191 | `--help' 192 | `-h' 193 | Print a summary of the options to `configure', and exit. 194 | 195 | `--version' 196 | `-V' 197 | Print the version of Autoconf used to generate the `configure' 198 | script, and exit. 199 | 200 | `--cache-file=FILE' 201 | Enable the cache: use and save the results of the tests in FILE, 202 | traditionally `config.cache'. FILE defaults to `/dev/null' to 203 | disable caching. 204 | 205 | `--config-cache' 206 | `-C' 207 | Alias for `--cache-file=config.cache'. 208 | 209 | `--quiet' 210 | `--silent' 211 | `-q' 212 | Do not print messages saying which checks are being made. To 213 | suppress all normal output, redirect it to `/dev/null' (any error 214 | messages will still be shown). 215 | 216 | `--srcdir=DIR' 217 | Look for the package's source code in directory DIR. Usually 218 | `configure' can determine that directory automatically. 219 | 220 | `configure' also accepts some other, not widely useful, options. Run 221 | `configure --help' for more details. 222 | 223 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/Makefile.in: -------------------------------------------------------------------------------- 1 | top_srcdir = @top_srcdir@ 2 | prefix = @prefix@ 3 | exec_prefix = @exec_prefix@ 4 | libdir = @libdir@ 5 | includedir = @includedir@ 6 | 7 | CC = @CC@ 8 | CFLAGS = @CFLAGS@ @GMP_CFLAGS@ @DEFS@ 9 | LDFLAGS = @LDFLAGS@ @GMP_LIBS@ @LIBS@ 10 | 11 | DISTNAME = @PACKAGE_TARNAME@-@PACKAGE_VERSION@ 12 | 13 | all: libpaillier.a TAGS 14 | 15 | # compilation and library making 16 | 17 | libpaillier.a: paillier.o 18 | rm -f $@ 19 | ar rc $@ $< 20 | 21 | test: test.o libpaillier.a 22 | $(CC) -o $@ $(LDFLAGS) $^ 23 | 24 | perf: perf.o libpaillier.a 25 | $(CC) -o $@ $(LDFLAGS) $^ 26 | 27 | %.o: %.c *.h Makefile 28 | $(CC) -c -o $@ $< $(CFLAGS) 29 | 30 | # installation 31 | 32 | dist: AUTHORS COPYING INSTALL Makefile.in NEWS README aclocal.m4 \ 33 | paillier.c paillier.h configure configure.ac install-sh m4 missing mkinstalldirs 34 | rm -rf $(DISTNAME) 35 | mkdir $(DISTNAME) 36 | cp -r $^ $(DISTNAME) 37 | rm -rf $(DISTNAME)/m4/.svn $(DISTNAME)/m4/*~ 38 | tar zc $(DISTNAME) > $(DISTNAME).tar.gz 39 | rm -rf $(DISTNAME) 40 | 41 | install: libpaillier.a paillier.h 42 | $(top_srcdir)/mkinstalldirs -m 755 $(libdir) 43 | $(top_srcdir)/mkinstalldirs -m 755 $(includedir) 44 | $(top_srcdir)/install-sh -m 755 libpaillier.a $(libdir) 45 | $(top_srcdir)/install-sh -m 644 paillier.h $(includedir) 46 | 47 | uninstall: 48 | /bin/rm -f $(libdir)/libpaillier.a 49 | /bin/rm -f $(includedir)/paillier.h 50 | 51 | # development and meta stuff 52 | 53 | TAGS: *.c *.h 54 | @(etags $^ || true) 2> /dev/null 55 | 56 | Makefile: Makefile.in config.status 57 | ./config.status 58 | 59 | config.status: configure 60 | ./config.status --recheck 61 | 62 | configure: configure.ac aclocal.m4 63 | autoconf 64 | 65 | # cleanup 66 | 67 | # remove everything an installing user can rebuild 68 | clean: 69 | rm -rf *.o *.a perf $(DISTNAME) *.tar.gz TAGS *~ 70 | 71 | # remove everything a package developer can rebuild 72 | distclean: clean 73 | rm -rf autom4te.cache Makefile config.status config.log config.cache \ 74 | configure configure.scan autoscan.log 75 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/NEWS: -------------------------------------------------------------------------------- 1 | Release 0.8: Saturday, January 30, 2010 2 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 | 4 | Fixed memory leak. Thanks to Thomas B. Pedersen for the bug report! 5 | 6 | Release 0.7: Friday, December 1, 2006 7 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 | 9 | Minor cleanup. 10 | 11 | Release 0.6: Tuesday, July 25, 2006 12 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 13 | 14 | Fixed major bug in paillier_plaintext_to_bytes. Updated the documentation 15 | a bit and did some other cleanup. 16 | 17 | Release 0.5: Monday, July 24, 2006 18 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 | 20 | First public release. No known bugs. Little documentation, but should 21 | be generally useable. 22 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/README: -------------------------------------------------------------------------------- 1 | For the most up to date version and information about this library, 2 | visit 3 | 4 | http://acsc.csl.sri.com/libpaillier 5 | 6 | Paillier [1] is a public key cryptosystem which offers an additive 7 | homomorphism, making it very useful for privacy preserving 8 | applications. This is a simple C library which implements Paillier key 9 | generation, encryption, decryption, and also makes it easy to use the 10 | homomorphism. 11 | 12 | The excellent GNU Multiple Precision Arithmetic Library (GMP) is used 13 | for the underlying number theoretic operations, so you will need to 14 | have that installed before building libpaillier. 15 | 16 | Generally, the library is written in the most straightforward manner 17 | that you may imagine given that it is written in C and uses GMP. The 18 | Paillier cryptosystem is not very complicated, and neither is 19 | libpaillier. 20 | 21 | Right now, the only documentation available is the series of comments 22 | in the header file (paillier.h). That is probably all that is 23 | necessary, however, given the simplicity of the library. 24 | 25 | There are couple security considerations to keep in mind if you intend 26 | to try to actually do something secure with libpaillier. Which you 27 | absolutely shouldn't do anyway, since there are probably other issues 28 | I'm overlooking. 29 | 30 | When the paillier_keygen function selects a modulus n = p q, it does 31 | not bother ensuring that p and q are strong primes [3]. While this was 32 | at one point considered important (e.g., it is required by ANSI 33 | X9.31), modern factoring algorithms make any advantage of strong 34 | primes doubtful [4]. RSA Laboratories no longer recommends the 35 | practice. 36 | 37 | More importantly, at no point is any special effort made to securely 38 | "shred" sensitive memory. This means that it is important that 39 | functions dealing with private keys and plaintexts (e.g., 40 | paillier_keygen and paillier_enc) only be run on trusted machines. The 41 | resulting ciphertexts and public keys, however, may of course be 42 | handled in an untrusted manner. 43 | 44 | The following paper introduced the basic algorithms that libpaillier 45 | implements. 46 | 47 | Public-Key Cryptosystems Based on Composite Degree Residuosity 48 | Classes. Pascal Paillier. EUROCRYPT 1999. 49 | 50 | This next paper is a collection of a number of improvements by Jurik 51 | and Damgard. We don't do any of this stuff, so implementing some of 52 | these things would be a great way to contribute. 53 | 54 | Extensions to the Paillier Cryptosystem with Applications to 55 | Cryptological Protocols. Mads Jurik. BRICS Dissertation Series, 56 | August 2003. 57 | 58 | Please report any bugs, questions, comments, and contributions to 59 | bethenco@cs.berkeley.edu. 60 | 61 | Cheers, 62 | 63 | John Bethencourt 64 | http://www.cs.berkeley.edu/~bethenco 65 | 66 | 67 | [1] http://en.wikipedia.org/wiki/Paillier_cryptosystem 68 | 69 | [2] http://www.swox.com/gmp 70 | 71 | [3] http://en.wikipedia.org/wiki/Strong_prime 72 | 73 | [4] http://eprint.iacr.org/2001/007 74 | 75 | [5] http://www.rsasecurity.com/rsalabs/node.asp?id=2217 76 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/aclocal.m4: -------------------------------------------------------------------------------- 1 | # generated automatically by aclocal 1.9.2 -*- Autoconf -*- 2 | 3 | # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 4 | # Free Software Foundation, Inc. 5 | # This file is free software; the Free Software Foundation 6 | # gives unlimited permission to copy and/or distribute it, 7 | # with or without modifications, as long as this notice is preserved. 8 | 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY, to the extent permitted by law; without 11 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | # PARTICULAR PURPOSE. 13 | 14 | m4_include([m4/gmp.m4]) 15 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/configure.ac: -------------------------------------------------------------------------------- 1 | dnl Process this file with autoconf to produce a configure script. 2 | AC_PREREQ(2.59) 3 | AC_INIT([libpaillier], [0.8], [bethenco@cs.berkeley.edu]) 4 | AC_CONFIG_SRCDIR([paillier.h]) 5 | 6 | dnl First, standard system stuff. 7 | 8 | dnl language and compiler support 9 | if test "x${CFLAGS}" == x; then 10 | CFLAGS="-O3 -Wall" 11 | fi 12 | if test "x${LDFLAGS}" == x; then 13 | LDFLAGS="-O3 -Wall" 14 | fi 15 | AC_PROG_CC 16 | AC_LANG(C) 17 | 18 | dnl standard headers 19 | AC_HEADER_STDC 20 | AC_CHECK_HEADERS([stdlib.h string.h],, 21 | [AC_MSG_ERROR([could not find standard headers stdlib.h, string.h])]) 22 | 23 | dnl specific library functions and types 24 | AC_FUNC_MALLOC 25 | AC_FUNC_REALLOC 26 | AC_TYPE_SIZE_T 27 | AC_CHECK_FUNCS([memset],, 28 | [AC_MSG_ERROR([could not link to required function memset])]) 29 | 30 | dnl Now, we check for specific packages we need. 31 | PRIVSS_GMP_CHECK 32 | 33 | dnl Specify the output. 34 | AC_CONFIG_FILES([Makefile]) 35 | AC_OUTPUT 36 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/install-sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # install - install a program, script, or datafile 3 | 4 | scriptversion=2004-09-10.20 5 | 6 | # This originates from X11R5 (mit/util/scripts/install.sh), which was 7 | # later released in X11R6 (xc/config/util/install.sh) with the 8 | # following copyright and license. 9 | # 10 | # Copyright (C) 1994 X Consortium 11 | # 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy 13 | # of this software and associated documentation files (the "Software"), to 14 | # deal in the Software without restriction, including without limitation the 15 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16 | # sell copies of the Software, and to permit persons to whom the Software is 17 | # furnished to do so, subject to the following conditions: 18 | # 19 | # The above copyright notice and this permission notice shall be included in 20 | # all copies or substantial portions of the Software. 21 | # 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27 | # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | # 29 | # Except as contained in this notice, the name of the X Consortium shall not 30 | # be used in advertising or otherwise to promote the sale, use or other deal- 31 | # ings in this Software without prior written authorization from the X Consor- 32 | # tium. 33 | # 34 | # 35 | # FSF changes to this file are in the public domain. 36 | # 37 | # Calling this script install-sh is preferred over install.sh, to prevent 38 | # `make' implicit rules from creating a file called install from it 39 | # when there is no Makefile. 40 | # 41 | # This script is compatible with the BSD install script, but was written 42 | # from scratch. It can only install one file at a time, a restriction 43 | # shared with many OS's install programs. 44 | 45 | # set DOITPROG to echo to test this script 46 | 47 | # Don't use :- since 4.3BSD and earlier shells don't like it. 48 | doit="${DOITPROG-}" 49 | 50 | # put in absolute paths if you don't have them in your path; or use env. vars. 51 | 52 | mvprog="${MVPROG-mv}" 53 | cpprog="${CPPROG-cp}" 54 | chmodprog="${CHMODPROG-chmod}" 55 | chownprog="${CHOWNPROG-chown}" 56 | chgrpprog="${CHGRPPROG-chgrp}" 57 | stripprog="${STRIPPROG-strip}" 58 | rmprog="${RMPROG-rm}" 59 | mkdirprog="${MKDIRPROG-mkdir}" 60 | 61 | chmodcmd="$chmodprog 0755" 62 | chowncmd= 63 | chgrpcmd= 64 | stripcmd= 65 | rmcmd="$rmprog -f" 66 | mvcmd="$mvprog" 67 | src= 68 | dst= 69 | dir_arg= 70 | dstarg= 71 | no_target_directory= 72 | 73 | usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74 | or: $0 [OPTION]... SRCFILES... DIRECTORY 75 | or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76 | or: $0 [OPTION]... -d DIRECTORIES... 77 | 78 | In the 1st form, copy SRCFILE to DSTFILE. 79 | In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80 | In the 4th, create DIRECTORIES. 81 | 82 | Options: 83 | -c (ignored) 84 | -d create directories instead of installing files. 85 | -g GROUP $chgrpprog installed files to GROUP. 86 | -m MODE $chmodprog installed files to MODE. 87 | -o USER $chownprog installed files to USER. 88 | -s $stripprog installed files. 89 | -t DIRECTORY install into DIRECTORY. 90 | -T report an error if DSTFILE is a directory. 91 | --help display this help and exit. 92 | --version display version info and exit. 93 | 94 | Environment variables override the default commands: 95 | CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96 | " 97 | 98 | while test -n "$1"; do 99 | case $1 in 100 | -c) shift 101 | continue;; 102 | 103 | -d) dir_arg=true 104 | shift 105 | continue;; 106 | 107 | -g) chgrpcmd="$chgrpprog $2" 108 | shift 109 | shift 110 | continue;; 111 | 112 | --help) echo "$usage"; exit 0;; 113 | 114 | -m) chmodcmd="$chmodprog $2" 115 | shift 116 | shift 117 | continue;; 118 | 119 | -o) chowncmd="$chownprog $2" 120 | shift 121 | shift 122 | continue;; 123 | 124 | -s) stripcmd=$stripprog 125 | shift 126 | continue;; 127 | 128 | -t) dstarg=$2 129 | shift 130 | shift 131 | continue;; 132 | 133 | -T) no_target_directory=true 134 | shift 135 | continue;; 136 | 137 | --version) echo "$0 $scriptversion"; exit 0;; 138 | 139 | *) # When -d is used, all remaining arguments are directories to create. 140 | # When -t is used, the destination is already specified. 141 | test -n "$dir_arg$dstarg" && break 142 | # Otherwise, the last argument is the destination. Remove it from $@. 143 | for arg 144 | do 145 | if test -n "$dstarg"; then 146 | # $@ is not empty: it contains at least $arg. 147 | set fnord "$@" "$dstarg" 148 | shift # fnord 149 | fi 150 | shift # arg 151 | dstarg=$arg 152 | done 153 | break;; 154 | esac 155 | done 156 | 157 | if test -z "$1"; then 158 | if test -z "$dir_arg"; then 159 | echo "$0: no input file specified." >&2 160 | exit 1 161 | fi 162 | # It's OK to call `install-sh -d' without argument. 163 | # This can happen when creating conditional directories. 164 | exit 0 165 | fi 166 | 167 | for src 168 | do 169 | # Protect names starting with `-'. 170 | case $src in 171 | -*) src=./$src ;; 172 | esac 173 | 174 | if test -n "$dir_arg"; then 175 | dst=$src 176 | src= 177 | 178 | if test -d "$dst"; then 179 | mkdircmd=: 180 | chmodcmd= 181 | else 182 | mkdircmd=$mkdirprog 183 | fi 184 | else 185 | # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186 | # might cause directories to be created, which would be especially bad 187 | # if $src (and thus $dsttmp) contains '*'. 188 | if test ! -f "$src" && test ! -d "$src"; then 189 | echo "$0: $src does not exist." >&2 190 | exit 1 191 | fi 192 | 193 | if test -z "$dstarg"; then 194 | echo "$0: no destination specified." >&2 195 | exit 1 196 | fi 197 | 198 | dst=$dstarg 199 | # Protect names starting with `-'. 200 | case $dst in 201 | -*) dst=./$dst ;; 202 | esac 203 | 204 | # If destination is a directory, append the input filename; won't work 205 | # if double slashes aren't ignored. 206 | if test -d "$dst"; then 207 | if test -n "$no_target_directory"; then 208 | echo "$0: $dstarg: Is a directory" >&2 209 | exit 1 210 | fi 211 | dst=$dst/`basename "$src"` 212 | fi 213 | fi 214 | 215 | # This sed command emulates the dirname command. 216 | dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 217 | 218 | # Make sure that the destination directory exists. 219 | 220 | # Skip lots of stat calls in the usual case. 221 | if test ! -d "$dstdir"; then 222 | defaultIFS=' 223 | ' 224 | IFS="${IFS-$defaultIFS}" 225 | 226 | oIFS=$IFS 227 | # Some sh's can't handle IFS=/ for some reason. 228 | IFS='%' 229 | set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230 | IFS=$oIFS 231 | 232 | pathcomp= 233 | 234 | while test $# -ne 0 ; do 235 | pathcomp=$pathcomp$1 236 | shift 237 | if test ! -d "$pathcomp"; then 238 | $mkdirprog "$pathcomp" 239 | # mkdir can fail with a `File exist' error in case several 240 | # install-sh are creating the directory concurrently. This 241 | # is OK. 242 | test -d "$pathcomp" || exit 243 | fi 244 | pathcomp=$pathcomp/ 245 | done 246 | fi 247 | 248 | if test -n "$dir_arg"; then 249 | $doit $mkdircmd "$dst" \ 250 | && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 251 | && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 252 | && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 253 | && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 254 | 255 | else 256 | dstfile=`basename "$dst"` 257 | 258 | # Make a couple of temp file names in the proper directory. 259 | dsttmp=$dstdir/_inst.$$_ 260 | rmtmp=$dstdir/_rm.$$_ 261 | 262 | # Trap to clean up those temp files at exit. 263 | trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 264 | trap '(exit $?); exit' 1 2 13 15 265 | 266 | # Copy the file name to the temp name. 267 | $doit $cpprog "$src" "$dsttmp" && 268 | 269 | # and set any options; do chmod last to preserve setuid bits. 270 | # 271 | # If any of these fail, we abort the whole thing. If we want to 272 | # ignore errors from any of these, just make sure not to ignore 273 | # errors from the above "$doit $cpprog $src $dsttmp" command. 274 | # 275 | { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 276 | && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 277 | && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 278 | && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 279 | 280 | # Now rename the file to the real destination. 281 | { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 282 | || { 283 | # The rename failed, perhaps because mv can't rename something else 284 | # to itself, or perhaps because mv is so ancient that it does not 285 | # support -f. 286 | 287 | # Now remove or move aside any old file at destination location. 288 | # We try this two ways since rm can't unlink itself on some 289 | # systems and the destination file might be busy for other 290 | # reasons. In this case, the final cleanup might fail but the new 291 | # file should still install successfully. 292 | { 293 | if test -f "$dstdir/$dstfile"; then 294 | $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 295 | || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 296 | || { 297 | echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 298 | (exit 1); exit 299 | } 300 | else 301 | : 302 | fi 303 | } && 304 | 305 | # Now rename the file to the real destination. 306 | $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 307 | } 308 | } 309 | fi || { (exit 1); exit; } 310 | done 311 | 312 | # The final little trick to "correctly" pass the exit status to the exit trap. 313 | { 314 | (exit 0); exit 315 | } 316 | 317 | # Local variables: 318 | # eval: (add-hook 'write-file-hooks 'time-stamp) 319 | # time-stamp-start: "scriptversion=" 320 | # time-stamp-format: "%:y-%02m-%02d.%02H" 321 | # time-stamp-end: "$" 322 | # End: 323 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/m4/gmp.m4: -------------------------------------------------------------------------------- 1 | dnl Check for GNU MP and set GMP_CFLAGS and GMP_LIBS appropriately. 2 | 3 | AC_DEFUN([PRIVSS_GMP_CHECK], 4 | [ 5 | 6 | AC_ARG_WITH(gmp-include, 7 | AC_HELP_STRING([--with-gmp-include=], 8 | [look for the header gmp.h in rather than the default search path]), 9 | [ 10 | GMP_INPUT_HEADER=$withval 11 | GMP_HEADER_PATH="${GMP_INPUT_HEADER} /usr/include /usr/local/include" 12 | ], 13 | [ 14 | GMP_HEADER_PATH="/usr/include /usr/local/include" 15 | ]) 16 | 17 | AC_ARG_WITH(gmp-lib, 18 | AC_HELP_STRING([--with-gmp-lib=], 19 | [look for libgmp in rather than the default search path]), 20 | [ 21 | GMP_INPUT_LIB=$withval 22 | GMP_LIBRARY_PATH="${GMP_INPUT_LIB} /lib /usr/lib /usr/local/lib /var/lib" 23 | ], 24 | [ 25 | GMP_LIBRARY_PATH="/lib /usr/lib /usr/local/lib /var/lib" 26 | ]) 27 | 28 | dnl input data check 29 | if test "x${GMP_INPUT_HEADER}" != x; then 30 | if test -z "${GMP_INPUT_LIB}"; then 31 | echo 'error: since you have specified the GMP header path, you must specify the' 32 | echo 'GMP library path using --with-gmp-lib.' 33 | exit 1 34 | fi 35 | fi 36 | 37 | if test "x${GMP_INPUT_LIB}" != x; then 38 | if test -z "${GMP_INPUT_HEADER}"; then 39 | echo 'error: since you have specified the GMP library path, you must specify the' 40 | echo 'GMP header path using --with-gmp-include.' 41 | exit 1 42 | fi 43 | fi 44 | 45 | min_gmp_version=ifelse([$1], ,3.1.1,$1) 46 | 47 | dnl check for existence 48 | BACKUP_CFLAGS=${CFLAGS} 49 | BACKUP_LIBS=${LIBS} 50 | 51 | AC_MSG_CHECKING(for GMP >= $min_gmp_version) 52 | 53 | gmp_found="no" 54 | 55 | for GMP_HEADER in ${GMP_HEADER_PATH} 56 | do 57 | if test -r "${GMP_HEADER}/gmp.h"; then 58 | if test "x${GMP_HEADER}" != "x/usr/include" -a "x${GMP_HEADER}" != "x/usr/local/include"; then 59 | for GMP_LIBRARY in ${GMP_LIBRARY_PATH} 60 | do 61 | if test "x${GMP_LIBRARY}" != "x/usr/lib" -a "x${GMP_LIBRARY}" != "x/usr/local/lib"; then 62 | GMP_CFLAGS="-I${GMP_HEADER}" 63 | GMP_LIBS="${GMP_LIBS} -L${GMP_LIBRARY} -lgmp" 64 | fi 65 | done 66 | else 67 | GMP_CFLAGS= 68 | GMP_LIBS="-lgmp" 69 | fi 70 | 71 | CFLAGS="${CFLAGS} ${GMP_CFLAGS}" 72 | LIBS="${LIBS} ${GMP_LIBS}" 73 | 74 | AC_TRY_LINK( 75 | [#include ], 76 | [mpz_t a; mpz_init (a);], 77 | [ 78 | AC_TRY_RUN( 79 | [#include 80 | int main() { if (__GNU_MP_VERSION < 3) return -1; 81 | else return 0; } 82 | ],[ 83 | AC_MSG_RESULT(found) 84 | gmp_found="yes" 85 | AC_SUBST(GMP_CFLAGS) 86 | AC_SUBST(GMP_LIBS) 87 | AC_DEFINE(HAVE_GMP,1,[Define if GMP is installed]) 88 | # see if we are running gmp 4.0 89 | AC_MSG_CHECKING(whether GMP is 4.0 or greater) 90 | AC_TRY_RUN( 91 | [#include 92 | int main () { if (__GNU_MP_VERSION < 4) return -1; else return 0; } 93 | ],[ 94 | AC_MSG_RESULT(yes) 95 | ],[ 96 | AC_MSG_RESULT(no) 97 | AC_DEFINE(GMP_VERSION_3,1,[Define if GMP is version 3.xxx]) 98 | GMP_VERSION="-DGMP_VERSION_3" 99 | AC_SUBST(GMP_VERSION) 100 | ],[ 101 | dnl this should never happen 102 | AC_MSG_RESULT(no) 103 | ]) 104 | break 105 | ],[ 106 | gmp_problem="$gmp_problem $GMP_HEADER" 107 | unset GMP_CFLAGS 108 | unset GMP_LIBS 109 | ],[ 110 | AC_MSG_RESULT(unknown) 111 | gmp_found="yes" 112 | echo 'You appear to be cross compiling, so there is' 113 | echo 'no way to determine whether your GMP version is new enough.' 114 | echo 'I am assuming it is' 115 | AC_SUBST(GMP_CFLAGS) 116 | AC_SUBST(GMP_LIBS) 117 | AC_DEFINE(HAVE_GMP,1,[Define if GMP is installed]) 118 | break 119 | ]) 120 | ],[ 121 | unset GMP_CFLAGS 122 | unset GMP_LIBS 123 | ]) 124 | fi 125 | done 126 | 127 | if test "x$gmp_found" = "xno"; then 128 | if test -n "$gmp_problem"; then 129 | AC_MSG_RESULT(old version) 130 | AC_MSG_ERROR([ 131 | 132 | Your version of the GNU Multiple Precision library (libgmp) is too 133 | old! Please install a more recent version from http://swox.com/gmp and 134 | try again. If more than one version is installed, try specifying a 135 | particular version with 136 | 137 | ./configure --with-gmp-include= --with-gmp-lib= 138 | 139 | See ./configure --help for more information. 140 | ]) 141 | else 142 | AC_MSG_RESULT(not found) 143 | AC_MSG_ERROR([ 144 | 145 | The GNU Multiple Precision library (libgmp) was not found on your 146 | system! Please obtain it from http://swox.com/gmp and install it 147 | before trying again. If libgmp is already installed in a non-standard 148 | location, try again with 149 | 150 | ./configure --with-gmp-include= --with-gmp-lib= 151 | 152 | See ./configure --help for more information. 153 | ]) 154 | fi 155 | fi 156 | 157 | CFLAGS=${BACKUP_CFLAGS} 158 | LIBS=${BACKUP_LIBS} 159 | 160 | ]) 161 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/missing: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Common stub for a few missing GNU programs while installing. 3 | 4 | scriptversion=2004-09-07.08 5 | 6 | # Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004 7 | # Free Software Foundation, Inc. 8 | # Originally by Fran,cois Pinard , 1996. 9 | 10 | # This program is free software; you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation; either version 2, or (at your option) 13 | # any later version. 14 | 15 | # This program is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | 20 | # You should have received a copy of the GNU General Public License 21 | # along with this program; if not, write to the Free Software 22 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23 | # 02111-1307, USA. 24 | 25 | # As a special exception to the GNU General Public License, if you 26 | # distribute this file as part of a program that contains a 27 | # configuration script generated by Autoconf, you may include it under 28 | # the same distribution terms that you use for the rest of that program. 29 | 30 | if test $# -eq 0; then 31 | echo 1>&2 "Try \`$0 --help' for more information" 32 | exit 1 33 | fi 34 | 35 | run=: 36 | 37 | # In the cases where this matters, `missing' is being run in the 38 | # srcdir already. 39 | if test -f configure.ac; then 40 | configure_ac=configure.ac 41 | else 42 | configure_ac=configure.in 43 | fi 44 | 45 | msg="missing on your system" 46 | 47 | case "$1" in 48 | --run) 49 | # Try to run requested program, and just exit if it succeeds. 50 | run= 51 | shift 52 | "$@" && exit 0 53 | # Exit code 63 means version mismatch. This often happens 54 | # when the user try to use an ancient version of a tool on 55 | # a file that requires a minimum version. In this case we 56 | # we should proceed has if the program had been absent, or 57 | # if --run hadn't been passed. 58 | if test $? = 63; then 59 | run=: 60 | msg="probably too old" 61 | fi 62 | ;; 63 | 64 | -h|--h|--he|--hel|--help) 65 | echo "\ 66 | $0 [OPTION]... PROGRAM [ARGUMENT]... 67 | 68 | Handle \`PROGRAM [ARGUMENT]...' for when PROGRAM is missing, or return an 69 | error status if there is no known handling for PROGRAM. 70 | 71 | Options: 72 | -h, --help display this help and exit 73 | -v, --version output version information and exit 74 | --run try to run the given command, and emulate it if it fails 75 | 76 | Supported PROGRAM values: 77 | aclocal touch file \`aclocal.m4' 78 | autoconf touch file \`configure' 79 | autoheader touch file \`config.h.in' 80 | automake touch all \`Makefile.in' files 81 | bison create \`y.tab.[ch]', if possible, from existing .[ch] 82 | flex create \`lex.yy.c', if possible, from existing .c 83 | help2man touch the output file 84 | lex create \`lex.yy.c', if possible, from existing .c 85 | makeinfo touch the output file 86 | tar try tar, gnutar, gtar, then tar without non-portable flags 87 | yacc create \`y.tab.[ch]', if possible, from existing .[ch] 88 | 89 | Send bug reports to ." 90 | exit 0 91 | ;; 92 | 93 | -v|--v|--ve|--ver|--vers|--versi|--versio|--version) 94 | echo "missing $scriptversion (GNU Automake)" 95 | exit 0 96 | ;; 97 | 98 | -*) 99 | echo 1>&2 "$0: Unknown \`$1' option" 100 | echo 1>&2 "Try \`$0 --help' for more information" 101 | exit 1 102 | ;; 103 | 104 | esac 105 | 106 | # Now exit if we have it, but it failed. Also exit now if we 107 | # don't have it and --version was passed (most likely to detect 108 | # the program). 109 | case "$1" in 110 | lex|yacc) 111 | # Not GNU programs, they don't have --version. 112 | ;; 113 | 114 | tar) 115 | if test -n "$run"; then 116 | echo 1>&2 "ERROR: \`tar' requires --run" 117 | exit 1 118 | elif test "x$2" = "x--version" || test "x$2" = "x--help"; then 119 | exit 1 120 | fi 121 | ;; 122 | 123 | *) 124 | if test -z "$run" && ($1 --version) > /dev/null 2>&1; then 125 | # We have it, but it failed. 126 | exit 1 127 | elif test "x$2" = "x--version" || test "x$2" = "x--help"; then 128 | # Could not run --version or --help. This is probably someone 129 | # running `$TOOL --version' or `$TOOL --help' to check whether 130 | # $TOOL exists and not knowing $TOOL uses missing. 131 | exit 1 132 | fi 133 | ;; 134 | esac 135 | 136 | # If it does not exist, or fails to run (possibly an outdated version), 137 | # try to emulate it. 138 | case "$1" in 139 | aclocal*) 140 | echo 1>&2 "\ 141 | WARNING: \`$1' is $msg. You should only need it if 142 | you modified \`acinclude.m4' or \`${configure_ac}'. You might want 143 | to install the \`Automake' and \`Perl' packages. Grab them from 144 | any GNU archive site." 145 | touch aclocal.m4 146 | ;; 147 | 148 | autoconf) 149 | echo 1>&2 "\ 150 | WARNING: \`$1' is $msg. You should only need it if 151 | you modified \`${configure_ac}'. You might want to install the 152 | \`Autoconf' and \`GNU m4' packages. Grab them from any GNU 153 | archive site." 154 | touch configure 155 | ;; 156 | 157 | autoheader) 158 | echo 1>&2 "\ 159 | WARNING: \`$1' is $msg. You should only need it if 160 | you modified \`acconfig.h' or \`${configure_ac}'. You might want 161 | to install the \`Autoconf' and \`GNU m4' packages. Grab them 162 | from any GNU archive site." 163 | files=`sed -n 's/^[ ]*A[CM]_CONFIG_HEADER(\([^)]*\)).*/\1/p' ${configure_ac}` 164 | test -z "$files" && files="config.h" 165 | touch_files= 166 | for f in $files; do 167 | case "$f" in 168 | *:*) touch_files="$touch_files "`echo "$f" | 169 | sed -e 's/^[^:]*://' -e 's/:.*//'`;; 170 | *) touch_files="$touch_files $f.in";; 171 | esac 172 | done 173 | touch $touch_files 174 | ;; 175 | 176 | automake*) 177 | echo 1>&2 "\ 178 | WARNING: \`$1' is $msg. You should only need it if 179 | you modified \`Makefile.am', \`acinclude.m4' or \`${configure_ac}'. 180 | You might want to install the \`Automake' and \`Perl' packages. 181 | Grab them from any GNU archive site." 182 | find . -type f -name Makefile.am -print | 183 | sed 's/\.am$/.in/' | 184 | while read f; do touch "$f"; done 185 | ;; 186 | 187 | autom4te) 188 | echo 1>&2 "\ 189 | WARNING: \`$1' is needed, but is $msg. 190 | You might have modified some files without having the 191 | proper tools for further handling them. 192 | You can get \`$1' as part of \`Autoconf' from any GNU 193 | archive site." 194 | 195 | file=`echo "$*" | sed -n 's/.*--output[ =]*\([^ ]*\).*/\1/p'` 196 | test -z "$file" && file=`echo "$*" | sed -n 's/.*-o[ ]*\([^ ]*\).*/\1/p'` 197 | if test -f "$file"; then 198 | touch $file 199 | else 200 | test -z "$file" || exec >$file 201 | echo "#! /bin/sh" 202 | echo "# Created by GNU Automake missing as a replacement of" 203 | echo "# $ $@" 204 | echo "exit 0" 205 | chmod +x $file 206 | exit 1 207 | fi 208 | ;; 209 | 210 | bison|yacc) 211 | echo 1>&2 "\ 212 | WARNING: \`$1' $msg. You should only need it if 213 | you modified a \`.y' file. You may need the \`Bison' package 214 | in order for those modifications to take effect. You can get 215 | \`Bison' from any GNU archive site." 216 | rm -f y.tab.c y.tab.h 217 | if [ $# -ne 1 ]; then 218 | eval LASTARG="\${$#}" 219 | case "$LASTARG" in 220 | *.y) 221 | SRCFILE=`echo "$LASTARG" | sed 's/y$/c/'` 222 | if [ -f "$SRCFILE" ]; then 223 | cp "$SRCFILE" y.tab.c 224 | fi 225 | SRCFILE=`echo "$LASTARG" | sed 's/y$/h/'` 226 | if [ -f "$SRCFILE" ]; then 227 | cp "$SRCFILE" y.tab.h 228 | fi 229 | ;; 230 | esac 231 | fi 232 | if [ ! -f y.tab.h ]; then 233 | echo >y.tab.h 234 | fi 235 | if [ ! -f y.tab.c ]; then 236 | echo 'main() { return 0; }' >y.tab.c 237 | fi 238 | ;; 239 | 240 | lex|flex) 241 | echo 1>&2 "\ 242 | WARNING: \`$1' is $msg. You should only need it if 243 | you modified a \`.l' file. You may need the \`Flex' package 244 | in order for those modifications to take effect. You can get 245 | \`Flex' from any GNU archive site." 246 | rm -f lex.yy.c 247 | if [ $# -ne 1 ]; then 248 | eval LASTARG="\${$#}" 249 | case "$LASTARG" in 250 | *.l) 251 | SRCFILE=`echo "$LASTARG" | sed 's/l$/c/'` 252 | if [ -f "$SRCFILE" ]; then 253 | cp "$SRCFILE" lex.yy.c 254 | fi 255 | ;; 256 | esac 257 | fi 258 | if [ ! -f lex.yy.c ]; then 259 | echo 'main() { return 0; }' >lex.yy.c 260 | fi 261 | ;; 262 | 263 | help2man) 264 | echo 1>&2 "\ 265 | WARNING: \`$1' is $msg. You should only need it if 266 | you modified a dependency of a manual page. You may need the 267 | \`Help2man' package in order for those modifications to take 268 | effect. You can get \`Help2man' from any GNU archive site." 269 | 270 | file=`echo "$*" | sed -n 's/.*-o \([^ ]*\).*/\1/p'` 271 | if test -z "$file"; then 272 | file=`echo "$*" | sed -n 's/.*--output=\([^ ]*\).*/\1/p'` 273 | fi 274 | if [ -f "$file" ]; then 275 | touch $file 276 | else 277 | test -z "$file" || exec >$file 278 | echo ".ab help2man is required to generate this page" 279 | exit 1 280 | fi 281 | ;; 282 | 283 | makeinfo) 284 | echo 1>&2 "\ 285 | WARNING: \`$1' is $msg. You should only need it if 286 | you modified a \`.texi' or \`.texinfo' file, or any other file 287 | indirectly affecting the aspect of the manual. The spurious 288 | call might also be the consequence of using a buggy \`make' (AIX, 289 | DU, IRIX). You might want to install the \`Texinfo' package or 290 | the \`GNU make' package. Grab either from any GNU archive site." 291 | file=`echo "$*" | sed -n 's/.*-o \([^ ]*\).*/\1/p'` 292 | if test -z "$file"; then 293 | file=`echo "$*" | sed 's/.* \([^ ]*\) *$/\1/'` 294 | file=`sed -n '/^@setfilename/ { s/.* \([^ ]*\) *$/\1/; p; q; }' $file` 295 | fi 296 | touch $file 297 | ;; 298 | 299 | tar) 300 | shift 301 | 302 | # We have already tried tar in the generic part. 303 | # Look for gnutar/gtar before invocation to avoid ugly error 304 | # messages. 305 | if (gnutar --version > /dev/null 2>&1); then 306 | gnutar "$@" && exit 0 307 | fi 308 | if (gtar --version > /dev/null 2>&1); then 309 | gtar "$@" && exit 0 310 | fi 311 | firstarg="$1" 312 | if shift; then 313 | case "$firstarg" in 314 | *o*) 315 | firstarg=`echo "$firstarg" | sed s/o//` 316 | tar "$firstarg" "$@" && exit 0 317 | ;; 318 | esac 319 | case "$firstarg" in 320 | *h*) 321 | firstarg=`echo "$firstarg" | sed s/h//` 322 | tar "$firstarg" "$@" && exit 0 323 | ;; 324 | esac 325 | fi 326 | 327 | echo 1>&2 "\ 328 | WARNING: I can't seem to be able to run \`tar' with the given arguments. 329 | You may want to install GNU tar or Free paxutils, or check the 330 | command line arguments." 331 | exit 1 332 | ;; 333 | 334 | *) 335 | echo 1>&2 "\ 336 | WARNING: \`$1' is needed, and is $msg. 337 | You might have modified some files without having the 338 | proper tools for further handling them. Check the \`README' file, 339 | it often tells you about the needed prerequisites for installing 340 | this package. You may also peek at any GNU archive site, in case 341 | some other package would contain this missing \`$1' program." 342 | exit 1 343 | ;; 344 | esac 345 | 346 | exit 0 347 | 348 | # Local variables: 349 | # eval: (add-hook 'write-file-hooks 'time-stamp) 350 | # time-stamp-start: "scriptversion=" 351 | # time-stamp-format: "%:y-%02m-%02d.%02H" 352 | # time-stamp-end: "$" 353 | # End: 354 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/mkinstalldirs: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # mkinstalldirs --- make directory hierarchy 3 | 4 | scriptversion=2004-02-15.20 5 | 6 | # Original author: Noah Friedman 7 | # Created: 1993-05-16 8 | # Public domain. 9 | # 10 | # This file is maintained in Automake, please report 11 | # bugs to or send patches to 12 | # . 13 | 14 | errstatus=0 15 | dirmode="" 16 | 17 | usage="\ 18 | Usage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ... 19 | 20 | Create each directory DIR (with mode MODE, if specified), including all 21 | leading file name components. 22 | 23 | Report bugs to ." 24 | 25 | # process command line arguments 26 | while test $# -gt 0 ; do 27 | case $1 in 28 | -h | --help | --h*) # -h for help 29 | echo "$usage" 30 | exit 0 31 | ;; 32 | -m) # -m PERM arg 33 | shift 34 | test $# -eq 0 && { echo "$usage" 1>&2; exit 1; } 35 | dirmode=$1 36 | shift 37 | ;; 38 | --version) 39 | echo "$0 $scriptversion" 40 | exit 0 41 | ;; 42 | --) # stop option processing 43 | shift 44 | break 45 | ;; 46 | -*) # unknown option 47 | echo "$usage" 1>&2 48 | exit 1 49 | ;; 50 | *) # first non-opt arg 51 | break 52 | ;; 53 | esac 54 | done 55 | 56 | for file 57 | do 58 | if test -d "$file"; then 59 | shift 60 | else 61 | break 62 | fi 63 | done 64 | 65 | case $# in 66 | 0) exit 0 ;; 67 | esac 68 | 69 | # Solaris 8's mkdir -p isn't thread-safe. If you mkdir -p a/b and 70 | # mkdir -p a/c at the same time, both will detect that a is missing, 71 | # one will create a, then the other will try to create a and die with 72 | # a "File exists" error. This is a problem when calling mkinstalldirs 73 | # from a parallel make. We use --version in the probe to restrict 74 | # ourselves to GNU mkdir, which is thread-safe. 75 | case $dirmode in 76 | '') 77 | if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then 78 | echo "mkdir -p -- $*" 79 | exec mkdir -p -- "$@" 80 | else 81 | # On NextStep and OpenStep, the `mkdir' command does not 82 | # recognize any option. It will interpret all options as 83 | # directories to create, and then abort because `.' already 84 | # exists. 85 | test -d ./-p && rmdir ./-p 86 | test -d ./--version && rmdir ./--version 87 | fi 88 | ;; 89 | *) 90 | if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 && 91 | test ! -d ./--version; then 92 | echo "mkdir -m $dirmode -p -- $*" 93 | exec mkdir -m "$dirmode" -p -- "$@" 94 | else 95 | # Clean up after NextStep and OpenStep mkdir. 96 | for d in ./-m ./-p ./--version "./$dirmode"; 97 | do 98 | test -d $d && rmdir $d 99 | done 100 | fi 101 | ;; 102 | esac 103 | 104 | for file 105 | do 106 | set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'` 107 | shift 108 | 109 | pathcomp= 110 | for d 111 | do 112 | pathcomp="$pathcomp$d" 113 | case $pathcomp in 114 | -*) pathcomp=./$pathcomp ;; 115 | esac 116 | 117 | if test ! -d "$pathcomp"; then 118 | echo "mkdir $pathcomp" 119 | 120 | mkdir "$pathcomp" || lasterr=$? 121 | 122 | if test ! -d "$pathcomp"; then 123 | errstatus=$lasterr 124 | else 125 | if test ! -z "$dirmode"; then 126 | echo "chmod $dirmode $pathcomp" 127 | lasterr="" 128 | chmod "$dirmode" "$pathcomp" || lasterr=$? 129 | 130 | if test ! -z "$lasterr"; then 131 | errstatus=$lasterr 132 | fi 133 | fi 134 | fi 135 | fi 136 | 137 | pathcomp="$pathcomp/" 138 | done 139 | done 140 | 141 | exit $errstatus 142 | 143 | # Local Variables: 144 | # mode: shell-script 145 | # sh-indentation: 2 146 | # eval: (add-hook 'write-file-hooks 'time-stamp) 147 | # time-stamp-start: "scriptversion=" 148 | # time-stamp-format: "%:y-%02m-%02d.%02H" 149 | # time-stamp-end: "$" 150 | # End: 151 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/paillier.c: -------------------------------------------------------------------------------- 1 | /* 2 | libpaillier - A library implementing the Paillier cryptosystem. 3 | 4 | Copyright (C) 2006 SRI International. 5 | 6 | This program is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation; either version 2 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "paillier.h" 22 | 23 | void 24 | init_rand( gmp_randstate_t rand, paillier_get_rand_t get_rand, int bytes ) 25 | { 26 | void* buf; 27 | mpz_t s; 28 | 29 | buf = malloc(bytes); 30 | get_rand(buf, bytes); 31 | 32 | gmp_randinit_default(rand); 33 | mpz_init(s); 34 | mpz_import(s, bytes, 1, 1, 0, 0, buf); 35 | gmp_randseed(rand, s); 36 | mpz_clear(s); 37 | 38 | free(buf); 39 | } 40 | 41 | void 42 | complete_pubkey( paillier_pubkey_t* pub ) 43 | { 44 | mpz_mul(pub->n_squared, pub->n, pub->n); 45 | mpz_add_ui(pub->n_plusone, pub->n, 1); 46 | } 47 | 48 | void 49 | complete_prvkey( paillier_prvkey_t* prv, paillier_pubkey_t* pub ) 50 | { 51 | mpz_powm(prv->x, pub->n_plusone, prv->lambda, pub->n_squared); 52 | mpz_sub_ui(prv->x, prv->x, 1); 53 | mpz_div(prv->x, prv->x, pub->n); 54 | mpz_invert(prv->x, prv->x, pub->n); 55 | } 56 | 57 | void 58 | paillier_keygen( int modulusbits, 59 | paillier_pubkey_t** pub, 60 | paillier_prvkey_t** prv, 61 | paillier_get_rand_t get_rand ) 62 | { 63 | mpz_t p; 64 | mpz_t q; 65 | gmp_randstate_t rand; 66 | 67 | /* allocate the new key structures */ 68 | 69 | *pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); 70 | *prv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); 71 | 72 | /* initialize our integers */ 73 | 74 | mpz_init((*pub)->n); 75 | mpz_init((*pub)->n_squared); 76 | mpz_init((*pub)->n_plusone); 77 | mpz_init((*prv)->lambda); 78 | mpz_init((*prv)->x); 79 | mpz_init(p); 80 | mpz_init(q); 81 | 82 | /* pick random (modulusbits/2)-bit primes p and q */ 83 | 84 | init_rand(rand, get_rand, modulusbits / 8 + 1); 85 | do 86 | { 87 | do 88 | mpz_urandomb(p, rand, modulusbits / 2); 89 | while( !mpz_probab_prime_p(p, 10) ); 90 | 91 | do 92 | mpz_urandomb(q, rand, modulusbits / 2); 93 | while( !mpz_probab_prime_p(q, 10) ); 94 | 95 | /* compute the public modulus n = p q */ 96 | 97 | mpz_mul((*pub)->n, p, q); 98 | } while( !mpz_tstbit((*pub)->n, modulusbits - 1) ); 99 | complete_pubkey(*pub); 100 | (*pub)->bits = modulusbits; 101 | 102 | /* compute the private key lambda = lcm(p-1,q-1) */ 103 | 104 | mpz_sub_ui(p, p, 1); 105 | mpz_sub_ui(q, q, 1); 106 | mpz_lcm((*prv)->lambda, p, q); 107 | complete_prvkey(*prv, *pub); 108 | 109 | /* clear temporary integers and randstate */ 110 | 111 | mpz_clear(p); 112 | mpz_clear(q); 113 | gmp_randclear(rand); 114 | } 115 | 116 | paillier_ciphertext_t* 117 | paillier_enc( paillier_ciphertext_t* res, 118 | paillier_pubkey_t* pub, 119 | paillier_plaintext_t* pt, 120 | paillier_get_rand_t get_rand ) 121 | { 122 | mpz_t r; 123 | gmp_randstate_t rand; 124 | mpz_t x; 125 | 126 | /* pick random blinding factor */ 127 | 128 | mpz_init(r); 129 | init_rand(rand, get_rand, pub->bits / 8 + 1); 130 | do 131 | mpz_urandomb(r, rand, pub->bits); 132 | while( mpz_cmp(r, pub->n) >= 0 ); 133 | 134 | /* compute ciphertext */ 135 | 136 | if( !res ) 137 | { 138 | res = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); 139 | mpz_init(res->c); 140 | } 141 | 142 | mpz_init(x); 143 | mpz_powm(res->c, pub->n_plusone, pt->m, pub->n_squared); 144 | mpz_powm(x, r, pub->n, pub->n_squared); 145 | 146 | mpz_mul(res->c, res->c, x); 147 | mpz_mod(res->c, res->c, pub->n_squared); 148 | 149 | mpz_clear(x); 150 | mpz_clear(r); 151 | gmp_randclear(rand); 152 | 153 | return res; 154 | } 155 | 156 | paillier_plaintext_t* 157 | paillier_dec( paillier_plaintext_t* res, 158 | paillier_pubkey_t* pub, 159 | paillier_prvkey_t* prv, 160 | paillier_ciphertext_t* ct ) 161 | { 162 | if( !res ) 163 | { 164 | res = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); 165 | mpz_init(res->m); 166 | } 167 | 168 | mpz_powm(res->m, ct->c, prv->lambda, pub->n_squared); 169 | mpz_sub_ui(res->m, res->m, 1); 170 | mpz_div(res->m, res->m, pub->n); 171 | mpz_mul(res->m, res->m, prv->x); 172 | mpz_mod(res->m, res->m, pub->n); 173 | 174 | return res; 175 | } 176 | 177 | void 178 | paillier_mul( paillier_pubkey_t* pub, 179 | paillier_ciphertext_t* res, 180 | paillier_ciphertext_t* ct0, 181 | paillier_ciphertext_t* ct1 ) 182 | { 183 | mpz_mul(res->c, ct0->c, ct1->c); 184 | mpz_mod(res->c, res->c, pub->n_squared); 185 | } 186 | 187 | void 188 | paillier_exp( paillier_pubkey_t* pub, 189 | paillier_ciphertext_t* res, 190 | paillier_ciphertext_t* ct, 191 | paillier_plaintext_t* pt ) 192 | { 193 | mpz_powm(res->c, ct->c, pt->m, pub->n_squared); 194 | } 195 | 196 | paillier_plaintext_t* 197 | paillier_plaintext_from_ui( unsigned long int x ) 198 | { 199 | paillier_plaintext_t* pt; 200 | 201 | pt = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); 202 | mpz_init_set_ui(pt->m, x); 203 | 204 | return pt; 205 | } 206 | 207 | paillier_plaintext_t* 208 | paillier_plaintext_from_bytes( void* m, int len ) 209 | { 210 | paillier_plaintext_t* pt; 211 | 212 | pt = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); 213 | mpz_init(pt->m); 214 | mpz_import(pt->m, len, 1, 1, 0, 0, m); 215 | 216 | return pt; 217 | } 218 | 219 | void* 220 | paillier_plaintext_to_bytes( int len, 221 | paillier_plaintext_t* pt ) 222 | { 223 | void* buf0; 224 | void* buf1; 225 | size_t written; 226 | 227 | buf0 = mpz_export(0, &written, 1, 1, 0, 0, pt->m); 228 | 229 | if( written == len ) 230 | return buf0; 231 | 232 | buf1 = malloc(len); 233 | memset(buf1, 0, len); 234 | 235 | if( written == 0 ) 236 | /* no need to copy anything, pt->m = 0 and buf0 was not allocated */ 237 | return buf1; 238 | else if( written < len ) 239 | /* pad with leading zeros */ 240 | memcpy(buf1 + (len - written), buf0, written); 241 | else 242 | /* truncate leading garbage */ 243 | memcpy(buf1, buf0 + (written - len), len); 244 | 245 | free(buf0); 246 | 247 | return buf1; 248 | } 249 | 250 | paillier_plaintext_t* 251 | paillier_plaintext_from_str( char* str ) 252 | { 253 | return paillier_plaintext_from_bytes(str, strlen(str)); 254 | } 255 | 256 | char* 257 | paillier_plaintext_to_str( paillier_plaintext_t* pt ) 258 | { 259 | char* buf; 260 | size_t len; 261 | 262 | buf = (char*) mpz_export(0, &len, 1, 1, 0, 0, pt->m); 263 | buf = (char*) realloc(buf, len + 1); 264 | buf[len] = 0; 265 | 266 | return buf; 267 | } 268 | 269 | paillier_ciphertext_t* 270 | paillier_ciphertext_from_bytes( void* c, int len ) 271 | { 272 | paillier_ciphertext_t* ct; 273 | 274 | ct = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); 275 | mpz_init(ct->c); 276 | mpz_import(ct->c, len, 1, 1, 0, 0, c); 277 | 278 | return ct; 279 | } 280 | 281 | void* 282 | paillier_ciphertext_to_bytes( int len, 283 | paillier_ciphertext_t* ct ) 284 | { 285 | void* buf; 286 | int cur_len; 287 | 288 | cur_len = mpz_sizeinbase(ct->c, 2); 289 | cur_len = PAILLIER_BITS_TO_BYTES(cur_len); 290 | buf = malloc(len); 291 | memset(buf, 0, len); 292 | mpz_export(buf + (len - cur_len), 0, 1, 1, 0, 0, ct->c); 293 | 294 | return buf; 295 | } 296 | 297 | char* 298 | paillier_pubkey_to_hex( paillier_pubkey_t* pub ) 299 | { 300 | return mpz_get_str(0, 16, pub->n); 301 | } 302 | 303 | char* 304 | paillier_prvkey_to_hex( paillier_prvkey_t* prv ) 305 | { 306 | return mpz_get_str(0, 16, prv->lambda); 307 | } 308 | 309 | paillier_pubkey_t* 310 | paillier_pubkey_from_hex( char* str ) 311 | { 312 | paillier_pubkey_t* pub; 313 | 314 | pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); 315 | mpz_init_set_str(pub->n, str, 16); 316 | pub->bits = mpz_sizeinbase(pub->n, 2); 317 | mpz_init(pub->n_squared); 318 | mpz_init(pub->n_plusone); 319 | complete_pubkey(pub); 320 | 321 | return pub; 322 | } 323 | 324 | paillier_prvkey_t* 325 | paillier_prvkey_from_hex( char* str, paillier_pubkey_t* pub ) 326 | { 327 | paillier_prvkey_t* prv; 328 | 329 | prv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); 330 | mpz_init_set_str(prv->lambda, str, 16); 331 | mpz_init(prv->x); 332 | complete_prvkey(prv, pub); 333 | 334 | return prv; 335 | } 336 | 337 | void 338 | paillier_freepubkey( paillier_pubkey_t* pub ) 339 | { 340 | mpz_clear(pub->n); 341 | mpz_clear(pub->n_squared); 342 | mpz_clear(pub->n_plusone); 343 | free(pub); 344 | } 345 | 346 | void 347 | paillier_freeprvkey( paillier_prvkey_t* prv ) 348 | { 349 | mpz_clear(prv->lambda); 350 | mpz_clear(prv->x); 351 | free(prv); 352 | } 353 | 354 | void 355 | paillier_freeplaintext( paillier_plaintext_t* pt ) 356 | { 357 | mpz_clear(pt->m); 358 | free(pt); 359 | } 360 | 361 | void 362 | paillier_freeciphertext( paillier_ciphertext_t* ct ) 363 | { 364 | mpz_clear(ct->c); 365 | free(ct); 366 | } 367 | 368 | void 369 | paillier_get_rand_file( void* buf, int len, char* file ) 370 | { 371 | FILE* fp; 372 | void* p; 373 | 374 | fp = fopen(file, "r"); 375 | 376 | p = buf; 377 | while( len ) 378 | { 379 | size_t s; 380 | s = fread(p, 1, len, fp); 381 | p += s; 382 | len -= s; 383 | } 384 | 385 | fclose(fp); 386 | } 387 | 388 | void 389 | paillier_get_rand_devrandom( void* buf, int len ) 390 | { 391 | paillier_get_rand_file(buf, len, "/dev/random"); 392 | } 393 | 394 | void 395 | paillier_get_rand_devurandom( void* buf, int len ) 396 | { 397 | paillier_get_rand_file(buf, len, "/dev/urandom"); 398 | } 399 | 400 | paillier_ciphertext_t* 401 | paillier_create_enc_zero() 402 | { 403 | paillier_ciphertext_t* ct; 404 | 405 | /* make a NON-RERANDOMIZED encryption of zero for the purposes of 406 | homomorphic computation */ 407 | 408 | /* note that this is just the number 1 */ 409 | 410 | ct = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); 411 | mpz_init_set_ui(ct->c, 1); 412 | 413 | return ct; 414 | } 415 | -------------------------------------------------------------------------------- /c-libpaillier/libpaillier-0.8/paillier.h: -------------------------------------------------------------------------------- 1 | /* 2 | libpaillier - A library implementing the Paillier cryptosystem. 3 | 4 | Copyright (C) 2006 SRI International. 5 | 6 | This program is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation; either version 2 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | */ 16 | 17 | /* 18 | Include gmp.h before including this file. 19 | */ 20 | 21 | /* 22 | IMPORTANT SECURITY NOTES: 23 | 24 | On paillier_keygen and strong primes: 25 | 26 | When selecting a modulus n = p q, we do not bother ensuring that p 27 | and q are strong primes. While this was at one point considered 28 | important (e.g., it is required by ANSI X9.31), modern factoring 29 | algorithms make any advantage of strong primes doubtful [1]. RSA 30 | Laboratories no longer recommends the practice [2]. 31 | 32 | On memory handling: 33 | 34 | At no point is any special effort made to securely "shred" sensitive 35 | memory or prevent it from being paged out to disk. This means that 36 | it is important that functions dealing with private keys and 37 | plaintexts (e.g., paillier_keygen and paillier_enc) only be run on 38 | trusted machines. The resulting ciphertexts and public keys, 39 | however, may of course be handled in an untrusted manner. 40 | 41 | [1] Are "strong" primes needed for RSA? Ron Rivest and Robert 42 | Silverman. Cryptology ePrint Archive, Report 2001/007, 2001. 43 | 44 | [2] RSA Laboratories' Frequently Asked Questions About Today's 45 | Cryptography, Version 4.1, Section 3.1.4. 46 | */ 47 | 48 | /****** 49 | TYPES 50 | *******/ 51 | 52 | /* 53 | This represents a Paillier public key, which is basically just the 54 | modulus n. The other values should be considered private. 55 | */ 56 | typedef struct 57 | { 58 | int bits; /* e.g., 1024 */ 59 | mpz_t n; /* public modulus n = p q */ 60 | mpz_t n_squared; /* cached to avoid recomputing */ 61 | mpz_t n_plusone; /* cached to avoid recomputing */ 62 | } paillier_pubkey_t; 63 | 64 | /* 65 | This represents a Paillier private key; it needs to be used with a 66 | paillier_pubkey_t to be meaningful. It only includes the Carmichael 67 | function (lambda) of the modulus. The other value is kept for 68 | efficiency and should be considered private. 69 | */ 70 | typedef struct 71 | { 72 | mpz_t lambda; /* lambda(n), i.e., lcm(p-1,q-1) */ 73 | mpz_t x; /* cached to avoid recomputing */ 74 | } paillier_prvkey_t; 75 | 76 | /* 77 | This is a (semantic rather than structural) type for plaintexts. 78 | These can be converted to and from ASCII strings and byte arrays. 79 | */ 80 | typedef struct 81 | { 82 | mpz_t m; 83 | } paillier_plaintext_t; 84 | 85 | /* 86 | This is a (semantic rather than structural) type for ciphertexts. 87 | These can also be converted to or from byte arrays (for example in 88 | order to store them in a file). 89 | */ 90 | typedef struct 91 | { 92 | mpz_t c; 93 | } paillier_ciphertext_t; 94 | 95 | /* 96 | This is the type of the callback functions used to obtain the 97 | randomness needed by the probabilistic algorithms. The functions 98 | paillier_get_rand_devrandom and paillier_get_rand_devurandom 99 | (documented later) may be passed to any library function requiring a 100 | paillier_get_rand_t, or you may implement your own. If you implement 101 | your own such function, it should fill in "len" random bytes in the 102 | array "buf". 103 | */ 104 | typedef void (*paillier_get_rand_t) ( void* buf, int len ); 105 | 106 | /***************** 107 | BASIC OPERATIONS 108 | *****************/ 109 | 110 | /* 111 | Generate a keypair of length modulusbits using randomness from the 112 | provided get_rand function. Space will be allocated for each of the 113 | keys, and the given pointers will be set to point to the new 114 | paillier_pubkey_t and paillier_prvkey_t structures. The functions 115 | paillier_get_rand_devrandom and paillier_get_rand_devurandom may be 116 | passed as the final argument. 117 | */ 118 | void paillier_keygen( int modulusbits, 119 | paillier_pubkey_t** pub, 120 | paillier_prvkey_t** prv, 121 | paillier_get_rand_t get_rand ); 122 | 123 | /* 124 | Encrypt the given plaintext with the given public key using 125 | randomness from get_rand for blinding. If res is not null, its 126 | contents will be overwritten with the result. Otherwise, a new 127 | paillier_ciphertext_t will be allocated and returned. 128 | */ 129 | paillier_ciphertext_t* paillier_enc( paillier_ciphertext_t* res, 130 | paillier_pubkey_t* pub, 131 | paillier_plaintext_t* pt, 132 | paillier_get_rand_t get_rand ); 133 | 134 | /* 135 | Decrypt the given ciphertext with the given key pair. If res is not 136 | null, its contents will be overwritten with the result. Otherwise, a 137 | new paillier_plaintext_t will be allocated and returned. 138 | */ 139 | paillier_plaintext_t* paillier_dec( paillier_plaintext_t* res, 140 | paillier_pubkey_t* pub, 141 | paillier_prvkey_t* prv, 142 | paillier_ciphertext_t* ct ); 143 | 144 | /***************************** 145 | USE OF ADDITIVE HOMOMORPHISM 146 | *****************************/ 147 | 148 | /* 149 | Multiply the two ciphertexts assuming the modulus in the given 150 | public key and store the result in the contents of res, which is 151 | assumed to have already been allocated. 152 | */ 153 | void paillier_mul( paillier_pubkey_t* pub, 154 | paillier_ciphertext_t* res, 155 | paillier_ciphertext_t* ct0, 156 | paillier_ciphertext_t* ct1 ); 157 | 158 | /* 159 | Raise the given ciphertext to power pt and store the result in res, 160 | which is assumed to be already allocated. If ct is an encryption of 161 | x, then res will become an encryption of x * pt mod n, where n is 162 | the modulus in pub. 163 | */ 164 | void paillier_exp( paillier_pubkey_t* pub, 165 | paillier_ciphertext_t* res, 166 | paillier_ciphertext_t* ct, 167 | paillier_plaintext_t* pt ); 168 | 169 | /**************************** 170 | PLAINTEXT IMPORT AND EXPORT 171 | ****************************/ 172 | 173 | /* 174 | Allocate and initialize a paillier_plaintext_t from an unsigned long 175 | integer, an array of bytes, or a null terminated string. 176 | */ 177 | paillier_plaintext_t* paillier_plaintext_from_ui( unsigned long int x ); 178 | paillier_plaintext_t* paillier_plaintext_from_bytes( void* m, int len ); 179 | paillier_plaintext_t* paillier_plaintext_from_str( char* str ); 180 | 181 | /* 182 | Export a paillier_plaintext_t as a null terminated string or an 183 | array of bytes. In either case the result is allocated for the 184 | caller and the original paillier_plaintext_t is unchanged. 185 | */ 186 | char* paillier_plaintext_to_str( paillier_plaintext_t* pt ); 187 | void* paillier_plaintext_to_bytes( int len, paillier_plaintext_t* pt ); 188 | 189 | /***************************** 190 | CIPHERTEXT IMPORT AND EXPORT 191 | *****************************/ 192 | 193 | /* 194 | Import or export a paillier_ciphertext_t from or to an array of 195 | bytes. These behave like the corresponding functions for 196 | paillier_plaintext_t's. 197 | */ 198 | paillier_ciphertext_t* paillier_ciphertext_from_bytes( void* c, int len ); 199 | void* paillier_ciphertext_to_bytes( int len, paillier_ciphertext_t* ct ); 200 | 201 | /********************** 202 | KEY IMPORT AND EXPORT 203 | **********************/ 204 | 205 | /* 206 | Import or export public and private keys from or to hexadecimal, 207 | ASCII strings, which are suitable for I/O. Note that the 208 | corresponding public key is necessary to initialize a private key 209 | from a hex string. In all cases, the returned value is allocated for 210 | the caller and the values passed are unchanged. 211 | */ 212 | char* paillier_pubkey_to_hex( paillier_pubkey_t* pub ); 213 | char* paillier_prvkey_to_hex( paillier_prvkey_t* prv ); 214 | paillier_pubkey_t* paillier_pubkey_from_hex( char* str ); 215 | paillier_prvkey_t* paillier_prvkey_from_hex( char* str, 216 | paillier_pubkey_t* pub ); 217 | 218 | /******** 219 | CLEANUP 220 | ********/ 221 | 222 | /* 223 | These free the structures allocated and returned by various 224 | functions within library and should be used when the structures are 225 | no longer needed. 226 | */ 227 | void paillier_freepubkey( paillier_pubkey_t* pub ); 228 | void paillier_freeprvkey( paillier_prvkey_t* prv ); 229 | void paillier_freeplaintext( paillier_plaintext_t* pt ); 230 | void paillier_freeciphertext( paillier_ciphertext_t* ct ); 231 | 232 | /*********** 233 | MISC STUFF 234 | ***********/ 235 | 236 | /* 237 | These functions may be passed to the paillier_keygen and 238 | paillier_enc functions to provide a source of random numbers. The 239 | first reads bytes from /dev/random. On Linux, this device 240 | exclusively returns entropy gathered from environmental noise and 241 | therefore frequently blocks when not enough is available. The second 242 | returns bytes from /dev/urandom. On Linux, this device also returns 243 | environmental noise, but augments it with a pseudo-random number 244 | generator when not enough is available. The latter is probably the 245 | better choice unless you have a specific reason to believe it is 246 | insufficient. 247 | */ 248 | void paillier_get_rand_devrandom( void* buf, int len ); 249 | void paillier_get_rand_devurandom( void* buf, int len ); 250 | 251 | /* 252 | This function just allocates and returns a paillier_ciphertext_t 253 | which is a valid, _unblinded_ encryption of zero (which may actually 254 | be done without knowledge of a public key). Note that this 255 | encryption is UNBLINDED, so don't use it unless you want anyone who 256 | sees it to know it is an encryption of zero. This function is 257 | sometimes handy to get some homomorphic computations started or 258 | quickly allocate a paillier_ciphertext_t in which to place some 259 | later result. 260 | */ 261 | paillier_ciphertext_t* paillier_create_enc_zero(); 262 | 263 | /* 264 | Just a utility used internally when we need round a number of bits 265 | up the number of bytes necessary to hold them. 266 | */ 267 | #define PAILLIER_BITS_TO_BYTES(n) ((n) % 8 ? (n) / 8 + 1 : (n) / 8) 268 | -------------------------------------------------------------------------------- /go-go-gadget-paillier/.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /go-go-gadget-paillier/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Olaoluwa Osuntokun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /go-go-gadget-paillier/README.md: -------------------------------------------------------------------------------- 1 | # go-go-gadget-paillier 2 | An optimazed Go implementation based on [go-go-gadget-paillier](https://github.com/Roasbeef/go-go-gadget-paillier). 3 | 4 | ## Prerequisites 5 | go-go-gadget-paillier must be installed. 6 | ```bash 7 | $ go get github.com/mcornejo/go-go-gadget-paillier 8 | ``` 9 | 10 | ## Benchmarks 11 | ```bash 12 | $ go test -timeout 5h -bench=. -benchtime=2m 13 | ``` 14 | 15 | ## Tests 16 | ```bash 17 | $ go test 18 | ``` 19 | 20 | 21 | ## Warning 22 | This library was created primarily for education purposes, with future application for a course project. You should **NOT USE THIS CODE IN PRODUCTION SYSTEMS**. 23 | -------------------------------------------------------------------------------- /go-go-gadget-paillier/example_test.go: -------------------------------------------------------------------------------- 1 | package paillier_test 2 | 3 | import ( 4 | "crypto/rand" 5 | "fmt" 6 | "math/big" 7 | 8 | "github.com/mcornejo/go-go-gadget-paillier" 9 | ) 10 | 11 | // This example demonstrates basic usage of this library. 12 | // Features shown: 13 | // * Encrypt/Decrypt 14 | // * Homomorphic cipher text addition 15 | // * Homomorphic addition with constant 16 | // * Homomorphic multiplication with constant 17 | func main() { 18 | // Generate a 128-bit private key. 19 | privKey, err := paillier.GenerateKey(rand.Reader, 128) 20 | if err != nil { 21 | fmt.Println(err) 22 | return 23 | } 24 | 25 | // Encrypt the number "15". 26 | m15 := new(big.Int).SetInt64(15) 27 | c15, err := paillier.Encrypt(&privKey.PublicKey, m15.Bytes()) 28 | if err != nil { 29 | fmt.Println(err) 30 | return 31 | } 32 | 33 | // Decrypt the number "15". 34 | d, err := paillier.Decrypt(privKey, c15) 35 | if err != nil { 36 | fmt.Println(err) 37 | return 38 | } 39 | plainText := new(big.Int).SetBytes(d) 40 | fmt.Println("Decryption Result of 15: ", plainText.String()) 41 | 42 | // Now for the fun stuff. 43 | // Encrypt the number "20". 44 | m20 := new(big.Int).SetInt64(20) 45 | c20, err := paillier.Encrypt(&privKey.PublicKey, m20.Bytes()) 46 | if err != nil { 47 | fmt.Println(err) 48 | return 49 | } 50 | 51 | // Add the encrypted integers 15 and 20 together. 52 | plusM16M20 := paillier.AddCipher(&privKey.PublicKey, c15, c20) 53 | decryptedAddition, err := paillier.Decrypt(privKey, plusM16M20) 54 | if err != nil { 55 | fmt.Println(err) 56 | return 57 | } 58 | fmt.Println("Result of 15+20 after decryption: ", 59 | new(big.Int).SetBytes(decryptedAddition).String()) // 35 60 | 61 | // Add the encrypted integer 15 to plaintext constant 10. 62 | plusE15and10 := paillier.Add(&privKey.PublicKey, c15, new(big.Int).SetInt64(10).Bytes()) 63 | decryptedAddition, err = paillier.Decrypt(privKey, plusE15and10) 64 | if err != nil { 65 | fmt.Println(err) 66 | return 67 | } 68 | fmt.Println("Result of 15+10 after decryption: ", 69 | new(big.Int).SetBytes(decryptedAddition).String()) // 25 70 | 71 | // Multiply the encrypted integer 15 by the plaintext constant 10. 72 | mulE15and10 := paillier.Mul(&privKey.PublicKey, c15, new(big.Int).SetInt64(10).Bytes()) 73 | decryptedMul, err := paillier.Decrypt(privKey, mulE15and10) 74 | if err != nil { 75 | fmt.Println(err) 76 | return 77 | } 78 | fmt.Println("Result of 15*10 after decryption: ", 79 | new(big.Int).SetBytes(decryptedMul).String()) // 150 80 | } 81 | -------------------------------------------------------------------------------- /go-go-gadget-paillier/imgs/Inspector-gadget.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snipsco/paillier-libraries-benchmarks/6a6881b3fdd67b744ce90d7baf11197778584576/go-go-gadget-paillier/imgs/Inspector-gadget.jpg -------------------------------------------------------------------------------- /java-javallier/README.md: -------------------------------------------------------------------------------- 1 | # Javallier / JMH 2 | 3 | This is the JMB bench for the [javallier](https://github.com/NICTA/javallier) library written in Java. 4 | 5 | ## Prerequisites 6 | 7 | Java and Maven must be installed. 8 | 9 | ## Build it 10 | 11 | `mvn install` 12 | 13 | ## Run it 14 | 15 | `java -jar target/benchmarks.jar` 16 | 17 | Due to JVM relatively high performance variability, this takes a while. You can 18 | tweak various running parameters or select different test from the command line. 19 | 20 | For instance: 21 | 22 | `java -jar target/benchmarks.jar Add -r 200ms -w 200ms -f 3` 23 | 24 | More options: 25 | 26 | `java -jar target/benchmarks.jar -h` 27 | -------------------------------------------------------------------------------- /java-javallier/pom.xml: -------------------------------------------------------------------------------- 1 | 31 | 32 | 34 | 4.0.0 35 | 36 | ai.snips 37 | jmh-javallier 38 | 1.0 39 | jar 40 | 41 | JMH benchmark sample: Java 42 | 43 | 47 | 48 | 49 | 3.0 50 | 51 | 52 | 53 | 54 | org.openjdk.jmh 55 | jmh-core 56 | ${jmh.version} 57 | 58 | 59 | org.openjdk.jmh 60 | jmh-generator-annprocess 61 | ${jmh.version} 62 | provided 63 | 64 | 65 | com.n1analytics 66 | javallier_2.10 67 | 0.4.2 68 | 69 | 70 | 71 | 72 | UTF-8 73 | 74 | 77 | 1.17.3 78 | 79 | 82 | 1.8 83 | 84 | 87 | benchmarks 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-compiler-plugin 95 | 3.1 96 | 97 | ${javac.target} 98 | ${javac.target} 99 | ${javac.target} 100 | 101 | 102 | 103 | org.apache.maven.plugins 104 | maven-shade-plugin 105 | 2.2 106 | 107 | 108 | package 109 | 110 | shade 111 | 112 | 113 | ${uberjar.name} 114 | 115 | 116 | org.openjdk.jmh.Main 117 | 118 | 119 | 120 | 121 | 125 | *:* 126 | 127 | META-INF/*.SF 128 | META-INF/*.DSA 129 | META-INF/*.RSA 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | maven-clean-plugin 142 | 2.5 143 | 144 | 145 | maven-deploy-plugin 146 | 2.8.1 147 | 148 | 149 | maven-install-plugin 150 | 2.5.1 151 | 152 | 153 | maven-jar-plugin 154 | 2.4 155 | 156 | 157 | maven-javadoc-plugin 158 | 2.9.1 159 | 160 | 161 | maven-resources-plugin 162 | 2.6 163 | 164 | 165 | maven-site-plugin 166 | 3.3 167 | 168 | 169 | maven-source-plugin 170 | 2.2.1 171 | 172 | 173 | maven-surefire-plugin 174 | 2.17 175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /java-javallier/src/main/java/ai/snips/Decrypt.java: -------------------------------------------------------------------------------- 1 | package ai.snips; 2 | 3 | import java.math.BigInteger; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.openjdk.jmh.infra.Blackhole; 8 | import org.openjdk.jmh.annotations.Benchmark; 9 | import org.openjdk.jmh.annotations.BenchmarkMode; 10 | import org.openjdk.jmh.annotations.OutputTimeUnit; 11 | import org.openjdk.jmh.annotations.Mode; 12 | import org.openjdk.jmh.annotations.Scope; 13 | import org.openjdk.jmh.annotations.State; 14 | 15 | import com.n1analytics.paillier.*; 16 | 17 | public class Decrypt { 18 | 19 | static BigInteger small = new BigInteger("42"); 20 | static BigInteger large = new BigInteger("9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"); 21 | 22 | @State(Scope.Benchmark) 23 | public static class Keys { 24 | public PaillierPrivateKey key_1024 = PaillierPrivateKey.create(1024); 25 | public PaillierPrivateKey key_2048 = PaillierPrivateKey.create(2048); 26 | public PaillierPrivateKey key_3072 = PaillierPrivateKey.create(3072); 27 | public PaillierPrivateKey key_4096 = PaillierPrivateKey.create(4096); 28 | public PaillierPublicKey pubkey_1024 = key_1024.getPublicKey(); 29 | public PaillierPublicKey pubkey_2048 = key_2048.getPublicKey(); 30 | public PaillierPublicKey pubkey_3072 = key_3072.getPublicKey(); 31 | public PaillierPublicKey pubkey_4096 = key_4096.getPublicKey(); 32 | public EncryptedNumber cipher_small_1024 = encrypt(pubkey_1024, small); 33 | public EncryptedNumber cipher_small_2048 = encrypt(pubkey_2048, small); 34 | public EncryptedNumber cipher_small_3072 = encrypt(pubkey_3072, small); 35 | public EncryptedNumber cipher_small_4096 = encrypt(pubkey_4096, small); 36 | public EncryptedNumber cipher_large_1024 = encrypt(pubkey_1024, large); 37 | public EncryptedNumber cipher_large_2048 = encrypt(pubkey_2048, large); 38 | public EncryptedNumber cipher_large_3072 = encrypt(pubkey_3072, large); 39 | public EncryptedNumber cipher_large_4096 = encrypt(pubkey_4096, large); 40 | } 41 | 42 | @Benchmark() 43 | @BenchmarkMode(Mode.AverageTime) 44 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 45 | public void testDecrypt_small_1024(Blackhole blackhole, Keys state) { 46 | decrypt(blackhole, state.key_1024, state.cipher_small_1024); 47 | } 48 | 49 | @Benchmark() 50 | @BenchmarkMode(Mode.AverageTime) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | public void testDecrypt_small_2048(Blackhole blackhole, Keys state) { 53 | decrypt(blackhole, state.key_2048, state.cipher_small_2048); 54 | } 55 | 56 | @Benchmark() 57 | @BenchmarkMode(Mode.AverageTime) 58 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 59 | public void testDecrypt_small_3072(Blackhole blackhole, Keys state) { 60 | decrypt(blackhole, state.key_3072, state.cipher_small_3072); 61 | } 62 | 63 | @Benchmark() 64 | @BenchmarkMode(Mode.AverageTime) 65 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 66 | public void testDecrypt_small_4096(Blackhole blackhole, Keys state) { 67 | decrypt(blackhole, state.key_4096, state.cipher_small_4096); 68 | } 69 | 70 | @Benchmark() 71 | @BenchmarkMode(Mode.AverageTime) 72 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 73 | public void testDecrypt_large_1024(Blackhole blackhole, Keys state) { 74 | decrypt(blackhole, state.key_1024, state.cipher_large_1024); 75 | } 76 | 77 | @Benchmark() 78 | @BenchmarkMode(Mode.AverageTime) 79 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 80 | public void testDecrypt_large_2048(Blackhole blackhole, Keys state) { 81 | decrypt(blackhole, state.key_2048, state.cipher_large_2048); 82 | } 83 | 84 | @Benchmark() 85 | @BenchmarkMode(Mode.AverageTime) 86 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 87 | public void testDecrypt_large_3072(Blackhole blackhole, Keys state) { 88 | decrypt(blackhole, state.key_3072, state.cipher_large_3072); 89 | } 90 | 91 | @Benchmark() 92 | @BenchmarkMode(Mode.AverageTime) 93 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 94 | public void testDecrypt_large_4096(Blackhole blackhole, Keys state) { 95 | decrypt(blackhole, state.key_4096, state.cipher_large_4096); 96 | } 97 | 98 | private static void decrypt(Blackhole blackhole, PaillierPrivateKey key, EncryptedNumber message) { 99 | blackhole.consume(key.decrypt(message)); 100 | } 101 | 102 | private static EncryptedNumber encrypt(PaillierPublicKey key, BigInteger message) { 103 | PaillierContext paillierContext = key.createSignedContext(); 104 | EncryptedNumber cipherText = paillierContext.encrypt(message); 105 | cipherText.obfuscate(); 106 | return cipherText; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /java-javallier/src/main/java/ai/snips/Encrypt.java: -------------------------------------------------------------------------------- 1 | package ai.snips; 2 | 3 | import java.math.BigInteger; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.openjdk.jmh.infra.Blackhole; 8 | import org.openjdk.jmh.annotations.Benchmark; 9 | import org.openjdk.jmh.annotations.BenchmarkMode; 10 | import org.openjdk.jmh.annotations.OutputTimeUnit; 11 | import org.openjdk.jmh.annotations.Mode; 12 | import org.openjdk.jmh.annotations.Scope; 13 | import org.openjdk.jmh.annotations.State; 14 | 15 | import com.n1analytics.paillier.*; 16 | 17 | public class Encrypt { 18 | 19 | static BigInteger small = new BigInteger("42"); 20 | static BigInteger large = new BigInteger("9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"); 21 | 22 | @State(Scope.Benchmark) 23 | public static class Keys { 24 | public PaillierPrivateKey key_1024 = PaillierPrivateKey.create(1024); 25 | public PaillierPublicKey pubkey_1024 = key_1024.getPublicKey(); 26 | public PaillierPrivateKey key_2048 = PaillierPrivateKey.create(2048); 27 | public PaillierPublicKey pubkey_2048 = key_2048.getPublicKey(); 28 | public PaillierPrivateKey key_3072 = PaillierPrivateKey.create(3072); 29 | public PaillierPublicKey pubkey_3072 = key_3072.getPublicKey(); 30 | public PaillierPrivateKey key_4096 = PaillierPrivateKey.create(4096); 31 | public PaillierPublicKey pubkey_4096 = key_4096.getPublicKey(); 32 | } 33 | 34 | @Benchmark() 35 | @BenchmarkMode(Mode.AverageTime) 36 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 37 | public void testEncrypt_small_1024(Blackhole blackhole, Keys state) { 38 | encrypt(blackhole, state.pubkey_1024, small); 39 | } 40 | 41 | @Benchmark() 42 | @BenchmarkMode(Mode.AverageTime) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | public void testEncrypt_small_2048(Blackhole blackhole, Keys state) { 45 | encrypt(blackhole, state.pubkey_2048, small); 46 | } 47 | 48 | @Benchmark() 49 | @BenchmarkMode(Mode.AverageTime) 50 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 51 | public void testEncrypt_small_3072(Blackhole blackhole, Keys state) { 52 | encrypt(blackhole, state.pubkey_3072, small); 53 | } 54 | 55 | @Benchmark() 56 | @BenchmarkMode(Mode.AverageTime) 57 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 58 | public void testEncrypt_small_4096(Blackhole blackhole, Keys state) { 59 | encrypt(blackhole, state.pubkey_4096, small); 60 | } 61 | 62 | @Benchmark() 63 | @BenchmarkMode(Mode.AverageTime) 64 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 65 | public void testEncrypt_large_1024(Blackhole blackhole, Keys state) { 66 | encrypt(blackhole, state.pubkey_1024, large); 67 | } 68 | 69 | @Benchmark() 70 | @BenchmarkMode(Mode.AverageTime) 71 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 72 | public void testEncrypt_large_2048(Blackhole blackhole, Keys state) { 73 | encrypt(blackhole, state.pubkey_2048, large); 74 | } 75 | 76 | @Benchmark() 77 | @BenchmarkMode(Mode.AverageTime) 78 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 79 | public void testEncrypt_large_3072(Blackhole blackhole, Keys state) { 80 | encrypt(blackhole, state.pubkey_3072, large); 81 | } 82 | 83 | @Benchmark() 84 | @BenchmarkMode(Mode.AverageTime) 85 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 86 | public void testEncrypt_large_4096(Blackhole blackhole, Keys state) { 87 | encrypt(blackhole, state.pubkey_4096, large); 88 | } 89 | 90 | private void encrypt(Blackhole blackhole, PaillierPublicKey key, BigInteger rawMessage) { 91 | PaillierContext paillierContext = key.createSignedContext(); 92 | BigInteger message = rawMessage.remainder(key.getModulus()); 93 | EncryptedNumber cipherText = paillierContext.encrypt(message); 94 | cipherText.obfuscate(); 95 | blackhole.consume(cipherText); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /java-javallier/src/main/java/ai/snips/Keygen.java: -------------------------------------------------------------------------------- 1 | package ai.snips; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.infra.Blackhole; 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.BenchmarkMode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Mode; 10 | 11 | import com.n1analytics.paillier.*; 12 | 13 | public class Keygen { 14 | 15 | @Benchmark() 16 | @BenchmarkMode(Mode.AverageTime) 17 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 18 | public void testKeyGen_1024(Blackhole blackhole) { 19 | blackhole.consume(PaillierPrivateKey.create(1024)); 20 | } 21 | 22 | @Benchmark() 23 | @BenchmarkMode(Mode.AverageTime) 24 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 25 | public void testKeyGen_2048(Blackhole blackhole) { 26 | blackhole.consume(PaillierPrivateKey.create(2048)); 27 | } 28 | 29 | @Benchmark() 30 | @BenchmarkMode(Mode.AverageTime) 31 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 32 | public void testKeyGen_3072(Blackhole blackhole) { 33 | blackhole.consume(PaillierPrivateKey.create(3072)); 34 | } 35 | 36 | @Benchmark() 37 | @BenchmarkMode(Mode.AverageTime) 38 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 39 | public void testKeyGen_4096(Blackhole blackhole) { 40 | blackhole.consume(PaillierPrivateKey.create(4096)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /java-javallier/src/main/java/ai/snips/Ops.java: -------------------------------------------------------------------------------- 1 | package ai.snips; 2 | 3 | import java.math.BigInteger; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.openjdk.jmh.infra.Blackhole; 8 | import org.openjdk.jmh.annotations.Benchmark; 9 | import org.openjdk.jmh.annotations.BenchmarkMode; 10 | import org.openjdk.jmh.annotations.OutputTimeUnit; 11 | import org.openjdk.jmh.annotations.Mode; 12 | import org.openjdk.jmh.annotations.Scope; 13 | import org.openjdk.jmh.annotations.State; 14 | 15 | import com.n1analytics.paillier.*; 16 | 17 | public class Ops { 18 | 19 | static BigInteger small = new BigInteger("42"); 20 | static BigInteger large = new BigInteger("9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"); 21 | 22 | @State(Scope.Benchmark) 23 | public static class Keys { 24 | public PaillierPrivateKey key_1024 = PaillierPrivateKey.create(1024); 25 | public PaillierPrivateKey key_2048 = PaillierPrivateKey.create(2048); 26 | public PaillierPrivateKey key_3072 = PaillierPrivateKey.create(3072); 27 | public PaillierPrivateKey key_4096 = PaillierPrivateKey.create(4096); 28 | public PaillierPublicKey pubkey_1024 = key_1024.getPublicKey(); 29 | public PaillierPublicKey pubkey_2048 = key_2048.getPublicKey(); 30 | public PaillierPublicKey pubkey_3072 = key_3072.getPublicKey(); 31 | public PaillierPublicKey pubkey_4096 = key_4096.getPublicKey(); 32 | public EncryptedNumber cipher_small_1024 = encrypt(pubkey_1024, small); 33 | public EncryptedNumber cipher_small_2048 = encrypt(pubkey_2048, small); 34 | public EncryptedNumber cipher_small_3072 = encrypt(pubkey_3072, small); 35 | public EncryptedNumber cipher_small_4096 = encrypt(pubkey_4096, small); 36 | public EncryptedNumber cipher_large_1024 = encrypt(pubkey_1024, large); 37 | public EncryptedNumber cipher_large_2048 = encrypt(pubkey_2048, large); 38 | public EncryptedNumber cipher_large_3072 = encrypt(pubkey_3072, large); 39 | public EncryptedNumber cipher_large_4096 = encrypt(pubkey_4096, large); 40 | } 41 | 42 | @Benchmark() 43 | @BenchmarkMode(Mode.AverageTime) 44 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 45 | public void testAdd_1024(Blackhole blackhole, Keys state) { 46 | add(blackhole, state.pubkey_1024, state.cipher_small_1024, state.cipher_large_1024); 47 | } 48 | 49 | @Benchmark() 50 | @BenchmarkMode(Mode.AverageTime) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | public void testAdd_2048(Blackhole blackhole, Keys state) { 53 | add(blackhole, state.pubkey_2048, state.cipher_small_2048, state.cipher_large_2048); 54 | } 55 | 56 | @Benchmark() 57 | @BenchmarkMode(Mode.AverageTime) 58 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 59 | public void testAdd_3072(Blackhole blackhole, Keys state) { 60 | add(blackhole, state.pubkey_3072, state.cipher_small_3072, state.cipher_large_3072); 61 | } 62 | 63 | @Benchmark() 64 | @BenchmarkMode(Mode.AverageTime) 65 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 66 | public void testAdd_4096(Blackhole blackhole, Keys state) { 67 | add(blackhole, state.pubkey_4096, state.cipher_small_4096, state.cipher_large_4096); 68 | } 69 | 70 | private static void add(Blackhole blackhole, PaillierPublicKey key, EncryptedNumber message1, EncryptedNumber message2) { 71 | PaillierContext paillierContext = key.createSignedContext(); 72 | blackhole.consume(paillierContext.add(message1, message2)); 73 | } 74 | 75 | private static EncryptedNumber encrypt(PaillierPublicKey key, BigInteger message) { 76 | PaillierContext paillierContext = key.createSignedContext(); 77 | EncryptedNumber cipherText = paillierContext.encrypt(message); 78 | cipherText.obfuscate(); 79 | return cipherText; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /julia-sketch/README.md: -------------------------------------------------------------------------------- 1 | # Julia 2 | 3 | This is the bench for a quick implementation of Paillier using Julia's native `BigInt` (which wraps GMP). Key generation is not implemented. 4 | 5 | ## Benching 6 | 7 | Either run all of them: 8 | ``` 9 | julia encryption.jl && julia decryption.jl && julia addition.jl 10 | ``` 11 | 12 | or specific ones: 13 | ``` 14 | julia encryption.jl 15 | julia decryption.jl 16 | julia addition.jl 17 | ``` 18 | -------------------------------------------------------------------------------- /julia-sketch/addition.jl: -------------------------------------------------------------------------------- 1 | include("paillier.jl") 2 | 3 | include("primes.jl") 4 | include("plaintexts.jl") 5 | 6 | ITERATIONS = 1000 7 | 8 | PUBKEYS = Dict() 9 | for (keysize, primes) in PRIMES 10 | PUBKEYS[keysize] = PublicEncryptionKey(primes[1], primes[2]) 11 | end 12 | 13 | function bench_encryption(keysize, plaintext1, plaintext2) 14 | pubkey = PUBKEYS[keysize] 15 | m1 = PLAINTEXTS[plaintext1] 16 | m2 = PLAINTEXTS[plaintext2] 17 | c1 = encrypt(pubkey, m1) 18 | c2 = encrypt(pubkey, m2) 19 | 20 | total_time = 0.0 21 | for i = 1:ITERATIONS 22 | tic() 23 | add(pubkey, c1, c2) 24 | total_time += toq() 25 | end 26 | 27 | total_time = total_time / ITERATIONS 28 | total_time_in_milliseconds = total_time * 1000.0 29 | println("Addition ($keysize, $plaintext1, $plaintext2): $total_time_in_milliseconds ms") 30 | end 31 | 32 | for keysize in keys(PRIMES) 33 | bench_encryption(keysize, "small", "large") 34 | end 35 | -------------------------------------------------------------------------------- /julia-sketch/decryption.jl: -------------------------------------------------------------------------------- 1 | include("paillier.jl") 2 | 3 | include("primes.jl") 4 | include("plaintexts.jl") 5 | 6 | ITERATIONS = 100 7 | 8 | PUBKEYS = Dict() 9 | for (keysize, primes) in PRIMES 10 | PUBKEYS[keysize] = PublicEncryptionKey(primes[1], primes[2]) 11 | end 12 | 13 | PRIKEYS = Dict() 14 | for (keysize, primes) in PRIMES 15 | PRIKEYS[keysize] = PrivateDecryptionKey(primes[1], primes[2]) 16 | end 17 | 18 | function bench_decryption(keysize, plaintext) 19 | pubkey = PUBKEYS[keysize] 20 | prikey = PRIKEYS[keysize] 21 | m = PLAINTEXTS[plaintext] 22 | c = encrypt(pubkey, m) 23 | 24 | total_time = 0.0 25 | for i = 1:ITERATIONS 26 | tic() 27 | decrypt(prikey, c) 28 | total_time += toq() 29 | end 30 | 31 | total_time = total_time / ITERATIONS 32 | total_time_in_milliseconds = total_time * 1000.0 33 | println("Decryption ($keysize, $plaintext): $total_time_in_milliseconds ms") 34 | end 35 | 36 | for keysize in keys(PRIMES) 37 | for plaintext in keys(PLAINTEXTS) 38 | bench_decryption(keysize, plaintext) 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /julia-sketch/encryption.jl: -------------------------------------------------------------------------------- 1 | include("paillier.jl") 2 | 3 | include("primes.jl") 4 | include("plaintexts.jl") 5 | 6 | ITERATIONS = 100 7 | 8 | PUBKEYS = Dict() 9 | for (keysize, primes) in PRIMES 10 | PUBKEYS[keysize] = PublicEncryptionKey(primes[1], primes[2]) 11 | end 12 | 13 | function bench_encryption(keysize, plaintext) 14 | pubkey = PUBKEYS[keysize] 15 | m = PLAINTEXTS[plaintext] 16 | 17 | total_time = 0.0 18 | for i = 1:ITERATIONS 19 | tic() 20 | encrypt(pubkey, m) 21 | total_time += toq() 22 | end 23 | 24 | total_time = total_time / ITERATIONS 25 | total_time_in_milliseconds = total_time * 1000.0 26 | println("Encryption ($keysize, $plaintext): $total_time_in_milliseconds ms") 27 | end 28 | 29 | for keysize in keys(PRIMES) 30 | for plaintext in keys(PLAINTEXTS) 31 | bench_encryption(keysize, plaintext) 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /julia-sketch/paillier.jl: -------------------------------------------------------------------------------- 1 | type PrivateDecryptionKey 2 | l::BigInt 3 | m::BigInt 4 | n::BigInt 5 | n_sq::BigInt 6 | end 7 | 8 | type PublicEncryptionKey 9 | n::BigInt 10 | n_sq::BigInt 11 | g::BigInt 12 | end 13 | 14 | PublicEncryptionKey(p,q) = PublicEncryptionKey(p*q) 15 | PublicEncryptionKey(n) = PublicEncryptionKey(n,n^2,n+1) 16 | 17 | PrivateDecryptionKey(p,q) = PrivateDecryptionKey(p,q,p*q) 18 | function PrivateDecryptionKey(p,q,n) 19 | l = (p-1)*(q-1) 20 | m = invmod(l, n) 21 | PrivateDecryptionKey(l,m,n,n^2) 22 | end 23 | 24 | 25 | function encrypt(pub::PublicEncryptionKey, m) 26 | rng = RandomDevice() 27 | r = rand(rng, 2:pub.n)::BigInt 28 | rn = powermod(r, pub.n, pub.n_sq) 29 | gm = mod( (pub.n * m) + 1, pub.n_sq ) 30 | c = mod(gm * rn, pub.n_sq) 31 | end 32 | 33 | function decrypt(priv::PrivateDecryptionKey, c) 34 | x = powermod(c, priv.l, priv.n_sq) - 1 35 | m = mod(div(x, priv.n) * priv.m, priv.n) 36 | end 37 | 38 | function add(pub::PublicEncryptionKey, c1, c2) 39 | c = mod(c1 * c2, pub.n_sq) 40 | end 41 | -------------------------------------------------------------------------------- /julia-sketch/plaintexts.jl: -------------------------------------------------------------------------------- 1 | 2 | PLAINTEXTS = Dict( 3 | "small" => 42, 4 | "large" => 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729, 5 | ) 6 | -------------------------------------------------------------------------------- /julia-sketch/primes.jl: -------------------------------------------------------------------------------- 1 | 2 | PRIMES = Dict( 3 | 1024 => ( 4 | 10571726675813419030747017621090503702128756690670143986371236034560692813599426412203713720690798688926328789878099626712676506524453320009379983584398337, 5 | 8719924186273017892025588228882782691633049350158678896348995628952203356872205587084227971878988519308049118884839383574776040781250397380876410886586727 6 | ), 7 | 2048 => ( 8 | 148677972634832330983979593310074301486537017973460461278300587514468301043894574906886127642530475786889672304776052879927627556769456140664043088700743909632312483413393134504352834240399191134336344285483935856491230340093391784574980688823380828143810804684752914935441384845195613674104960646037368551517, 9 | 158741574437007245654463598139927898730476924736461654463975966787719309357536545869203069369466212089132653564188443272208127277664424448947476335413293018778018615899291704693105620242763173357203898195318179150836424196645745308205164116144020613415407736216097185962171301808761138424668335445923774195463 10 | ), 11 | 3072 => ( 12 | 1938773915809394652529298172057636923094290044236022219931405863255082495690032347855183473653298293534659871606091067450014243413240921523500066901919532469137383540029911930316112378749470524457574096696184138193328907669709294330021181329645958261364199493839206628608371081790201428976669130612791242250366444274647280994509241517314575270113602067100993462694264597398469360278093929014852299016988820503145962252559265563066644322641666271029932106499903769, 13 | 1729333955039865312779070302545112355153622867941487879568286271974837188743634878570511341017833291903010427157003137764792255404604305248413507023181875936443712803443246199493193151194550883242324165542306335726099840941650497652937763433867479001683992006377674425496949134225079918435712621197998483974705724043646871299584388204139826795364916574148808511393182730523683601386821553913932390524372176763041155058084219750937709633053529138023382650343754253 14 | ), 15 | 4096 => ( 16 | 28261848413797921050747373790009157475207696600542845759127858334524810394892019587127898078272132395052472769648401097284232757529788630483814946869569002852478396595944244675534735105889321618122474758297393210151468647685747824195258058282068991024204474796049803286388451684224063796077775570765421334497363384354999202901558621538826039758620015278461613759009745213605970355014303833754007641745757347772278772997148831420440876715231559482881530450784440840032110192425362753030300069287596054036239220049206915941278085840877712031976306500253853091618747957831881866466537268377840871519638412119230139490767, 17 | 27320329943961504504949007102163043917650132014364161544882518267176004147597134555329724714487682581532712141674636798503421162701111085999869445909928826112515701752096106236431763411185665259456274656977605110699167613924917533335624140260702229403777488672195828976830865485865262771999567379548038071312755600178975660416672267500965264928789126530448091749895299935378529809339912106262530945639286851709843600337905255291738889952029163800976672706117961832354889533333517431787753974001568371327838181548092337994848828672904504791605188547648282557283655262324819544746399243591989901033723167621115350663317 18 | ), 19 | ) 20 | -------------------------------------------------------------------------------- /python-paillier/.gitignore: -------------------------------------------------------------------------------- 1 | env/ 2 | paillier/build/ 3 | paillier/dist/ 4 | paillier/paillier.egg-info/ 5 | -------------------------------------------------------------------------------- /python-paillier/README.md: -------------------------------------------------------------------------------- 1 | Library available at https://github.com/mikeivanov/paillier. 2 | 3 | ### Create virtual environment (optionally) 4 | ``` 5 | virtualenv -p python2 env 6 | source env/bin/activate 7 | ``` 8 | 9 | ### Install dependencies 10 | ``` 11 | cd paillier && python2 setup.py install && cd .. 12 | ``` 13 | 14 | ### Run benches 15 | ``` 16 | python2 keygen.py 17 | python2 encryption.py 18 | python2 decryption.py 19 | python2 addition.py 20 | ``` 21 | -------------------------------------------------------------------------------- /python-paillier/addition.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | from paillier.paillier import * 4 | 5 | ITERATIONS = 10 6 | KEY_SIZES = [1024, 2048] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def addition(key_size, plaintext1, plaintext2): 14 | 15 | prikey, pubkey = generate_keypair(key_size) 16 | m1 = PLAINTEXTS[plaintext1] 17 | m2 = PLAINTEXTS[plaintext2] 18 | c1 = encrypt(pubkey, m1) 19 | c2 = encrypt(pubkey, m2) 20 | 21 | start = time.time() 22 | 23 | for _ in range(ITERATIONS): 24 | c = e_add(pubkey, c1, c2) 25 | 26 | stop = time.time() 27 | 28 | diff = stop - start 29 | diff = diff / ITERATIONS 30 | print("addition (%d, %s, %s): %fms" % (key_size, plaintext1, plaintext2, diff * 1000)) 31 | 32 | 33 | if __name__ == "__main__": 34 | for key_size in KEY_SIZES: 35 | addition(key_size, 'small', 'large') 36 | -------------------------------------------------------------------------------- /python-paillier/decryption.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | from paillier.paillier import * 4 | 5 | ITERATIONS = 10 6 | KEY_SIZES = [1024, 2048] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def decryption(key_size, plaintext): 14 | 15 | prikey, pubkey = generate_keypair(key_size) 16 | m = PLAINTEXTS[plaintext] 17 | c = encrypt(pubkey, m) 18 | 19 | start = time.time() 20 | 21 | for _ in range(ITERATIONS): 22 | n = decrypt(prikey, pubkey, c) 23 | 24 | stop = time.time() 25 | 26 | diff = stop - start 27 | diff = diff / ITERATIONS 28 | print("decryption (%d, %s): %fms" % (key_size, plaintext, diff * 1000)) 29 | 30 | 31 | if __name__ == "__main__": 32 | for key_size in KEY_SIZES: 33 | for plaintext in PLAINTEXTS.keys(): 34 | decryption(key_size, plaintext) 35 | -------------------------------------------------------------------------------- /python-paillier/encryption.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | from paillier.paillier import * 4 | 5 | ITERATIONS = 10 6 | KEY_SIZES = [1024, 2048] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def encryption(key_size, plaintext): 14 | 15 | prikey, pubkey = generate_keypair(key_size) 16 | m = PLAINTEXTS[plaintext] 17 | 18 | start = time.time() 19 | 20 | for _ in range(ITERATIONS): 21 | c = encrypt(pubkey, m) 22 | 23 | stop = time.time() 24 | 25 | diff = stop - start 26 | diff = diff / ITERATIONS 27 | print("encryption (%d, %s): %fms" % (key_size, plaintext, diff * 1000)) 28 | 29 | 30 | if __name__ == "__main__": 31 | for key_size in KEY_SIZES: 32 | for plaintext in PLAINTEXTS.keys(): 33 | encryption(key_size, plaintext) 34 | -------------------------------------------------------------------------------- /python-paillier/keygen.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | from paillier.paillier import * 4 | 5 | ITERATIONS = 10 6 | KEY_SIZES = [1024, 2048] 7 | 8 | def keygen(key_size): 9 | 10 | start = time.time() 11 | 12 | for _ in range(ITERATIONS): 13 | pubkey, prikey = generate_keypair(key_size) 14 | 15 | stop = time.time() 16 | 17 | diff = stop - start 18 | diff = diff / ITERATIONS 19 | print("keygen (%d): %fms" % (key_size, diff * 1000)) 20 | 21 | 22 | if __name__ == "__main__": 23 | for key_size in KEY_SIZES: 24 | keygen(key_size) 25 | -------------------------------------------------------------------------------- /python-paillier/paillier/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | /py/ 3 | -------------------------------------------------------------------------------- /python-paillier/paillier/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /python-paillier/paillier/README.md: -------------------------------------------------------------------------------- 1 | Pure Python Paillier Homomorphic Cryptosystem 2 | ============================================= 3 | 4 | This is a very basic pure Python implementation of the Paillier 5 | Homomorphic Cryptosystem. 6 | 7 | Homomorphic Cryptosystems 8 | ------------------------- 9 | 10 | The idea of homomorphic computation is to encrypt some numbers, 11 | perform algebraic operations like "add" and "multiply" on 12 | *cyphertexts*, then decrypt the result and find it to be exactly the 13 | same as if corresponding "+" and "*" operations were applied to the 14 | plaintexts. 15 | 16 | In other words, a homomorphic cryptosystem enables cryptographically 17 | secure computations in an untrusted environment. 18 | 19 | Paillier cryptosystem 20 | --------------------- 21 | 22 | Paillier cryptosystem is a probabilistic asymmetric algorithm for 23 | public key cryptography. Paillier cryptosystem is partially 24 | homomorphic as it can only add encrypted numbers or multiply an 25 | encrypted number by an unencrypted multiplier. 26 | 27 | Implementation 28 | -------------- 29 | 30 | This pure Python implementation exploits Python's long type with 31 | its arbitrary precision arithmetics. Public key is serializable, thus 32 | it can be pickled along with the encrypted numbers and sent to a 33 | remote server for computation. 34 | 35 | The code is loosely based on [Thep][1] and a few ActiveState recipes. 36 | 37 | Please note that this implementation's primary purpose is education; 38 | it is **not suitable for production use** as it is. 39 | 40 | Installation and Tests 41 | ---------------------- 42 | 43 | The paillier.py module has no external dependencies besides included 44 | primes.py. Simply run demo.py to see it in action. 45 | 46 | To run unit tests please install [Nose][2]: 47 | 48 | $ pip install -r requirements.txt 49 | $ nosetests 50 | ............... 51 | Ran 814 tests in 11.544s 52 | OK 53 | 54 | Usage 55 | ----- 56 | 57 | $ ipython 58 | Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 59 | Type "copyright", "credits" or "license" for more information. 60 | 61 | In [1]: from paillier.paillier import * 62 | 63 | In [2]: priv, pub = generate_keypair(128) 64 | 65 | In [3]: x = encrypt(pub, 2) 66 | 67 | In [4]: y = encrypt(pub, 3) 68 | 69 | In [5]: x,y 70 | Out[5]: 71 | (72109737005643982735171545918..., 9615446835366886883470187...) 72 | 73 | In [6]: z = e_add(pub, x, y) 74 | 75 | In [7]: z 76 | Out[7]: 71624230283745591274688669... 77 | 78 | In [8]: decrypt(priv, pub, z) 79 | Out[8]: 5L 80 | 81 | 82 | License and Copyright 83 | --------------------- 84 | LGPL v3, see [LICENSE][3] 85 | 86 | (C) 2011 Mike Ivanov 87 | 88 | 89 | [1]: http://code.google.com/p/thep/ 90 | [2]: http://readthedocs.org/docs/nose/en/latest/index.html 91 | [3]: https://github.com/mikeivanov/paillier/blob/master/LICENSE 92 | 93 | -------------------------------------------------------------------------------- /python-paillier/paillier/demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from paillier.paillier import * 4 | 5 | print "Generating keypair..." 6 | priv, pub = generate_keypair(512) 7 | 8 | x = 3 9 | print "x =", x 10 | print "Encrypting x..." 11 | cx = encrypt(pub, x) 12 | print "cx =", cx 13 | 14 | y = 5 15 | print "y =", y 16 | print "Encrypting y..." 17 | cy = encrypt(pub, y) 18 | print "cy =", cy 19 | 20 | print "Computing cx + cy..." 21 | cz = e_add(pub, cx, cy) 22 | print "cz =", cz 23 | 24 | print "Decrypting cz..." 25 | z = decrypt(priv, pub, cz) 26 | print "z =", z 27 | 28 | print "Computing decrypt((cz + 2) * 3) ..." 29 | print "result =", decrypt(priv, pub, 30 | e_mul_const(pub, e_add_const(pub, cz, 2), 3)) 31 | -------------------------------------------------------------------------------- /python-paillier/paillier/paillier/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snipsco/paillier-libraries-benchmarks/6a6881b3fdd67b744ce90d7baf11197778584576/python-paillier/paillier/paillier/__init__.py -------------------------------------------------------------------------------- /python-paillier/paillier/paillier/paillier.py: -------------------------------------------------------------------------------- 1 | import math 2 | import primes 3 | 4 | def invmod(a, p, maxiter=1000000): 5 | """The multiplicitive inverse of a in the integers modulo p: 6 | a * b == 1 mod p 7 | Returns b. 8 | (http://code.activestate.com/recipes/576737-inverse-modulo-p/)""" 9 | if a == 0: 10 | raise ValueError('0 has no inverse mod %d' % p) 11 | r = a 12 | d = 1 13 | for i in xrange(min(p, maxiter)): 14 | d = ((p // r + 1) * d) % p 15 | r = (d * a) % p 16 | if r == 1: 17 | break 18 | else: 19 | raise ValueError('%d has no inverse mod %d' % (a, p)) 20 | return d 21 | 22 | def modpow(base, exponent, modulus): 23 | """Modular exponent: 24 | c = b ^ e mod m 25 | Returns c. 26 | (http://www.programmish.com/?p=34)""" 27 | result = 1 28 | while exponent > 0: 29 | if exponent & 1 == 1: 30 | result = (result * base) % modulus 31 | exponent = exponent >> 1 32 | base = (base * base) % modulus 33 | return result 34 | 35 | class PrivateKey(object): 36 | 37 | def __init__(self, p, q, n): 38 | self.l = (p-1) * (q-1) 39 | self.m = invmod(self.l, n) 40 | 41 | def __repr__(self): 42 | return '' % (self.l, self.m) 43 | 44 | class PublicKey(object): 45 | 46 | @classmethod 47 | def from_n(cls, n): 48 | return cls(n) 49 | 50 | def __init__(self, n): 51 | self.n = n 52 | self.n_sq = n * n 53 | self.g = n + 1 54 | 55 | def __repr__(self): 56 | return '' % self.n 57 | 58 | def generate_keypair(bits): 59 | p = primes.generate_prime(bits / 2) 60 | q = primes.generate_prime(bits / 2) 61 | n = p * q 62 | return PrivateKey(p, q, n), PublicKey(n) 63 | 64 | def encrypt(pub, plain): 65 | while True: 66 | r = primes.generate_prime(long(round(math.log(pub.n, 2)))) 67 | if r > 0 and r < pub.n: 68 | break 69 | x = pow(r, pub.n, pub.n_sq) 70 | cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq 71 | return cipher 72 | 73 | def e_add(pub, a, b): 74 | """Add one encrypted integer to another""" 75 | return a * b % pub.n_sq 76 | 77 | def e_add_const(pub, a, n): 78 | """Add constant n to an encrypted integer""" 79 | return a * modpow(pub.g, n, pub.n_sq) % pub.n_sq 80 | 81 | def e_mul_const(pub, a, n): 82 | """Multiplies an ancrypted integer by a constant""" 83 | return modpow(a, n, pub.n_sq) 84 | 85 | def decrypt(priv, pub, cipher): 86 | x = pow(cipher, priv.l, pub.n_sq) - 1 87 | plain = ((x // pub.n) * priv.m) % pub.n 88 | return plain 89 | 90 | -------------------------------------------------------------------------------- /python-paillier/paillier/paillier/primes.py: -------------------------------------------------------------------------------- 1 | import random 2 | import sys 3 | 4 | def ipow(a, b, n): 5 | """calculates (a**b) % n via binary exponentiation, yielding itermediate 6 | results as Rabin-Miller requires""" 7 | A = a = long(a % n) 8 | yield A 9 | t = 1L 10 | while t <= b: 11 | t <<= 1 12 | 13 | # t = 2**k, and t > b 14 | t >>= 2 15 | 16 | while t: 17 | A = (A * A) % n 18 | if t & b: 19 | A = (A * a) % n 20 | yield A 21 | t >>= 1 22 | 23 | def rabin_miller_witness(test, possible): 24 | """Using Rabin-Miller witness test, will return True if possible is 25 | definitely not prime (composite), False if it may be prime.""" 26 | return 1 not in ipow(test, possible-1, possible) 27 | 28 | smallprimes = (2,3,5,7,11,13,17,19,23,29,31,37,41,43, 29 | 47,53,59,61,67,71,73,79,83,89,97) 30 | 31 | def default_k(bits): 32 | return max(40, 2 * bits) 33 | 34 | def is_probably_prime(possible, k=None): 35 | if possible == 1: 36 | return True 37 | if k is None: 38 | k = default_k(possible.bit_length()) 39 | for i in smallprimes: 40 | if possible == i: 41 | return True 42 | if possible % i == 0: 43 | return False 44 | for i in xrange(k): 45 | test = random.randrange(2, possible - 1) | 1 46 | if rabin_miller_witness(test, possible): 47 | return False 48 | return True 49 | 50 | def generate_prime(bits, k=None): 51 | """Will generate an integer of b bits that is probably prime 52 | (after k trials). Reasonably fast on current hardware for 53 | values of up to around 512 bits.""" 54 | assert bits >= 8 55 | 56 | if k is None: 57 | k = default_k(bits) 58 | 59 | while True: 60 | possible = random.randrange(2 ** (bits-1) + 1, 2 ** bits) | 1 61 | if is_probably_prime(possible, k): 62 | return possible 63 | 64 | -------------------------------------------------------------------------------- /python-paillier/paillier/requirements.txt: -------------------------------------------------------------------------------- 1 | nose>1.1 2 | -------------------------------------------------------------------------------- /python-paillier/paillier/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='paillier', 5 | version='0.1', 6 | packages=find_packages(), 7 | ) 8 | -------------------------------------------------------------------------------- /python-paillier/paillier/tests/test_paillier.py: -------------------------------------------------------------------------------- 1 | import random 2 | from nose.tools import assert_raises 3 | import paillier 4 | 5 | def test_invmod(): 6 | assert_raises(ValueError, paillier.invmod, 0, 7) 7 | assert paillier.invmod(1, 7) == 1 8 | p = 101 9 | for i in range(1, p): 10 | iinv = paillier.invmod(i, p) 11 | assert (iinv * i) % p == 1 12 | 13 | def test_keys_int(): 14 | priv = paillier.PrivateKey(7, 11, 77) 15 | assert priv.l == 60 16 | assert priv.m == 9 17 | pub = paillier.PublicKey(77) 18 | assert pub.g == 78 19 | 20 | def test_keys_long(): 21 | priv, pub = paillier.generate_keypair(256) 22 | for i in range(5): 23 | assert paillier.invmod(priv.m, pub.n) == priv.l 24 | 25 | def test_encrypt_non_repeatable(): 26 | pub = paillier.PublicKey(15484279*32451217) 27 | for i in range(10): 28 | pt = random.randint(0, 1000000) 29 | assert paillier.encrypt(pub, pt) != paillier.encrypt(pub, pt) 30 | 31 | def test_decrypt(): 32 | for i in range(5): 33 | priv, pub = paillier.generate_keypair(64) 34 | for j in range(5): 35 | pt = long(random.randint(0, 1000000)) 36 | ct = paillier.encrypt(pub, pt) 37 | assert pt == paillier.decrypt(priv, pub, ct) 38 | 39 | def test_e_add(): 40 | for i in range(5): 41 | priv, pub = paillier.generate_keypair(128) 42 | for j in range(5): 43 | a = long(random.randint(0, 1000000)) 44 | b = long(random.randint(0, 1000000)) 45 | ca, cb = paillier.encrypt(pub, a), paillier.encrypt(pub, b) 46 | cs = paillier.e_add(pub, ca, cb) 47 | s = paillier.decrypt(priv, pub, cs) 48 | assert a + b == s 49 | 50 | def test_e_add_const(): 51 | for i in range(5): 52 | priv, pub = paillier.generate_keypair(128) 53 | for j in range(5): 54 | a = long(random.randint(0, 1000000)) 55 | c = paillier.encrypt(pub, a) 56 | for n in range(0, 11): 57 | cs = paillier.e_add_const(pub, c, n) 58 | s = paillier.decrypt(priv, pub, cs) 59 | assert a + n == s 60 | 61 | def test_e_mul_const(): 62 | for i in range(5): 63 | priv, pub = paillier.generate_keypair(128) 64 | for j in range(5): 65 | a = long(random.randint(0, 1000000)) 66 | c = paillier.encrypt(pub, a) 67 | for n in range(0, 11): 68 | cs = paillier.e_mul_const(pub, c, n) 69 | s = paillier.decrypt(priv, pub, cs) 70 | assert a * n == s 71 | 72 | -------------------------------------------------------------------------------- /python-paillier/paillier/tests/test_primes.py: -------------------------------------------------------------------------------- 1 | import primes 2 | 3 | def test_ipows(): 4 | for a in range(1, 10): 5 | for b in range (1, 10): 6 | for n in range(2, 11): 7 | yield check_ipow, a, b, n 8 | 9 | def check_ipow(a, b, n): 10 | result = list(primes.ipow(a, b, n)) 11 | assert result[-1] == pow(a, b) % n 12 | 13 | def test_is_probably_prime(): 14 | known_primes = (15484279, 32451217, 86027297, 179424097, 982451653, 15 | 2038072919, 18125114801, 22801762469) 16 | for prime in known_primes: 17 | yield check_is_probably_prime, prime, True 18 | for other in known_primes: 19 | yield check_is_probably_prime, prime * other, False 20 | 21 | def check_is_probably_prime(x, is_prime): 22 | assert primes.is_probably_prime(x) == is_prime 23 | 24 | def test_generate_prime(): 25 | for i in xrange(3, 10): 26 | yield check_generate_prime, pow(2, i) 27 | 28 | def check_generate_prime(bits): 29 | assert primes.is_probably_prime(primes.generate_prime(bits)) 30 | 31 | -------------------------------------------------------------------------------- /python-phe/.gitignore: -------------------------------------------------------------------------------- 1 | env/ 2 | -------------------------------------------------------------------------------- /python-phe/README.md: -------------------------------------------------------------------------------- 1 | 2 | # phe 3 | 4 | This is the bench of the [phe](https://github.com/NICTA/python-paillier) library. 5 | 6 | ## Prerequisites 7 | 8 | GMP. 9 | 10 | ## Installation 11 | 12 | ### Create virtual environment (optionally) 13 | ``` 14 | virtualenv -p python3 env 15 | source env/bin/activate 16 | ``` 17 | 18 | ### Install dependencies 19 | ``` 20 | pip3 install phe 21 | ``` 22 | 23 | ## Benching 24 | 25 | Run all in sequence: 26 | ``` 27 | python3 keygen.py && python3 encryption.py && python3 decryption.py && python3 addition.py 28 | ``` 29 | 30 | or specific ones: 31 | ``` 32 | python3 keygen.py 33 | python3 encryption.py 34 | python3 decryption.py 35 | python3 addition.py 36 | ``` 37 | -------------------------------------------------------------------------------- /python-phe/addition.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | import phe.paillier as paillier 4 | 5 | ITERATIONS = 100 6 | KEY_SIZES = [1024, 2048, 3072, 4096] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def addition(key_size, plaintext1, plaintext2): 14 | 15 | pubkey, prikey = paillier.generate_paillier_keypair(n_length=key_size) 16 | m1 = PLAINTEXTS[plaintext1] 17 | m2 = PLAINTEXTS[plaintext2] 18 | c1 = pubkey.encrypt(m1) 19 | c2 = pubkey.encrypt(m2) 20 | 21 | start = time.time() 22 | 23 | for _ in range(ITERATIONS): 24 | c = c1 + c2 # this does NOT rerandomise the ciphertext 25 | 26 | stop = time.time() 27 | 28 | diff = stop - start 29 | diff = diff / ITERATIONS 30 | print("addition (%d): %fms" % (key_size, diff * 1000)) 31 | 32 | 33 | if __name__ == "__main__": 34 | for key_size in KEY_SIZES: 35 | addition(key_size, 'small', 'large') 36 | -------------------------------------------------------------------------------- /python-phe/decryption.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | import phe.paillier as paillier 4 | 5 | ITERATIONS = 100 6 | KEY_SIZES = [1024, 2048, 3072, 4096] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def decryption(key_size, plaintext): 14 | 15 | pubkey, prikey = paillier.generate_paillier_keypair(n_length=key_size) 16 | m = PLAINTEXTS[plaintext] 17 | c = pubkey.encrypt(m) 18 | 19 | start = time.time() 20 | 21 | for _ in range(ITERATIONS): 22 | n = prikey.decrypt(c) 23 | 24 | stop = time.time() 25 | 26 | diff = stop - start 27 | diff = diff / ITERATIONS 28 | print("decryption (%d, %s): %fms" % (key_size, plaintext, diff * 1000)) 29 | 30 | 31 | if __name__ == "__main__": 32 | for key_size in KEY_SIZES: 33 | for plaintext in PLAINTEXTS.keys(): 34 | decryption(key_size, plaintext) 35 | -------------------------------------------------------------------------------- /python-phe/encryption.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | import phe.paillier as paillier 4 | 5 | ITERATIONS = 100 6 | KEY_SIZES = [1024, 2048, 3072, 4096] 7 | 8 | PLAINTEXTS = { 9 | 'small': 42, 10 | 'large': 9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729 11 | } 12 | 13 | def encryption(key_size, plaintext): 14 | 15 | pubkey, prikey = paillier.generate_paillier_keypair(n_length=key_size) 16 | m = PLAINTEXTS[plaintext] 17 | 18 | start = time.time() 19 | 20 | for _ in range(ITERATIONS): 21 | c = pubkey.encrypt(m) 22 | c.ciphertext() # force obfuscation if not done so already 23 | 24 | stop = time.time() 25 | 26 | diff = stop - start 27 | diff = diff / ITERATIONS 28 | print("encryption (%d, %s): %fms" % (key_size, plaintext, diff * 1000)) 29 | 30 | 31 | if __name__ == "__main__": 32 | for key_size in KEY_SIZES: 33 | for plaintext in PLAINTEXTS.keys(): 34 | encryption(key_size, plaintext) 35 | -------------------------------------------------------------------------------- /python-phe/keygen.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | import phe.paillier as paillier 4 | 5 | ITERATIONS = 100 6 | KEY_SIZES = [1024, 2048, 3072, 4096] 7 | 8 | def keygen(key_size): 9 | 10 | start = time.time() 11 | 12 | for _ in range(ITERATIONS): 13 | pubkey, prikey = paillier.generate_paillier_keypair(n_length=key_size) 14 | 15 | stop = time.time() 16 | 17 | diff = stop - start 18 | diff = diff / ITERATIONS 19 | print("keygen (%d): %fms" % (key_size, diff * 1000)) 20 | 21 | 22 | if __name__ == "__main__": 23 | for key_size in KEY_SIZES: 24 | keygen(key_size) 25 | -------------------------------------------------------------------------------- /rust-paillier-gmp/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /rust-paillier-gmp/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust" 3 | version = "0.1.0" 4 | authors = ["Morten Dahl "] 5 | 6 | [dependencies] 7 | paillier = { version="0.1", default-features=false, features=["inclgmp", "defaultgmp", "keygen"] } 8 | 9 | [dev-dependencies] 10 | bencher = "0.1" 11 | 12 | [[bench]] 13 | name = "keygen" 14 | harness = false 15 | 16 | [[bench]] 17 | name = "encryption" 18 | harness = false 19 | 20 | [[bench]] 21 | name = "decryption" 22 | harness = false 23 | 24 | [[bench]] 25 | name = "addition" 26 | harness = false 27 | -------------------------------------------------------------------------------- /rust-paillier-gmp/README.md: -------------------------------------------------------------------------------- 1 | # rust-paillier 2 | 3 | This is the bench of the [rust-paillier](https://github.com/snipsco/rust-paillier) library written in Rust and using GMP. 4 | 5 | ## Prerequisites 6 | 7 | Requires GMP to be installed. 8 | 9 | 10 | ## Benching 11 | 12 | Either run all of them: 13 | ``` 14 | cargo bench 15 | ``` 16 | 17 | or specific ones: 18 | ``` 19 | cargo bench --bench keygen 20 | cargo bench --bench encryption 21 | cargo bench --bench decryption 22 | cargo bench --bench addition 23 | ``` 24 | -------------------------------------------------------------------------------- /rust-paillier-gmp/benches/addition.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn addition(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT1: Plaintext, 15 | PT2: Plaintext 16 | { 17 | let (ek, _) = Paillier::keypair_with_modulus_size(KS::get()).keys(); 18 | let m1 = core::Plaintext(PT1::get()); 19 | let m2 = core::Plaintext(PT2::get()); 20 | let c1 = Paillier::encrypt(&ek, &m1); 21 | let c2 = Paillier::encrypt(&ek, &m2); 22 | 23 | b.iter(|| { 24 | let _ = Paillier::add(&ek, &c1, &c2); 25 | }); 26 | } 27 | 28 | benchmark_group!(group, 29 | addition, 30 | addition, 31 | addition, 32 | addition 33 | ); 34 | 35 | benchmark_main!(group); 36 | -------------------------------------------------------------------------------- /rust-paillier-gmp/benches/constants.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | use paillier::*; 5 | 6 | static SMALL: &'static str = "42"; 7 | static LARGE: &'static str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 8 | 9 | pub trait KeySize { fn get() -> usize; } 10 | pub struct KeySize1024; impl KeySize for KeySize1024 { fn get() -> usize { 1024 } } 11 | pub struct KeySize2048; impl KeySize for KeySize2048 { fn get() -> usize { 2048 } } 12 | pub struct KeySize3072; impl KeySize for KeySize3072 { fn get() -> usize { 3072 } } 13 | pub struct KeySize4096; impl KeySize for KeySize4096 { fn get() -> usize { 4096 } } 14 | 15 | pub trait Plaintext { fn get() -> BigInteger; } 16 | pub struct PlaintextSmall; impl Plaintext for PlaintextSmall { fn get() -> BigInteger { str::parse(SMALL).unwrap() } } 17 | pub struct PlaintextLarge; impl Plaintext for PlaintextLarge { fn get() -> BigInteger { str::parse(LARGE).unwrap() } } 18 | 19 | pub type StandardEncryption = core::standard::EncryptionKey; 20 | pub type GenericEncryption = core::generic::EncryptionKey; 21 | 22 | pub type StandardDecryption = core::standard::DecryptionKey; 23 | pub type CrtDecryption = core::crt::DecryptionKey; 24 | -------------------------------------------------------------------------------- /rust-paillier-gmp/benches/decryption.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn decryption(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT: Plaintext, 15 | for<'kp> DK: From<&'kp Keypair>, 16 | Paillier: Decryption, core::Plaintext>, 17 | { 18 | let keypair = Paillier::keypair_with_modulus_size(KS::get()); 19 | let ek = StandardEncryption::from(&keypair); 20 | let dk = DK::from(&keypair); 21 | let m = core::Plaintext(PT::get()); 22 | let c = Paillier::encrypt(&ek, &m); 23 | 24 | b.iter(|| { 25 | let _ = Paillier::decrypt(&dk, &c); 26 | }); 27 | } 28 | 29 | benchmark_group!(standard, 30 | decryption, 31 | decryption, 32 | 33 | decryption, 34 | decryption, 35 | 36 | decryption, 37 | decryption, 38 | 39 | decryption, 40 | decryption 41 | ); 42 | 43 | benchmark_group!(crt, 44 | decryption, 45 | decryption, 46 | 47 | decryption, 48 | decryption, 49 | 50 | decryption, 51 | decryption, 52 | 53 | decryption, 54 | decryption 55 | ); 56 | 57 | benchmark_main!(standard, crt); 58 | -------------------------------------------------------------------------------- /rust-paillier-gmp/benches/encryption.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn encryption(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT: Plaintext, 15 | for<'kp> EK: From<&'kp Keypair>, 16 | Paillier: Encryption, core::Ciphertext>, 17 | { 18 | let keypair = Paillier::keypair_with_modulus_size(KS::get()); 19 | let ek = EK::from(&keypair); 20 | let m = core::Plaintext(PT::get()); 21 | 22 | b.iter(|| { 23 | let _ = Paillier::encrypt(&ek, &m); 24 | }); 25 | } 26 | 27 | benchmark_group!(generic, 28 | encryption, 29 | encryption, 30 | 31 | encryption, 32 | encryption, 33 | 34 | encryption, 35 | encryption, 36 | 37 | encryption, 38 | encryption 39 | ); 40 | 41 | benchmark_group!(standard, 42 | encryption, 43 | encryption, 44 | 45 | encryption, 46 | encryption, 47 | 48 | encryption, 49 | encryption, 50 | 51 | encryption, 52 | encryption 53 | ); 54 | 55 | benchmark_main!(standard, generic); 56 | -------------------------------------------------------------------------------- /rust-paillier-gmp/benches/keygen.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn keygen(b: &mut Bencher) 12 | where 13 | KS: KeySize 14 | { 15 | b.iter(|| { 16 | let _ = Paillier::keypair_with_modulus_size(KS::get()); 17 | }); 18 | } 19 | 20 | benchmark_group!(group, 21 | keygen, 22 | keygen, 23 | keygen, 24 | keygen 25 | ); 26 | 27 | benchmark_main!(group); 28 | -------------------------------------------------------------------------------- /rust-paillier-gmp/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | #[test] 4 | fn it_works() { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /rust-paillier-ramp/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /rust-paillier-ramp/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust" 3 | version = "0.1.0" 4 | authors = ["Morten Dahl "] 5 | 6 | [dependencies] 7 | paillier = { version="0.1" } 8 | 9 | [dev-dependencies] 10 | bencher = "0.1" 11 | 12 | [[bench]] 13 | name = "keygen" 14 | harness = false 15 | 16 | [[bench]] 17 | name = "encryption" 18 | harness = false 19 | 20 | [[bench]] 21 | name = "decryption" 22 | harness = false 23 | 24 | [[bench]] 25 | name = "addition" 26 | harness = false 27 | -------------------------------------------------------------------------------- /rust-paillier-ramp/README.md: -------------------------------------------------------------------------------- 1 | # rust-paillier 2 | 3 | This is the bench of the [rust-paillier](https://github.com/snipsco/rust-paillier) library written in Rust and using RAMP. 4 | 5 | ## Benching 6 | 7 | Either run all of them: 8 | ``` 9 | cargo bench 10 | ``` 11 | 12 | or specific ones: 13 | ``` 14 | cargo bench --bench keygen 15 | cargo bench --bench encryption 16 | cargo bench --bench decryption 17 | cargo bench --bench addition 18 | ``` 19 | -------------------------------------------------------------------------------- /rust-paillier-ramp/benches/addition.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn addition(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT1: Plaintext, 15 | PT2: Plaintext 16 | { 17 | let (ek, _) = Paillier::keypair_with_modulus_size(KS::get()).keys(); 18 | let m1 = core::Plaintext(PT1::get()); 19 | let m2 = core::Plaintext(PT2::get()); 20 | let c1 = Paillier::encrypt(&ek, &m1); 21 | let c2 = Paillier::encrypt(&ek, &m2); 22 | 23 | b.iter(|| { 24 | let _ = Paillier::add(&ek, &c1, &c2); 25 | }); 26 | } 27 | 28 | benchmark_group!(group, 29 | addition, 30 | addition, 31 | addition, 32 | addition 33 | ); 34 | 35 | benchmark_main!(group); 36 | -------------------------------------------------------------------------------- /rust-paillier-ramp/benches/constants.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | use paillier::*; 5 | 6 | static SMALL: &'static str = "42"; 7 | static LARGE: &'static str = "9601375721773960030826048348718350956180868954786249183055522621772391594913965263068361191091587324151101807311169301869981191762119859865346892157945421998951222949069729370836921713919282283633399891943869137940899827469813950721928452427835958620445001112962904065293585229146038515621140909326729"; 8 | 9 | pub trait KeySize { fn get() -> usize; } 10 | pub struct KeySize1024; impl KeySize for KeySize1024 { fn get() -> usize { 1024 } } 11 | pub struct KeySize2048; impl KeySize for KeySize2048 { fn get() -> usize { 2048 } } 12 | pub struct KeySize3072; impl KeySize for KeySize3072 { fn get() -> usize { 3072 } } 13 | pub struct KeySize4096; impl KeySize for KeySize4096 { fn get() -> usize { 4096 } } 14 | 15 | pub trait Plaintext { fn get() -> BigInteger; } 16 | pub struct PlaintextSmall; impl Plaintext for PlaintextSmall { fn get() -> BigInteger { str::parse(SMALL).unwrap() } } 17 | pub struct PlaintextLarge; impl Plaintext for PlaintextLarge { fn get() -> BigInteger { str::parse(LARGE).unwrap() } } 18 | 19 | pub type StandardEncryption = core::standard::EncryptionKey; 20 | pub type GenericEncryption = core::generic::EncryptionKey; 21 | 22 | pub type StandardDecryption = core::standard::DecryptionKey; 23 | pub type CrtDecryption = core::crt::DecryptionKey; 24 | -------------------------------------------------------------------------------- /rust-paillier-ramp/benches/decryption.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn decryption(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT: Plaintext, 15 | for<'kp> DK: From<&'kp Keypair>, 16 | Paillier: Decryption, core::Plaintext>, 17 | { 18 | let keypair = Paillier::keypair_with_modulus_size(KS::get()); 19 | let ek = StandardEncryption::from(&keypair); 20 | let dk = DK::from(&keypair); 21 | let m = core::Plaintext(PT::get()); 22 | let c = Paillier::encrypt(&ek, &m); 23 | 24 | b.iter(|| { 25 | let _ = Paillier::decrypt(&dk, &c); 26 | }); 27 | } 28 | 29 | benchmark_group!(standard, 30 | decryption, 31 | decryption, 32 | 33 | decryption, 34 | decryption, 35 | 36 | decryption, 37 | decryption, 38 | 39 | decryption, 40 | decryption 41 | ); 42 | 43 | benchmark_group!(crt, 44 | decryption, 45 | decryption, 46 | 47 | decryption, 48 | decryption, 49 | 50 | decryption, 51 | decryption, 52 | 53 | decryption, 54 | decryption 55 | ); 56 | 57 | benchmark_main!(standard, crt); 58 | -------------------------------------------------------------------------------- /rust-paillier-ramp/benches/encryption.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn encryption(b: &mut Bencher) 12 | where 13 | KS: KeySize, 14 | PT: Plaintext, 15 | for<'kp> EK: From<&'kp Keypair>, 16 | Paillier: Encryption, core::Ciphertext>, 17 | { 18 | let keypair = Paillier::keypair_with_modulus_size(KS::get()); 19 | let ek = EK::from(&keypair); 20 | let m = core::Plaintext(PT::get()); 21 | 22 | b.iter(|| { 23 | let _ = Paillier::encrypt(&ek, &m); 24 | }); 25 | } 26 | 27 | benchmark_group!(generic, 28 | encryption, 29 | encryption, 30 | 31 | encryption, 32 | encryption, 33 | 34 | encryption, 35 | encryption, 36 | 37 | encryption, 38 | encryption 39 | ); 40 | 41 | benchmark_group!(standard, 42 | encryption, 43 | encryption, 44 | 45 | encryption, 46 | encryption, 47 | 48 | encryption, 49 | encryption, 50 | 51 | encryption, 52 | encryption 53 | ); 54 | 55 | benchmark_main!(standard, generic); 56 | -------------------------------------------------------------------------------- /rust-paillier-ramp/benches/keygen.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate paillier; 4 | 5 | use bencher::Bencher; 6 | use paillier::*; 7 | 8 | mod constants; 9 | use constants::*; 10 | 11 | pub fn keygen(b: &mut Bencher) 12 | where 13 | KS: KeySize 14 | { 15 | b.iter(|| { 16 | let _ = Paillier::keypair_with_modulus_size(KS::get()); 17 | }); 18 | } 19 | 20 | benchmark_group!(group, 21 | keygen, 22 | keygen, 23 | keygen, 24 | keygen 25 | ); 26 | 27 | benchmark_main!(group); 28 | -------------------------------------------------------------------------------- /rust-paillier-ramp/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | #[test] 4 | fn it_works() { 5 | } 6 | } 7 | --------------------------------------------------------------------------------