0

私の最終的な目標は、GMP ライブラリを使用して C で構造体変数として実装されている二次イデアルのべき乗を計算することです。

私は、C++ テンプレート、名前空間、および NTL を使用した一般的な最適化指数を含むライブラリ (ANTL) を受け取りました。このライブラリは、NTL 型 ZZ_p などと、long、float などの基本型の累乗を行います。

最終的な目標を達成するには、ANTL ライブラリを使用する必要があります。それは、理想的なC struct variableの力を計算することです。テンプレートや名前空間を扱ったことがなかったので、基本的な mpz_t 変数の機能を実装して、すべてがどのように機能するかを確認したいと思いました。

現在、次のように、4 つのヘッダー ファイル exp.hpp、expbin.hpp、impl.hpp、com.hpp とメイン ファイル exp.cpp があります。

COM.HPP

#ifndef GMPL_COM_H
#define GMPL_COM_H

#include <gmp.h>

namespace GMPL {
    template < class T >
    inline void assign(T C, const T A)
    {
        mpz_set(C, A);
    }

    template<class T>
    void mul (T C, const T A, const T B)
    {
        mpz_mul(C, A, B);
    }

    template<class T>
    void sqr (T C, const T A)
    {
        mpz_mul(C, A, A);
    }

}
#endif // guard

EXP.HPP

#ifndef EXP_H
#define EXP_H

#include "com.hpp"

namespace GMPL
{
    template < class T >
    class exp
    {
        public:
        exp() {};
        virtual ~exp() {};

        virtual void power (T C, const T A, const NTL::ZZ & n) = 0;
    };

} // GMPL

#endif // EXP_H

EXPBIN.HPP

#ifndef EXPBIN_H
#define EXPBIN_H

#include "exp.hpp"

namespace GMPL
{
    template < class T >
    class expbin : public exp<T>
    {
        public:
        expbin() {};
        ~expbin() {};

        void power (T C, const T A, const NTL::ZZ & n);
    };

} // GMPL

// Unspecialized template definitions.
#include "impl.hpp"

#endif // EXPBIN_H

IMPL.HPP

using namespace GMPL;

//
// compute A^n using standard left-to-right binary method
//
template < class T > 
void expbin<T>::power (T C, const T A, const NTL::ZZ & n)
{
    assign(C,A);
    for (register long i = NumBits(n)-2 ; i >= 0 ; i--)
    {
        sqr(C, C);
        if (bit(n, i) == 1)
            mul(C, C, A);
    }
}

EXP.CPP

#include <NTL/lzz_p.h>
#include <gmp.h>

namespace GMPL {}
using namespace GMPL;


#include "expbin.hpp"

NTL_CLIENT

int main ()
{
    // NTL variables
    ZZ n;

    // GMP variables
    mpz_t aa;
    mpz_t bb;
    mpz_init(aa);
    mpz_init(bb);

    // generate random exponent of size 512 bits
    RandomLen (n, 512); // NTL function

    // initialize exponentiation classes
    expbin<mpz_t> obj;

    // compute a^n with available methods
    obj.power (bb,aa,n);

    // check and output results
    gmp_printf("%Zd", bb);
}

EXP.CPPを使用してコンパイルしようとすると(Victor Shoup の NTL ドキュメント オンラインで説明されているように)

g++ -g -O2 -std=c++11 -pthread -march=native exp.cpp -o t -lntl -lgmp -lm

次のエラー メッセージが表示されます。

$ g++ -g -O2 -std=c++11 -pthread -march=native exp.cpp -o t -lntl -lgmp -lm
In file included from exp.cpp:8:
In file included from ./expbin.hpp:46:
./impl.hpp:16:10: warning: 'register' storage class specifier is deprecated and
      incompatible with C++1z [-Wdeprecated-register]
    for (register long i = NumBits(n)-2 ; i >= 0 ; i--)
         ^~~~~~~~~
./impl.hpp:15:5: error: no matching function for call to 'assign'
    assign(C,A);
    ^~~~~~
exp.cpp:28:9: note: in instantiation of member function
      'GMPL::expbin<__mpz_struct [1]>::power' requested here
    obj.power (bb,aa,n);
        ^
./com.hpp:16:17: note: candidate template ignored: deduced conflicting types for
      parameter 'T' ('__mpz_struct *' vs. 'const __mpz_struct *')
    inline void assign(T C, const T A)
                ^
In file included from exp.cpp:8:
In file included from ./expbin.hpp:46:
./impl.hpp:20:13: error: no matching function for call to 'mul'
            mul(C, C, A);
            ^~~
./com.hpp:22:10: note: candidate template ignored: deduced conflicting types for
      parameter 'T' ('__mpz_struct *' vs. 'const __mpz_struct *')
    void mul (T C, const T A, const T B)

これらのエラーについて慎重にグーグル検索すると、親クラスに空のコンストラクターが必要であることがわかりますが、私は既にそれを持っています。

NTLを使用する場合、それ以外に何も機能しないため、コンパイルステートメントが正しいことはわかっています。この時点で、これを修正するためのアイデアが不足していました。前もって感謝します。

編集 この質問は解決されました。この質問をクローズまたは削除していただきたいと思います。

4

1 に答える 1