move ecdsa code to ecdsa-wasm

update-fork
MITSUNARI Shigeo 4 years ago
parent 49b27ddbd7
commit 3f65ce22ac
  1. 19
      Makefile
  2. 105
      include/mcl/ecdsa.h
  3. 242
      include/mcl/ecdsa.hpp
  4. 110
      src/ecdsa_c.cpp
  5. 51
      test/ecdsa_c_test.cpp
  6. 69
      test/ecdsa_test.cpp

@ -3,13 +3,12 @@ LIB_DIR=lib
OBJ_DIR=obj
EXE_DIR=bin
SRC_SRC=fp.cpp bn_c256.cpp bn_c384.cpp bn_c384_256.cpp bn_c512.cpp she_c256.cpp
TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn384_test.cpp glv_test.cpp paillier_test.cpp she_test.cpp vint_test.cpp bn512_test.cpp ecdsa_test.cpp conversion_test.cpp
TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn384_test.cpp glv_test.cpp paillier_test.cpp she_test.cpp vint_test.cpp bn512_test.cpp conversion_test.cpp
TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c384_256_test.cpp bn_c512_test.cpp
TEST_SRC+=she_c256_test.cpp she_c384_test.cpp she_c384_256_test.cpp
TEST_SRC+=aggregate_sig_test.cpp array_test.cpp
TEST_SRC+=bls12_test.cpp
TEST_SRC+=mapto_wb19_test.cpp
TEST_SRC+=ecdsa_c_test.cpp
TEST_SRC+=modp_test.cpp
LIB_OBJ=$(OBJ_DIR)/fp.o
ifeq ($(MCL_STATIC_CODE),1)
@ -66,9 +65,8 @@ SHE384_LIB=$(LIB_DIR)/libmclshe384.a
SHE384_SLIB=$(LIB_DIR)/lib$(SHE384_SNAME).$(LIB_SUF)
SHE384_256_LIB=$(LIB_DIR)/libmclshe384_256.a
SHE384_256_SLIB=$(LIB_DIR)/lib$(SHE384_256_SNAME).$(LIB_SUF)
ECDSA_LIB=$(LIB_DIR)/libmclecdsa.a
SHE_LIB_ALL=$(SHE256_LIB) $(SHE256_SLIB) $(SHE384_LIB) $(SHE384_SLIB) $(SHE384_256_LIB) $(SHE384_256_SLIB)
all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN384_256_LIB) $(BN384_256_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE_LIB_ALL) $(ECDSA_LIB)
all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN384_256_LIB) $(BN384_256_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE_LIB_ALL)
#LLVM_VER=-3.8
LLVM_LLC=llc$(LLVM_VER)
@ -102,7 +100,6 @@ BN512_OBJ=$(OBJ_DIR)/bn_c512.o
SHE256_OBJ=$(OBJ_DIR)/she_c256.o
SHE384_OBJ=$(OBJ_DIR)/she_c384.o
SHE384_256_OBJ=$(OBJ_DIR)/she_c384_256.o
ECDSA_OBJ=$(OBJ_DIR)/ecdsa_c.o
FUNC_LIST=src/func.list
ifeq ($(findstring $(OS),mingw64/cygwin),)
MCL_USE_LLVM?=1
@ -191,9 +188,6 @@ $(SHE384_SLIB): $(SHE384_OBJ) $(MCL_LIB)
$(SHE384_256_SLIB): $(SHE384_256_OBJ) $(MCL_LIB)
$(PRE)$(CXX) -o $@ $(SHE384_256_OBJ) $(MCL_LIB) -shared $(LDFLAGS) $(SHE384_256_SLIB_LDFLAGS)
$(ECDSA_LIB): $(ECDSA_OBJ)
$(AR) $@ $(ECDSA_OBJ)
$(BN256_SLIB): $(BN256_OBJ) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN256_OBJ) -shared $(LDFLAGS) $(BN256_SLIB_LDFLAGS)
@ -329,9 +323,6 @@ $(EXE_DIR)/she_c384_test.exe: $(OBJ_DIR)/she_c384_test.o $(SHE384_LIB) $(MCL_LIB
$(EXE_DIR)/she_c384_256_test.exe: $(OBJ_DIR)/she_c384_256_test.o $(SHE384_256_LIB) $(MCL_LIB)
$(PRE)$(CXX) $< -o $@ $(SHE384_256_LIB) $(MCL_LIB) $(LDFLAGS)
$(EXE_DIR)/ecdsa_c_test.exe: $(OBJ_DIR)/ecdsa_c_test.o $(ECDSA_LIB) $(MCL_LIB) src/ecdsa_c.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h
$(PRE)$(CXX) $< -o $@ $(ECDSA_LIB) $(MCL_LIB) $(LDFLAGS)
$(OBJ_DIR)/modp_test.o: test/modp_test.cpp
$(PRE)$(CXX) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) -DMCL_USE_VINT -DMCL_MAX_BIT_SIZE=384 -DMCL_VINT_64BIT_PORTABLE -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_FIXED_BUFFER -I./include -O2 $(CFLAGS_WARN)
@ -361,12 +352,6 @@ ifeq ($(MCL_USE_LLVM),2)
SHE_C_DEP+=src/base64m.ll
endif
../ecdsa-wasm/ecdsa_c.js: src/ecdsa_c.cpp src/fp.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h Makefile
emcc -o $@ src/fp.cpp src/ecdsa_c.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions
ecdsa-wasm:
$(MAKE) ../ecdsa-wasm/ecdsa_c.js
# test
bin/emu:
$(CXX) -g -o $@ src/fp.cpp src/bn_c256.cpp test/bn_c256_test.cpp -DMCL_DONT_USE_XBYAK -DMCL_DONT_USE_OPENSSL -DMCL_USE_VINT -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_64BIT_PORTABLE -DMCL_VINT_FIXED_BUFFER -DMCL_MAX_BIT_SIZE=256 -I./include

