Skip to content
Snippets Groups Projects
ec_util_test.cc 2.69 KiB
Newer Older
Quan Nguyen's avatar
Quan Nguyen committed
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

Quan Nguyen's avatar
Quan Nguyen committed
#include "tink/subtle/common_enums.h"
#include "tink/subtle/ec_util.h"
#include "tink/util/status.h"
#include "tink/util/statusor.h"
#include "tink/util/test_util.h"
Quan Nguyen's avatar
Quan Nguyen committed
#include "gtest/gtest.h"

namespace crypto {
namespace tink {
namespace subtle {
Quan Nguyen's avatar
Quan Nguyen committed
namespace {

Quan Nguyen's avatar
Quan Nguyen committed
TEST(EcUtilTest, testFieldSizeInBytes) {
  EXPECT_EQ(256/8, EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P256));
  EXPECT_EQ(384/8, EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P384));
  EXPECT_EQ((521 + 7)/8,
            EcUtil::FieldSizeInBytes(EllipticCurveType::NIST_P521));
Quan Nguyen's avatar
Quan Nguyen committed

Quan Nguyen's avatar
Quan Nguyen committed
  EXPECT_EQ(0, EcUtil::FieldSizeInBytes(EllipticCurveType::UNKNOWN_CURVE));
}
Quan Nguyen's avatar
Quan Nguyen committed

Quan Nguyen's avatar
Quan Nguyen committed
TEST(EcUtilTest, testEncodingSizeInBytes) {
  EXPECT_EQ(2 * (256/8) + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
                                        EcPointFormat::UNCOMPRESSED)
            .ValueOrDie());
  EXPECT_EQ(256/8 + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
                                        EcPointFormat::COMPRESSED)
            .ValueOrDie());
  EXPECT_EQ(2 * (384/8) + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P384,
                                        EcPointFormat::UNCOMPRESSED)
            .ValueOrDie());
  EXPECT_EQ(384/8 + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P384,
                                        EcPointFormat::COMPRESSED)
            .ValueOrDie());
  EXPECT_EQ(2 * ((521 + 7)/8) + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P521,
                                        EcPointFormat::UNCOMPRESSED)
            .ValueOrDie());
  EXPECT_EQ((521 + 7)/8 + 1,
            EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P521,
                                        EcPointFormat::COMPRESSED)
            .ValueOrDie());
Quan Nguyen's avatar
Quan Nguyen committed

Quan Nguyen's avatar
Quan Nguyen committed
  EXPECT_FALSE(EcUtil::EncodingSizeInBytes(EllipticCurveType::NIST_P256,
                                           EcPointFormat::UNKNOWN_FORMAT).ok());
Quan Nguyen's avatar
Quan Nguyen committed
}

}  // namespace
}  // namespace subtle
Quan Nguyen's avatar
Quan Nguyen committed
}  // namespace tink
}  // namespace crypto