bn.js icon indicating copy to clipboard operation
bn.js copied to clipboard

bn: introduce in-place pow, .ipow() [WIP]

Open axic opened this issue 8 years ago • 3 comments

This changes pow() to be in-place and adds the usual wrapper.

Also added a test case for the special 0 power case.

axic avatar Feb 04 '16 02:02 axic

Benchmarks? Last time I checked it - in-place multiplication was a bit slower than regular one. This is why I haven't used it in elliptic.

indutny avatar Feb 04 '16 04:02 indutny

Perhaps the benchmarks aren't the best. The code is:

var x = new bn(13);

add('pow', {
  'bn.js pow': function (fixture) {
    fixture.a1.clone().pow(x);
  },
  'bn.js ipow': function (fixture) {
    fixture.a1.clone().ipow(x);
  },

The results are:

new:
bn.js#pow x 7,371 ops/sec ±1.31% (9 runs sampled)
bn.js ipow#pow x 7,072 ops/sec ±4.80% (7 runs sampled)
bn.js#pow x 6,836 ops/sec ±7.52% (7 runs sampled)
bn.js ipow#pow x 6,955 ops/sec ±4.15% (7 runs sampled)
bn.js#pow x 7,504 ops/sec ±3.62% (9 runs sampled)
bn.js ipow#pow x 7,451 ops/sec ±2.85% (9 runs sampled)

old:
bn.js#pow x 7,475 ops/sec ±3.68% (9 runs sampled)
bn.js#pow x 6,838 ops/sec ±5.94% (9 runs sampled)
bn.js#pow x 6,669 ops/sec ±4.90% (9 runs sampled)
bn.js#pow x 7,335 ops/sec ±3.60% (9 runs sampled)

I do see a bigger difference between mul & imul on its own. That should be documented though if it cannot be improved. One would assume the in-place versions are faster.

axic avatar Feb 04 '16 11:02 axic

Yeah, I was even thinking about removing them at all. What's the point of having in-place functions if there are not faster? They are definitely not easier, and not safer to use.

indutny avatar Feb 04 '16 20:02 indutny