@ -1,105 +0,0 @@
#pragma once
/**
@file
@brief C interface of ECDSA
@author MITSUNARI Shigeo(@herumi)
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <stdint.h> // for uint64_t, uint8_t
#include <stdlib.h> // for size_t
#if defined(_MSC_VER)
#ifdef ECDSA_DLL_EXPORT
#define ECDSA_DLL_API __declspec(dllexport)
#else
#define ECDSA_DLL_API __declspec(dllimport)
#ifndef ECDSA_NO_AUTOLINK
#pragma comment(lib, "mclecdsa.lib")
#endif
#endif
#elif defined(__EMSCRIPTEN__)
#define ECDSA_DLL_API __attribute__((used))
#else
#define ECDSA_DLL_API
#endif
#ifndef mclSize
#ifdef __EMSCRIPTEN__
// avoid 64-bit integer
#define mclSize unsigned int
#define mclInt int
#else
// use #define for cgo
#define mclSize size_t
#define mclInt int64_t
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef ECDSA_NOT_DEFINE_STRUCT
typedef struct ecdsaSecretKey ecdsaSecretKey;
typedef struct ecdsaPublicKey ecdsaPublicKey;
typedef struct ecdsaSignature ecdsaSignature;
#else
typedef struct {
uint64_t d[4];
} ecdsaSecretKey;
typedef struct {
uint64_t d[4 * 3];
} ecdsaPublicKey;
typedef struct {
uint64_t d[4 * 2];
} ecdsaSignature;
#endif
struct ecdsaPrecomputedPublicKey;
/*
init library
return 0 if success
@note not threadsafe
*/
ECDSA_DLL_API int ecdsaInit(void);
// return written byte size if success else 0
ECDSA_DLL_API mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec);
ECDSA_DLL_API mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub);
ECDSA_DLL_API mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig);
// return read byte size if sucess else 0
ECDSA_DLL_API mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize);
ECDSA_DLL_API mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize);
ECDSA_DLL_API mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize);
// return 0 if success
ECDSA_DLL_API int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec);
ECDSA_DLL_API void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec);
ECDSA_DLL_API void ecdsaSign(ecdsaSignature *sig, const ecdsaSecretKey *sec, const void *m, mclSize size);
// return 1 if valid
ECDSA_DLL_API int ecdsaVerify(const ecdsaSignature *sig, const ecdsaPublicKey *pub, const void *m, mclSize size);
ECDSA_DLL_API int ecdsaVerifyPrecomputed(const ecdsaSignature *sig, const ecdsaPrecomputedPublicKey *pub, const void *m, mclSize size);
// return nonzero if success
ECDSA_DLL_API ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate();
// call this function to avoid memory leak
ECDSA_DLL_API void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub);
// return 0 if success
ECDSA_DLL_API int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub);
#ifdef __cplusplus
}
#endif

