Skip to main content

A pure Python implemention of curve25519

Project description

x25519 (curve25519)

A pure Python implemention of curve25519 like Go package x/crypto/curve25519:

package main

import (
	"fmt"
	"bytes"
	"golang.org/x/crypto/curve25519"
)

func main() {
	var publicKey [32]byte
	privateKey := (*[32]byte)(bytes.Repeat([]byte("1"), 32))
	curve25519.ScalarBaseMult(&publicKey, privateKey)
	fmt.Printf("%x\n", publicKey)
	
	var sharedSecret [32]byte
	peerPublicKey := (*[32]byte)(bytes.Repeat([]byte("2"), 32))
	curve25519.ScalarMult(&sharedSecret, privateKey, peerPublicKey)
	fmt.Printf("%x\n", sharedSecret)
}

/* output:
04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a
a6d830c3561f210fc006c77768369af0f5b3e3e502e74bd3e80991d7cb7bfa50
*/

Usage of this package:

# this code supports both python 2 and 3
from binascii import hexlify
import x25519

private_key = b'1' * 32
public_key = x25519.scalar_base_mult(private_key)
print(hexlify(public_key))

peer_public_key = b'2' * 32
shared_secret = x25519.scalar_mult(private_key, peer_public_key)
print(hexlify(shared_secret))

'''output:
b'04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a'
b'a6d830c3561f210fc006c77768369af0f5b3e3e502e74bd3e80991d7cb7bfa50'
'''

Generally, other C implementions are much faster:

import pysodium

private_key = b'1' * 32
public_key = pysodium.crypto_scalarmult_curve25519_base(private_key)
print(public_key.hex())

peer_public_key = b'2' * 32
shared_secret = pysodium.crypto_scalarmult_curve25519(private_key, peer_public_key)
print(shared_secret.hex())

'''output:
04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a
a6d830c3561f210fc006c77768369af0f5b3e3e502e74bd3e80991d7cb7bfa50
'''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x25519-0.0.2.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

x25519-0.0.2-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page