/* * Multiply the provided (encoded) point G by a scalar x. Scalar * encoding is big-endian. The scalar value shall be non-zero and * lower than the subgroup order (exception: some curves allow * larger ranges). * * The result is written in the provided D[] array, using either * compressed or uncompressed format (for some curves, output is * always compressed). The array shall have the appropriate length. * Returned value is -1 on success, 0 on error. If 0 is returned * then the array contents are indeterminate. * * G and D need not be distinct arrays. */ public uint Mul(byte[] G, byte[] x, byte[] D, bool compressed) { MutableECPoint P = MakeZero(); uint good = P.DecodeCT(G); good &= ~P.IsInfinityCT; good &= P.MulSpecCT(x); good &= P.Encode(D, compressed); return(good); }
/* * Given points A and B, and scalar x and y, return x*A+y*B. This * is used for ECDSA. Scalars use big-endian encoding and must be * non-zero and lower than the subgroup order. * * The result is written in the provided D[] array, using either * compressed or uncompressed format (for some curves, output is * always compressed). The array shall have the appropriate length. * Returned value is -1 on success, 0 on error. If 0 is returned * then the array contents are indeterminate. * * Not all curves support this operation; if the curve does not, * then an exception is thrown. * * A, B and D need not be distinct arrays. */ public uint MulAdd(byte[] A, byte[] x, byte[] B, byte[] y, byte[] D, bool compressed) { MutableECPoint P = MakeZero(); MutableECPoint Q = MakeZero(); /* * Decode both points. */ uint good = P.DecodeCT(A); good &= Q.DecodeCT(B); good &= ~P.IsInfinityCT & ~Q.IsInfinityCT; /* * Perform both point multiplications. */ good &= P.MulSpecCT(x); good &= Q.MulSpecCT(y); good &= ~P.IsInfinityCT & ~Q.IsInfinityCT; /* * Perform addition. The AddCT() function may fail if * P = Q, in which case we must compute 2Q and use that * value instead. */ uint z = P.AddCT(Q); Q.DoubleCT(); P.Set(Q, ~z); /* * Encode the result. The Encode() function will report * an error if the addition result is infinity. */ good &= P.Encode(D, compressed); return(good); }