Skip to content

Commit b8dfaba

Browse files
committed
Initial working implementation without improvements
0 parents  commit b8dfaba

File tree

7 files changed

+273
-0
lines changed

7 files changed

+273
-0
lines changed

.gitignore

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: python
2+
python:
3+
- "3.6"
4+
5+
install:
6+
- pip install -r requirements.txt
7+
- pip install pytest
8+
script:
9+
- py.test

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Christian Burkert
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FCS: Fuzzy Commitment Scheme
2+
============================
3+
4+
.. image:: https://travis-ci.org/cburkert/fuzzy-commitment.svg?branch=master
5+
:target: https://travis-ci.org/cburkert/fuzzy-commitment
6+
7+
This implementation follows the scheme presented by `Juels and Wattenberg in 1999 <http://doi.acm.org/10.1145/319709.319714>`
8+
and includes the improvements of `Kelkboom et al. in 2011 <https://ieeexplore.ieee.org/abstract/document/5634099/>`.
9+
10+
**Warning**: This has not been independently audited and should **not**
11+
be used for productive or even critical applications.

fcs/fcs.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import hashlib
2+
import secrets
3+
from typing import Any, Callable, Generic, Optional, Tuple, TypeVar
4+
5+
from BitVector import BitVector
6+
import bchlib
7+
8+
9+
BCH_POLYNOMIAL = 8219
10+
11+
K = TypeVar('K')
12+
13+
14+
class FCS(Generic[K]):
15+
"""Fuzzy commitment scheme"""
16+
17+
def __init__(self, n: int, k: int, t: int,
18+
extractor: Optional[Callable[[K], BitVector]] = None) -> None:
19+
self.n = n
20+
self.k = k
21+
self.t = t
22+
if extractor is None:
23+
extractor = self.byte_extractor
24+
self.extractor = extractor
25+
self.bch = bchlib.BCH(BCH_POLYNOMIAL, self.t)
26+
27+
@classmethod
28+
def byte_extractor(cls, value: Any) -> BitVector:
29+
value_bytes = b""
30+
try:
31+
value_bytes = bytes(value)
32+
except TypeError as e:
33+
raise TypeError("Type %s is not directly convertible to bytes. "
34+
"You need to specifiy a custom extractor."
35+
% type(value), e)
36+
return BitVector(hexstring=value_bytes.hex())
37+
38+
def commit_raw(self, message: bytes,
39+
witness: BitVector) -> 'Commitment':
40+
ecc = self.bch.encode(message)
41+
codeword = message + ecc
42+
codeword_bv = BitVector(hexstring=codeword.hex())
43+
# TODO check if those bit string should not be rather
44+
# of the same length to protect the witness
45+
#assert len(codeword_bv) == len(witness) == self.n
46+
commitment = Commitment(
47+
hashlib.sha256(message).digest(),
48+
codeword_bv ^ witness,
49+
)
50+
return commitment
51+
52+
def commit_random_codeword_raw(self, witness: BitVector) -> 'Commitment':
53+
assert self.k % 8 == 0
54+
key = secrets.token_bytes((self.k + 7) // 8)
55+
return self.commit_raw(key, witness)
56+
57+
def commit(self, witness: K,
58+
codeword: Optional[BitVector] = None) -> 'Commitment':
59+
if codeword:
60+
return self.commit_raw(codeword, self.extractor(witness))
61+
else:
62+
return self.commit_random_codeword_raw(self.extractor(witness))
63+
64+
def verify_raw(self, commitment: 'Commitment',
65+
candidate: BitVector) -> Tuple[bool, bytes]:
66+
codeword_cand = candidate ^ commitment.auxiliar
67+
codeword_cand_bytes = bytes.fromhex(
68+
codeword_cand.get_bitvector_in_hex())
69+
bitflips, msg_cand, _ = self.bch.decode(
70+
codeword_cand_bytes[:-self.bch.ecc_bytes],
71+
codeword_cand_bytes[-self.bch.ecc_bytes:]
72+
)
73+
msg_cand = bytes(msg_cand)[-((self.k + 7) // 8):]
74+
msg_match = secrets.compare_digest(
75+
commitment.pseudonym,
76+
hashlib.sha256(msg_cand).digest()
77+
)
78+
return (msg_match, msg_cand)
79+
80+
def verify(self, commitment: 'Commitment',
81+
candidate: K) -> Tuple[bool, bytes]:
82+
return self.verify_raw(commitment, self.extractor(candidate))
83+
84+
85+
class Commitment(object):
86+
def __init__(self, pseudonym: bytes, auxiliar: BitVector) -> None:
87+
self.pseudonym = pseudonym
88+
self.auxiliar = auxiliar

fcs/test_fcs.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
import secrets
3+
4+
import fcs
5+
6+
7+
class TestFCS(unittest.TestCase):
8+
def setUp(self):
9+
self.threshold = 2
10+
self.cs = fcs.FCS(256, 128, self.threshold)
11+
self.witness = secrets.token_bytes(32)
12+
self.commitment = self.cs.commit(self.witness)
13+
14+
def random_flip_witness(self, numbits: int) -> bytes:
15+
witness_mod = bytearray(self.witness)
16+
for i in range(numbits):
17+
byte_num = secrets.randbelow(len(self.witness))
18+
bit_num = secrets.randbelow(8)
19+
witness_mod[byte_num] ^= (1 << bit_num)
20+
return bytes(witness_mod)
21+
22+
def test_unaltered_witness(self):
23+
valid, key = self.cs.verify(self.commitment, self.witness)
24+
self.assertTrue(valid)
25+
26+
def test_altered_tolerable(self):
27+
witness_mod = self.random_flip_witness(self.threshold)
28+
valid, key = self.cs.verify(self.commitment, witness_mod)
29+
self.assertTrue(valid)
30+
31+
def test_altered_intolerable(self):
32+
witness_mod = self.random_flip_witness(self.threshold + 1)
33+
valid, key = self.cs.verify(self.commitment, witness_mod)
34+
self.assertFalse(valid)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bchlib >= 0.7
2+
BitVector >= 3.4

0 commit comments

Comments
 (0)