Coder Social home page Coder Social logo

hareinweed / gec Goto Github PK

View Code? Open in Web Editor NEW
15.0 15.0 3.0 300 KB

elliptic curve cryptography with GPU acceleration

License: MIT License

CMake 1.20% C++ 87.12% Cuda 11.68%
bigint cuda discrete-logarithm ecc elliptic-curve-cryptography elliptic-curves finite-fields

gec's People

Contributors

hareinweed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gec's Issues

windows 10 VS 2019 ,New Project on gec setup

I am currently using windows 10.
Microsoft Visual Studio Community 2019 Version 16.11.22

I wanted to create a clean "gec project". It asked me to install Catch2 and Cmake. but I could not create a project correctly. I wanted to create a main.cpp project by simply taking the "gec" folder inside the "include" folder and use the gpu, but the IDE gave a lot of errors. Cuda toolkit Cmake Catch is defined but how can I activate this library with an empty project in Cpp.

Nonsence output

Doing everything as in README.md:

using namespace gec::bigint::literal;

// Elliptic curves need to be defined before any ECC operations can be carried out. Take secp256k1 as an example, define the finite field Field for secp256k1 first.

// use uint64 x 4 to store a single element on finite field
using Bigint256 = gec::bigint::ArrayBE<uint64_t, 4>;

// define parameters required by montgomery multiplication:
// cardinality of finite field
GEC_DEF_GLOBAL(MOD, Bigint256, 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_int);
constexpr Bigint256::LimbT MOD_P = 0xd838091dd2253531ull; // -MOD^-1 mod 2^64
GEC_DEF_GLOBAL(RR, Bigint256, 0x01000007a2000e90a1_int); // 2^512 mod MOD
GEC_DEF_GLOBAL(ONE_R, Bigint256, 0x1000003d1_int); // 2^256 mod MOD

// define the finite field type
using Field = GEC_BASE_FIELD(Bigint256, MOD, MOD_P, RR, ONE_R);

// Then define Scalar as the scalar of secp256k1.

// define parameters required by montgomery multiplication:
// cardinality of the elliptic curve
GEC_DEF_GLOBAL(CARD, Bigint256, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141_int);
constexpr Bigint256::LimbT CARD_P = 0x4b0dff665588b13full; // -CARD^-1 mod 2^64
GEC_DEF_GLOBAL(CARD_RR, Bigint256, 0x9d671cd581c69bc5e697f5e45bcd07c6741496c20e7cf878896cf21467d7d140_int); // 2^512 mod CARD
GEC_DEF_GLOBAL(CARD_ONE_R, Bigint256, 0x14551231950b75fc4402da1732fc9bebf_int);// 2^256 mod CARD

// define the scalar type
using Scalar = GEC_BASE_FIELD(Bigint256, CARD, CARD_P, CARD_RR, CARD_ONE_R);

// Finally, define Secp256k1 as curve secp256k1.

// parameters of the elliptic curve, in montgomery form
GEC_DEF_GLOBAL(A, Field, 0); // = A * 2^256 mod MOD
GEC_DEF_GLOBAL(B, Field, 0x700001ab7_int); // = B * 2^256 mod MOD

// define the curve with Jacobian coordinate
using Secp256k1_ = GEC_CURVE(gec::curve::JacobianCurve, Field, A, B);
// use the specialized implementation for curves whose A = 0 to boost performance
using Secp256k1 = GEC_CURVE_B(gec::curve::JacobianCurve, Field, B);

// define the generator, in montgomery form
GEC_DEF_GLOBAL(GEN, Secp256k1, (
    Field(0x9981e643e9089f48979f48c033fd129c231e295329bc66dbd7362e5a487e2097_int),
    Field(0xcf3f851fd4a582d670b6b59aac19c1368dfc5d5d1f1dc64db15ea6d2d3dbabe2_int),
    Field(0x1000003d1_int)
));

__global__ void kernel_create_eth_public_keys(eth_private_key * private_keys, eth_public_key * public_keys, uint32_t count) {
    uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= count)
        return;

    // Load the private key
    Scalar priv_key;
    memcpy(priv_key.arr, private_keys[idx]._limbs, sizeof(eth_private_key));

    // Perform elliptic curve scalar multiplication
    Secp256k1 pub_key;
    Secp256k1::mul(pub_key, priv_key, d_GEN);

    // Store the public key
    memcpy(public_keys[idx].x._limbs, pub_key.x().arr, sizeof(eth_public_key) / 2);
    memcpy(public_keys[idx].y._limbs, pub_key.y().arr, sizeof(eth_public_key) / 2);
    
    // Debug
    printf("private_key: { ");
    for (int i = 0; i < sizeof(priv_key.arr) / sizeof(*priv_key.arr); ++i) {
        printf("%016lx, ", priv_key.arr[i]);
    }
    printf("}\n");

    printf("public_key: { x: { ");
    for (int i = 0; i < sizeof(pub_key.x().arr) / sizeof(*pub_key.x().arr); ++i) {
        printf("%016lx, ", pub_key.x().arr[i]);
    }
    printf("}, y: { ");
    for (int i = 0; i < sizeof(pub_key.y().arr) / sizeof(*pub_key.y().arr); ++i) {
        printf("%016lx, ", pub_key.y().arr[i]);
    }
    printf("}, z: { ");
    for (int i = 0; i < sizeof(pub_key.z().arr) / sizeof(*pub_key.z().arr); ++i) {
        printf("%016lx, ", pub_key.z().arr[i]);
    }
    printf("} }\n");
}

And I'm getting the following:

private_key: { 703e556dbd7f03b6, 283067fd845af0c5, 7666f33f1245241f, 5450d66d3a46d453, }
public_key: { x: { 0000000900002259, 0000000000000000, 0000000000000000, 0000000000000000, }, y: { ffffffe3ffff9524, ffffffffffffffff, ffffffffffffffff, ffffffffffffffff, }, z: { 0000000000000000, 0000000000000000, 0000000000000000, 0000000000000000, } }

For this private key, it should be

public_key: { x: { 3d67a201808ae629, dfe044a756acbf82, 9bda7c80a653d4d4, 3c07eb284e1e6696 }, y: { 83cdb5a6704dcb70, e3c05e25f5d45805, 666eda0d9e7d5b83, caf2306259be9167 } z: { 1021a492b0450fe2, 3d74d41de68a50d6, 8189b2b7571ef9d9, 1dd2ce02dccec4e1 } }

What am I doing wrong?

multiplication issue

I configured a test file as mentioned in the read me. when multiplied with 2 or 8 etc the result is. may be the point doubling is having issue,

{0x0000000000000000 0000000000000000 0000000000000000 0000000000000000,
0x0000000000000000 0000000000000000 0000000000000000 0000000000000000,
0x0000000000000000 0000000000000000 0000000000000000 0000000000000000}

In addition i am really trying to understand few parts of parameters eg.

const Secp256k1 GEN(
Field(0x9981e643e9089f48979f48c033fd129c231e295329bc66dbd7362e5a487e2097_int),
Field(0xcf3f851fd4a582d670b6b59aac19c1368dfc5d5d1f1dc64db15ea6d2d3dbabe2_int),
Field(0x1000003d1_int),
and
const Field B(0x700001ab7_int); // = B * 2^256 mod MOD

not sure if its a different way of defining the parameters and documentation around this would be helpful for the users.

Strange output

when I ran this code:

int main() {
  
    Secp256k1 p1;
    std::cout << p1 << std::endl;      

    Secp256k1::mul(p1, 3, GEN);     

    std::cout << p1 << std::endl; 
    }

this is the output that I see:


{0x0000000000000000 0000000000000000 0000000000000000 0000000000000000,
 0x0000000000000000 0000000000000000 0000000000000000 0000000000000000,
 0x0000000000000000 0000000000000000 0000000000000000 0000000000000000}

{0x019fa59f6f459fc6 748fa0a875006844 fc39bed026e15b27 69cd0e0931000a12,
 0xf03f524e8729a2d6 70f5f5be0a33eedc 2fc8d898b67b2802 b68ef68395abd131,
 0xc2c26ed3e5be9201 db856e0c5e96b76d 5d182c134369ed8e cd3f6a303370697b}

whereas the expectation is to see this:

_PointJacobi__coords = {
43150999984920195402721557287970751909215378357684930847008742850115047381891,
59926971897449277329076473160900726794672445021794582518265033533443990978073,
24812014379610181492537499413510995242366288494600126160509686236072294602767}

what am I missing?

Ubuntu setup and dependencies for gpu cuda

Hello, can you share information about the requirements and installation for Ubuntu? I want to do a scalar operation on the GPU. for example, multiplying the number 978 by the point G.

Sorry, maths is good, but if you guide me for software, you will bring one more software madman. I wish you good work.
thank you

GPU Computation Issue

Hey.. I have a very interesting project for which this project's functionality suits perfectly. Is there any way I can DM you so that we can discuss it in detail? Mail exchanges? or Twitter? I dont see any external link for contact in your github profile page.. My twitter id is @Nikhil76347259 , mail id is [email protected].

Compilation Error

#include <gec/bigint.hpp>
#include <gec/utils/macros.hpp>
#include <gec/bigint/preset.hpp>
#include <gec/curve/preset.hpp>
using namespace std;
using namespace gec::bigint::literal; // use the bigint literal
using Bigint256 = bigint::ArrayBE<uint64_t, 4>;

Compilation result:
error: ‘bigint’ does not name a type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.