|
58 | 58 | import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
|
59 | 59 | import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
60 | 60 |
|
| 61 | +import org.bouncycastle.math.ec.ECAlgorithms; |
| 62 | +import org.bouncycastle.math.ec.ECCurve; |
61 | 63 | import org.jruby.Ruby;
|
62 | 64 | import org.jruby.RubyArray;
|
| 65 | +import org.jruby.RubyBignum; |
63 | 66 | import org.jruby.RubyBoolean;
|
64 | 67 | import org.jruby.RubyClass;
|
| 68 | +import org.jruby.RubyFixnum; |
65 | 69 | import org.jruby.RubyModule;
|
66 | 70 | import org.jruby.RubyObject;
|
67 | 71 | import org.jruby.RubyString;
|
@@ -972,6 +976,7 @@ private boolean getPointAndGroup(ThreadContext context, IRubyObject groupOrPoint
|
972 | 976 |
|
973 | 977 | if ( groupOrPoint instanceof Group) {
|
974 | 978 | this.group = (Group) groupOrPoint;
|
| 979 | + this.point = (ECPoint) ((Group) groupOrPoint).generator(context); |
975 | 980 | } else {
|
976 | 981 | throw runtime.newTypeError(groupOrPoint, _EC(runtime).getClass("Group"));
|
977 | 982 | }
|
@@ -1068,6 +1073,76 @@ public IRubyObject inspect() {
|
1068 | 1073 | return ObjectSupport.inspect(this, (List) Collections.singletonList(entry));
|
1069 | 1074 | }
|
1070 | 1075 |
|
| 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 | + |
1071 | 1146 | @Deprecated
|
1072 | 1147 | public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
|
1073 | 1148 | final int argc = Arity.checkArgumentCount(context.runtime, args, 1, 2);
|
|
0 commit comments