1 /* 2 * Copyright 2019 Google LLC. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * https://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PRIVATE_JOIN_AND_COMPUTE_CRYPTO_EC_POINT_H_ 17 #define PRIVATE_JOIN_AND_COMPUTE_CRYPTO_EC_POINT_H_ 18 19 #include <memory> 20 #include <string> 21 22 #include "private_join_and_compute/crypto/openssl.inc" 23 #include "private_join_and_compute/util/status.inc" 24 25 namespace private_join_and_compute { 26 27 class BigNum; 28 class ECGroup; 29 30 // Wrapper class for openssl EC_POINT. 31 class ECPoint { 32 public: 33 // Deletes an EC_POINT. 34 class ECPointDeleter { 35 public: operator()36 void operator()(EC_POINT* point) { EC_POINT_clear_free(point); } 37 }; 38 typedef std::unique_ptr<EC_POINT, ECPointDeleter> ECPointPtr; 39 40 // ECPoint is movable. 41 ECPoint(ECPoint&& that) = default; 42 ECPoint& operator=(ECPoint&& that) = default; 43 44 // ECPoint is not copyable. Use Clone to copy, instead. 45 explicit ECPoint(const ECPoint& that) = delete; 46 ECPoint& operator=(const ECPoint& that) = delete; 47 48 // Converts this point to octet string in compressed form as defined in ANSI 49 // X9.62 ECDSA. 50 StatusOr<std::string> ToBytesCompressed() const; 51 52 // Allows faster conversions than ToBytesCompressed but doubles the size of 53 // the serialized point. 54 StatusOr<std::string> ToBytesUnCompressed() const; 55 56 // Returns an ECPoint whose value is (this * scalar). 57 // Returns an INTERNAL error code if it fails. 58 StatusOr<ECPoint> Mul(const BigNum& scalar) const; 59 60 // Returns an ECPoint whose value is (this + point). 61 // Returns an INTERNAL error code if it fails. 62 StatusOr<ECPoint> Add(const ECPoint& point) const; 63 64 // Returns an ECPoint whose value is (- this), the additive inverse of this. 65 // Returns an INTERNAL error code if it fails. 66 StatusOr<ECPoint> Inverse() const; 67 68 // Returns "true" if the value of this ECPoint is the point-at-infinity. 69 // (The point-at-infinity is the additive unit in the EC group). 70 bool IsPointAtInfinity() const; 71 72 // Returns true if this equals point, false otherwise. 73 bool CompareTo(const ECPoint& point) const; 74 75 // Returns an ECPoint that is a copy of this. 76 StatusOr<ECPoint> Clone() const; 77 78 private: 79 // Creates an ECPoint on the given group; 80 ECPoint(const EC_GROUP* group, BN_CTX* bn_ctx); 81 82 // Creates an ECPoint on the given group from the given EC_POINT; 83 ECPoint(const EC_GROUP* group, BN_CTX* bn_ctx, ECPointPtr point); 84 85 // Creates an ECPoint object with the given x, y affine coordinates. 86 ECPoint(const EC_GROUP* group, BN_CTX* bn_ctx, const BigNum& x, 87 const BigNum& y); 88 89 BN_CTX* bn_ctx_; 90 const EC_GROUP* group_; 91 ECPointPtr point_; 92 93 // ECGroup is a factory for ECPoint. 94 friend class ECGroup; 95 }; 96 97 inline bool operator==(const ECPoint& a, const ECPoint& b) { 98 return a.CompareTo(b); 99 } 100 101 inline bool operator!=(const ECPoint& a, const ECPoint& b) { return !(a == b); } 102 103 } // namespace private_join_and_compute 104 105 #endif // PRIVATE_JOIN_AND_COMPUTE_CRYPTO_EC_POINT_H_ 106