对用户定义类型项的矩阵执行 Numpy 运算

发布于 2024-12-05 16:29:49 字数 1360 浏览 2 评论 0原文

在我的密码学项目中,我必须处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-12-12 16:29:49

a声明为dtype object

a = np.array([(GF28(10), GF28(20), GF28(30)),
              (GF28(12), GF28(21), GF28(4)),
              (GF28(9), GF28(16), GF28(13))],dtype='object')

则数组中元素的类型为GF28。例如:

type(a[0,0])
# <class '__main__.GF28'>

Declare a to be of dtype object:

a = np.array([(GF28(10), GF28(20), GF28(30)),
              (GF28(12), GF28(21), GF28(4)),
              (GF28(9), GF28(16), GF28(13))],dtype='object')

Then the type of the elements in the array are GF28. For example:

type(a[0,0])
# <class '__main__.GF28'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文