create account

How Ethereum Classic Nodes Create Shared Secrets by cseberino

View this thread on: hive.blogpeakd.comecency.com
· @cseberino · (edited)
How Ethereum Classic Nodes Create Shared Secrets
![]([Imgur](https://i.imgur.com/TxX75UM.jpg))

Amazingly, two strangers communicating over an insecure channel can create shared secrets only they know. One way is using the Elliptic Curve Diffie Hellman algorithm. Ethereum Classic (ETC) network nodes use this algorithm to create shared secrets for encryption and authentication.

# Basics

![](https://i.imgur.com/aZcmLK3.jpg)

ETC nodes are distinguished by Elliptic Curve Digital Signature Algorithm (ECDSA) public keys. These public keys are derived from random numbers, referred to as private keys, by an odd type of multiplication involving number pairs.

If node A wants to establish a shared secret with node B, node A generates new private and public keys.  The new public key is sent to node B. The shared secret will be the result of multiplying the new private key and the public key of node B.  Node B can also determine the shared secret by multiplying its private key and the received public key.  Each calculation will produce the *same* results! A shared secret is then securely established despite the communication channel potentially being insecure.

As an analogy, consider Alice and Bob trying to establish a shared secret paint color.  Both mix a random color with yellow paint and share the resulting mixture. Each then combines their received mixture with their random color:

![](https://i.imgur.com/9XCPeM3.png)

Alice and Bob both end up with the same color! Furthermore, assuming it is infeasible to isolate the contents, analyzing the mixtures does not reveal the final color.  The random colors correspond to private keys. The mixtures correspond to public keys. The final color corresponds to the shared secret.

# Implementation

![](https://i.imgur.com/McDetpD.jpg)

Here is Python code ETC nodes could use to establish a shared secret.


```
#!/usr/bin/env python3

import random

N  = 115792089237316195423570985008687907852837564279074904382605163141518161494337
P  = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424

def invert(number, modulus):
        """
        Finds the inverses of natural numbers.
        """

        result = 1
        power  = number
        for e in bin(modulus - 2)[2:][::-1]:
                if int(e):
                        result = (result * power) % modulus
                power = (power ** 2) % modulus

        return result

def add(pair_1, pair_2):
        """
        Finds the sums of two pairs of natural numbers.
        """

        if   pair_1 == [0, 0]:
                result = pair_2
        elif pair_2 == [0, 0]:
                result = pair_1
        else:
                if pair_1 == pair_2:
                        temp = 3 * pair_1[0] ** 2
                        temp = (temp * invert(2 * pair_1[1], P)) % P
                else:
                        temp = pair_2[1] - pair_1[1]
                        temp = (temp * invert(pair_2[0] - pair_1[0], P)) % P
                result = (temp ** 2 - pair_1[0]  - pair_2[0]) % P
                result = [result, (temp * (pair_1[0] - result) - pair_1[1]) % P]

        return result

def multiply(number, pair):
        """
        Finds the products of natural numbers and pairs of natural numbers.
        """

        result = [0, 0]
        power  = pair[:]
        for e in bin(number)[2:][::-1]:
                if int(e):
                        result = add(result, power)
                power = add(power, power)

        return result

def make_private_key():
        """
        Creates random private keys.
        """

        return random.randint(1, N)

def get_public_key(private_key):
        """
        Calculates public keys from private keys.
        """

        return multiply(private_key, (Gx, Gy))

def get_shared_secret(private_key, public_key):
        """
        Calculates shared secrets from public and private keys.
        """

        return multiply(private_key, public_key)
```

# Example

![](https://i.imgur.com/NGo6sVQ.jpg)

Here is a sample session in a Python shell:

```
>>> private_key_1 = make_private_key()

>>> private_key_1
97213711440252288069501705296405261088977963748356197430111018664708007898694

>>> public_key_1 = get_public_key(private_key_1)

>>> public_key_1
[55347035383335640130848668571903826856724561960411750372299562338900707959329, 56987405272845225816494709602265870995367253030969066517972976881897442374061]

>>> private_key_2 = make_private_key()

>>> private_key_2
114442312191143111200801983145915249881179214150404268170023204631602354988735

>>> public_key_2  = get_public_key(private_key_2)

>>> public_key_2
[46199435999571260839088012456481977088200767000880651780506032156594168787887, 42363090186990936702404058277398340643564495339378028219129719226661858237092]

>>> get_shared_secret(private_key_1, public_key_2)
[44866827294122005264365746646122394461069726466915377589895810140980695289009, 62851244191510099945744865372108107629218872326463084894219243426735245634939]

>>> get_shared_secret(private_key_2, public_key_1)
[44866827294122005264365746646122394461069726466915377589895810140980695289009, 62851244191510099945744865372108107629218872326463084894219243426735245634939]
```
<br>Note although both shared secret calculations use different inputs, they produce the *same* result!

# Feedback

Feel free to leave any comments or questions below. You can also contact me by email at cs@etcplanet.org or by clicking any of these icons:

[![](https://cdn-images-1.medium.com/max/1600/0*eoFC6QOWZ--bCngK.png)](https://twitter.com/chris_seberino)

[![](https://cdn-images-1.medium.com/max/1600/0*i3CwTFEKUnKYHMf0.png)](https://www.facebook.com/cseberino)

[![](https://cdn-images-1.medium.com/max/1600/0*HQj6HSHxE7pkIBjk.png)](https://www.linkedin.com/in/christian-seberino-776897110/)

# Acknowledgements

I would like to thank IOHK (Input Output Hong Kong) for funding this effort.

# License

[![](https://cdn-images-1.medium.com/max/1600/0*hocpUZXBcjzNJeQ2.png)](https://creativecommons.org/licenses/by-sa/4.0/)

This work is licensed under the Creative Commons Attribution ShareAlike 4.0 International License.
👍  , ,
properties (23)
authorcseberino
permlinkhow-ethereum-classic-nodes-create-shared-secrets
categoryethereum
json_metadata{"tags":["ethereum","ethereumclassic","eth","etc","blockchain"],"image":["[Imgur](https://i.imgur.com/TxX75UM.jpg)","https://i.imgur.com/aZcmLK3.jpg","https://i.imgur.com/9XCPeM3.png","https://i.imgur.com/McDetpD.jpg","https://i.imgur.com/NGo6sVQ.jpg","https://cdn-images-1.medium.com/max/1600/0*eoFC6QOWZ--bCngK.png","https://cdn-images-1.medium.com/max/1600/0*i3CwTFEKUnKYHMf0.png","https://cdn-images-1.medium.com/max/1600/0*HQj6HSHxE7pkIBjk.png","https://cdn-images-1.medium.com/max/1600/0*hocpUZXBcjzNJeQ2.png"],"app":"steemit/0.1","format":"markdown","links":["https://twitter.com/chris_seberino","https://www.facebook.com/cseberino","https://www.linkedin.com/in/christian-seberino-776897110/","https://creativecommons.org/licenses/by-sa/4.0/"]}
created2019-07-09 21:50:00
last_update2019-07-10 16:18:06
depth0
children0
last_payout2019-07-16 21:50:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,293
author_reputation5,161,857,859,658
root_title"How Ethereum Classic Nodes Create Shared Secrets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id88,083,377
net_rshares36,717,792,818
author_curate_reward""
vote details (3)