def get_offer_from_blockchain(self, offer_custon_id): # TODO # NOW IT IS BUMP f = open('./blockchain', 'rb') data = pickle.load(f) ciphertext = data[0] capsule = pre.Capsule.from_bytes(data[1], UmbralParameters(Curve(714))) #f.close() return ciphertext, capsule
def test_supported_curves(): # Ensure we have the correct number opf supported curves hardcoded number_of_supported_curves = 3 assert len(Curve._supported_curves) == number_of_supported_curves # Manually ensure the `_supported curves` dict contains only valid supported curves assert Curve._supported_curves[415] == 'secp256r1' assert Curve._supported_curves[714] == 'secp256k1' assert Curve._supported_curves[715] == 'secp384r1' nid, name = 714, 'secp256k1' # # Create by NID # # supported _curve_714 = Curve(nid=nid) assert _curve_714.curve_nid == nid assert _curve_714.name == name # unsuported with pytest.raises(NotImplementedError): _ = Curve(711) # # Create by Name # # Supported _curve_secp256k1 = Curve.from_name(name) assert _curve_secp256k1.name == name assert _curve_secp256k1.curve_nid == nid # Unsupported with pytest.raises(NotImplementedError): _ = Curve.from_name('abcd123e4') # Import curve constants from umbral.curve import SECP256R1, SECP256K1, SECP384R1 test_p256 = SECP256R1 test_secp256k1 = SECP256K1 test_p384 = SECP384R1 # Test the hardcoded curve NIDs are correct: assert test_p256.curve_nid == 415 assert test_secp256k1.curve_nid == 714 assert test_p384.curve_nid == 715 # Ensure every curve constant is in the CURVES collection from umbral.curve import CURVES assert len(CURVES) == number_of_supported_curves # Ensure all supported curves can be initialized for nid, name in Curve._supported_curves.items(): _curve_nid, _curve_name = Curve(nid=nid), Curve.from_name(name) assert _curve_nid.name == name assert _curve_name.curve_nid == nid
def Capsule(self, request, context): curve = rpc_api.pre.UmbralParameters(Curve(714)) capsule = rpc_api.pre.Capsule.from_bytes(bytes.fromhex(request.capsule), curve) flags = request.flags cpk = keys.UmbralPublicKey.from_bytes(bytes.fromhex(request.cpk)) rpk = keys.UmbralPublicKey.from_bytes(bytes.fromhex(request.rpk)) ppk = keys.UmbralPublicKey.from_bytes(bytes.fromhex(request.ppk)) kFrags = list() for flag in flags: kFrags.append(pre.KFrag.from_bytes(bytes.fromhex(flag))) text = rpc_api.UmbralApi.capsule_attach(capsule, kFrags, cpk, rpk, ppk) return rpc_pb2.CapsuleReply(text=text.hex())
def Decrypt(self, request, context): sk = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(request.sk)) encrypt_text = bytes.fromhex(request.text) cpk = keys.UmbralPublicKey.from_bytes(bytes.fromhex(request.cpk)) ppk = keys.UmbralPublicKey.from_bytes(bytes.fromhex(request.ppk)) capsule = rpc_api.pre.Capsule.from_bytes(bytes.fromhex(request.capsule), rpc_api.pre.UmbralParameters(Curve(714))) flags = request.flags kFrags = list() for flag in flags: kFrags.append(pre.KFrag.from_bytes(bytes.fromhex(flag))) text = rpc_api.UmbralApi.decrypt_by_sk(sk, cpk, ppk, encrypt_text, capsule, kFrags) return rpc_pb2.DecryptReply(text=text)
def decrypt(): api = ipfsapi.connect('127.0.0.1', 5001) res = {} cfrags = list() if request.headers['Content-Type'] == 'application/json': account = request.json['account'] ciphertexthex = request.json['ciphertext'] b_ciphertext = bytes.fromhex(ciphertexthex) decryptkey = request.json['decryptkey'] b_decryptkey = bytes.fromhex(decryptkey) deckey = UmbralPrivateKey.from_bytes(b_decryptkey) capsuleaddr = request.json['capsule'] b_capsule_all = api.cat(capsuleaddr) splitarr1 = b_capsule_all.split(b'ZAtech') b_basic_capsule = splitarr1[0] capsule = Capsule.from_bytes(b_basic_capsule, UmbralParameters(Curve(714))) print("0") correctness_keys = splitarr1[1] splitarr2 = correctness_keys.split(b'ZBtech') delegating = UmbralPublicKey.from_bytes(splitarr2[0]) receiving = UmbralPublicKey.from_bytes(splitarr2[1]) verifying = UmbralPublicKey.from_bytes(splitarr2[2]) # 用带入的参数capsule_all的各种byte,重现绑定correctness keys的capsule. capsule.set_correctness_keys(delegating=delegating, receiving=receiving, verifying=verifying) print("1") b_cfrag_all = splitarr1[2].split(b'ZCtech') for b_cfrag in b_cfrag_all: cfrags.append(CapsuleFrag.from_bytes(b_cfrag)) for cfrag in cfrags: capsule.attach_cfrag(cfrag) print("2") print(capsule) print(capsule.get_correctness_keys()) print(cfrags) cleartext = pre.decrypt(ciphertext=b_ciphertext, capsule=capsule, decrypting_key=deckey) print("3") res = {"cleartext": cleartext.decode("utf-8")} print("\nbob_cleartext: ") print(cleartext) return jsonify(res), {'Content-Type': 'application/json'} return
def test_curve_whitelist(): # Test the AVAIL_CURVES dict to have only these three curves: assert len(_AVAIL_CURVES) == 3 assert _AVAIL_CURVES['secp256r1'] == 415 assert _AVAIL_CURVES['secp256k1'] == 714 assert _AVAIL_CURVES['secp384r1'] == 715 # Test that we can't instantiate other curves: with pytest.raises(ValueError): Curve(711) # Test the hardcoded curves are what they're supposed to be: test_p256 = SECP256R1 test_secp256k1 = SECP256K1 test_p384 = SECP384R1 assert test_p256.curve_nid == 415 assert test_secp256k1.curve_nid == 714 assert test_p384.curve_nid == 715 # Test the supported curves property assert test_p256.supported_curves == _AVAIL_CURVES assert test_secp256k1.supported_curves == _AVAIL_CURVES assert test_p384.supported_curves == _AVAIL_CURVES
def fetch(): api = ipfsapi.connect('127.0.0.1', 5001) if request.headers['Content-Type'] == 'application/json': account = request.json['account'] # 所有的传入参数都是hex key capsuleaddr = request.json['capsule'] b_capsule_all = api.cat(capsuleaddr) splitarr1 = b_capsule_all.split(b'ZAtech') b_basic_capsule = splitarr1[0] capsule = Capsule.from_bytes(b_basic_capsule, UmbralParameters(Curve(714))) correctness_keys = splitarr1[1] splitarr2 = correctness_keys.split(b'ZBtech') delegating = UmbralPublicKey.from_bytes(splitarr2[0]) receiving = UmbralPublicKey.from_bytes(splitarr2[1]) verifying = UmbralPublicKey.from_bytes(splitarr2[2]) # print(splitarr1[0]) # print(splitarr1[1]) # print(splitarr2[0]) # print(splitarr2[1]) # print(splitarr2[2]) print(delegating) print(receiving) print(verifying) caddrs = request.json['addresses'] # 用带入的参数capsule_all的各种byte,重现绑定correctness keys的capsule. capsule.set_correctness_keys(delegating=delegating, receiving=receiving, verifying=verifying) print(capsule.get_correctness_keys()) cfrags = list() all_bytes = b'' index = 0 for addr in caddrs: index += 1 b_cfrag = api.cat(addr) all_bytes += b_cfrag if index < len(caddrs): all_bytes += b'ZCtech' cfrags.append(CapsuleFrag.from_bytes(api.cat(addr))) for cfrag in cfrags: capsule.attach_cfrag(cfrag) # 再将append的内容写入capsule,然后就可以将解密单独拎出来。 b_capsule_all += b'ZAtech' + all_bytes savedcapaddr = api.add_bytes(b_capsule_all) # splitarr = b_capsule_all.split(b'ZAtech') # splitarrmiddle = splitarr[1].split(b'ZBtech') # splitarrlast = splitarr[2].split(b'ZCtech') # print(len(cfrags)) # for s in splitarrmiddle: # print(s) # print(len(splitarrlast)) # for s in splitarrlast: # print(s) res = {"capsule": savedcapaddr} return jsonify(res), {'Content-Type': 'application/json'} return
def reencrypt(): api = ipfsapi.connect('127.0.0.1', 5001) addrs = list() caddrs = list() res = {} if request.headers['Content-Type'] == 'application/json': account = request.json['account'] # 所有的传入参数都是hex key threshold = request.json['threshold'] capsulehex = request.json['capsule'] b_capsule = api.cat(capsulehex) capsule = Capsule.from_bytes(b_capsule, UmbralParameters(Curve(714))) addrs = request.json['addresses'] delegatinghex = request.json['delegating'] b_delegating = bytes.fromhex(delegatinghex) delegating = UmbralPublicKey.from_bytes(b_delegating) receivinghex = request.json['receiving'] b_receiving = bytes.fromhex(receivinghex) receiving = UmbralPublicKey.from_bytes(b_receiving) verifyinghex = request.json['verifying'] b_verifying = bytes.fromhex(verifyinghex) verifying = UmbralPublicKey.from_bytes(b_verifying) if threshold > len(addrs): return "Not enough addresses." capsule.set_correctness_keys(delegating=delegating, receiving=receiving, verifying=verifying) cfrags = list() # Receiver's cfrag collection # each kfrag is a rk segment for addr in addrs: rkseg = KFrag.from_bytes(api.cat(addr)) # cfrag = pre.reencrypt(kfrag=kfrag, capsule=capsule) cfrag = pre.reencrypt(kfrag=rkseg, capsule=capsule) cfrags.append(cfrag) # Receiver's collects a cfrag for cfrag in cfrags: caddrs.append(api.add_bytes(cfrag.to_bytes())) savedcap = capsule.to_bytes_all() # savedcap包括三块,basic为capsule,correctness为set的key,cfrag是append上去的东西,此时还为空 print(type(savedcap)) print(type(savedcap['basic'])) print(savedcap['correctness']) delegating_key = savedcap['correctness']['delegating'] receiving_key = savedcap['correctness']['receiving'] verifying_key = savedcap['correctness']['verifying'] b_delegating_key = delegating_key.to_bytes() b_receiving_key = receiving_key.to_bytes() b_verifying_key = verifying_key.to_bytes() print(b_delegating_key) print(b_receiving_key) print(b_verifying_key) sendbytes = savedcap[ 'basic'] + b'ZAtech' + b_delegating_key + b'ZBtech' + b_receiving_key + b'ZBtech' + b_verifying_key print(sendbytes) savedcapaddr = api.add_bytes(sendbytes) res = {"caddrs": caddrs, "capsule": savedcapaddr} return jsonify(res), {'Content-Type': 'application/json'} return