Search code examples
phpprivate-keypublic-keyelliptic-curvegenerate

PHP Fatal error: Uncaught Error: Class 'Elliptic\EC' not found


I want to generate private/public keys using Elliptic Curve Cryptography in PHP.

I have used this library: https://github.com/simplito/elliptic-php

my code:

<?php
use Elliptic\EC;

// Create and initialize EC context
// (better do it once and reuse it)
$ec = new EC('secp256k1');

// Generate keys
$key = $ec->genKeyPair();

$publicKey = $key->getPublic('hex');
$privateKey = $key->getPrivate('hex');

// Print the keys to the console

echo "The address1 is {$publicKey}. \r\n";
echo "The address1 is {$privateKey}. \r\n";

but it shows me this error:

PHP Fatal error:  Uncaught Error: Class 'Elliptic\EC' not found in /home/istabraq/bctest/test1/keygenerator.php:6

I have installed composer Composer 1.6.3 from this tutorial: https://linuxize.com/post/how-to-install-and-use-composer-on-ubuntu-18-04/

then installed sudo apt-get install php7.2-gmp and installed composer require simplito/elliptic-php and finally installed composer require simplito/bn-php but the last command line shows me this output:

Using version ^1.1 for simplito/bn-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

what I have missed please help? I search for problem but did not have tutorial.


Solution

  • try include file with class or if exists vendor/autoload.php include it, for example:

    <?php
    
    include 'path/vendor/autoload.php'; //or 'path/file/EC.php'
    
    use Elliptic\EC;
    
    // Create and initialize EC context
    // (better do it once and reuse it)
    $ec = new EC('secp256k1');
    
    // Generate keys
    $key = $ec->genKeyPair();
    
    $publicKey = $key->getPublic('hex');
    $privateKey = $key->getPrivate('hex');
    
    // Print the keys to the console
    
    echo "The address1 is {$publicKey}. \r\n";
    echo "The address1 is {$privateKey}. \r\n";