Skip to content

Commit 23ff684

Browse files
committed
Refactor and arity-split Point#mul
1 parent dd59f74 commit 23ff684

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

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

+58-53
Original file line numberDiff line numberDiff line change
@@ -1099,74 +1099,64 @@ public IRubyObject add(final ThreadContext context, final IRubyObject other) {
10991099
return result;
11001100
}
11011101

1102-
@JRubyMethod(name = "mul", required = 1, optional = 2)
1103-
public IRubyObject mul(final ThreadContext context, final IRubyObject[] args) {
1102+
@JRubyMethod(name = "mul")
1103+
public IRubyObject mul(final ThreadContext context, final IRubyObject bn1) {
11041104
Ruby runtime = context.runtime;
11051105

1106-
org.bouncycastle.math.ec.ECPoint pointSelf, pointResult;
1107-
1108-
Group groupV = this.group;
1106+
if (bn1 instanceof RubyArray) {
1107+
throw runtime.newNotImplementedError("calling #mul with arrays is not supported by this OpenSSL version");
1108+
}
11091109

1110-
Point result;
1110+
org.bouncycastle.math.ec.ECPoint pointSelf;
11111111

1112-
BigInteger bn_g = null;
1112+
Group groupV = this.group;
11131113

1114-
ECCurve selfCurve = EC5Util.convertCurve(group.getCurve());
1114+
ECCurve selfCurve = EC5Util.convertCurve(groupV.getCurve());
11151115
pointSelf = EC5Util.convertPoint(selfCurve, asECPoint());
11161116

1117-
result = new Point(runtime, getMetaClass());
1118-
result.initialize(context, groupV);
1119-
ECCurve resultCurve = EC5Util.convertCurve(result.group.getCurve());
1120-
pointResult = EC5Util.convertPoint(resultCurve, result.point);
1117+
BigInteger bn = getBigInteger(context, bn1);
11211118

1122-
int argc = Arity.checkArgumentCount(runtime, args, 1, 3);
1123-
IRubyObject arg1 = null, arg2 = null;
1124-
switch (argc) {
1125-
case 2:
1126-
arg2 = args[1];
1127-
case 1:
1128-
arg1 = args[0];
1119+
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.referenceMultiply(pointSelf, bn);
1120+
if (mulPoint == null) {
1121+
throw newECError(runtime, "bad multiply result");
11291122
}
1130-
if (!(arg1 instanceof RubyArray)) {
1131-
BigInteger bn;
1132-
if (arg1 instanceof RubyFixnum) {
1133-
bn = BigInteger.valueOf(arg1.convertToInteger().getLongValue());
1134-
} else if (arg1 instanceof RubyBignum) {
1135-
bn = ((RubyBignum) arg1).getValue();
1136-
} else if (arg1 instanceof BN) {
1137-
bn = ((BN) arg1).getValue();
1138-
} else {
1139-
throw runtime.newTypeError(arg1, runtime.getInteger());
1140-
}
11411123

1142-
if (arg2 != null) {
1143-
if (arg2 instanceof RubyFixnum) {
1144-
bn_g = BigInteger.valueOf(arg2.convertToInteger().getLongValue());
1145-
} else if (arg2 instanceof RubyBignum) {
1146-
bn_g = ((RubyBignum) arg2).getValue();
1147-
} else if (arg2 instanceof BN) {
1148-
bn_g = ((BN) arg2).getValue();
1149-
} else {
1150-
throw runtime.newTypeError(arg2, runtime.getInteger());
1151-
}
1152-
}
1124+
return new Point(runtime, EC5Util.convertPoint(mulPoint), groupV);
1125+
}
11531126

1154-
if (bn_g == null) {
1155-
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.referenceMultiply(pointSelf, bn);
1156-
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group);
1157-
} else {
1158-
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.sumOfTwoMultiplies(pointResult, bn_g, pointSelf, bn);
1159-
result = new Point(runtime, EC5Util.convertPoint(mulPoint), result.group);
1160-
}
1127+
@JRubyMethod(name = "mul")
1128+
public IRubyObject mul(final ThreadContext context, final IRubyObject bn1, final IRubyObject bn2) {
1129+
Ruby runtime = context.runtime;
11611130

1162-
if (result == null) {
1163-
newECError(runtime, "bad multiply result");
1164-
}
1165-
} else {
1131+
if (bn1 instanceof RubyArray) {
11661132
throw runtime.newNotImplementedError("calling #mul with arrays is not supported by this OpenSSL version");
11671133
}
11681134

1169-
return result;
1135+
org.bouncycastle.math.ec.ECPoint pointSelf, pointResult;
1136+
1137+
Group groupV = this.group;
1138+
1139+
ECCurve selfCurve = EC5Util.convertCurve(groupV.getCurve());
1140+
pointSelf = EC5Util.convertPoint(selfCurve, asECPoint());
1141+
1142+
ECCurve resultCurve = EC5Util.convertCurve(groupV.getCurve());
1143+
pointResult = EC5Util.convertPoint(resultCurve, ((Point) groupV.generator(context)).asECPoint());
1144+
1145+
BigInteger bn = getBigInteger(context, bn1);
1146+
BigInteger bn_g = getBigInteger(context, bn2);
1147+
1148+
org.bouncycastle.math.ec.ECPoint mulPoint = ECAlgorithms.sumOfTwoMultiplies(pointResult, bn_g, pointSelf, bn);
1149+
1150+
if (mulPoint == null) {
1151+
throw newECError(runtime, "bad multiply result");
1152+
}
1153+
1154+
return new Point(runtime, EC5Util.convertPoint(mulPoint), groupV);
1155+
}
1156+
1157+
@JRubyMethod(name = "mul")
1158+
public IRubyObject mul(final ThreadContext context, final IRubyObject bns, final IRubyObject points, final IRubyObject bn2) {
1159+
throw context.runtime.newNotImplementedError("calling #mul with arrays is not supported by this OpenSSL version");
11701160
}
11711161

11721162
@Deprecated
@@ -1185,6 +1175,21 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
11851175

11861176
}
11871177

1178+
private static BigInteger getBigInteger(ThreadContext context, IRubyObject arg1) {
1179+
BigInteger bn;
1180+
if (arg1 instanceof RubyFixnum) {
1181+
bn = BigInteger.valueOf(arg1.convertToInteger().getLongValue());
1182+
} else if (arg1 instanceof RubyBignum) {
1183+
bn = ((RubyBignum) arg1).getValue();
1184+
} else if (arg1 instanceof BN) {
1185+
bn = ((BN) arg1).getValue();
1186+
} else {
1187+
Ruby runtime = context.runtime;
1188+
throw runtime.newTypeError(arg1, runtime.getInteger());
1189+
}
1190+
return bn;
1191+
}
1192+
11881193
static byte[] encode(final ECPublicKey pubKey) {
11891194
return encodeUncompressed(pubKey.getParams().getOrder().bitLength(), pubKey.getW());
11901195
}

0 commit comments

Comments
 (0)