Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve comments from upstream PR#31 #2

Open
wants to merge 12 commits into
base: feat/pk-enc
Choose a base branch
from
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ Cargo.lock
*.pdb

# KZG PARAMS
params
params

# Python virtual environment
.venv/
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ python3 scripts/circuit_sk.py -n 4096 -qis '[
]' -t 65537
```

To generate the parameters for the public key proof of encryption circuit run the following command:

```bash
python3 scripts/circuit_pk.py -n 4096 -qis '[
27424203952895201,
27424203952895203
]' -t 65537
```

Where `-n` is the degree of the cyclotomic polynomial that defines the ring, `-qis` is the list of moduli qis such that qis[i] is the modulus of the i-th CRT basis of the modulus q of the ciphertext space, `-t` is the plaintext modulus. The value of `𝜎` for the gaussian distribution is set to 3.2 by default.

You can modify these parameters to fit your needs. Note that the python script used to generate the inputs is largely unoptimized and can take a while to run for parameters with large `n`, since the polynomial multiplication is not done using NTT.

As a result:
- A file `./src/data/sk_enc_{n}_{qis_len}x{qis_bitsize}_{t}.json` is generated including the input to the circuit that can be used for testing for those specific parameters. It includes a random secret key, a random plaintext message and the corresponding ciphertext encrypted under the secret key.
- A file `./src/data/sk_enc_{n}_{qis_len}x{qis_bitsize}_{t}_zeroes.json` is generated. In this file all the coefficients of the input polynomials are set to zero. This input is used at key generation time, when the actual inputs are not known.
- A file `./src/constants/sk_enc_constants_{n}_{qis_len}x{qis_bitsize}_{t}.rs` is generated including the generic constants for the circuit. Note that we separate them from the input because these should be known at compile time.
- A file `./src/data/{p|s}k_enc_{n}_{qis_len}x{qis_bitsize}_{t}.json` is generated including the input to the circuit that can be used for testing for those specific parameters. It includes a random secret key, a random plaintext message and the corresponding ciphertext encrypted under the secret key.
- A file `./src/data/{p|s}k_enc_{n}_{qis_len}x{qis_bitsize}_{t}_zeroes.json` is generated. In this file all the coefficients of the input polynomials are set to zero. This input is used at key generation time, when the actual inputs are not known.
- A file `./src/constants/{p|s}k_enc_constants_{n}_{qis_len}x{qis_bitsize}_{t}.rs` is generated including the generic constants for the circuit. Note that we separate them from the input because these should be known at compile time.

On top of that, the console will print an estimatation of the number of advice cells needed to compile the circuit in halo2 considering a single advice column and a lookup table of size 2^8. Spoiler: around 90% of the constraints are generated by the range checks on the polynomial coefficients.

Expand Down
117 changes: 18 additions & 99 deletions scripts/circuit_pk.py

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions scripts/circuit_sk.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import os
from bfv.crt import CRTModuli
from bfv.bfv import BFVCrt
from bfv.bfv import BFV
from bfv.discrete_gauss import DiscreteGaussian
from bfv.polynomial import Polynomial, poly_div
from random import randint
import copy
from utils import assign_to_circuit, count_advice_cells_needed_for_poly_range_check, print_advice_cells_info
import argparse
import json
import numpy as np




Expand Down Expand Up @@ -292,8 +289,7 @@ def main(args):

# sanity check. The coefficients of ai * s + e should be in the range $- (N \cdot \frac{q_i - 1}{2} + B), N \cdot \frac{q_i - 1}{2} + B]$
bound = int((qis[i] - 1) / 2) * n + b
print(f" sk r2 bound = {bound}")
res = Polynomial(ais[i]) * s + e
res = ais[i] * s + e
assert all(coeff >= -bound and coeff <= bound for coeff in res.coefficients)

# constraint. The coefficients of r`2i should be in the range [-(qi-1)/2, (qi-1)/2]
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bfv @ git+https://github.com/enricobottazzi/bfv-py.git@e8239e946cab58c5c9fc974ff726010b71d92dbf
git+https://github.com/gnosisguild/bfv-py@no-ntt#egg=bfv
99 changes: 0 additions & 99 deletions scripts/test_pk_enc.py

This file was deleted.

157,903 changes: 157,902 additions & 1 deletion src/data/pk_enc_data/pk_enc_1024_15x60_65537.json

Large diffs are not rendered by default.

157,903 changes: 157,902 additions & 1 deletion src/data/pk_enc_data/pk_enc_1024_15x60_65537_zeroes.json

Large diffs are not rendered by default.

Loading