Infinity point how it defined - Secp256K Curve

asked 2021-03-14 14:58:29 +0100

Miroslaw gravatar image

updated 2021-03-14 17:49:11 +0100

slelievre gravatar image

I have problem with point infinity. How to solve it?

Curve Secp256k

# A Python3 program to check if a given point
# lies inside a given polygon
#  
# for explanation of functions onSegment(),
# orientation() and doIntersect()

# Define Infinity (Using INT_MAX
# caused overflow problems)
INT_MAX = 9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

def onSegment(p: tuple, q: tuple, r: tuple) -> bool:
    r"""
    Check whether point q lies on line segment 'pr'
    """
    if (q[0] <= max(p[0], r[0]) and
        q[0] >= min(p[0], r[0]) and
        q[1] <= max(p[1], r[1]) and
        q[1] >= min(p[1], r[1])):
        return True
    return False

def orientation(p: tuple, q: tuple, r: tuple) -> int:
    r"""
    Return the orientation of the ordered triple (p, q, r).

    The orientation is encoded as:

    - 0 if p, q and r are collinear
    - 1 if they turn clockwise
    - 2 if they turn counterclockwise
    """
    val = ((q[1] - p[1]) * (r[0] - q[0])
           - (q[0] - p[0]) * (r[1] - q[1]))
    if val == 0:
        return 0
    if val > 0:
        return 1
    return 2

def doIntersect(p1, q1, p2, q2):
    # Find the four orientations needed for
    # general and special cases
    o1 = orientation(p1, q1, p2)
    o2 = orientation(p1, q1, q2)
    o3 = orientation(p2, q2, p1)
    o4 = orientation(p2, q2, q1)

    # General case
    if o1 != o2 and o3 != o4:
        return True

    # Special cases
    # p1, q1 and p2 are collinear and
    # p2 lies on segment p1q1
    if o1 == 0 and onSegment(p1, p2, q1):
        return True

    # p1, q1 and p2 are collinear and
    # q2 lies on segment p1q1
    if o2 == 0 and onSegment(p1, q2, q1):
        return True

    # p2, q2 and p1 are collinear and
    # p1 lies on segment p2q2
    if o3 == 0 and onSegment(p2, p1, q2):
        return True

    # p2, q2 and q1 are collinear and
    # q1 lies on segment p2q2
    if o4 == 0 and onSegment(p2, q1, q2):
        return True

    return False

def is_inside_polygon(points: list, p: tuple) -> bool:
    r"""
    Check whether the point ``p`` lies inside
    the polygon defined by ``points``.
    """
    n = len(points)

    # There must be at least 3 vertices
    # in polygon
    if n < 3:
        return False

    # Create a point for line segment
    # from p to infinity - how to define this point?

    extreme = (INT_MAX, p[1])
    count = i = 0

    while True:
        next = (i + 1) % n

        # Check if the line segment from 'p' to
        # 'extreme' intersects with the line
        # segment from 'polygon[i]' to 'polygon[next]'
        if doIntersect(points[i],
                       points[next],
                       p, extreme):

            # If the point 'p' is collinear with line
            # segment 'i-next', then check if it lies
            # on segment. If it lies, return true, otherwise false
            if orientation(points[i], p,
                           points[next]) == 0:
                return onSegment(points[i], p,
                                 points[next])

            count += 1

        i = next

        if i == 0:
            break

    # Return True if count is odd, False otherwise
    return count % 2 == 1

# Driver code
if __name__ == '__main__':
    # i: 1
    x = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
    y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
    # i: 2
    x2 = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
    y2 = 0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a
    # i: 3
    x3 = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9
    y3 = 0x388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672
    # i: 4
    x4 = 0xe493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13
    y4 = 0x51ed993ea0d455b75642e2098ea51448d967ae33bfbdfe40cfe97bdc47739922
    # i: 5
    x5 = 0x2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4
    y5 = 0xd8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6
    # i: 6
    x6 = 0xfff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556
    y6 = 0xae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297

    polygon1 = [(x, y), (x2, y2), (x4, y4), (x5, x5), (x6, y6)]

    p = (x3, y3)
    if is_inside_polygon(points=polygon1, p=p):
        print ('Yes')
    else:
        print ('No')
edit retag flag offensive close merge delete

Comments

I think your function onSegment checks whether q is in the rectangle with corners p and r and horizontal and vertical sides; not whether it is on the segment from p to r.

slelievre gravatar imageslelievre ( 2021-03-14 18:01:59 +0100 )edit