Skip to content

Commit

Permalink
hotfix: add missing env file
Browse files Browse the repository at this point in the history
  • Loading branch information
von-steinkirch committed Jan 25, 2024
1 parent 842cb16 commit 56d2818
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 24 deletions.
6 changes: 6 additions & 0 deletions magick-py/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
VECTOR_SIZE_N = 10
VECTOR_SIZE_M = 100
MOD_P = 50
BOUND = 3
MOD = 1000
LOG_LEVEL = debug
61 changes: 61 additions & 0 deletions magick-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# this repom
./api/*pyc
.DS_Store
/results/*txt

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST


# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
9 changes: 4 additions & 5 deletions magick-py/src/experiments/homomorphism.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# -*- encoding: utf-8 -*-
# src/experiments/additive_homomorphism.py
# Experiment with secret key regev encryption.
# Experiment with secret key Regev encryption.


from src.primitives.regev import Regev
from src.utils.os import exit_with_error


def additive_homomorphism() -> None:
"""
This method prooves that the secret key regev encryption scheme is
This method proves that the secret key Regev encryption scheme is
additive homomorphic, i.e., if c0 encrypts m0 and c1 encrypts m1,
both under s, then c0 + c1 decrypts to m0 + m1.
"""

########################################################################
# 1. Key generation for two indepedent messages m0 and m1
# 1. Key generation for two independent messages m0 and m1
########################################################################

r0 = Regev()
Expand Down Expand Up @@ -62,7 +61,7 @@ def additive_homomorphism() -> None:

def plaintext_inner_product() -> None:
"""
This method proves that the secret key regev encryption scheme is
This method proves that the secret key Regev encryption scheme is
supports plaintext inner product, i.e., if c0 encrypts m0 and c1
encrypts m1, both under s, then c0 * c1 decrypts to m0 * m1.
"""
Expand Down
8 changes: 4 additions & 4 deletions magick-py/src/experiments/simple_encryption.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- encoding: utf-8 -*-
# src/experiments/simple_encryption.py
# Experiment with secret key regev encryption.
# Experiment with secret key Regev encryption.


from src.primitives.regev import Regev


def linear_secret_key_regev_encryption_with_error() -> None:
"""
This method runs a secret key regev encryption and decryption
This method runs a secret key Regev encryption and decryption
experiment for a msg vector with a sampled error vector.
In this simple example of learning with error (LWE), we operate
Expand Down Expand Up @@ -52,13 +52,13 @@ def linear_secret_key_regev_encryption_scaled() -> None:
In this another simple example of learning with error (LWE), we loose
information on least significant bits by adding noise, i.e., by scaling
the message vector by delta = mod / p before addint it to encryption.
the message vector by delta = mod / p before adding it to encryption.
Then, during the decryption, we scale the message vector by 1 / delta.
The scaling ensures that m is in the highest bits of the message vector,
without losing information with the addition of the error vector e.
Now, the message m0 vectir has each element module p (not mod), where
Now, the message m0 vector has each element module p (not mod), where
p < q. The scaled message is now m0_scaled = m0 * delta = m0 * mod / p.
The cipertext c is B = A * s + e + m0_scaled, which can be decrypted as
c = (B, A), i.e., m0 = (B - A * s) / delta = (delta * m0 + e) / delta.
Expand Down
14 changes: 7 additions & 7 deletions magick-py/src/experiments/simple_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from src.primitives.regev import Regev
from src.primitives.message import Message
from src.utils.os import exit_with_error, log_info, log_debug
from src.utils.os import log_info, log_debug


def no_encryption_example() -> None:
Expand All @@ -20,16 +20,16 @@ def no_encryption_example() -> None:
log_debug('In this PIR tutorial, we represent a database as a square matrix, ' +
'where columns are the database entries and rows are the database attributes.')

log_debug('We intantiate the class Message(), creating a random database ' +
'with mod 500, and 20 entries and 20 attributes.\n')
log_debug('We start the class Message(), creating a random database ' +
'with mod 500, and 20 entries and 20 attributes.\n')

msg = Message()
db = msg.create_random_message(500, 20, 20)

log_debug(f'db: {db}\n')

########################################################################
# 2. Create some random query valye for row and column
# 2. Create some random query value for row and column
########################################################################
log_debug('Now, let\'s create a random query value for row and column. ' +
'Say, row 10 and column 10.')
Expand Down Expand Up @@ -75,7 +75,7 @@ def no_encryption_example() -> None:


def secret_key_regev_example() -> None:
"""Run a secret key regev encryption and decryption PIR experiment."""
"""Run a secret key Regev encryption and decryption PIR experiment."""
########################################################################
# 1. Represent a database as a square matrix, where the columns are
# the database entries and the rows are the database attributes
Expand Down Expand Up @@ -114,7 +114,7 @@ def secret_key_regev_example() -> None:
log_debug(f'query vector: {query.message}\n')

########################################################################
# 4. Encrypty query message vector
# 4. Encrypt query message vector
########################################################################
log_debug('4. Let\'s encrypt the query message vector, calculating A and e.')

Expand All @@ -128,7 +128,7 @@ def secret_key_regev_example() -> None:
########################################################################
# 5. Scale query vector by delta = mod / p and db vector from p to mod
########################################################################
log_debug('5. We scale the query vector by delta=mod/p and db vecto to 1/p')
log_debug('5. We scale the query vector by delta=mod/p and db vector to 1/p')

scaled_query = query.calculate_scaling(regev.mod, regev.p, regev.mod)
scaled_db = db.calculate_scaling(1, 1, regev.mod)
Expand Down
5 changes: 2 additions & 3 deletions magick-py/src/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# src/main.py
# Entry point for magick-py, a set of
# libraries and experiments for pir.
# by @bt3gl

import argparse

Expand All @@ -14,7 +13,7 @@

def run_menu() -> argparse.ArgumentParser:

parser = argparse.ArgumentParser(description='✨ Magick ✨')
parser = argparse.ArgumentParser(description='✨ Magick: PIR Experiment ✨')
parser.add_argument('-e', dest='lre', action='store_true',
help="Run simple linear key Regev encryption experiment with sampled error. \
Example: magick -e")
Expand Down
6 changes: 3 additions & 3 deletions magick-py/src/primitives/regev.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
# src/lib/regev.py
# Class for secret key regev encryption.
# Class for secret key Regev encryption.


from src.utils.os import load_config
from src.utils.os import log_info, log_debug, log_error
from src.utils.os import log_info, log_error
from src.primitives.message import Message


Expand Down Expand Up @@ -99,7 +99,7 @@ def calculate_encryption(A, s, e, m0):
"""
Encrypt this message with a simple `B = A * s + e + m0`,
where `s` is the secret and `e` is the error vector.
Set the cypher as the tuple c = (B, A).
Set the cipher as the tuple c = (B, A).
"""

B = (A * s) + e + m0
Expand Down
2 changes: 1 addition & 1 deletion magick-py/src/utils/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import random

def sample_error(bound):
"""Sample an error givena its bound"""
"""Sample an error given its bound"""

return random.randint(- bound, + bound)
1 change: 0 additions & 1 deletion magick-py/src/utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def load_config() -> dict:
env_vars['p'] = os.getenv('MOD_P')
env_vars['bound'] = os.getenv('BOUND')
set_logging(os.getenv("LOG_LEVEL"))

return env_vars

except KeyError as e:
Expand Down

0 comments on commit 56d2818

Please sign in to comment.