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
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3169,7 +3169,10 @@

Red.prototype.imod = function imod (a) {
if (this.prime) return this.prime.ireduce(a)._forceRed(this);
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

return a;
};

Red.prototype.neg = function neg (a) {
Expand Down
9 changes: 9 additions & 0 deletions test/red-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

var red = BN.red(new BN(13));
var a = new BN(2).toRed(red);
var b = new BN(7).toRed(red);
var c = a.redIMul(b);
assert.equal(a.toNumber(), 1);
assert.equal(c.toNumber(), 1);
});
});