Skip to content

Commit

Permalink
Use latest rustbn.js API
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Jul 20, 2018
1 parent f389fe7 commit 908aa9c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
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

0 comments on commit 908aa9c

Please sign in to comment.