0

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")
colidyre
  • 4,170
  • 12
  • 37
  • 53
GraphicalDot
  • 2,644
  • 2
  • 28
  • 43
  • I may be wrong, but I'm pretty sure that Diffie-Hellman doesn't take any sort of input. – Aran-Fey Sep 25 '18 at 19:31
  • Elliptic-curve Diffie–Hellman (ECDH) is an anonymous key agreement protocol that allows two parties, each having an elliptic-curve public–private key pair, to establish a shared secret over an insecure channel.[1][2][3] This shared secret may be directly used as a key, or to derive another key. The key, or the derived key, can then be used to encrypt subsequent communications using a symmetric-key cipher. It is a variant of the Diffie–Hellman protocol using elliptic-curve cryptography. – GraphicalDot Sep 25 '18 at 20:16
  • Umm, so what's the question? Is it "How do I compile secp256k1"? – Aran-Fey Sep 25 '18 at 20:20
  • I just need a librabry which can help me generate dh key based on the keys i have generated from 24 word mnemonic, The only library i have found is secp256k1 a python wrapper over libsecp256k1, but there is no documentation whats so ever on how to compile python wrapper with libsecp256k1, so that dh actually works. – GraphicalDot Sep 25 '18 at 20:21
  • Well, asking for library recommendations is off-topic. And if you need help making secp256k1 work, you should explain what isn't working. – Aran-Fey Sep 25 '18 at 20:24

1 Answers1

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)
GraphicalDot
  • 2,644
  • 2
  • 28
  • 43
  • 1
    Only problem is that shared secret delivered by `coincurve` is not just x-coordinate of the point on the curve but `sha256(x)` so it could occur that shared secret will not match with value on the other side if different library is used. – Marek Dec 10 '20 at 14:50