Basic Usage

In this notebook, we give a few examples of how to use Cryptomite and accompanying utility functions. A similar, more introductory, guide to getting started can be found in this blog post.

Quick Start

pip install cryptomite

Initializing an extractor

Using Cryptomite, extractors can be initialized by giving the input length (in bits) n_1 and output length (in bits) m, or in the case of the Trevisan extractor, giving the input length n, output length m and maximum acceptable extractor error,error. We generate all the extractors of Cryptomite below, for an example n_1, m, error:

[1]:
import cryptomite
n_1, m, error = 100, 90, 0.0001
circulant = cryptomite.Circulant(n_1, m)
dodis = cryptomite.Dodis(n_1, m)
toeplitz = cryptomite.Toeplitz(n_1, m)
trevisan = cryptomite.Trevisan(n_1, m, error)

Performing extraction

Now the extractors have been initialized, we demonstrate how to perform extraction. The extractors take a list of input bits and a list of (weak) seed bits as input and output a new list of bits. Each extractor of Cryptomite requires a different amount of (weak) seed bits and the calculation of this is left to the user (either manually or using our from_params utility functions). In the following example, we generate both the input string and the (weak) seed using randint function from the random Python library, which users would replace with their desired generation methods, and use n_1 and n_2 to represent the input and (weak) seed lengths, respectively.

[2]:
from random import randint
input_bits = [randint(0, 1) for _ in range(n_1)]
circulant.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
dodis.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
toeplitz.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
trevisan.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 3
      1 from random import randint
      2 input_bits = [randint(0, 1) for _ in range(n_1)]
----> 3 circulant.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
      4 dodis.extract(input_bits, [randint(0, 1) for _ in range(n_2)])
      5 toeplitz.extract(input_bits, [randint(0, 1) for _ in range(n_2)])

NameError: name 'n_2' is not defined

Using the from_params utility functions

Alternatively, all Cryptomite extractors (except Trevisan) can be generated without manually calculating the output or seed length, by using the from_params utility functions. Simply provide the input length, input min-entropy, (weak) seed length, (weak) seed min-entropy, maximum acceptable extractor error and specify whether to calculate the quantum-proof output length or not. For example:

[3]:
import cryptomite
from math import log2
n_1, k_1 = 100, 60
n_2, k_2 = 100, 80
error = 0.00001
q_proof = False
log2_error = log2(error)
circulant = cryptomite.Circulant.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)
dodis = cryptomite.Dodis.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)
toeplitz = cryptomite.Toeplitz.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)
--- New Circulant Extractor Parameters ---
Input Length 1 (n_1): 100, Min Entropy of Input 1 (k_1): 60, Input Length 2 (n_2): 101, Min Entropy of Input 2 (k_2): 80, Output Length (m): 5, Extraction Error (log2_error): -16.609640474436812.
Adjust the length of the input
                  and (weak) seed accordingly.
--- New Dodis et al. Extractor Parameters ---
Input Length 1 (n_1): 101, Min Entropy of Input 1 (k_1): 60, Input Length 2 (n_2): 101, Min Entropy of Input 2 (k_2): 80, Output Length (m): 6, Extraction Error (log2_error): -16.609640474436812.
Adjust the length of the input
                  and (weak) seed accordingly.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 10
      8 circulant = cryptomite.Circulant.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)
      9 dodis = cryptomite.Dodis.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)
---> 10 toeplitz = cryptomite.Toeplitz.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/cryptomite/toeplitz.py:120, in Toeplitz.from_params(n_1, k_1, n_2, k_2, log2_error, q_proof, verbose)
    118 # Ensure the second input is longer than the first.
    119 if n_2 <= n_1:
--> 120     raise ValueError(
    121         'The second input must be longer than the first.'
    122         'Re-order the inputs or increase n_2.'
    123     )
    125 n_2_adjusted = n_2
    126 k_2_adjusted = k_2

ValueError: The second input must be longer than the first.Re-order the inputs or increase n_2.

In this case, a valid extractor is generated from the specified parameters (i.e. one that adheres to the required input and seed length criteria for that extractor). Any changes to input length, seed length, input entropy and seed entropy printed.