Skip to content

Commit

Permalink
src/native: expose native schnorr backend from libtorsion.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 28, 2020
1 parent 730d45f commit 2a0f350
Show file tree
Hide file tree
Showing 12 changed files with 1,046 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TODO
| rsa | c (libtorsion²) | c (libtorsion²) | c (libtorsion) | js |
| rsaies | c (libtorsion¹) | c (libtorsion¹) | c (libtorsion¹) | js |
| salsa20 | c (libtorsion¹) | c (libtorsion¹) | c (libtorsion¹) | js |
| schnorr | c (libtorsion) | c (libtorsion) | c (libtorsion) | js |
| scrypt | c (libtorsion) | c (libtorsion) | c (libtorsion) | js |
| secp256k1 | c (libsecp256k1⁴) | c (libsecp256k1⁴) | c (libsecp256k1⁴) | js |
| sha1 | c (libtorsion) | c (libtorsion) | c (libtorsion) | js |
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"./src/rsa.cc",
"./src/rsa_async.cc",
"./src/salsa20.cc",
"./src/schnorr.cc",
"./src/scrypt.cc",
"./src/scrypt_async.cc",
"./src/siphash.cc",
Expand Down
1 change: 1 addition & 0 deletions lib/bcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ exports.rsa = require('./rsa');
exports.rsaies = require('./rsaies');
exports.safe = require('./safe');
exports.Salsa20 = require('./salsa20');
exports.schnorr = require('./schnorr');
exports.scrypt = require('./scrypt');
exports.secp256k1 = require('./secp256k1');
exports.secretbox = require('./secretbox');
Expand Down
69 changes: 69 additions & 0 deletions lib/native/schnorr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*!
* schnorr.js - schnorr wrapper for libtorsion
* Copyright (c) 2018-2019, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*/

'use strict';

const assert = require('../internal/assert');
const binding = require('./binding');

/**
* Schnorr
*/

class Schnorr extends binding.Schnorr {
constructor(name) {
super(binding.curves[name]);

this.id = name;
this.type = 'schnorr';
this.size = this._size();
this.bits = this._bits();
this.native = 2;

this._randomize(binding.entropy());
}

privateKeyGenerate() {
return super.privateKeyGenerate(binding.entropy());
}

privateKeyExport(key) {
const pub = this.publicKeyCreate(key);
const [x, y] = super.publicKeyExport(pub);
const d = super.privateKeyExport(key);

return { d, x, y };
}

privateKeyImport(json) {
assert(json && typeof json === 'object');
return super.privateKeyImport(json.d);
}

publicKeyExport(key) {
const [x, y] = super.publicKeyExport(key);
return { x, y };
}

publicKeyImport(json) {
assert(json && typeof json === 'object');
return super.publicKeyImport(json.x);
}

publicKeyToUniform(key, hint = binding.hint()) {
return super.publicKeyToUniform(key, hint);
}

publicKeyToHash(key) {
return super.publicKeyToHash(key, binding.entropy());
}
}

/*
* Expose
*/

module.exports = new Schnorr('SECP256K1');
9 changes: 9 additions & 0 deletions lib/schnorr-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* schnorr.js - schnorr for bcrypto
* Copyright (c) 2020, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*/

'use strict';

module.exports = require('./js/schnorr');
12 changes: 12 additions & 0 deletions lib/schnorr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*!
* schnorr.js - schnorr for bcrypto
* Copyright (c) 2020, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*/

'use strict';

if (process.env.NODE_BACKEND === 'js')
module.exports = require('./js/schnorr');
else
module.exports = require('./native/schnorr');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"./lib/ripemd160": "./lib/ripemd160-browser.js",
"./lib/rsa": "./lib/rsa-browser.js",
"./lib/salsa20": "./lib/salsa20-browser.js",
"./lib/schnorr": "./lib/schnorr-browser.js",
"./lib/scrypt": "./lib/scrypt-browser.js",
"./lib/secp256k1": "./lib/secp256k1-browser.js",
"./lib/sha1": "./lib/sha1-browser.js",
Expand Down
2 changes: 2 additions & 0 deletions src/bcrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "pbkdf2.h"
#include "rsa.h"
#include "salsa20.h"
#include "schnorr.h"
#include "scrypt.h"
#ifdef BCRYPTO_USE_SECP256K1
#include "secp256k1.h"
Expand Down Expand Up @@ -53,6 +54,7 @@ NAN_MODULE_INIT(init) {
BPBKDF2::Init(target);
BRSA::Init(target);
BSalsa20::Init(target);
BSchnorr::Init(target);
BScrypt::Init(target);
#ifdef BCRYPTO_USE_SECP256K1
BSecp256k1::Init(target);
Expand Down
Loading

0 comments on commit 2a0f350

Please sign in to comment.