Skip to main content

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
'''

Metadata

Release files for x25519 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for x25519 0.0.2
File Size Uploaded
x25519-0.0.2.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for x25519 0.0.2
File Interpreter ABI Platform
x25519-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / x25519-0.0.2.tar.gz

Download URL x25519-0.0.2.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ed91d0aba7f4f4959ed8b37118c11d94f56d36c38bb6f2e6c20d0438d75b1556
BLAKE2b-256 checksum
How to use checksums
c7b6fca895aff0800cdf941f856df0685a5513094163664b904576e3e3ef1460
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.1

Release files / x25519-0.0.2-py3-none-any.whl

Download URL x25519-0.0.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c0833260a548bea9137a5a1b5c30334b751a59d148a62832df0c9e7b919ce99
BLAKE2b-256 checksum
How to use checksums
f2d166c637eb8e7a9601675bf7f04bb9a3015358a0f49e4c31d29a2b9a9d72d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page