-
Notifications
You must be signed in to change notification settings - Fork 82
Implement Point#mul #307
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
Merged
Merged
Implement Point#mul #307
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa1db88
Arity-split Point#initialize
headius 9c100aa
Implement Point#mul
headius dd59f74
Add Point#mul
headius 23ff684
Refactor and arity-split Point#mul
headius 3ea09d2
Updates to pom
headius 50fb847
Get generator point from Group's paramSpec
headius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement Point#mul
- Loading branch information
commit 9c100aae0c91a2b01a856c456e34e36ab0a3bea5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,10 +58,14 @@ | |
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec; | ||
import org.bouncycastle.jce.spec.ECNamedCurveSpec; | ||
|
||
import org.bouncycastle.math.ec.ECAlgorithms; | ||
import org.bouncycastle.math.ec.ECCurve; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyArray; | ||
import org.jruby.RubyBignum; | ||
import org.jruby.RubyBoolean; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyFixnum; | ||
import org.jruby.RubyModule; | ||
import org.jruby.RubyObject; | ||
import org.jruby.RubyString; | ||
|
@@ -972,6 +976,7 @@ private boolean getPointAndGroup(ThreadContext context, IRubyObject groupOrPoint | |
|
||
if ( groupOrPoint instanceof Group) { | ||
this.group = (Group) groupOrPoint; | ||
this.point = (ECPoint) ((Group) groupOrPoint).generator(context); | ||
} else { | ||
throw runtime.newTypeError(groupOrPoint, _EC(runtime).getClass("Group")); | ||
} | ||
|
@@ -1068,6 +1073,76 @@ public IRubyObject inspect() { | |
return ObjectSupport.inspect(this, (List) Collections.singletonList(entry)); | ||
} | ||
|
||
@JRubyMethod(name = "mul", required = 1, optional = 2) | ||
public IRubyObject mul(final ThreadContext context, final IRubyObject[] args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is at least one other math operation on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I'll do add in another PR! |
||
Ruby runtime = context.runtime; | ||
|
||
org.bouncycastle.math.ec.ECPoint pointSelf, pointResult; | ||
|
||
Group groupV = this.group; | ||
|
||
Point result; | ||
|
||
BigInteger bn_g = null; | ||
|
||
ECCurve selfCurve = EC5Util.convertCurve(group.getCurve()); | ||
pointSelf = EC5Util.convertPoint(selfCurve, asECPoint()); | ||
|
||
result = new Point(runtime, getMetaClass()); | ||
result.initialize(context, groupV); | ||
ECCurve resultCurve = EC5Util.convertCurve(result.group.getCurve()); | ||
pointResult = EC5Util.convertPoint(resultCurve, result.point); | ||
|
||
int argc = Arity.checkArgumentCount(runtime, args, 1, 3); | ||
IRubyObject arg1 = null, arg2 = null; | ||
switch (argc) { | ||
case 2: | ||
arg2 = args[1]; | ||
case 1: | ||
arg1 = args[0]; | ||
} | ||
if (!(arg1 instanceof RubyArray)) { | ||
BigInteger bn; | ||
if (arg1 instanceof RubyFixnum) { | ||
bn = BigInteger.valueOf(arg1.convertToInteger().getLongValue()); | ||
} else if (arg1 instanceof RubyBignum) { | ||
bn = ((RubyBignum) arg1).getValue(); | ||
} else if (arg1 instanceof BN) { | ||
bn = ((BN) arg1).getValue(); | ||
} else { | ||
throw runtime.newTypeError(arg1, runtime.getInteger()); | ||
} | ||
|
||
if (arg2 != null) { | ||
if (arg2 instanceof RubyFixnum) { | ||
bn_g = BigInteger.valueOf(arg2.convertToInteger().getLongValue()); | ||
} else if (arg2 instanceof RubyBignum) { | ||
bn_g = ((RubyBignum) arg2).getValue(); | ||
} else if (arg2 instanceof BN) { | ||
bn_g = ((BN) arg2).getValue(); | ||
} else { | ||
throw runtime.newTypeError(arg2, runtime.getInteger()); | ||
} | ||
} | ||
|
||
if (bn_g == null) { | ||
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.referenceMultiply(pointSelf, bn); | ||
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group); | ||
} else { | ||
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.sumOfTwoMultiplies(pointResult, bn_g, pointSelf, bn); | ||
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group); | ||
} | ||
|
||
if (result == null) { | ||
newECError(runtime, "bad multiply result"); | ||
} | ||
} else { | ||
throw runtime.newNotImplementedError("calling #mul with arrays is not supported by this OpenSSL version"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Deprecated | ||
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) { | ||
final int argc = Arity.checkArgumentCount(context.runtime, args, 1, 2); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.