ethereum / py_ecc

Python implementation of ECC pairing and bn_128 and bls12_381 curve operations
MIT License
191 stars 82 forks source link

stackoverflow while computing paring() on curve bn128 #120

Closed xingzhibo2019 closed 3 years ago

xingzhibo2019 commented 3 years ago

when compute paring(G2, G1), the process gives me "Process finished with exit code -1073741571 (0xC00000FD)", even i've set "threading.stack_size(200000000)", which doesn't work. here's my test code:

import py_ecc.bn128.bn128_curve as curve import py_ecc.bn128.bn128_pairing as pairing

def test(): Z = pairing.pairing(curve.G2, curve.G1) print(Z) return

if name == 'main': threading.stack_size(200000000) thread = threading.Thread(target=test()) thread.start()

a reasonable guess is that when computing pow()(which is in py_ecc/fields/field_properties.py) , the huge integer make the depth of stack too deep, which finally overflowed. but i still don't know how can i make it works.

Devil-fire commented 3 years ago

You can try to modify the pow (), as follows

    def __pow__(self, other):
        return self.pow(other)

    def pow(self, other):
        if other == 0:
            return self.__class__([1] + [0] * (self.degree - 1))
        elif other == 1:
            return self.__class__(self.coeffs)
        elif other % 2 == 0:
            base = self * self
            power = other // 2
            return (base).pow(power)
        else:
            base = self * self
            power = int(other // 2)
            return ((base).pow(power)) * self
xingzhibo2019 commented 3 years ago

You can try to modify the pow (), as follows

    def __pow__(self, other):
        return self.pow(other)

    def pow(self, other):
        if other == 0:
            return self.__class__([1] + [0] * (self.degree - 1))
        elif other == 1:
            return self.__class__(self.coeffs)
        elif other % 2 == 0:
            base = self * self
            power = other // 2
            return (base).pow(power)
        else:
            base = self * self
            power = int(other // 2)
            return ((base).pow(power)) * self

thx!!! it works!!!

1zx999 commented 1 year ago

I had the same problem as you,i find the pow () function at py_ecc/fields/field_elements.py and i modify it ,but it still don't work。