Implementing the Okamoto-Uchiyama cryptosystem in Rust
The Okamoto-Uchiyama cryptosystem is a semantically secure, asymmetric encryption algorithm introduced in 1998 by Tatsuaki Okamoto and Shigenori Uchiyama. It is additively homomorphic: multiplying two ciphertexts adds their plaintexts, so you can add encrypted values without decrypting them.
While searching GitHub for implementations of this algorithm, I found only two, in Python and Go, both at the proof-of-concept stage. I was also looking for a project to get my hands on Rust, so I decided to write my own.
Table of contents:
- The cryptosystem
- Encrypt and decrypt
- Homomorphic?
- Cool things I discovered while creating this project
The cryptosystem
This is a description in broad brush strokes. For a more formal definition, see the original publication.
Generate a key pair
A public/private key pair is generated as follows:
- Generate two large primes and .
- Compute .
- Choose a random integer such that .
- Compute .
The public key is then and the private key is .
use num_bigint_dig::BigUint;
use okamoto_uchiyama::{OkamotoUchiyama, PrivateKey, PublicKey};
// Initialization
// In this example we use a 1024-bit key
let length = okamoto_uchiyama::key::KeySize::Bits1024;
let okamoto_uchiyama = OkamotoUchiyama::init(length);
// Generate the key pair
let private_key = okamoto_uchiyama.generate_private_key();
let public_key = private_key.public_key.clone();
Keys can be 512, 1024, 2048 or 4096 bits long, using KeySize::Bits512, KeySize::Bits1024, KeySize::Bits2048 or KeySize::Bits4096 from okamoto_uchiyama::key.
Load existing keys
You can also load existing private and public keys:
use num_bigint_dig::BigUint;
use okamoto_uchiyama::{OkamotoUchiyama, PrivateKey, PublicKey};
let public_key = PublicKey::new(
&BigUint::from(9432233159u64),
&BigUint::from(8083706871u64),
&BigUint::from(7988052977u64),
);
let private_key = PrivateKey::new(
&public_key,
&BigUint::from(2003u64),
&BigUint::from(2351u64),
);
Encrypt and decrypt
Encryption
A message is encrypted with the public key as follows:
- Choose a random integer .
- Compute .
The value is the encryption of .
Decryption
An encrypted message is decrypted with the private key as follows:
- Compute .
- Compute . Both and are integers.
- Use the extended Euclidean algorithm to compute the inverse of modulo : .
- Compute .
The value is the decryption of .
use num_bigint_dig::BigUint;
use okamoto_uchiyama::{OkamotoUchiyama, PrivateKey, PublicKey};
let cleartext = BigUint::from(6u64);
// Initialization
let length = okamoto_uchiyama::key::KeySize::Bits1024;
let okamoto_uchiyama = OkamotoUchiyama::init(length);
// Generate the key pair
let private_key = okamoto_uchiyama.generate_private_key();
let public_key = private_key.public_key.clone();
// Encrypt and decrypt
let c = OkamotoUchiyama::encrypt(&cleartext, &public_key);
let m = OkamotoUchiyama::decrypt(&c, &private_key);
Homomorphic?
Homomorphic encryption allows computations to be performed on encrypted data without first decrypting it. Once decrypted, the result is identical to that of the same operations performed on the unencrypted data.
A notable feature of the Okamoto-Uchiyama cryptosystem is its homomorphic properties. It is, however, only partially homomorphic: you can add encrypted values together, but not multiply them.
Add two encrypted messages
The product of two ciphertexts decrypts to the sum of their plaintexts. The sum is computed modulo , so it must stay below to be recovered exactly:
use num_bigint_dig::BigUint;
use okamoto_uchiyama::{OkamotoUchiyama, PrivateKey, PublicKey};
let m1 = BigUint::from(6u64);
let m2 = BigUint::from(7u64);
// Initialization
let length = okamoto_uchiyama::key::KeySize::Bits1024;
let okamoto_uchiyama = OkamotoUchiyama::init(length);
// Generate the key pair
let private_key = okamoto_uchiyama.generate_private_key();
let public_key = private_key.public_key.clone();
let c1 = OkamotoUchiyama::encrypt(&m1, &public_key);
let c2 = OkamotoUchiyama::encrypt(&m2, &public_key);
let c1_c2 = public_key.homomorphic_encrypt_two(&c1, &c2).unwrap();
// Result is m1 + m2 = 6 + 7 = 13
let decrypted_c1_c2 = OkamotoUchiyama::decrypt(&c1_c2, &private_key);
Add multiple encrypted messages
The same goes for any number of ciphertexts:
use num_bigint_dig::BigUint;
use okamoto_uchiyama::{OkamotoUchiyama, PrivateKey, PublicKey};
let m1 = BigUint::from(6u64);
let m2 = BigUint::from(7u64);
let m3 = BigUint::from(8u64);
// Initialization
let length = okamoto_uchiyama::key::KeySize::Bits1024;
let okamoto_uchiyama = OkamotoUchiyama::init(length);
// Generate the key pair
let private_key = okamoto_uchiyama.generate_private_key();
let public_key = private_key.public_key.clone();
let c1 = OkamotoUchiyama::encrypt(&m1, &public_key);
let c2 = OkamotoUchiyama::encrypt(&m2, &public_key);
let c3 = OkamotoUchiyama::encrypt(&m3, &public_key);
let c1_c2_c3 = public_key
.homomorphic_encrypt_multiple(vec![&c1, &c2, &c3])
.unwrap();
// Result is m1 + m2 + m3 = 6 + 7 + 8 = 21
let decrypted_c1_c2_c3 = OkamotoUchiyama::decrypt(&c1_c2_c3, &private_key);
Cool things I discovered while creating this project
Anyone who has implemented a cryptographic algorithm knows that it involves very large numbers. I started with the num-bigint library, which worked very well at first. Then I discovered a pretty cool fork, num-bigint-dig, which implements many functions useful in cryptography, such as the modular inverse I use in my code.
I also came across this pretty cool paper, a PoC of a voting system using the Okamoto-Uchiyama cryptosystem.