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

Breaking changes for the next major release

Open axic opened this issue 8 years ago • 34 comments

(Taking the discussion from #112 to here)

Proposed changes:

  • [x] Make .modn() return a BN (#112)
  • [x] Renaming .strip() to ._strip() is a breaking change too (#105)
  • [x] Maybe reviewing the constructor method(s) could lead to such a change (see #90 & #91)
  • [ ] Rename .andln() to .andn() (see the README). Perhaps fold the functionality into toArrayLike and make .andn() safe in terms of working up to 53 bits
  • [ ] Maybe at that stage two's complement could be made more internal (part of constructor and toString) (see #73)
  • [ ] Split out the extended functions (saving destroyed bits) off iusrhn() into a specific shift right version, because it is only used in MPrime.split() and is complex.
  • [ ] Remove internal functions from the BN context (good candidates are: _countBits, _zeroBits, etc. Others could be moved too with passing a self variable instead of using this, however that might have a speed penalty or increase?)
  • [ ] Perhaps rethink naming/functionality of the following bitwise methods: setn (should be renamed isetn as it is in-place), testn, maskn, bincn

axic avatar Feb 13 '16 14:02 axic

I also propose to create a v5.x branch to merge these changes in.

axic avatar Feb 13 '16 14:02 axic

Perhaps also the following could be discussed as part of this:

  • #79
  • #107

axic avatar Feb 14 '16 17:02 axic

Any plans regarding https://github.com/indutny/bn.js/issues/35 as part of next major release ?

fanatid avatar Feb 23 '16 05:02 fanatid

@indutny just would like to know your opinion about Buffer (node/browser) instead Array here

fanatid avatar Oct 30 '16 19:10 fanatid

@fanatid for what?

indutny avatar Oct 30 '16 20:10 indutny

out of curiosity... as I understand you used Array instead Buffer for zero dependencies, why not Uint8Array for example? if there were no question about dependencies, would you use Buffer instead Array?

fanatid avatar Oct 30 '16 20:10 fanatid

@fanatid both Buffer and Uint8Array were slower for the purposes of bn.js . I've experimented with it several times, and at each try it was worse.

indutny avatar Oct 30 '16 20:10 indutny

@fanatid additionally, allocation is a huge problem.

indutny avatar Oct 30 '16 20:10 indutny

oh, okay, so the main reason is performance? How long ago are you tested? Array allocation work better? Maybe I'm wrong, but as I remember, Buffer allocate more then requested? (or pre-allocated buffers works in such way, at least in node)

fanatid avatar Oct 30 '16 20:10 fanatid

I've tested it about 6 months ago, but feel free to give it another try.

Array allocation is fast as hell, while Buffer/Uint8Array generally takes more time to create and additional time to manage (GC). Pre-allocating buffers works, but it doesn't currently fit into the scheme of things, and considering that they were slower anyway - I didn't see a reason to change APIs.

indutny avatar Oct 30 '16 20:10 indutny

Thank you for explanation. Can you publish your tests for Array/Buffer/Uint8Array comparison?

fanatid avatar Oct 30 '16 20:10 fanatid

Something like this, I guess: https://gist.github.com/indutny/f37c466b4765e686b766b0b32557557c . Btw, just recalled that Buffers can't be used for it, only Uint32Array

indutny avatar Oct 31 '16 00:10 indutny

Updated gist to make results more fair. Should be started either as node bench.js u or just node bench.js.

indutny avatar Oct 31 '16 00:10 indutny

I think #90 #91 #141 #151 should be added to list

fanatid avatar Nov 07 '16 20:11 fanatid

Should we do it after January 1st? cc @fanatid @dcousens

indutny avatar Dec 27 '16 22:12 indutny

ACK, sounds good to me. I'll happily put the time in bumping some of the crypto-browserify packages up to the new changes.

dcousens avatar Dec 27 '16 23:12 dcousens

Maybe at that stage two's complement could be made more internal (part of constructor and toString) (see #73)

Did we want to elaborate more on what this change might entail? (Aka, summarise #73)

dcousens avatar Jan 03 '17 04:01 dcousens

@axic how do you use toTwos/fromTwos to parse a DER integer?

dcousens avatar May 23 '17 06:05 dcousens

@dcousens luckily I am not dealing with ASN.1. toTwos/fromTwos is used within https://github.com/ethereumjs.

axic avatar Aug 21 '17 22:08 axic

Didn't mean to close this. @dcousens what do you think about the rest of the list? Is it something that we want to address in this major bump?

indutny avatar Nov 29 '17 20:11 indutny

@indutny I'd pretty much like modn to return a BN but take a Number if possible

axic avatar Nov 29 '17 22:11 axic

@indutny if you don't intend to change modn to return a BN, I'd remove it. Changing it is additionally dangerous for existing users who blindly upgrade, so removing is the better option until maybe later some minor bump that re-adds it.

andln

s/andln/andrn/g? What was l?

Is this function intentionally public?

I think the rest of the list is good, but, I don't have the time to do it, and assuming you don't, I think the changes in the last 24 hours have been fantastic.

Non-strict hex has been a liability from the get-go for me.

dcousens avatar Nov 29 '17 22:11 dcousens

Yeah, I don't want to change it for exactly these reasons. Good call! Is the benefit of more sound API outweighs breaking changes? It would be better to deprecate it with a warning, but there is no code to do this yet.

Oh, perhaps l was for returning number... I guess we should rename it to modln then. Sincerely, I can't recall...

indutny avatar Nov 30 '17 02:11 indutny

I don't mind naming it modln instead of modrn, and mentioning it in the docs. Would you care to open a PR?

indutny avatar Nov 30 '17 02:11 indutny

I don't mind naming it modln instead of modrn, and mentioning it in the docs. Would you care to open a PR?

I suppose, but what did ln mean? rn is more intuitive from your explanation of return number.

dcousens avatar Nov 30 '17 03:11 dcousens

Additional breaking change request:

  • [ ] assert(str.length % 2 === 0) for base16/hex

dcousens avatar Nov 30 '17 03:11 dcousens

I have no idea. Should we do same renaming for andln => andrn then?

-1 for hex padding. This is going to be hell of a breaking change.

indutny avatar Nov 30 '17 03:11 indutny

@indutny yes, but, is andln useful? It only works on the first word?

-1 for hex padding. This is going to be hell of a breaking change.

I know... how about require('bn.js/strict') which enforces that? Unexpected padding errors have caused countless problems for libraries.

dcousens avatar Nov 30 '17 03:11 dcousens

I think it is useful.

Unexpected padding errors have caused countless problems for libraries.

Maybe we can make it throw for just hex, and accept without padding for base-16?

indutny avatar Dec 02 '17 20:12 indutny

Maybe we can make it throw for just hex, and accept without padding for base-16?

That sounds reasonable.

dcousens avatar Dec 02 '17 23:12 dcousens

Here was another potential thing: Rename toJSON, see https://github.com/indutny/bn.js/pull/164#issuecomment-303332320

axic avatar Jul 07 '19 16:07 axic

@axic how you think, what toJSON actually should return?

fanatid avatar Jul 07 '19 16:07 fanatid

Never mind, I've only realised now that toJSON is potentially used by JSON.stringify and it should have no other use beyond that. Perhaps documenting this would be enough. See this.

axic avatar Jul 07 '19 16:07 axic

Reviewing the release, it seems that #112 could have been more clear. Perhaps for the 6.x release then 😉

axic avatar Jul 07 '19 16:07 axic