@ -1,242 +0,0 @@
#pragma once
/**
@file
@brief ECDSA
@author MITSUNARI Shigeo(@herumi)
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <mcl/fp.hpp>
#include <mcl/ec.hpp>
#include <mcl/ecparam.hpp>
#include <mcl/window_method.hpp>
namespace mcl { namespace ecdsa {
namespace local {
#ifndef MCLSHE_WIN_SIZE
#define MCLSHE_WIN_SIZE 10
#endif
static const size_t winSize = MCLSHE_WIN_SIZE;
struct FpTag;
struct ZnTag;
} // mcl::ecdsa::local
typedef mcl::FpT<local::FpTag, 256> Fp;
typedef mcl::FpT<local::ZnTag, 256> Zn;
typedef mcl::EcT<Fp> Ec;
namespace local {
struct Param {
Ec P;
mcl::fp::WindowMethod<Ec> Pbase;
size_t bitSize;
};
inline Param& getParam()
{
static Param p;
return p;
}
inline void be32toZn(Zn& x, const mcl::fp::Unit *buf)
{
const size_t n = 32;
const unsigned char *p = (const unsigned char*)buf;
unsigned char be[n];
for (size_t i = 0; i < n; i++) {
be[i] = p[n - 1 - i];
}
x.setArrayMaskMod(be, n);
}
/*
y = x mod n
*/
inline void FpToZn(Zn& y, const Fp& x)
{
fp::Block b;
x.getBlock(b);
y.setArrayMaskMod(b.p, b.n);
}
inline void setHashOf(Zn& x, const void *msg, size_t msgSize)
{
mcl::fp::Unit xBuf[256 / 8 / sizeof(mcl::fp::Unit)];
uint32_t hashSize = mcl::fp::sha256(xBuf, sizeof(xBuf), msg, (uint32_t)msgSize);
assert(hashSize == sizeof(xBuf));
(void)hashSize;
be32toZn(x, xBuf);
}
} // mcl::ecdsa::local
const local::Param& param = local::getParam();
inline void init(bool *pb)
{
local::Param& p = local::getParam();
mcl::initCurve<Ec, Zn>(pb, MCL_SECP256K1, &p.P);
if (!*pb) return;
p.bitSize = 256;
p.Pbase.init(pb, p.P, p.bitSize, local::winSize);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
inline void init()
{
bool b;
init(&b);
if (!b) throw cybozu::Exception("ecdsa:init");
}
#endif
typedef Zn SecretKey;
typedef Ec PublicKey;
struct PrecomputedPublicKey {
mcl::fp::WindowMethod<Ec> pubBase_;
void init(bool *pb, const PublicKey& pub)
{
pubBase_.init(pb, pub, param.bitSize, local::winSize);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
void init(const PublicKey& pub)
{
bool b;
init(&b, pub);
if (!b) throw cybozu::Exception("ecdsa:PrecomputedPublicKey:init");
}
#endif
};
inline void getPublicKey(PublicKey& pub, const SecretKey& sec)
{
Ec::mul(pub, param.P, sec);
pub.normalize();
}
struct Signature : public mcl::fp::Serializable<Signature> {
Zn r, s;
template<class InputStream>
void load(bool *pb, InputStream& is, int ioMode = IoSerialize)
{
r.load(pb, is, ioMode); if (!*pb) return;
s.load(pb, is, ioMode);
}
template<class OutputStream>
void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const
{
const char sep = *fp::getIoSeparator(ioMode);
r.save(pb, os, ioMode); if (!*pb) return;
if (sep) {
cybozu::writeChar(pb, os, sep);
if (!*pb) return;
}
s.save(pb, os, ioMode);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("ecdsa:Signature:load");
}
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("ecdsa:Signature:save");
}
#endif
#ifndef CYBOZU_DONT_USE_STRING
friend std::istream& operator>>(std::istream& is, Signature& self)
{
self.load(is, fp::detectIoMode(Ec::getIoMode(), is));
return is;
}
friend std::ostream& operator<<(std::ostream& os, const Signature& self)
{
self.save(os, fp::detectIoMode(Ec::getIoMode(), os));
return os;
}
#endif
};
inline void sign(Signature& sig, const SecretKey& sec, const void *msg, size_t msgSize)
{
Zn& r = sig.r;
Zn& s = sig.s;
Zn z, k;
local::setHashOf(z, msg, msgSize);
Ec Q;
for (;;) {
k.setByCSPRNG();
param.Pbase.mul(Q, k);
if (Q.isZero()) continue;
Q.normalize();
local::FpToZn(r, Q.x);
if (r.isZero()) continue;
Zn::mul(s, r, sec);
s += z;
if (s.isZero()) continue;
s /= k;
return;
}
}
namespace local {
inline void mulDispatch(Ec& Q, const PublicKey& pub, const Zn& y)
{
Ec::mul(Q, pub, y);
}
inline void mulDispatch(Ec& Q, const PrecomputedPublicKey& ppub, const Zn& y)
{
ppub.pubBase_.mul(Q, y);
}
template<class Pub>
inline bool verify(const Signature& sig, const Pub& pub, const void *msg, size_t msgSize)
{
const Zn& r = sig.r;
const Zn& s = sig.s;
if (r.isZero() || s.isZero()) return false;
Zn z, w, u1, u2;
local::setHashOf(z, msg, msgSize);
Zn::inv(w, s);
Zn::mul(u1, z, w);
Zn::mul(u2, r, w);
Ec Q1, Q2;
param.Pbase.mul(Q1, u1);
// Ec::mul(Q2, pub, u2);
local::mulDispatch(Q2, pub, u2);
Q1 += Q2;
if (Q1.isZero()) return false;
Q1.normalize();
Zn x;
local::FpToZn(x, Q1.x);
return r == x;
}
} // mcl::ecdsa::local
inline bool verify(const Signature& sig, const PublicKey& pub, const void *msg, size_t msgSize)
{
return local::verify(sig, pub, msg, msgSize);
}
inline bool verify(const Signature& sig, const PrecomputedPublicKey& ppub, const void *msg, size_t msgSize)
{
return local::verify(sig, ppub, msg, msgSize);
}
} } // mcl::ecdsa

@ -1,110 +0,0 @@
#define ECDSA_DLL_EXPORT
#include <mcl/ecdsa.h>
#include <mcl/ecdsa.hpp>
#include <new>
using namespace mcl::ecdsa;
static SecretKey *cast(ecdsaSecretKey *p) { return reinterpret_cast<SecretKey*>(p); }
static const SecretKey *cast(const ecdsaSecretKey *p) { return reinterpret_cast<const SecretKey*>(p); }
static PublicKey *cast(ecdsaPublicKey *p) { return reinterpret_cast<PublicKey*>(p); }
static const PublicKey *cast(const ecdsaPublicKey *p) { return reinterpret_cast<const PublicKey*>(p); }
static Signature *cast(ecdsaSignature *p) { return reinterpret_cast<Signature*>(p); }
static const Signature *cast(const ecdsaSignature *p) { return reinterpret_cast<const Signature*>(p); }
static PrecomputedPublicKey *cast(ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<PrecomputedPublicKey*>(p); }
static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); }
#ifdef __EMSCRIPTEN__
// use these functions forcibly
extern "C" ECDSA_DLL_API void *ecdsaMalloc(size_t n)
{
return malloc(n);
}
extern "C" ECDSA_DLL_API void ecdsaFree(void *p)
{
free(p);
}
#endif
int ecdsaInit(void)
{
bool b;
init(&b);
return b ? 0 : -1;
}
mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec)
{
return (mclSize)cast(sec)->serialize(buf, maxBufSize);
}
mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub)
{
return (mclSize)cast(pub)->serialize(buf, maxBufSize);
}
mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig)
{
return (mclSize)cast(sig)->serialize(buf, maxBufSize);
}
mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize)
{
return (mclSize)cast(sec)->deserialize(buf, bufSize);
}
mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize)
{
return (mclSize)cast(pub)->deserialize(buf, bufSize);
}
mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize)
{
return (mclSize)cast(sig)->deserialize(buf, bufSize);
}
// return 0 if success
int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec)
{
cast(sec)->setByCSPRNG();
return 0;
}
void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec)
{
getPublicKey(*cast(pub), *cast(sec));
}
void ecdsaSign(ecdsaSignature *sig, const ecdsaSecretKey *sec, const void *m, mclSize size)
{
sign(*cast(sig), *cast(sec), m, size);
}
int ecdsaVerify(const ecdsaSignature *sig, const ecdsaPublicKey *pub, const void *m, mclSize size)
{
return verify(*cast(sig), *cast(pub), m, size);
}
int ecdsaVerifyPrecomputed(const ecdsaSignature *sig, const ecdsaPrecomputedPublicKey *ppub, const void *m, mclSize size)
{
return verify(*cast(sig), *cast(ppub), m, size);
}
ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate()
{
PrecomputedPublicKey *ppub = (PrecomputedPublicKey*)malloc(sizeof(PrecomputedPublicKey));
if (ppub == 0) return 0;
new(ppub) PrecomputedPublicKey();
return reinterpret_cast<ecdsaPrecomputedPublicKey*>(ppub);
}
void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub)
{
cast(ppub)->~PrecomputedPublicKey();
free(ppub);
}
int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub)
{
bool b;
cast(ppub)->init(&b, *cast(pub));
return b ? 0 : -1;
}

@ -1,51 +0,0 @@
#include <mcl/ecdsa.h>
#include <cybozu/test.hpp>
#include <string.h>
template<class T, class Serializer, class Deserializer>
void serializeTest(const T& x, const Serializer& serialize, const Deserializer& deserialize)
{
char buf[128];
size_t n = serialize(buf, sizeof(buf), &x);
CYBOZU_TEST_ASSERT(n > 0);
T y;
size_t m = deserialize(&y, buf, n);
CYBOZU_TEST_EQUAL(m, n);
CYBOZU_TEST_ASSERT(memcmp(&x, &y, n) == 0);
}
CYBOZU_TEST_AUTO(ecdsa)
{
int ret;
ret = ecdsaInit();
CYBOZU_TEST_EQUAL(ret, 0);
ecdsaSecretKey sec;
ecdsaPublicKey pub;
ecdsaPrecomputedPublicKey *ppub;
ecdsaSignature sig;
const char *msg = "hello";
mclSize msgSize = strlen(msg);
ret = ecdsaSecretKeySetByCSPRNG(&sec);
CYBOZU_TEST_EQUAL(ret, 0);
serializeTest(sec, ecdsaSecretKeySerialize, ecdsaSecretKeyDeserialize);
ecdsaGetPublicKey(&pub, &sec);
serializeTest(pub, ecdsaPublicKeySerialize, ecdsaPublicKeyDeserialize);
ecdsaSign(&sig, &sec, msg, msgSize);
serializeTest(sig, ecdsaSignatureSerialize, ecdsaSignatureDeserialize);
CYBOZU_TEST_ASSERT(ecdsaVerify(&sig, &pub, msg, msgSize));
ppub = ecdsaPrecomputedPublicKeyCreate();
CYBOZU_TEST_ASSERT(ppub);
ret = ecdsaPrecomputedPublicKeyInit(ppub, &pub);
CYBOZU_TEST_EQUAL(ret, 0);
CYBOZU_TEST_ASSERT(ecdsaVerifyPrecomputed(&sig, ppub, msg, msgSize));
sig.d[0]++;
CYBOZU_TEST_ASSERT(!ecdsaVerify(&sig, &pub, msg, msgSize));
CYBOZU_TEST_ASSERT(!ecdsaVerifyPrecomputed(&sig, ppub, msg, msgSize));
ecdsaPrecomputedPublicKeyDestroy(ppub);
}

@ -1,69 +0,0 @@
#define PUT(x) std::cout << #x "=" << (x) << std::endl;
#include <stdlib.h>
#include <stdio.h>
void put(const void *buf, size_t bufSize)
{
const unsigned char* p = (const unsigned char*)buf;
for (size_t i = 0; i < bufSize; i++) {
printf("%02x", p[i]);
}
printf("\n");
}
#include <mcl/ecdsa.hpp>
#include <cybozu/test.hpp>
#include <cybozu/benchmark.hpp>
using namespace mcl::ecdsa;
CYBOZU_TEST_AUTO(ecdsa)
{
init();
SecretKey sec;
PublicKey pub;
sec.setByCSPRNG();
getPublicKey(pub, sec);
Signature sig;
const std::string msg = "hello";
sign(sig, sec, msg.c_str(), msg.size());
CYBOZU_TEST_ASSERT(verify(sig, pub, msg.c_str(), msg.size()));
sig.s += 1;
CYBOZU_TEST_ASSERT(!verify(sig, pub, msg.c_str(), msg.size()));
}
CYBOZU_TEST_AUTO(value)
{
const std::string msg = "hello";
const char *secStr = "83ecb3984a4f9ff03e84d5f9c0d7f888a81833643047acc58eb6431e01d9bac8";
const char *pubxStr = "653bd02ba1367e5d4cd695b6f857d1cd90d4d8d42bc155d85377b7d2d0ed2e71";
const char *pubyStr = "04e8f5da403ab78decec1f19e2396739ea544e2b14159beb5091b30b418b813a";
const char *sigStr = "a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5de5d79a2ba44e311d04fdca263639283965780bce9169822be9cc81756e95a24";
SecretKey sec;
sec.setStr(secStr, 16);
CYBOZU_TEST_EQUAL(sec.getStr(16), secStr);
PublicKey pub;
getPublicKey(pub, sec);
pub.normalize();
Ec t(Fp(pubxStr, 16), Fp(pubyStr, 16));
CYBOZU_TEST_EQUAL(pub, t);
Signature sig;
sig.r.setStr(std::string(sigStr, 64), 16);
sig.s.setStr(std::string(sigStr + 64, 64), 16);
PUT(sig);
CYBOZU_TEST_ASSERT(verify(sig, pub, msg.c_str(), msg.size()));
}
CYBOZU_TEST_AUTO(bench)
{
const std::string msg = "hello";
SecretKey sec;
PublicKey pub;
PrecomputedPublicKey ppub;
sec.setByCSPRNG();
getPublicKey(pub, sec);
ppub.init(pub);
Signature sig;
CYBOZU_BENCH_C("sign", 1000, sign, sig, sec, msg.c_str(), msg.size());
CYBOZU_BENCH_C("pub.verify ", 1000, verify, sig, pub, msg.c_str(), msg.size());
CYBOZU_BENCH_C("ppub.verify", 1000, verify, sig, ppub, msg.c_str(), msg.size());
}
Loading…
Cancel
Save