Content-Length: 354371 | pFad | http://github.com/jruby/jruby-openssl/pull/307/commits/9c100aae0c91a2b01a856c456e34e36ab0a3bea5

0E Implement Point#mul by headius · Pull Request #307 · jruby/jruby-openssl · GitHub
Skip to content

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 6 commits into from
Jun 17, 2024
Merged
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
Prev Previous commit
Next Next commit
Implement Point#mul
  • Loading branch information
headius committed Jun 11, 2024
commit 9c100aae0c91a2b01a856c456e34e36ab0a3bea5
75 changes: 75 additions & 0 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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) {

Choose a reason for hiding this comment

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

I think there is at least one other math operation on ECPoint that this change now makes it easy to support: add

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jruby/jruby-openssl/pull/307/commits/9c100aae0c91a2b01a856c456e34e36ab0a3bea5

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy