Diffie-Hellman encryption scheme in python

本秂侑毒 提交于 2019-12-25 01:03:35

问题


I have generated master pub/priv key pairs from 24 word mnemonics for Alice.

master_private_key='9f74f4534cbdf01a1f925e20108d86045bd96849af9c94534a10ef2a26ff133b',
master_public_key="0308de0952b00ebc83a41830794534ae912b86d3718832a36ce98c256ab5bfdc4e"

mnemonic='flash city relief spirit federal own metal history great hello toy volcano same subway loan bleak rapid swamp pigeon secret pyramid spoon famous blouse',

Similarly I have repeated the same process to generate keys for Bob. Now I want to generate Diffie-Hellman keys from Alice's private key and Bob's public key. All the implementations of Diffie-Hellman in python generates their own public/private key pair.

From secp256k1 python documentation:

ecdh(scalar) -> bytes
compute an EC Diffie-Hellman secret in constant time. The instance
public_key is used as the public point, and the scalar specified must be
composed of 32 bytes. It outputs 32 bytes representing the ECDH secret
computed. If the scalar is invalid, an Exception is raised.

NOTE: ecdh can only be used if the secp256k1 C library is compiled with support for it. If there is no support, an Exception will be raised when calling it.

I just don't know how to compile this library with libsecp256k1. I think Diffie-Hellman ECC version is more suitable here.

This is the error I am getting:

from sawtooth_signing.secp256k1 import Secp256k1PublicKey
bob_pub='033036dd96b7bef82556fe09eef42bef5e66545317c92a5deca99275f616729fef'
 public_key = Secp256k1PublicKey.from_hex(bob_pub)

 public_key.secp256k1_public_key.ecdh(alice_private_key)
 ----------------------
  Exception                                 Traceback (most recent call last)
 <ipython-input-4-5175cf593934> in <module>()
----> 1 public_key.secp256k1_public_key.ecdh("dsds")

~/SawtoothClient/SawtoothClient/lib/python3.6/site-packages/secp256k1-0.13.2-py3.6-linux-x86_64.egg/secp256k1/__init__.py in ecdh(self, scalar)
 305         assert self.public_key, "No public key defined"
306         if not HAS_ECDH:
--> 307             raise Exception("secp256k1_ecdh not enabled")
308         if not isinstance(scalar, bytes) or len(scalar) != 32:
309             raise TypeError('scalar must be composed of 32 bytes')

Exception: secp256k1_ecdh not enabled

I have tried to install https://github.com/bitcoin-core/secp256k1, followed the instructions and it got installed with output:

Libraries have been installed in:
/usr/local/lib

Now when I tried installing secp256k1 with python 3.6 again, as mentioned on their git page:

INCLUDE_DIR=include/ LIB_DIR=/usr/local/lib pip install --no-binary secp256k1,

I get this error:

ERROR: You must give at least one requirement to install (see "pip help install")

回答1:


I did it using another library coincurve.

import coincurve   
alice_priv = "29307c4354b7d9d311d2cec4878c0de56c93a921d300273c19577e9004de3c9f"

alice_pub = "02f3c25355c29f64ea8e9b4e11b583ac0a7d0d8235f156cffec2b73e5756aab206"

bob_pub = "03a1db8e8b047e1350958a55e0a853151d0e1f685fa5cf3772e01bccc5aa5cb2eb"

bob_priv = "4138d1b6dde34f81c38cef2630429e85847dd5b70508e37f53c844f66f19f983"

alice_coin_priv =  coincurve.PrivateKey.from_hex(alice_priv)

bob_coin_priv = coincurve.PrivateKey.from_hex(bob_priv)

binascii.hexlify(alice_coin_priv.ecdh(bob_coin_priv.public_key.public_key))

hex encoded shared secret is

b'92959cb394b71a05d440e0e2973bc9d0e7182eb86bb94d3a260ce8353c7a0317'

Verification works

bob_coin_priv.ecdh(alice_coin_priv.public_key.public_key)==  alice_coin_priv.ecdh(bob_coin_priv.public_key.public_key)


来源:https://stackoverflow.com/questions/52505432/diffie-hellman-encryption-scheme-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!