charm-crypto-framework 0.61.1__cp313-cp313-macosx_10_13_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- charm/__init__.py +5 -0
- charm/adapters/__init__.py +0 -0
- charm/adapters/abenc_adapt_hybrid.py +90 -0
- charm/adapters/dabenc_adapt_hybrid.py +145 -0
- charm/adapters/ibenc_adapt_hybrid.py +72 -0
- charm/adapters/ibenc_adapt_identityhash.py +80 -0
- charm/adapters/kpabenc_adapt_hybrid.py +91 -0
- charm/adapters/pkenc_adapt_bchk05.py +121 -0
- charm/adapters/pkenc_adapt_chk04.py +91 -0
- charm/adapters/pkenc_adapt_hybrid.py +98 -0
- charm/adapters/pksig_adapt_naor01.py +89 -0
- charm/config.py +7 -0
- charm/core/__init__.py +0 -0
- charm/core/benchmark/benchmark_util.c +353 -0
- charm/core/benchmark/benchmark_util.h +61 -0
- charm/core/benchmark/benchmarkmodule.c +476 -0
- charm/core/benchmark/benchmarkmodule.h +162 -0
- charm/core/benchmark.cpython-313-darwin.so +0 -0
- charm/core/crypto/AES/AES.c +1464 -0
- charm/core/crypto/AES.cpython-313-darwin.so +0 -0
- charm/core/crypto/DES/DES.c +113 -0
- charm/core/crypto/DES.cpython-313-darwin.so +0 -0
- charm/core/crypto/DES3/DES3.c +26 -0
- charm/core/crypto/DES3.cpython-313-darwin.so +0 -0
- charm/core/crypto/__init__.py +0 -0
- charm/core/crypto/cryptobase/XOR.c +80 -0
- charm/core/crypto/cryptobase/_counter.c +496 -0
- charm/core/crypto/cryptobase/_counter.h +54 -0
- charm/core/crypto/cryptobase/block_template.c +900 -0
- charm/core/crypto/cryptobase/block_template.h +69 -0
- charm/core/crypto/cryptobase/cryptobasemodule.c +220 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt.h +90 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_argchk.h +44 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_cfg.h +186 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_cipher.h +941 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_custom.h +556 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_des.c +1912 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_hash.h +407 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_mac.h +496 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_macros.h +435 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_math.h +534 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_misc.h +103 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_pk.h +653 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_pkcs.h +90 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_prng.h +199 -0
- charm/core/crypto/cryptobase/stream_template.c +271 -0
- charm/core/crypto/cryptobase/strxor.c +229 -0
- charm/core/crypto/cryptobase.cpython-313-darwin.so +0 -0
- charm/core/engine/__init__.py +5 -0
- charm/core/engine/protocol.py +293 -0
- charm/core/engine/util.py +174 -0
- charm/core/math/__init__.py +0 -0
- charm/core/math/elliptic_curve/ecmodule.c +1986 -0
- charm/core/math/elliptic_curve/ecmodule.h +230 -0
- charm/core/math/elliptic_curve.cpython-313-darwin.so +0 -0
- charm/core/math/elliptic_curve.pyi +63 -0
- charm/core/math/integer/integermodule.c +2539 -0
- charm/core/math/integer/integermodule.h +145 -0
- charm/core/math/integer.cpython-313-darwin.so +0 -0
- charm/core/math/integer.pyi +76 -0
- charm/core/math/pairing/miracl/miracl_config.h +37 -0
- charm/core/math/pairing/miracl/miracl_interface.h +118 -0
- charm/core/math/pairing/miracl/miracl_interface2.h +126 -0
- charm/core/math/pairing/miracl/pairingmodule2.c +2094 -0
- charm/core/math/pairing/miracl/pairingmodule2.h +307 -0
- charm/core/math/pairing/pairingmodule.c +2230 -0
- charm/core/math/pairing/pairingmodule.h +241 -0
- charm/core/math/pairing/relic/pairingmodule3.c +1853 -0
- charm/core/math/pairing/relic/pairingmodule3.h +233 -0
- charm/core/math/pairing/relic/relic_interface.c +1337 -0
- charm/core/math/pairing/relic/relic_interface.h +217 -0
- charm/core/math/pairing/relic/test_relic.c +171 -0
- charm/core/math/pairing.cpython-313-darwin.so +0 -0
- charm/core/math/pairing.pyi +69 -0
- charm/core/utilities/base64.c +248 -0
- charm/core/utilities/base64.h +15 -0
- charm/schemes/__init__.py +0 -0
- charm/schemes/abenc/__init__.py +0 -0
- charm/schemes/abenc/abenc_accountability_jyjxgd20.py +647 -0
- charm/schemes/abenc/abenc_bsw07.py +146 -0
- charm/schemes/abenc/abenc_ca_cpabe_ar17.py +684 -0
- charm/schemes/abenc/abenc_dacmacs_yj14.py +298 -0
- charm/schemes/abenc/abenc_lsw08.py +159 -0
- charm/schemes/abenc/abenc_maabe_rw15.py +236 -0
- charm/schemes/abenc/abenc_maabe_yj14.py +297 -0
- charm/schemes/abenc/abenc_tbpre_lww14.py +309 -0
- charm/schemes/abenc/abenc_unmcpabe_yahk14.py +223 -0
- charm/schemes/abenc/abenc_waters09.py +144 -0
- charm/schemes/abenc/abenc_yct14.py +208 -0
- charm/schemes/abenc/abenc_yllc15.py +178 -0
- charm/schemes/abenc/ac17.py +248 -0
- charm/schemes/abenc/bsw07.py +141 -0
- charm/schemes/abenc/cgw15.py +277 -0
- charm/schemes/abenc/dabe_aw11.py +204 -0
- charm/schemes/abenc/dfa_fe12.py +144 -0
- charm/schemes/abenc/pk_hve08.py +179 -0
- charm/schemes/abenc/waters11.py +143 -0
- charm/schemes/aggrsign_MuSig.py +150 -0
- charm/schemes/aggrsign_bls.py +267 -0
- charm/schemes/blindsig_ps16.py +654 -0
- charm/schemes/chamhash_adm05.py +113 -0
- charm/schemes/chamhash_rsa_hw09.py +100 -0
- charm/schemes/commit/__init__.py +0 -0
- charm/schemes/commit/commit_gs08.py +77 -0
- charm/schemes/commit/commit_pedersen92.py +53 -0
- charm/schemes/encap_bchk05.py +62 -0
- charm/schemes/grpsig/__init__.py +0 -0
- charm/schemes/grpsig/groupsig_bgls04.py +114 -0
- charm/schemes/grpsig/groupsig_bgls04_var.py +115 -0
- charm/schemes/hibenc/__init__.py +0 -0
- charm/schemes/hibenc/hibenc_bb04.py +105 -0
- charm/schemes/hibenc/hibenc_lew11.py +193 -0
- charm/schemes/ibenc/__init__.py +0 -0
- charm/schemes/ibenc/clpkc_rp03.py +119 -0
- charm/schemes/ibenc/ibenc_CW13_z.py +168 -0
- charm/schemes/ibenc/ibenc_bb03.py +94 -0
- charm/schemes/ibenc/ibenc_bf01.py +121 -0
- charm/schemes/ibenc/ibenc_ckrs09.py +120 -0
- charm/schemes/ibenc/ibenc_cllww12_z.py +172 -0
- charm/schemes/ibenc/ibenc_lsw08.py +120 -0
- charm/schemes/ibenc/ibenc_sw05.py +238 -0
- charm/schemes/ibenc/ibenc_waters05.py +144 -0
- charm/schemes/ibenc/ibenc_waters05_z.py +164 -0
- charm/schemes/ibenc/ibenc_waters09.py +107 -0
- charm/schemes/ibenc/ibenc_waters09_z.py +147 -0
- charm/schemes/joye_scheme.py +106 -0
- charm/schemes/lem_scheme.py +207 -0
- charm/schemes/pk_fre_ccv11.py +107 -0
- charm/schemes/pk_vrf.py +127 -0
- charm/schemes/pkenc/__init__.py +0 -0
- charm/schemes/pkenc/pkenc_cs98.py +108 -0
- charm/schemes/pkenc/pkenc_elgamal85.py +122 -0
- charm/schemes/pkenc/pkenc_gm82.py +98 -0
- charm/schemes/pkenc/pkenc_paillier99.py +118 -0
- charm/schemes/pkenc/pkenc_rabin.py +254 -0
- charm/schemes/pkenc/pkenc_rsa.py +186 -0
- charm/schemes/pksig/__init__.py +0 -0
- charm/schemes/pksig/pksig_CW13_z.py +135 -0
- charm/schemes/pksig/pksig_bls04.py +87 -0
- charm/schemes/pksig/pksig_boyen.py +156 -0
- charm/schemes/pksig/pksig_chch.py +97 -0
- charm/schemes/pksig/pksig_chp.py +70 -0
- charm/schemes/pksig/pksig_cl03.py +150 -0
- charm/schemes/pksig/pksig_cl04.py +87 -0
- charm/schemes/pksig/pksig_cllww12_z.py +142 -0
- charm/schemes/pksig/pksig_cyh.py +132 -0
- charm/schemes/pksig/pksig_dsa.py +76 -0
- charm/schemes/pksig/pksig_ecdsa.py +71 -0
- charm/schemes/pksig/pksig_hess.py +104 -0
- charm/schemes/pksig/pksig_hw.py +110 -0
- charm/schemes/pksig/pksig_lamport.py +63 -0
- charm/schemes/pksig/pksig_ps01.py +135 -0
- charm/schemes/pksig/pksig_ps02.py +124 -0
- charm/schemes/pksig/pksig_ps03.py +119 -0
- charm/schemes/pksig/pksig_rsa_hw09.py +206 -0
- charm/schemes/pksig/pksig_schnorr91.py +77 -0
- charm/schemes/pksig/pksig_waters.py +115 -0
- charm/schemes/pksig/pksig_waters05.py +121 -0
- charm/schemes/pksig/pksig_waters09.py +121 -0
- charm/schemes/pre_mg07.py +150 -0
- charm/schemes/prenc/pre_afgh06.py +126 -0
- charm/schemes/prenc/pre_bbs98.py +123 -0
- charm/schemes/prenc/pre_nal16.py +216 -0
- charm/schemes/protocol_a01.py +272 -0
- charm/schemes/protocol_ao00.py +215 -0
- charm/schemes/protocol_cns07.py +274 -0
- charm/schemes/protocol_schnorr91.py +125 -0
- charm/schemes/sigma1.py +64 -0
- charm/schemes/sigma2.py +129 -0
- charm/schemes/sigma3.py +126 -0
- charm/schemes/threshold/__init__.py +59 -0
- charm/schemes/threshold/dkls23_dkg.py +556 -0
- charm/schemes/threshold/dkls23_presign.py +1089 -0
- charm/schemes/threshold/dkls23_sign.py +761 -0
- charm/schemes/threshold/xrpl_wallet.py +967 -0
- charm/test/__init__.py +0 -0
- charm/test/adapters/__init__.py +0 -0
- charm/test/adapters/abenc_adapt_hybrid_test.py +29 -0
- charm/test/adapters/dabenc_adapt_hybrid_test.py +56 -0
- charm/test/adapters/ibenc_adapt_hybrid_test.py +36 -0
- charm/test/adapters/ibenc_adapt_identityhash_test.py +32 -0
- charm/test/adapters/kpabenc_adapt_hybrid_test.py +30 -0
- charm/test/benchmark/abenc_yllc15_bench.py +92 -0
- charm/test/benchmark/benchmark_test.py +148 -0
- charm/test/benchmark_threshold.py +260 -0
- charm/test/conftest.py +38 -0
- charm/test/fuzz/__init__.py +1 -0
- charm/test/fuzz/conftest.py +5 -0
- charm/test/fuzz/fuzz_policy_parser.py +76 -0
- charm/test/fuzz/fuzz_serialization.py +83 -0
- charm/test/schemes/__init__.py +0 -0
- charm/test/schemes/abenc/__init__.py +0 -0
- charm/test/schemes/abenc/abenc_bsw07_test.py +39 -0
- charm/test/schemes/abenc/abenc_dacmacs_yj14_test.py +16 -0
- charm/test/schemes/abenc/abenc_lsw08_test.py +33 -0
- charm/test/schemes/abenc/abenc_maabe_yj14_test.py +16 -0
- charm/test/schemes/abenc/abenc_tbpre_lww14_test.py +16 -0
- charm/test/schemes/abenc/abenc_waters09_test.py +38 -0
- charm/test/schemes/abenc/abenc_yllc15_test.py +74 -0
- charm/test/schemes/chamhash_adm05_test.py +31 -0
- charm/test/schemes/chamhash_rsa_hw09_test.py +29 -0
- charm/test/schemes/commit/__init__.py +0 -0
- charm/test/schemes/commit/commit_gs08_test.py +24 -0
- charm/test/schemes/commit/commit_pedersen92_test.py +26 -0
- charm/test/schemes/dabe_aw11_test.py +45 -0
- charm/test/schemes/encap_bchk05_test.py +21 -0
- charm/test/schemes/grpsig/__init__.py +0 -0
- charm/test/schemes/grpsig/groupsig_bgls04_test.py +35 -0
- charm/test/schemes/grpsig/groupsig_bgls04_var_test.py +39 -0
- charm/test/schemes/hibenc/__init__.py +0 -0
- charm/test/schemes/hibenc/hibenc_bb04_test.py +28 -0
- charm/test/schemes/ibenc/__init__.py +0 -0
- charm/test/schemes/ibenc/ibenc_bb03_test.py +26 -0
- charm/test/schemes/ibenc/ibenc_bf01_test.py +24 -0
- charm/test/schemes/ibenc/ibenc_ckrs09_test.py +25 -0
- charm/test/schemes/ibenc/ibenc_lsw08_test.py +31 -0
- charm/test/schemes/ibenc/ibenc_sw05_test.py +32 -0
- charm/test/schemes/ibenc/ibenc_waters05_test.py +31 -0
- charm/test/schemes/ibenc/ibenc_waters09_test.py +27 -0
- charm/test/schemes/pk_vrf_test.py +29 -0
- charm/test/schemes/pkenc/__init__.py +0 -0
- charm/test/schemes/pkenc_test.py +255 -0
- charm/test/schemes/pksig/__init__.py +0 -0
- charm/test/schemes/pksig_test.py +376 -0
- charm/test/schemes/rsa_alg_test.py +340 -0
- charm/test/schemes/threshold_test.py +1792 -0
- charm/test/serialize/__init__.py +0 -0
- charm/test/serialize/serialize_test.py +40 -0
- charm/test/toolbox/__init__.py +0 -0
- charm/test/toolbox/conversion_test.py +30 -0
- charm/test/toolbox/ecgroup_test.py +53 -0
- charm/test/toolbox/integer_arithmetic_test.py +441 -0
- charm/test/toolbox/paddingschemes_test.py +238 -0
- charm/test/toolbox/policy_parser_stress_test.py +969 -0
- charm/test/toolbox/secretshare_test.py +28 -0
- charm/test/toolbox/symcrypto_test.py +108 -0
- charm/test/toolbox/test_policy_expression.py +16 -0
- charm/test/vectors/__init__.py +1 -0
- charm/test/vectors/test_bls_vectors.py +289 -0
- charm/test/vectors/test_pedersen_vectors.py +315 -0
- charm/test/vectors/test_schnorr_vectors.py +368 -0
- charm/test/zkp_compiler/__init__.py +9 -0
- charm/test/zkp_compiler/benchmark_zkp.py +258 -0
- charm/test/zkp_compiler/test_and_proof.py +240 -0
- charm/test/zkp_compiler/test_batch_verify.py +248 -0
- charm/test/zkp_compiler/test_dleq_proof.py +264 -0
- charm/test/zkp_compiler/test_or_proof.py +231 -0
- charm/test/zkp_compiler/test_proof_serialization.py +121 -0
- charm/test/zkp_compiler/test_range_proof.py +241 -0
- charm/test/zkp_compiler/test_representation_proof.py +325 -0
- charm/test/zkp_compiler/test_schnorr_proof.py +221 -0
- charm/test/zkp_compiler/test_thread_safety.py +169 -0
- charm/test/zkp_compiler/test_zkp_parser.py +139 -0
- charm/toolbox/ABEnc.py +26 -0
- charm/toolbox/ABEncMultiAuth.py +66 -0
- charm/toolbox/ABEnumeric.py +800 -0
- charm/toolbox/Commit.py +24 -0
- charm/toolbox/DFA.py +89 -0
- charm/toolbox/FSA.py +1254 -0
- charm/toolbox/Hash.py +39 -0
- charm/toolbox/IBEnc.py +62 -0
- charm/toolbox/IBSig.py +64 -0
- charm/toolbox/PKEnc.py +66 -0
- charm/toolbox/PKSig.py +56 -0
- charm/toolbox/PREnc.py +32 -0
- charm/toolbox/ZKProof.py +289 -0
- charm/toolbox/__init__.py +0 -0
- charm/toolbox/bitstring.py +49 -0
- charm/toolbox/broadcast.py +220 -0
- charm/toolbox/conversion.py +100 -0
- charm/toolbox/eccurve.py +149 -0
- charm/toolbox/ecgroup.py +143 -0
- charm/toolbox/enum.py +60 -0
- charm/toolbox/hash_module.py +91 -0
- charm/toolbox/integergroup.py +323 -0
- charm/toolbox/iterate.py +22 -0
- charm/toolbox/matrixops.py +76 -0
- charm/toolbox/mpc_utils.py +296 -0
- charm/toolbox/msp.py +175 -0
- charm/toolbox/mta.py +985 -0
- charm/toolbox/node.py +120 -0
- charm/toolbox/ot/__init__.py +22 -0
- charm/toolbox/ot/base_ot.py +374 -0
- charm/toolbox/ot/dpf.py +642 -0
- charm/toolbox/ot/mpfss.py +228 -0
- charm/toolbox/ot/ot_extension.py +589 -0
- charm/toolbox/ot/silent_ot.py +378 -0
- charm/toolbox/paddingschemes.py +423 -0
- charm/toolbox/paddingschemes_test.py +238 -0
- charm/toolbox/pairingcurves.py +85 -0
- charm/toolbox/pairinggroup.py +186 -0
- charm/toolbox/policy_expression_spec.py +70 -0
- charm/toolbox/policytree.py +189 -0
- charm/toolbox/reCompiler.py +346 -0
- charm/toolbox/redundancyschemes.py +65 -0
- charm/toolbox/schemebase.py +188 -0
- charm/toolbox/secretshare.py +104 -0
- charm/toolbox/secretutil.py +174 -0
- charm/toolbox/securerandom.py +73 -0
- charm/toolbox/sigmaprotocol.py +46 -0
- charm/toolbox/specialprimes.py +45 -0
- charm/toolbox/symcrypto.py +279 -0
- charm/toolbox/threshold_sharing.py +553 -0
- charm/toolbox/xmlserialize.py +94 -0
- charm/toolbox/zknode.py +105 -0
- charm/zkp_compiler/__init__.py +89 -0
- charm/zkp_compiler/and_proof.py +460 -0
- charm/zkp_compiler/batch_verify.py +324 -0
- charm/zkp_compiler/dleq_proof.py +423 -0
- charm/zkp_compiler/or_proof.py +305 -0
- charm/zkp_compiler/range_proof.py +417 -0
- charm/zkp_compiler/representation_proof.py +466 -0
- charm/zkp_compiler/schnorr_proof.py +273 -0
- charm/zkp_compiler/thread_safe.py +150 -0
- charm/zkp_compiler/zk_demo.py +489 -0
- charm/zkp_compiler/zkp_factory.py +330 -0
- charm/zkp_compiler/zkp_generator.py +370 -0
- charm/zkp_compiler/zkparser.py +269 -0
- charm_crypto_framework-0.61.1.dist-info/METADATA +337 -0
- charm_crypto_framework-0.61.1.dist-info/RECORD +323 -0
- charm_crypto_framework-0.61.1.dist-info/WHEEL +5 -0
- charm_crypto_framework-0.61.1.dist-info/licenses/LICENSE.txt +165 -0
- charm_crypto_framework-0.61.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
r""" Module re_compile -- compile a regular expression into an FSA
|
|
2
|
+
|
|
3
|
+
To Do
|
|
4
|
+
-----
|
|
5
|
+
New features:
|
|
6
|
+
- add \-, \~
|
|
7
|
+
- add remaining metachars
|
|
8
|
+
- char set with ^ as first char will print wrong
|
|
9
|
+
- figure out when to print spaces between operators
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__author__ = "Oliver Steele <steele@osteele.com>"
|
|
13
|
+
|
|
14
|
+
from functools import reduce
|
|
15
|
+
import charm.toolbox.FSA as FSA
|
|
16
|
+
|
|
17
|
+
def compileSymbolRE(str):
|
|
18
|
+
return SymbolRECompiler(str).toFSA()
|
|
19
|
+
|
|
20
|
+
def dummy_func(a, b):
|
|
21
|
+
return a, b
|
|
22
|
+
|
|
23
|
+
class SymbolRECompiler:
|
|
24
|
+
EOF = -1
|
|
25
|
+
|
|
26
|
+
def __init__(self, str, recordSourcePositions=0):
|
|
27
|
+
self.str = str
|
|
28
|
+
self.recordSourcePositions = recordSourcePositions
|
|
29
|
+
|
|
30
|
+
def toFSA(self, minimize=1):
|
|
31
|
+
self.index = 0
|
|
32
|
+
self.nextToken = None
|
|
33
|
+
fsa = self.compileExpr()
|
|
34
|
+
if self.index < len(self.str):
|
|
35
|
+
raise ValueError('extra ' + str(')'))
|
|
36
|
+
del self.index
|
|
37
|
+
fsa.label = self.str
|
|
38
|
+
if minimize:
|
|
39
|
+
fsa = fsa.minimized()
|
|
40
|
+
return fsa
|
|
41
|
+
|
|
42
|
+
def readChar(self):
|
|
43
|
+
if self.index < len(self.str):
|
|
44
|
+
c, self.index = self.str[self.index], self.index + 1
|
|
45
|
+
return c
|
|
46
|
+
|
|
47
|
+
def peekChar(self):
|
|
48
|
+
if self.index < len(self.str):
|
|
49
|
+
return self.str[self.index]
|
|
50
|
+
|
|
51
|
+
def readToken(self):
|
|
52
|
+
token = self.nextToken or self._readNextToken()
|
|
53
|
+
self.nextToken = None
|
|
54
|
+
return token != self.EOF and token
|
|
55
|
+
|
|
56
|
+
def peekToken(self):
|
|
57
|
+
token = self.nextToken = self.nextToken or self._readNextToken()
|
|
58
|
+
#print 'peekToken', token
|
|
59
|
+
return token != self.EOF and token
|
|
60
|
+
|
|
61
|
+
def _readNextToken(self):
|
|
62
|
+
c = self.readChar()
|
|
63
|
+
if not c:
|
|
64
|
+
return self.EOF
|
|
65
|
+
elif c in '()|&':
|
|
66
|
+
return c
|
|
67
|
+
elif c == '.':
|
|
68
|
+
return ANY
|
|
69
|
+
return c
|
|
70
|
+
|
|
71
|
+
def skipTokens(self, bag):
|
|
72
|
+
while self.peekToken() and self.peekToken() in bag:
|
|
73
|
+
self.readToken()
|
|
74
|
+
|
|
75
|
+
def compileExpr(self):
|
|
76
|
+
fsa = FSA.NULL_FSA
|
|
77
|
+
while self.peekToken() and self.peekToken() != ')':
|
|
78
|
+
fsa = FSA.union(fsa, self.compileConjunction())
|
|
79
|
+
self.skipTokens(['|'])
|
|
80
|
+
return fsa
|
|
81
|
+
|
|
82
|
+
def compileConjunction(self):
|
|
83
|
+
fsa = None
|
|
84
|
+
while self.peekToken() and self.peekToken() not in (')', '|'):
|
|
85
|
+
sequence = self.compileSequence()
|
|
86
|
+
fsa = fsa and FSA.intersection(fsa, sequence) or sequence
|
|
87
|
+
self.skipTokens(['&'])
|
|
88
|
+
return fsa
|
|
89
|
+
|
|
90
|
+
def compileSequence(self):
|
|
91
|
+
fsa = FSA.EMPTY_STRING_FSA
|
|
92
|
+
while self.peekToken() and self.peekToken() not in (')', '|', '&'):
|
|
93
|
+
fsa = FSA.concatenation(fsa, self.compileItem())
|
|
94
|
+
return fsa
|
|
95
|
+
|
|
96
|
+
def compileItem(self):
|
|
97
|
+
startPosition = self.index
|
|
98
|
+
c = self.readToken()
|
|
99
|
+
if c == '(':
|
|
100
|
+
fsa = self.compileExpr()
|
|
101
|
+
if self.readToken() != ')':
|
|
102
|
+
raise ValueError("missing ')'")
|
|
103
|
+
elif c == '~':
|
|
104
|
+
fsa = FSA.complement(self.compileItem())
|
|
105
|
+
else:
|
|
106
|
+
fsa = FSA.singleton(c, arcMetadata=self.recordSourcePositions and [startPosition])
|
|
107
|
+
while self.peekChar() and self.peekChar() in '?*+':
|
|
108
|
+
c = self.readChar()
|
|
109
|
+
if c == '*':
|
|
110
|
+
fsa = FSA.closure(fsa)
|
|
111
|
+
elif c == '?':
|
|
112
|
+
fsa = FSA.union(fsa, FSA.EMPTY_STRING_FSA)
|
|
113
|
+
elif c == '+':
|
|
114
|
+
fsa = FSA.iteration(fsa)
|
|
115
|
+
else:
|
|
116
|
+
raise ValueError('program error')
|
|
117
|
+
return fsa
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
#
|
|
121
|
+
# Character REs
|
|
122
|
+
#
|
|
123
|
+
|
|
124
|
+
class CharacterSet:
|
|
125
|
+
def __init__(self, ranges):
|
|
126
|
+
if type(ranges) == str:
|
|
127
|
+
ranges = self.convertString(ranges)
|
|
128
|
+
accum = []
|
|
129
|
+
# copy, so sort doesn't destroy the arg
|
|
130
|
+
for item in ranges:
|
|
131
|
+
if type(item) == tuple:
|
|
132
|
+
if len(item) == 1:
|
|
133
|
+
accum.append((item, item))
|
|
134
|
+
elif len(item) == 2:
|
|
135
|
+
accum.append(item)
|
|
136
|
+
else:
|
|
137
|
+
raise ValueError("invalid argument to CharacterSet")
|
|
138
|
+
elif type(item) == str:
|
|
139
|
+
for c in item:
|
|
140
|
+
accum.append((c, c))
|
|
141
|
+
else:
|
|
142
|
+
raise ValueError("invalid argument to CharacterSet")
|
|
143
|
+
ranges = accum
|
|
144
|
+
ranges.sort()
|
|
145
|
+
index = 0
|
|
146
|
+
while index < len(ranges) - 1:
|
|
147
|
+
[(c0, c1), (c2, c3)] = ranges[index:index + 2]
|
|
148
|
+
if c1 >= c2:
|
|
149
|
+
ranges[index:index + 2] = [(c0, max(c1, c3))]
|
|
150
|
+
else:
|
|
151
|
+
index = index + 1
|
|
152
|
+
self.ranges = ranges
|
|
153
|
+
|
|
154
|
+
def __cmp__(self, other):
|
|
155
|
+
return cmp(type(self), type(other)) or cmp(self.__class__, other.__class__) or cmp(self.ranges, other.ranges)
|
|
156
|
+
|
|
157
|
+
def __hash__(self):
|
|
158
|
+
return reduce(lambda a, b:a ^ b, map(hash, self.ranges))
|
|
159
|
+
|
|
160
|
+
def convertString(self, _str):
|
|
161
|
+
ranges = []
|
|
162
|
+
index = 0
|
|
163
|
+
while index < len(_str):
|
|
164
|
+
c0 = c1 = _str[index]
|
|
165
|
+
index = index + 1
|
|
166
|
+
if index + 1 < len(_str) and _str[index ] == '-':
|
|
167
|
+
c1 = _str[index + 1]
|
|
168
|
+
index = index + 2
|
|
169
|
+
ranges.append((c0, c1))
|
|
170
|
+
return ranges
|
|
171
|
+
|
|
172
|
+
def matches(self, c):
|
|
173
|
+
for c0, c1 in self.ranges:
|
|
174
|
+
if c0 <= c and c <= c1:
|
|
175
|
+
return 1
|
|
176
|
+
return 0
|
|
177
|
+
|
|
178
|
+
def complement(self):
|
|
179
|
+
results = []
|
|
180
|
+
for (_, c0), (c1, _) in map(dummy_func, [(None, None)] + self.ranges, self.ranges + [(None, None)]):
|
|
181
|
+
i0 = c0 and ord(c0) + 1 or 0
|
|
182
|
+
i1 = c1 and ord(c1) - 1 or 255
|
|
183
|
+
if i0 <= i1:
|
|
184
|
+
results.append((chr(i0), chr(i1)))
|
|
185
|
+
if results:
|
|
186
|
+
return CharacterSet(results)
|
|
187
|
+
|
|
188
|
+
def union(self, other):
|
|
189
|
+
a = self.complement()
|
|
190
|
+
b = other.complement()
|
|
191
|
+
if a and b:
|
|
192
|
+
c = a.intersection(b)
|
|
193
|
+
if c:
|
|
194
|
+
return c.complement()
|
|
195
|
+
else:
|
|
196
|
+
return self.ANY
|
|
197
|
+
else:
|
|
198
|
+
return a or b
|
|
199
|
+
|
|
200
|
+
def __add__(self, other):
|
|
201
|
+
return self.union(other)
|
|
202
|
+
|
|
203
|
+
def intersection(self, other):
|
|
204
|
+
if self.ranges == other.ranges:
|
|
205
|
+
return self
|
|
206
|
+
results = []
|
|
207
|
+
for (a0, a1) in self.ranges:
|
|
208
|
+
for (b0, b1) in other.ranges:
|
|
209
|
+
c0 = max(a0, b0)
|
|
210
|
+
c1 = min(a1, b1)
|
|
211
|
+
if c0 <= c1:
|
|
212
|
+
results.append((c0, c1))
|
|
213
|
+
results.sort()
|
|
214
|
+
if results:
|
|
215
|
+
return CharacterSet(results)
|
|
216
|
+
|
|
217
|
+
def __str__(self):
|
|
218
|
+
"""
|
|
219
|
+
### print(CharacterSet([('a', 'a')]))
|
|
220
|
+
a
|
|
221
|
+
### print(CharacterSet([('a', 'b')]))
|
|
222
|
+
[ab]
|
|
223
|
+
"""
|
|
224
|
+
if self == self.ANY:
|
|
225
|
+
return '.'
|
|
226
|
+
elif not self.ranges:
|
|
227
|
+
return '[^.]'
|
|
228
|
+
for key, value in METACHARS.items():
|
|
229
|
+
if self == value:
|
|
230
|
+
return '\\' + key
|
|
231
|
+
ranges = self.ranges
|
|
232
|
+
if len(ranges) == 1 and ranges[0][0] == ranges[0][1]:
|
|
233
|
+
return ranges[0][0]
|
|
234
|
+
if ranges[0][0] == chr(0) and ranges[-1][1] == chr(255):
|
|
235
|
+
s = str(self.complement())
|
|
236
|
+
if s[0] == '[' and s[-1] == ']':
|
|
237
|
+
s = s[1:-1]
|
|
238
|
+
return '[^' + s + ']'
|
|
239
|
+
s = ''
|
|
240
|
+
for c0, c1 in ranges:
|
|
241
|
+
if c0 == c1 and c0 != '-':
|
|
242
|
+
s = s + self.crep(c0)
|
|
243
|
+
elif ord(c0) + 1 == ord(c1) and c0 != '-' and c1 != '-':
|
|
244
|
+
s = s + "%s%s" % (self.crep(c0), self.crep(c1))
|
|
245
|
+
else:
|
|
246
|
+
s = s + "%s-%s" % (self.crep(c0), self.crep(c1))
|
|
247
|
+
return '[' + s + ']'
|
|
248
|
+
|
|
249
|
+
def crep(self, c):
|
|
250
|
+
return {'\t': '\\t', '\n': '\\n', '\r': '\\r', '\f': '\\f', '\v': '\\v'}.get(c, c)
|
|
251
|
+
|
|
252
|
+
def __repr__(self):
|
|
253
|
+
return '<' + self.__class__.__name__ + ' ' + str(self) + '>'
|
|
254
|
+
|
|
255
|
+
METACHARS = {
|
|
256
|
+
'd': CharacterSet('0-9'),
|
|
257
|
+
's': CharacterSet(' \t\n\r\f\v'),
|
|
258
|
+
'w': CharacterSet('a-zA-Z0-9')}
|
|
259
|
+
METACHARS['D'] = METACHARS['d'].complement()
|
|
260
|
+
METACHARS['S'] = METACHARS['s'].complement()
|
|
261
|
+
METACHARS['W'] = METACHARS['w'].complement()
|
|
262
|
+
|
|
263
|
+
CharacterSet.ANY = CharacterSet([(chr(0), chr(255))])
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class RECompiler(SymbolRECompiler):
|
|
267
|
+
def _readNextToken(self):
|
|
268
|
+
c = self.readChar()
|
|
269
|
+
if not c:
|
|
270
|
+
return self.EOF
|
|
271
|
+
elif c in '()|':
|
|
272
|
+
return c
|
|
273
|
+
elif c == '.':
|
|
274
|
+
return CharacterSet.ANY
|
|
275
|
+
elif c == '[':
|
|
276
|
+
if self.peekChar() == '~':
|
|
277
|
+
self.readChar()
|
|
278
|
+
return self.readCSetInnards().complement()
|
|
279
|
+
else:
|
|
280
|
+
return self.readCSetInnards()
|
|
281
|
+
elif c == '\\':
|
|
282
|
+
c = self.readChar()
|
|
283
|
+
if METACHARS.get(c):
|
|
284
|
+
return METACHARS.get(c)
|
|
285
|
+
elif c == '&':
|
|
286
|
+
return c
|
|
287
|
+
else:
|
|
288
|
+
return CharacterSet([(c,c)])
|
|
289
|
+
else:
|
|
290
|
+
return CharacterSet([(c,c)])
|
|
291
|
+
|
|
292
|
+
def readCSetInnards(self):
|
|
293
|
+
cset = CharacterSet([])
|
|
294
|
+
while 1:
|
|
295
|
+
c = self.readChar()
|
|
296
|
+
if c == ']':
|
|
297
|
+
return cset
|
|
298
|
+
if self.peekChar() == '-':
|
|
299
|
+
self.readChar()
|
|
300
|
+
cset = cset.union(CharacterSet([(c, self.readChar())]))
|
|
301
|
+
else:
|
|
302
|
+
cset = cset.union(CharacterSet([(c, c)]))
|
|
303
|
+
|
|
304
|
+
def compileRE(_str, minimize=1, recordSourcePositions=0):
|
|
305
|
+
return RECompiler(_str, recordSourcePositions=recordSourcePositions).toFSA(minimize=minimize)
|
|
306
|
+
|
|
307
|
+
#
|
|
308
|
+
# testing
|
|
309
|
+
#
|
|
310
|
+
def _printCompiledREs():
|
|
311
|
+
print (compileRE('a'))
|
|
312
|
+
print (compileRE('ab'))
|
|
313
|
+
print (compileRE('a|b'))
|
|
314
|
+
print (compileRE('abc'))
|
|
315
|
+
print (compileRE('ab*c'))
|
|
316
|
+
print (compileRE('ab?c'))
|
|
317
|
+
print (compileRE('ab+c'))
|
|
318
|
+
print (compileRE('ab|c'))
|
|
319
|
+
print (compileRE('a(b|c)'))
|
|
320
|
+
#print compileRE('a\&a')
|
|
321
|
+
#print compileRE('ab+\&a+b')
|
|
322
|
+
#print compileRE('ab*\&a*b')
|
|
323
|
+
print (compileRE('ab|c?'))
|
|
324
|
+
print (compileRE('ab|bc?'))
|
|
325
|
+
print (compileRE('a?'))
|
|
326
|
+
print (compileRE('abc|acb|bac|bca|cab|cba'))
|
|
327
|
+
print (compileRE('abc|acb|bac|bca|cab|cba', 0).determinized())
|
|
328
|
+
print (compileRE('abc|acb|bac|bca|cab|cba', 0).determinized())
|
|
329
|
+
print (compileRE('abc|acb|bac|bca|cab|cba', 0).minimized())
|
|
330
|
+
print (compileRE('abc|acb|bac|bca|cab', 0).determinized())
|
|
331
|
+
|
|
332
|
+
print (compileRE('a', 0))
|
|
333
|
+
print (compileRE('a', 0).determinized())
|
|
334
|
+
print (compileRE('ab', 0).determinized())
|
|
335
|
+
print (compileRE('a', 0).minimized())
|
|
336
|
+
print (compileRE('ab', 0).minimized())
|
|
337
|
+
print (compileRE('a'))
|
|
338
|
+
print (compileRE('a|b', 0).determinized())
|
|
339
|
+
print (compileRE('a|b', 0).minimized().getArcMetadata())
|
|
340
|
+
print (compileRE('a|b', 0).minimized())
|
|
341
|
+
|
|
342
|
+
def _test(reset=0):
|
|
343
|
+
import doctest, compileRE
|
|
344
|
+
if reset:
|
|
345
|
+
doctest.master = None # This keeps doctest from complaining after a reload.
|
|
346
|
+
return doctest.testmod(compileRE)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'''A collection of redundancy schemes'''
|
|
2
|
+
from charm.toolbox.bitstring import Bytes,py3
|
|
3
|
+
from charm.toolbox.securerandom import SecureRandomFactory
|
|
4
|
+
import charm.core.crypto.cryptobase
|
|
5
|
+
import hashlib
|
|
6
|
+
import math
|
|
7
|
+
import struct
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
debug = False
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InMessageRedundancy:
|
|
14
|
+
'''
|
|
15
|
+
:Authors: Christina Garman
|
|
16
|
+
'''
|
|
17
|
+
def __init__(self):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
def encode(self, message):
|
|
21
|
+
str_message = message.decode("utf-8")
|
|
22
|
+
str_message += str_message[-8:]
|
|
23
|
+
|
|
24
|
+
return str_message.encode("utf-8")
|
|
25
|
+
|
|
26
|
+
def decode(self, encMessage):
|
|
27
|
+
byte_message = bytearray(encMessage)
|
|
28
|
+
|
|
29
|
+
if(byte_message[-8:] == byte_message[-16:-8]):
|
|
30
|
+
return (True,bytes(byte_message[:-8]))
|
|
31
|
+
else:
|
|
32
|
+
return (False,bytes(byte_message[:-8]))
|
|
33
|
+
|
|
34
|
+
class ExtraBitsRedundancy:
|
|
35
|
+
'''
|
|
36
|
+
:Authors: Christina Garman
|
|
37
|
+
|
|
38
|
+
TODO
|
|
39
|
+
'''
|
|
40
|
+
def __init__(self):
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
def encode(self, message):
|
|
44
|
+
|
|
45
|
+
return Bytes(b'\x00') + maskedSeed + maskedDB
|
|
46
|
+
|
|
47
|
+
def decode(self, encMessage, label=""):
|
|
48
|
+
|
|
49
|
+
return M
|
|
50
|
+
class WilliamsRedundancy:
|
|
51
|
+
'''
|
|
52
|
+
:Authors: Christina Garman
|
|
53
|
+
|
|
54
|
+
TODO
|
|
55
|
+
'''
|
|
56
|
+
def __init__(self):
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
def encode(self, message):
|
|
60
|
+
|
|
61
|
+
return Bytes(b'\x00') + maskedSeed + maskedDB
|
|
62
|
+
|
|
63
|
+
def decode(self, encMessage, label=""):
|
|
64
|
+
|
|
65
|
+
return M
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
from charm.toolbox.enum import *
|
|
2
|
+
|
|
3
|
+
# user-map
|
|
4
|
+
EU_CMA,SU_CMA="EU_CMA","SU_CMA"
|
|
5
|
+
SM,ROM,CRS = "SM","ROM","CRS"
|
|
6
|
+
OW,RSA,StrongRSA,DL,DH,CDH,DDH,DBDH,q_SDH,LRSW = "OW","RSA","StrongRSA","DL","DH","CDH","DDH","DBDH","q_SDH","LRSW"
|
|
7
|
+
|
|
8
|
+
# security models: standard, random oracle and common reference string
|
|
9
|
+
baseSecModels = Enum('SM', 'ROM', 'CRS')
|
|
10
|
+
# scheme types
|
|
11
|
+
SchemeType = Enum('PKEnc', 'PKSig', 'IBEnc', 'IBSig', 'RingSig', 'GroupSig', 'ABEnc', 'DABEnc','Commitment', 'Hash', 'ChamHash', 'Protocol', 'PREnc')
|
|
12
|
+
# security hardness assumptions
|
|
13
|
+
secAssump = Enum('OW','RSA','StrongRSA','DL','DH','CDH','DDH','DBDH','q_SDH','LRSW') # need to expand this since it captures implications
|
|
14
|
+
|
|
15
|
+
schemeType = "scheme"
|
|
16
|
+
assumptionType = "assumption"
|
|
17
|
+
messageSpaceType = "messageSpace"
|
|
18
|
+
secModelType = "secModel"
|
|
19
|
+
secDefType = "secDef"
|
|
20
|
+
|
|
21
|
+
class SchemeBase:
|
|
22
|
+
'''Base class for all crypto, which defines security properties of cryptosystem'''
|
|
23
|
+
def __init__(self):
|
|
24
|
+
self.properties = {}
|
|
25
|
+
|
|
26
|
+
def _setProperty(self, scheme=None, secDef=None, assumption=None, messageSpace=None, secModel=None, **kwargs):
|
|
27
|
+
if scheme is not None and scheme in SchemeType.getList(): self.properties[ schemeType ] = SchemeType[scheme]
|
|
28
|
+
if assumption is not None and assumption in secAssump.getList(): self.properties[ assumptionType ] = secAssump[assumption]
|
|
29
|
+
if messageSpace is not None and type(messageSpace) == list:
|
|
30
|
+
self.properties[ messageSpaceType ] = list(messageSpace)
|
|
31
|
+
elif messageSpace is not None:
|
|
32
|
+
self.properties[ messageSpaceType ] = messageSpace # TODO: better error handling here
|
|
33
|
+
|
|
34
|
+
if secModel is not None and secModel in baseSecModels.getList(): self.properties[ secModelType ] = baseSecModels[secModel]
|
|
35
|
+
if secDef is not None: self.properties[ secDefType ] = secDef # defined by subclass
|
|
36
|
+
for key in kwargs.keys():
|
|
37
|
+
self.properties[ key ] = kwargs[key]
|
|
38
|
+
return True
|
|
39
|
+
|
|
40
|
+
def _getProperty(self):
|
|
41
|
+
return dict(self.properties)
|
|
42
|
+
|
|
43
|
+
def _checkProperty(self, scheme, prop):
|
|
44
|
+
# verify scheme is a subclass of SchemeBase
|
|
45
|
+
if not hasattr(scheme, 'getProperty'):
|
|
46
|
+
assert False, "ERROR: Scheme class not derived from any of the Charm scheme types."
|
|
47
|
+
|
|
48
|
+
if type(prop) == list:
|
|
49
|
+
criteria = list(prop)
|
|
50
|
+
#print("criteria: ", criteria)
|
|
51
|
+
targetProps = scheme.getProperty()
|
|
52
|
+
#print("check list =>", targetProps)
|
|
53
|
+
for k,v in criteria:
|
|
54
|
+
#print(k, ":", v)
|
|
55
|
+
if k in targetProps.keys():
|
|
56
|
+
# found a match
|
|
57
|
+
if (v == str(targetProps[k])):
|
|
58
|
+
continue
|
|
59
|
+
# criteria value is less than target value
|
|
60
|
+
elif v in baseSecModels.getList() and baseSecModels[v] < targetProps[k]:
|
|
61
|
+
continue
|
|
62
|
+
else:
|
|
63
|
+
assert False, "ERROR: required property not in scheme dictionary or not satisfied: %s" % k
|
|
64
|
+
return True
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def verifyTypeStruct(self, source, target, _types=dict):
|
|
68
|
+
# make sure src and targ the same type otherwise raise error
|
|
69
|
+
if type(source) != type(target):
|
|
70
|
+
assert False, "type mismatch between src='%s' and targ='%s'" % (type(source), type(target))
|
|
71
|
+
if _types == dict: _iter = target.keys()
|
|
72
|
+
elif _types in [list, tuple]:
|
|
73
|
+
_iter = range(len(source))
|
|
74
|
+
target = [target[0] for i in _iter]
|
|
75
|
+
#print("target =>", target)
|
|
76
|
+
#if struct unknown, then we shouldn't be calling this method
|
|
77
|
+
else:
|
|
78
|
+
assert False, "invalid structure type. wrong method"
|
|
79
|
+
|
|
80
|
+
for i in _iter:
|
|
81
|
+
if hasattr(source[i], 'type'): # check for charm elements
|
|
82
|
+
assert source[i].type == target[i], "invalid type: '%s' should be '%s' not '%s'" % (i, target[i], source[i].type)
|
|
83
|
+
elif type(source[i]) in [dict, tuple, list]: # all dict elements (charm or python) must match target type
|
|
84
|
+
keys = source[i].keys() if type(source[i]) == dict else range(len(source[i]))
|
|
85
|
+
for j in keys:
|
|
86
|
+
if hasattr(source[i][j ], 'type'):
|
|
87
|
+
assert source[i][j].type == target[i], "invalid type: '%s' should be '%s' not '%s'" % (j, target[i], source[i][j].type)
|
|
88
|
+
else:
|
|
89
|
+
assert type(source[i][j]) == target[i], "invalid type: %s" % (target[i], type(source[i][j]))
|
|
90
|
+
else: # normal python type
|
|
91
|
+
assert type(source[i]) == target[i], "invalid type: %s not %s" % (target[i], type(source[i]))
|
|
92
|
+
return True
|
|
93
|
+
|
|
94
|
+
@classmethod
|
|
95
|
+
def verifyType(self, source, target):
|
|
96
|
+
if hasattr(source, 'type'):
|
|
97
|
+
# source must be one of our base module types
|
|
98
|
+
if source.type == target:
|
|
99
|
+
return True
|
|
100
|
+
else: return False
|
|
101
|
+
elif type(source) == target:
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
@classmethod
|
|
105
|
+
def getTypes(self, object, keys, _type=tuple):
|
|
106
|
+
if _type == tuple:
|
|
107
|
+
ret = []
|
|
108
|
+
else: ret = {}
|
|
109
|
+
# get the data
|
|
110
|
+
for i in keys:
|
|
111
|
+
if _type == tuple:
|
|
112
|
+
ret.append(object.__annotations__[i])
|
|
113
|
+
else: # dict
|
|
114
|
+
ret[ i ] = object.__annotations__[i]
|
|
115
|
+
# return data
|
|
116
|
+
if _type == tuple:
|
|
117
|
+
return tuple(ret)
|
|
118
|
+
return ret
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
Decorator to handle checking an algorithms inputs and validating that types
|
|
122
|
+
match. The only requirement other than structure def matching is that the type
|
|
123
|
+
associated with the elements match target type (both python and charm types).
|
|
124
|
+
"""
|
|
125
|
+
class Input:
|
|
126
|
+
def __init__(self, *_types):
|
|
127
|
+
self._types = _types
|
|
128
|
+
#print("INPUT TYPE: Defined types: ", self._types)
|
|
129
|
+
|
|
130
|
+
def __call__(self, func, *args):
|
|
131
|
+
def check_input(*args):
|
|
132
|
+
result = None
|
|
133
|
+
try:
|
|
134
|
+
# check inputs
|
|
135
|
+
inputs = args[1:]
|
|
136
|
+
for i in range(0, len(self._types)):
|
|
137
|
+
_res_type = type(self._types[i])
|
|
138
|
+
if _res_type in [list, dict]: # make sure it's either a dict, list or tuple
|
|
139
|
+
assert SchemeBase.verifyTypeStruct(inputs[i], self._types[i], _res_type), "invalid '%s' type for '%s'" % (self._types[i], i)
|
|
140
|
+
else:
|
|
141
|
+
assert SchemeBase.verifyType(inputs[i], self._types[i]), "invalid '%s' type for '%s'" % (self._types[i], i)
|
|
142
|
+
result = func(*args)
|
|
143
|
+
except Exception as e:
|
|
144
|
+
print(e)
|
|
145
|
+
return result
|
|
146
|
+
|
|
147
|
+
return check_input
|
|
148
|
+
|
|
149
|
+
"""
|
|
150
|
+
Decorator to handle checking an algorithms outputs and validating that types
|
|
151
|
+
match. Similar to input, the only requirement other than structure def matching is that the type
|
|
152
|
+
associated with the elements match target type (both python and charm types).
|
|
153
|
+
"""
|
|
154
|
+
class Output:
|
|
155
|
+
def __init__(self, *_types):
|
|
156
|
+
self._types = _types
|
|
157
|
+
self._type_len = len(_types)
|
|
158
|
+
self.check_first = True
|
|
159
|
+
if self._type_len > 1: self.check_first = False
|
|
160
|
+
#print("OUTPUT TYPE: ", self._types)
|
|
161
|
+
|
|
162
|
+
def __call__(self, func, *args):
|
|
163
|
+
def check_output(*args):
|
|
164
|
+
# we do not mask error raised by the function not related to types
|
|
165
|
+
output = func(*args)
|
|
166
|
+
try:
|
|
167
|
+
# check the output
|
|
168
|
+
if self.check_first:
|
|
169
|
+
# situation where only one type is defined and it could be a single dict or list of many types,
|
|
170
|
+
# or a single object with one type
|
|
171
|
+
_res_type = type(self._types[0])
|
|
172
|
+
if _res_type in [list, dict]:
|
|
173
|
+
assert SchemeBase.verifyTypeStruct(output, self._types[0], _res_type), "invalid return type"
|
|
174
|
+
else:
|
|
175
|
+
assert SchemeBase.verifyType(output, self._types[0]), "invalid return output for '%s'" % func.__name__
|
|
176
|
+
else:
|
|
177
|
+
# situation where a list of types is defined and mirrors how we look at inputs
|
|
178
|
+
for i in range(0, self._type_len):
|
|
179
|
+
if type(self._types[i]) == dict:
|
|
180
|
+
assert SchemeBase.verifyTypeStruct(output[i], self._types[i]), "invalid return type"
|
|
181
|
+
elif type(self._types[i]) == tuple:
|
|
182
|
+
assert SchemeBase.verifyTypeStruct(output[i], self._types[i], list)
|
|
183
|
+
else:
|
|
184
|
+
assert SchemeBase.verifyType(output[i], self._types[i]), "invalid return type"
|
|
185
|
+
except Exception as e:
|
|
186
|
+
print(e)
|
|
187
|
+
return output
|
|
188
|
+
return check_output
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Implementing the proof of concept secret sharing
|
|
2
|
+
from charm.toolbox.pairinggroup import PairingGroup,ZR,order
|
|
3
|
+
|
|
4
|
+
class SecretShare:
|
|
5
|
+
def __init__(self, element, verbose_status=True):
|
|
6
|
+
self.elem = element
|
|
7
|
+
self.verbose = verbose_status
|
|
8
|
+
|
|
9
|
+
def P(self, coeff, x):
|
|
10
|
+
share = 0
|
|
11
|
+
# evaluate polynomial
|
|
12
|
+
for i in range(0, len(coeff)):
|
|
13
|
+
share += (coeff[i] * (x ** i))
|
|
14
|
+
return share
|
|
15
|
+
|
|
16
|
+
def genShares(self, secret, k=0, n=0, q=None, x_points=None):
|
|
17
|
+
if(k <= n):
|
|
18
|
+
if q == None:
|
|
19
|
+
q = [self.elem.random(ZR) for i in range(0, k)]
|
|
20
|
+
q[0] = secret
|
|
21
|
+
|
|
22
|
+
if x_points == None: # just go from 0 to n
|
|
23
|
+
shares = [self.P(q, i) for i in range(0, n+1)] # evaluating poly. q at i for all i
|
|
24
|
+
else:
|
|
25
|
+
shares = {}
|
|
26
|
+
for i in range(len(x_points)):
|
|
27
|
+
shares[i] = (x_points[i], self.P(q, x_points[i]))
|
|
28
|
+
# = [self.P(q, i) for i in x_points] # x_points should be a list
|
|
29
|
+
|
|
30
|
+
# debug
|
|
31
|
+
if self.verbose:
|
|
32
|
+
print('Secret: %s' % secret)
|
|
33
|
+
for i in range(1, k):
|
|
34
|
+
print("a %s: %s" % (i, q[i]))
|
|
35
|
+
print('')
|
|
36
|
+
if x_points == None:
|
|
37
|
+
for i in range(1,n+1):
|
|
38
|
+
print('Share %s: %s' % (i, shares[i]))
|
|
39
|
+
else:
|
|
40
|
+
for i in range(len(x_points)):
|
|
41
|
+
print('Share %s: %s' % (i, shares[i]))
|
|
42
|
+
|
|
43
|
+
return shares
|
|
44
|
+
|
|
45
|
+
# shares is a dictionary
|
|
46
|
+
def recoverCoefficients(self, list):
|
|
47
|
+
coeff = {}
|
|
48
|
+
for i in list:
|
|
49
|
+
result = 1
|
|
50
|
+
for j in list:
|
|
51
|
+
if not (i == j):
|
|
52
|
+
# lagrange basis poly
|
|
53
|
+
result *= (0 - j) / (i - j)
|
|
54
|
+
if self.verbose: print("coeff '%d' => '%s'" % (i, result))
|
|
55
|
+
coeff[i] = result
|
|
56
|
+
return coeff
|
|
57
|
+
|
|
58
|
+
# shares is a dictionary
|
|
59
|
+
def recoverCoefficientsDict(self, dict):
|
|
60
|
+
coeff = {}
|
|
61
|
+
for i in dict.values():
|
|
62
|
+
result = 1
|
|
63
|
+
for j in dict.values():
|
|
64
|
+
if not (i == j):
|
|
65
|
+
# lagrange basis poly
|
|
66
|
+
result *= (0 - j) / (i - j)
|
|
67
|
+
if self.verbose: print("coeff '%d' => '%s'" % (i, result))
|
|
68
|
+
coeff[i] = result
|
|
69
|
+
return coeff
|
|
70
|
+
|
|
71
|
+
def recoverSecret(self, shares):
|
|
72
|
+
list = shares.keys()
|
|
73
|
+
if self.verbose: print(list)
|
|
74
|
+
coeff = self.recoverCoefficients(list)
|
|
75
|
+
if self.verbose: print("coefficients: ", coeff)
|
|
76
|
+
secret = 0
|
|
77
|
+
for i in list:
|
|
78
|
+
secret += (coeff[i] * shares[i])
|
|
79
|
+
|
|
80
|
+
return secret
|
|
81
|
+
|
|
82
|
+
if __name__ == "__main__":
|
|
83
|
+
|
|
84
|
+
# Testing Secret sharing python API
|
|
85
|
+
k = 3
|
|
86
|
+
n = 4
|
|
87
|
+
group = PairingGroup('SS512')
|
|
88
|
+
|
|
89
|
+
s = SecretShare(group, True)
|
|
90
|
+
sec = group.random(ZR)
|
|
91
|
+
shares = s.genShares(sec, k, n)
|
|
92
|
+
|
|
93
|
+
K = shares[0]
|
|
94
|
+
print('\nOriginal secret: %s' % K)
|
|
95
|
+
y = {group.init(ZR, 1):shares[1], group.init(ZR, 2):shares[2], group.init(ZR, 3):shares[3]}
|
|
96
|
+
|
|
97
|
+
secret = s.recoverSecret(y)
|
|
98
|
+
|
|
99
|
+
if(K == secret):
|
|
100
|
+
print('\nSuccessfully recovered secret: %s' % secret)
|
|
101
|
+
else:
|
|
102
|
+
print('\nCould not recover the secret!')
|
|
103
|
+
|
|
104
|
+
|