Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use latest rustbn.js API #312

Merged
merged 1 commit into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/precompiled/06-ecadd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ const BN = utils.BN
const error = require('../exceptions.js').ERROR
const assert = require('assert')

const bn128Module = require('rustbn.js')
const ecAddPrecompile = bn128Module.cwrap('ec_add', 'string', ['string'])
const bn128 = require('rustbn.js')

module.exports = function (opts) {
assert(opts.data)

let results = {}
let data = opts.data
let inputHexStr = data.toString('hex')
let inputData = opts.data

results.gasUsed = new BN(opts._common.param('gasPrices', 'ecAdd'))
if (opts.gasLimit.lt(results.gasUsed)) {
Expand All @@ -22,16 +20,16 @@ module.exports = function (opts) {
return results
}

let returnData = ecAddPrecompile(inputHexStr)
let returnData = bn128.add(inputData)

// check ecadd success or failure by comparing the output length
if (returnData.length !== 128) {
if (returnData.length !== 64) {
results.return = Buffer.alloc(0)
results.exception = 0
results.gasUsed = new BN(opts.gasLimit)
results.exceptionError = error.OUT_OF_GAS
} else {
results.return = Buffer.from(returnData, 'hex')
results.return = returnData
results.exception = 1
}

Expand Down
12 changes: 5 additions & 7 deletions lib/precompiled/07-ecmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ const ERROR = require('../exceptions.js').ERROR
const BN = utils.BN
const assert = require('assert')

const bn128Module = require('rustbn.js')
const ecMulPrecompile = bn128Module.cwrap('ec_mul', 'string', ['string'])
const bn128 = require('rustbn.js')

module.exports = function (opts) {
assert(opts.data)

let results = {}
let data = opts.data
let inputData = opts.data

let inputHexStr = data.toString('hex')
results.gasUsed = new BN(opts._common.param('gasPrices', 'ecMul'))

if (opts.gasLimit.lt(results.gasUsed)) {
Expand All @@ -23,14 +21,14 @@ module.exports = function (opts) {
return results
}

let returnData = ecMulPrecompile(inputHexStr)
let returnData = bn128.mul(inputData)

// check ecmul success or failure by comparing the output length
if (returnData.length !== 128) {
if (returnData.length !== 64) {
results.return = Buffer.alloc(0)
results.exception = 0
} else {
results.return = Buffer.from(returnData, 'hex')
results.return = returnData
results.exception = 1
}

Expand Down
14 changes: 6 additions & 8 deletions lib/precompiled/08-ecpairing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ const BN = utils.BN
const error = require('../exceptions.js').ERROR
const assert = require('assert')

const bn128Module = require('rustbn.js')
const ecPairingPrecompile = bn128Module.cwrap('ec_pairing', 'string', ['string'])
const bn128 = require('rustbn.js')

module.exports = function (opts) {
assert(opts.data)

let results = {}
let data = opts.data
let inputData = opts.data

let inputHexStr = data.toString('hex')
let inputData = Buffer.from(inputHexStr, 'hex')
// no need to care about non-divisible-by-192, because bn128.pairing will properly fail in that case
let inputDataSize = Math.floor(inputData.length / 192)

const gascost = opts._common.param('gasPrices', 'ecPairing') + (inputDataSize * opts._common.param('gasPrices', 'ecPairingWord'))
Expand All @@ -26,16 +24,16 @@ module.exports = function (opts) {
return results
}

let returnData = ecPairingPrecompile(inputHexStr)
let returnData = bn128.pairing(inputData)

// check ecpairing success or failure by comparing the output length
if (returnData.length !== 64) {
if (returnData.length !== 32) {
results.return = Buffer.alloc(0)
results.gasUsed = opts.gasLimit
results.exceptionError = error.OUT_OF_GAS
results.exception = 0
} else {
results.return = Buffer.from(returnData, 'hex')
results.return = returnData
results.exception = 1
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"fake-merkle-patricia-tree": "^1.0.1",
"functional-red-black-tree": "^1.0.1",
"merkle-patricia-tree": "^2.1.2",
"rustbn.js": "~0.1.1",
"rustbn.js": "~0.2.0",
"safe-buffer": "^5.1.1"
},
"devDependencies": {
Expand Down