This is a Solidity library for generating and verifying Schnorr signatures. It provides functions for generating key pairs, signing messages with private keys, and verifying signatures with public keys. The library is compatible with the secp256k1 curve used in Bitcoin.
- Solidity compiler version ^0.8.0
- EllipticCurve library (provided)
- Hashing library (provided)
- Copy the library files into your Solidity project directory.
- In your contract or script that uses the library, import the
Schnorr.sol
file usingimport "./Schnorr.sol";
.
To generate a key pair, call the Schnorr.generateKeyPair
function and pass a private key as a parameter:
SchnorrKeyPair.KeyPair memory keyPair = Schnorr.generateKeyPair(123456789);
The function returns a SchnorrKeyPair.KeyPair
struct that contains the public and private keys.
To sign a message with a private key, call the Schnorr.sign
function and pass the message and key pair as parameters:
bytes32 message = bytes32(uint256(123));
Schnorr.Signature memory signature = Schnorr.sign(message, keyPair);
The function returns a Schnorr.Signature
struct that contains the signature.
To verify a signature with a public key, call the Schnorr.verify
function and pass the message, signature, and public key as parameters:
bool isSignatureValid = Schnorr.verify(message, signature, keyPair.publicPoint);
The function returns a boolean value that indicates whether the signature is valid.
The test/TestSchnorr.sol
file contains a set of unit tests that cover the key pair generation, signature generation, and signature verification functionality of the library. The tests can be run using the Truffle framework.
- Install the Truffle framework by running
npm install -g truffle
. - Navigate to the
test
directory in the project and runtruffle test
.
Limited curve support: The library currently only supports the secp256k1 curve, which may not be suitable for all use cases. Users who require a different curve may need to look elsewhere or modify the code.
Possible timing attacks: The current implementation of the library uses scalar multiplication in the signature verification process, which may be vulnerable to timing attacks. To mitigate this, users may need to implement additional measures such as blinding or using a constant-time multiplication algorithm.
Limited testing: While I have provided some unit tests for the library, there may be other use cases and edge cases that have not been tested thoroughly. Users should exercise caution and perform their own testing to ensure that the library functions as expected in their specific use case.
Lack of audit: The library has not undergone a formal security audit by a third-party, which may increase the risk of vulnerabilities or weaknesses. Users should be aware of this and take appropriate precautions to mitigate potential risks.
Smart contract considerations: While the library is designed to be used in Solidity smart contracts, it may not be suitable for all contract architectures or deployment environments. Users should take into account the limitations and requirements of their specific smart contract when using this library.
This project is licensed under the MIT License.
Please note that while this library has been carefully designed and tested, it is not guaranteed to be free from errors or vulnerabilities. Use at your own risk, and always review the code carefully before using it in a production environment.