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

bn: fix Red#imod #178

Merged
merged 3 commits into from
Jun 6, 2018
Merged

bn: fix Red#imod #178

merged 3 commits into from
Jun 6, 2018

Conversation

fanatid
Copy link
Collaborator

@fanatid fanatid commented Feb 16, 2018

Issue #177

lib/bn.js Outdated
return a.umod(this.m)._forceRed(this);

var mod = a.umod(this.m)._forceRed(this);
if (mod !== a) mod.copy(a);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be not so good performance-wise. Any ideas?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we introduce iumod instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that this can introduce performance issues... I thought about new method .move instead .copy.

Should we introduce iumod instead?

do you mean BN#iumod?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.move might actually work great here!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want it as separate method or only as few lines in Red#imod? What about BN#iumod? It's doesn't cost it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanatid do you mean new method for .move? If so, I think yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BN#move added in fa40b8c

lib/bn.js Outdated
return a.umod(this.m)._forceRed(this);

var mod = a.umod(this.m)._forceRed(this);
if (mod !== a) mod.move(a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it even worth checking if they are different? (performance diff?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In one case mod === a. Not sure does it cost check it or not...

bn.js/lib/bn.js

Line 2413 in 98ac84e

mod: this

Copy link
Contributor

@dcousens dcousens Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, if mod === a, no need to move, but, as move guarantees that a == mod, why bother verifying?
It looks like an optimization, but, IMHO serves to confuse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to a.umod(this.m)._forceRed(this).move(a);

Copy link
Contributor

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -262,4 +262,13 @@ describe('BN.js/Reduction context', function () {
red.prime.split(input, output);
assert.equal(input.cmp(output), 0);
});

it('imod should change host object', function () {
Copy link
Contributor

@dcousens dcousens Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe indicate (via redIMul)

@dcousens dcousens added the bug label Mar 9, 2018
Copy link
Owner

@indutny indutny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

lib/bn.js Outdated
@@ -327,6 +327,13 @@
dest.red = this.red;
};

BN.prototype.move = function move (dest) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, shouldn't it be private? It looks like this could lead to funny state issues if used as a part of API.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll not mind of making it private. Do you want see it as separate function or as "inline" function?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it makes sense to have it on a prototype. Just prefix it with _. @dcousens what do you think?

Copy link
Contributor

@dcousens dcousens Mar 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@indutny I think the public interface semantics of move can be assumed that the originating object is reset or garbage after use.
I think it is fine to have on the public interface; however, I don't mind either way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanatid prefix it with _?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcousens added prefix _, let me know if I should squash commits

Copy link
Contributor

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants