对用户定义类型项的矩阵执行 Numpy 运算
在我的密码学项目中,我必须处理 GF(2^8) 算术运算。本质上,这个this字段中的求和运算(GF(2^8))是正常的异或运算,乘积运算a*b是(a*b) mod m(x)。你可以跳过这个数学部分。
我的工作是在 GF(2^8) 中反转矩阵 4x4 这并不困难,但是手动重新实现每个矩阵运算太繁琐了,我想在 numpy 中使用此功能(反转矩阵),但是。我是无法使 numpy 在矩阵的项目之间使用我的操作。比如说,我希望它在使用“+”操作时执行 XOR,而不是使用“+”在两个项目之间执行添加
。类型,调用 GF28。代码可以在下面找到:
class GF28(float):
def __init__(self, value):
self.value = value
def __add__(self, b):
if isinstance(b, GF28):
return GF28(ops.add(self.value, b.value))
else:
return GF28(ops.add(self.value, b))
__rsub__ = __isub__ = __sub__ = __radd__ = __iadd__ = __add__
def __mul__(self, b):
if isinstance(b, GF28):
return GF28(ops.mul(self.value, b.value))
else:
return GF28(ops.mul(self.value, b))
__rmul__ = __imul__ = __mul__
def __div__(self, b):
return self.__mul__(self.inv(b))
__rdiv__ = __idiv__ = __div__
def inv(self):
return GF28(ops.inv(self.value))
def __float__(self):
return self
GF28 项的矩阵:
a = array([[GF28(10), GF28(20), GF28(30)],
[GF28(12), GF28(21), GF28(4)],
[GF28(9), GF28(16), GF28(13)]])
看来只使用了 float() 转换函数,其余部分根本没有触及。
然后我使用以下方法创建这些 使此操作覆盖非常感谢。
[编辑]如果有人可以通过矩阵求逆来完成这项工作,我将非常感激。谢谢!
In my cryptography project, I have to deal with GF(2^8) arithmetic operations. Essentially, the sum operation in this this field (GF(2^8) is normal XOR operation, and the product operation a*b is (a*b) mod m(x). You can skip this mathematics part.
My job is to inverse a matrix 4x4 in GF(2^8). This is not difficult, but it's too tedious to re-implement every matrix operations by hand. I would like to make use of this functionality (inverse a matrix) in numpy, but I was not able to make numpy use my operations between items of a matrix. Say, instead of performing adding between 2 items using '+', I want it to perform XOR whenever '+' operation is used.
I have created a class of my customer type, call GF28. The code can be found below:
class GF28(float):
def __init__(self, value):
self.value = value
def __add__(self, b):
if isinstance(b, GF28):
return GF28(ops.add(self.value, b.value))
else:
return GF28(ops.add(self.value, b))
__rsub__ = __isub__ = __sub__ = __radd__ = __iadd__ = __add__
def __mul__(self, b):
if isinstance(b, GF28):
return GF28(ops.mul(self.value, b.value))
else:
return GF28(ops.mul(self.value, b))
__rmul__ = __imul__ = __mul__
def __div__(self, b):
return self.__mul__(self.inv(b))
__rdiv__ = __idiv__ = __div__
def inv(self):
return GF28(ops.inv(self.value))
def __float__(self):
return self
And then I create a matrix of these GF28 items using:
a = array([[GF28(10), GF28(20), GF28(30)],
[GF28(12), GF28(21), GF28(4)],
[GF28(9), GF28(16), GF28(13)]])
It appears that only the float() cast function is being used, the rest is not touched at all.
Please help me to make this operation overriding work. Thanks a lot.
[edit] Performing basic matrix operation is already ok. If somebody could make this work with matrix inversion, I would really appreciate it. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
a
声明为dtype
object
:则数组中元素的类型为
GF28
。例如:Declare
a
to be ofdtype
object
:Then the type of the elements in the array are
GF28
. For example: