我正在通过Cython包装一些代码。C++实现可以找到wrapper.h。目前,我的重点是包装类CiphertextInterfaceType
,为了方便起见复制了下面的内容。
#include <palisade.h>
namespace pycrypto {
/*
* Ciphertext python wrapper
*/
class CiphertextInterfaceType {
public:
/**
* Default constructor
*/
CiphertextInterfaceType();
/**
* Constructor from Ciphertext
*/
CiphertextInterfaceType(lbcrypto::Ciphertext<lbcrypto::DCRTPoly> ciphertext);
/**
* Destructor
*/
~CiphertextInterfaceType();
const lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly> &GetCiphertext() const;
private:
lbcrypto::Ciphertext<lbcrypto::DCRTPoly> m_ciphertext;
};
}
我面临的问题是,我不知道如何“包装”或使用lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>
,我称之为cipherType
。cipherType
是来自不符合标准的外部库的自定义类型。我需要1)将它声明为私有变量,2)创建接受它的构造函数,3)实现getter。
下面是我的.pxd
文件( cython“equicalent?)
cdef extern from "ckks_wrapper.cpp":
pass
cdef extern from "ckks_wrapper.h" namespace "pycrypto":
cdef cppclass CiphertextInterfaceType:
####################################
# Attrs
####################################
# TODO: define the private variable m_ciphertext from above
####################################
# Constructors/ destructors
####################################
CiphertextInterfaceType() except +
# TODO: the constructor below is supposed to accept an
CiphertextInterfaceType() except +
####################################
# Methods
####################################
# TODO:
# Implement the *GetCiphertext
因此,我已经完成了有关包装一些Rectangle
类的cython教程,Cython -包装矩形很有帮助,但我显然遗漏了一些东西。如何在我的cipherType
文件中“定义”.pxd
类型?
DavidW的后续行动
所以,我的ckks_wrapper.hpp
有一个名称空间,pycrypto
。在ckks_wrapper.hpp
中,我还使用了include <palisade.h>
,它本身具有名称空间lbcrypto
。根据答案,这是否意味着我可以直接访问库palisade.h
、lbcrypto
的命名空间,而Cython编译器将知道如何查看palisade.h
我只想确保我能理解
关于在何处定义CiphertextDCRTPoly "Ciphertext<DCRTPoly>"
cdef extern from "ckks_wrapper.cpp":
pass
cdef extern from "ckks_wrapper.hpp" namespace "libcrypto":
cdef cppclass CiphertextDCRTPoly "Ciphertext<DCRTPoly>":
pass
cdef extern from "ckks_wrapper.h" namespace "pycrypto":
cdef cppclass CiphertextInterfaceType:
...
或
cdef extern from "ckks_wrapper.cpp":
pass
cdef extern from "ckks_wrapper.h" namespace "pycrypto":
cdef extern from "ckks_wrapper.hpp" namespace "libcrypto":
cdef cppclass CiphertextDCRTPoly "Ciphertext<DCRTPoly>":
pass
cdef cppclass CiphertextInterfaceType:
...
CiphertextInterfaceType(CiphertextDCRTPoly) except +
...
我倾向于第二个,因为如果不是,我不知道如何在CiphertextInterfaceType
中使用“CiphertextInterfaceType
”
发布于 2021-09-09 22:44:54
首先,您不需要将它声明为私有变量。这样做是没有办法的,也没有目的的。根据注释中的建议,您可能可以使用双下划线(__x
),而不是Python: Cython无法生成可以访问私有变量的C++代码,因此这里没有任何值。
一般情况下,Cython只需要了解要访问的公共接口的各个部分。因此,如果您不需要其他函数,您也可以愉快地省略它们。不过,我想你会这么做的。
Cython确实有使用方括号的对C++模板的几点认识。这里有一些限制,但您可以涵盖大多数代码:
cdef extern from "somewhere.hpp" namespace "libcrypto":
cdef cppclass Ciphertext[T]
pass
cdef cppclass DCRTPoly:
pass
然后按照您的预期声明构造函数和函数,使用CiphertextInterfaceType[DCRTPoly]
作为参数。
或者,您可以跳过使用模板,只需使用Cython的"cname“特征,您可以将其命名为Cython包装器,但在生成代码时为它提供一个可供使用的替代名称:
cdef extern from "something.hpp" namespace "libcrypto":
cdef cppclass CiphertextDCRTPoly "Ciphertext<DCRTPoly>":
pass
如果Ciphertext<DCRTPoly>
是您计划使用的模板的唯一版本,这可能是有意义的。
https://stackoverflow.com/questions/69124758
复制相似问题