antonin

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:

  1. The cryptosystem
  2. Encrypt and decrypt
  3. Homomorphic?
  4. 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:

  1. Generate two large primes pp and qq.
  2. Compute n=p2qn=p^{2}q.
  3. Choose a random integer g∈{2…n−1}g\in \{2\dots n-1\} such that gp−1≢1(modp2)g^{p-1}\not\equiv 1 \pmod{p^{2}}.
  4. Compute h=gn mod nh=g^{n}\bmod n.

The public key is then (n,g,h)(n,g,h) and the private key is (p,q)(p,q).

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 m<pm<p is encrypted with the public key (n,g,h)(n,g,h) as follows:

  1. Choose a random integer r∈{1…n−1}r\in \{1\dots n-1\}.
  2. Compute c=gmhr mod nc=g^{m}h^{r}\bmod n.

The value cc is the encryption of mm.

Decryption

An encrypted message cc is decrypted with the private key (p,q)(p,q) as follows:

  1. Compute a=(cp−1 mod p2)−1pa=\dfrac{(c^{p-1}\bmod p^{2})-1}{p}.
  2. Compute b=(gp−1 mod p2)−1pb=\dfrac{(g^{p-1}\bmod p^{2})-1}{p}. Both aa and bb are integers.
  3. Use the extended Euclidean algorithm to compute the inverse of bb modulo pp: b′=b−1 mod pb'=b^{-1}\bmod p.
  4. Compute m=ab′ mod pm=ab'\bmod p.

The value mm is the decryption of cc.

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 pp, so it must stay below pp to be recovered exactly:

D(E(m1)⋅E(m2))=m1+m2 mod pD\big(E(m_1) \cdot E(m_2)\big) = m_1 + m_2 \bmod p
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:

D(E(m1)⋅E(m2)⋯E(mn))=m1+m2+⋯+mn mod pD\big(E(m_1) \cdot E(m_2) \cdots E(m_n)\big) = m_1 + m_2 + \dots + m_n \bmod p
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.