{"id":20367179,"url":"https://github.com/rubycrypto/x25519","last_synced_at":"2026-03-09T15:31:20.790Z","repository":{"id":48233500,"uuid":"113616124","full_name":"RubyCrypto/x25519","owner":"RubyCrypto","description":"Public key cryptography library for Ruby providing the X25519 Diffie-Hellman function","archived":false,"fork":false,"pushed_at":"2025-01-21T19:50:58.000Z","size":225,"stargazers_count":48,"open_issues_count":2,"forks_count":11,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T08:05:05.033Z","etag":null,"topics":["cryptography","curve25519","diffie-hellman","elliptic-curves","x25519"],"latest_commit_sha":null,"homepage":"https://cr.yp.to/ecdh.html","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RubyCrypto.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-08T20:49:37.000Z","updated_at":"2025-02-03T08:20:28.000Z","dependencies_parsed_at":"2023-12-02T02:08:48.944Z","dependency_job_id":"143ac53a-27f0-4c02-beab-83e8aad3b9aa","html_url":"https://github.com/RubyCrypto/x25519","commit_stats":{"total_commits":78,"total_committers":4,"mean_commits":19.5,"dds":"0.15384615384615385","last_synced_commit":"9d8acb860e5b4c2a353eda5a31e24a02f385e661"},"previous_names":["crypto-rb/x25519"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyCrypto%2Fx25519","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyCrypto%2Fx25519/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyCrypto%2Fx25519/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyCrypto%2Fx25519/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RubyCrypto","download_url":"https://codeload.github.com/RubyCrypto/x25519/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247149500,"owners_count":20891954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","curve25519","diffie-hellman","elliptic-curves","x25519"],"created_at":"2024-11-15T00:29:30.923Z","updated_at":"2026-03-09T15:31:20.736Z","avatar_url":"https://github.com/RubyCrypto.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# x25519.rb [![Latest Version][gem-shield]][gem-link] [![Yard Docs][docs-image]][docs-link] [![License: BSD 3-Clause][license-image]][license-link] [![Build Status][build-image]][build-link]\n\n[gem-shield]: https://img.shields.io/gem/v/x25519?logo=ruby\n[gem-link]: https://rubygems.org/gems/x25519\n[docs-image]: https://img.shields.io/badge/yard-docs-blue.svg\n[docs-link]: http://www.rubydoc.info/gems/x25519/1.0.6\n[license-image]: https://img.shields.io/badge/License-BSD%203--Clause-blue.svg\n[license-link]: https://spdx.org/licenses/BSD-3-Clause.html\n[build-image]: https://github.com/RubyCrypto/x25519/actions/workflows/ci.yml/badge.svg\n[build-link]: https://github.com/RubyCrypto/x25519/actions/workflows/ci.yml\n\nAn efficient public key cryptography library for Ruby providing key\nexchange/agreement.\n\nThis gem implements X25519 (a.k.a. Curve25519) Elliptic Curve Diffie-Hellman\nfunction as described in [RFC7748] as a C extension using the\nhigh performance [rfc7748_precomputed] implementation based on the paper\n[How to (pre-)compute a ladder]\n(with fallback to the ref10 C implementation).\n\nX25519 is one of two notable algorithms implemented atop the Curve25519\nelliptic curve. The [ed25519 gem] is a related project of this one,\nand implements the Ed25519 signature scheme on the twisted Edwards form of\nCurve25519.\n\n[RFC7748]: https://tools.ietf.org/html/rfc7748\n[How to (pre-)compute a ladder]: https://eprint.iacr.org/2017/264\n[rfc7748_precomputed]: https://github.com/armfazh/rfc7748_precomputed\n[ed25519 gem]: https://github.com/RubyCrypto/ed25519\n\n### Is it any good?\n\n[Yes.](http://news.ycombinator.com/item?id=3067434)\n\n### What is it useful for?\n\nX25519 is a key exchange/agreement algorithm generally used as a low-level\nbuilding block in cryptographic protocols.\n\n### Can I use X25519 to encrypt things?\n\nPlease use [RbNaCl::Box] if you would like a high-level construction which uses\nX25519 for public-key encryption. Otherwise, the X25519 algorithm is not directly\nuseful for encryption without a higher-level encryption protocol built on top of it.\n\n[RbNaCl::Box]: https://github.com/RubyCrypto/rbnacl/wiki/Public-Key-Encryption\n\n## Requirements\n\n**x25519.rb** is supported on and tested against the following platforms:\n\n- MRI 2.7, 3.0, 3.1, 3.2, 3.3, 3.4\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"x25519\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install x25519\n\n## Usage\n\nThe example below shows how to perform a full Diffie-Hellman key exchange:\n\n```ruby\nrequire \"x25519\"\n\n# Alice generates random scalar (private key)\nalice_sk = X25519::Scalar.generate\n\n# Alice obtains public key for her private key/scalar\nalice_pk = alice_sk.public_key\n\n# Bob generates random scalar (private key)\n# Ostensibly this would be on a different computer somewhere\nbob_sk = X25519::Scalar.generate\nbob_pk = bob_sk.public_key\n\n# Alice can perform Diffie-Hellman with Bob's public key\nalice_secret = alice_sk.diffie_hellman(bob_pk).to_bytes\n\n# Bob can perform Diffie-Hellman with Alice's public key\nbob_secret = bob_sk.diffie_hellman(alice_pk).to_bytes\n\n# The resulting secrets should be the same\nalice_secret == bob_secret # true\n```\n\n## X25519::Scalar: private keys\n\nThe `X25519::Scalar` class represents secret integers used as X25519 private\nkeys. These secret integers are multiplied by a well-known base point to\nobtain X25519 public keys (`X25519::MontgomeryU`).\n\n### `X25519::Scalar.generate()`: make a random private key\n\nGenerate a random private scalar (using `SecureRandom`)\n\n**Example:**\n\n```ruby\nsecret_key = X25519::Scalar.generate\n```\n\n### `X25519::Scalar.new(bytes)`: load existing private key\n\n* `bytes`: a 32-byte `String` value containing the private key\n\n**Example:**\n\n```ruby\nsecret_key = X25519::Scalar.new(File.read(\"alice.key\"))\n```\n\n### `X25519::Scalar#public_key()`: obtain public key for this scalar\n\nNOTE: The `#multiply_base` method is an alias of this one.\n\nPerforms fixed-base scalar multiplication (i.e. calculates public key)\n\n**Return Value:**\n\nReturns a `X25519::MontgomeryU` object which represents the public key for this private key/scalar.\n\n**Example:**\n\n```ruby\nsecret_key = X25519::Scalar.generate\npublic_key = secret_key.public_key\n```\n\n### `X25519::Scalar#diffie_hellman(other_public_key)`: obtain public key for this scalar\n\nNOTE: The `#multiply` method is an alias of this one.\n\nPerforms variable-base scalar multiplication, computing a shared secret between\nour private scalar and someone else's public key/point.\n\n**Arguments:**\n\n* `other_public_key`: a `X25519::MontgomeryU` object containing the public key\n  with which we'd like to compute a shared secret.\n\n**Return Value:**\n\nReturns a `X25519::MontgomeryU` object which represents the shared secret.\n\n**Example:**\n\n```ruby\nsecret_key = X25519::Scalar.generate\npublic_key = X25519::MontgomeryU.new(File.read(\"bob.pub\"))\n\n# Returns an X25519::MontgomeryU\nshared_secret = secret_key.multiply(public_key)\n\n# Obtain the shared secret as a serialized byte representation\nshared_secret_bytes = shared_secret.to_bytes\n```\n\n### `X25519::Scalar#to_bytes`: serialize a scalar as a `String`\n\n**Return Value:**\n\nReturns a `String` containing a byte representation of this scalar:\n\n**Example:**\n\n```ruby\nsecret_key = X25519::Scalar.new(...)\nFile.write(\"alice.key\", secret_key.to_bytes)\n```\n\n## X25519::MontgomeryU: public keys and shared secrets\n\nThe `X25519::MontgomeryU` class represents a coordinate (specifically a\nMontgomery-u coordinate) on the elliptic curve. In the X25519 Diffie-Hellman\nfunction, these serve both as public keys and as shared secrets.\n\n### `X25519::MontgomeryU.new(bytes)`: load existing public key\n\n**Arguments:**\n\n* `bytes`: a 32-byte `String` value containing the public key\n\n**Example:**\n\n```ruby\npublic_key = X25519::MontgomeryU.new(File.read(\"bob.pub\"))\n```\n\n### `X25519::MontgomeryU#to_bytes`: serialize a Montgomery-u coordinate as a `String`\n\n**Return Value:**\n\nReturns a `String` containing a byte representation of a compressed Montgomery-u coordinate:\n\n**Example:**\n\n```ruby\npublic_key = X25519::MontgomeryU..new(...)\nFile.write(\"bob.pub\", public_key.to_bytes)\n```\n\n## X25519: module-level functionality\n\n### `X25519.diffie_hellman(secret_key, public_key)`: shorthand `String`-oriented API\n\nIf you'd like to avoid the object-oriented API, you can use a simplified API which\nacts entirely on bytestrings.\n\n**Arguments:**\n\n* `secret_key`: a 32-byte `String` containing a private scalar\n* `public_key`: a 32-byte `String` containing a compressed Montgomery-u coordinate\n\n**Return Value:**\n\nReturns a `String` containing a 32-byte compressed Montgomery-u coordinate\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/RubyCrypto/x25519.\nThis project is intended to be a safe, welcoming space for collaboration,\nand contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org)\ncode of conduct.\n\n## Implementation Details\n\nThis gem contains two implementations of X25519: an optimized assembly\nimplementation and a portable C implementation. Implementations are selected\nbased on available CPU features.\n\n### [rfc7748_precomputed]: optimized assembly implementation\n\n* Prime field arithmetic is optimized for the 4th and 6th generation of Intel Core processors\n  (Haswell and Skylake micro-architectures).\n* Efficient integer multiplication using MULX instruction.\n* Integer additions accelerated with ADCX/ADOX instructions.\n* Key generation uses a read-only table of 8 KB for X25519.\n\n### ref10: portable C implementation\n\n* Taken from the [SUPERCOP] cryptographic benchmarking suite (supercop-20171020)\n* Portable C code which should compile on any architecture\n\n[SUPERCOP]: https://bench.cr.yp.to/supercop.html\n\n## Designers\n\nThe X25519 Diffie-Hellman function was originally designed by Dan Bernstein:\n\nhttps://cr.yp.to/ecdh.html\n\nThe optimized [rfc7748_precomputed] implementation was designed by:\n\n* Thomaz Oliveira, Computer Science Department, Cinvestav-IPN, Mexico.\n* Julio López, University of Campinas, Brazil.\n* Hüseyin Hisil, Yasar University, Turkey.\n* Armando Faz-Hernández, University of Campinas, Brazil.\n* Francisco Rodríguez-Henríquez, Computer Science Department, Cinvestav-IPN, Mexico.\n\n## License\n\n* Copyright (c) 2017-2018 Armando Faz\n* Copyright (c) 2017-2026 Tony Arcieri\n\nThis gem is available as open source under the terms of the\nBSD-3 Clause License ([LICENSE](./LICENSE))\n\n## Code of Conduct\n\nEveryone interacting in the x25519.rb project’s codebases, issue trackers, chat\nrooms and mailing lists is expected to follow the [code of conduct].\n\n[code of conduct]: https://github.com/RubyCrypto/x25519/blob/main/CODE_OF_CONDUCT.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubycrypto%2Fx25519","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubycrypto%2Fx25519","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubycrypto%2Fx25519/lists"}