Skip to content

Commit 9c100aa

Browse files
committed
Implement Point#mul
1 parent aa1db88 commit 9c100aa

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/main/java/org/jruby/ext/openssl/PKeyEC.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@
5858
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
5959
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
6060

61+
import org.bouncycastle.math.ec.ECAlgorithms;
62+
import org.bouncycastle.math.ec.ECCurve;
6163
import org.jruby.Ruby;
6264
import org.jruby.RubyArray;
65+
import org.jruby.RubyBignum;
6366
import org.jruby.RubyBoolean;
6467
import org.jruby.RubyClass;
68+
import org.jruby.RubyFixnum;
6569
import org.jruby.RubyModule;
6670
import org.jruby.RubyObject;
6771
import org.jruby.RubyString;
@@ -972,6 +976,7 @@ private boolean getPointAndGroup(ThreadContext context, IRubyObject groupOrPoint
972976

973977
if ( groupOrPoint instanceof Group) {
974978
this.group = (Group) groupOrPoint;
979+
this.point = (ECPoint) ((Group) groupOrPoint).generator(context);
975980
} else {
976981
throw runtime.newTypeError(groupOrPoint, _EC(runtime).getClass("Group"));
977982
}
@@ -1068,6 +1073,76 @@ public IRubyObject inspect() {
10681073
return ObjectSupport.inspect(this, (List) Collections.singletonList(entry));
10691074
}
10701075

1076+
@JRubyMethod(name = "mul", required = 1, optional = 2)
1077+
public IRubyObject mul(final ThreadContext context, final IRubyObject[] args) {
1078+
Ruby runtime = context.runtime;
1079+
1080+
org.bouncycastle.math.ec.ECPoint pointSelf, pointResult;
1081+
1082+
Group groupV = this.group;
1083+
1084+
Point result;
1085+
1086+
BigInteger bn_g = null;
1087+
1088+
ECCurve selfCurve = EC5Util.convertCurve(group.getCurve());
1089+
pointSelf = EC5Util.convertPoint(selfCurve, asECPoint());
1090+
1091+
result = new Point(runtime, getMetaClass());
1092+
result.initialize(context, groupV);
1093+
ECCurve resultCurve = EC5Util.convertCurve(result.group.getCurve());
1094+
pointResult = EC5Util.convertPoint(resultCurve, result.point);
1095+
1096+
int argc = Arity.checkArgumentCount(runtime, args, 1, 3);
1097+
IRubyObject arg1 = null, arg2 = null;
1098+
switch (argc) {
1099+
case 2:
1100+
arg2 = args[1];
1101+
case 1:
1102+
arg1 = args[0];
1103+
}
1104+
if (!(arg1 instanceof RubyArray)) {
1105+
BigInteger bn;
1106+
if (arg1 instanceof RubyFixnum) {
1107+
bn = BigInteger.valueOf(arg1.convertToInteger().getLongValue());
1108+
} else if (arg1 instanceof RubyBignum) {
1109+
bn = ((RubyBignum) arg1).getValue();
1110+
} else if (arg1 instanceof BN) {
1111+
bn = ((BN) arg1).getValue();
1112+
} else {
1113+
throw runtime.newTypeError(arg1, runtime.getInteger());
1114+
}
1115+
1116+
if (arg2 != null) {
1117+
if (arg2 instanceof RubyFixnum) {
1118+
bn_g = BigInteger.valueOf(arg2.convertToInteger().getLongValue());
1119+
} else if (arg2 instanceof RubyBignum) {
1120+
bn_g = ((RubyBignum) arg2).getValue();
1121+
} else if (arg2 instanceof BN) {
1122+
bn_g = ((BN) arg2).getValue();
1123+
} else {
1124+
throw runtime.newTypeError(arg2, runtime.getInteger());
1125+
}
1126+
}
1127+
1128+
if (bn_g == null) {
1129+
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.referenceMultiply(pointSelf, bn);
1130+
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group);
1131+
} else {
1132+
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.sumOfTwoMultiplies(pointResult, bn_g, pointSelf, bn);
1133+
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group);
1134+
}
1135+
1136+
if (result == null) {
1137+
newECError(runtime, "bad multiply result");
1138+
}
1139+
} else {
1140+
throw runtime.newNotImplementedError("calling #mul with arrays is not supported by this OpenSSL version");
1141+
}
1142+
1143+
return result;
1144+
}
1145+
10711146
@Deprecated
10721147
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
10731148
final int argc = Arity.checkArgumentCount(context.runtime, args, 1, 2);

0 commit comments

Comments
 (0)