Ask Your Question

Revision history [back]

I can confirm the bug and give a minimal form of it.

The following produces a segfault using Sage 7.3 or Sage 7.4 built from source on OS X 10.10.5 "Yosemite".

sage: F = GF(631)
sage: (x + F(2))/(x + F(1))
(x + 2)/(x + 1)
sage: x, y = SR.var('x y')
sage: num = F(62)*x + y + F(232)
sage: den = x + F(14)
sage: num / den
[1]    ... segmentation fault  sage

I can confirm the bug and give a minimal form of it.

The following produces a segfault using Sage 7.3 or Sage 7.4 built from source on OS X 10.10.5 "Yosemite".

sage: F = GF(631)
sage: (x + F(2))/(x + F(1))
(x + 2)/(x + 1)
sage: x, y = SR.var('x y')
sage: num = F(62)*x + y + F(232)
sage: num, den = F(6)*x + F(8), x + F(14)
sage: num / den
[1]    ... segmentation fault  sage
...

I can confirm the bug and give a minimal form of it.

The following produces a segfault using Sage 7.3 or Sage 7.4 built from source on OS X 10.10.5 "Yosemite".

sage: F = GF(631)
GF(3)
sage: num, den = F(6)*x F(2)*x + F(8), x + F(14)
F(1), x
sage: num / den
num/den
[1]    ... segmentation fault  ...

I can confirm the bug and give segmentation fault bug.

Please find below

  • a fix for your code,
  • a minimal form example triggering the segmentation fault.

Better working Weil Pairing code

Replace the definition of it.

x, y by the following:

R.<x, y> = GF(p)[]

Then everything will work nicely.

For instance, define the setup as follows.

p = 631

F = GF(p)
R.<x,y> = F[]

a = 30
b = 34

E = EllipticCurve(F, [a, b])

P = E((36, 60))
Q = E((121, 387))
S = E((0, 36))

n = 5

def g(P, Q):
    (x_P, y_P) = P.xy()
    (x_Q, y_Q) = Q.xy()
    if x_P == x_Q and y_P + y_Q == 0:
        return x - x_P
    if P == Q:
        slope = (3 * x_P^2 + a)/(2 * y_P)
    else:
        slope = (y_P - y_Q)/(x_P - x_Q)
    return (y - y_P - slope * (x - x_P))/(x + x_P + x_Q - slope^2)

def miller(m, P):
    m = bin(m)[3:]
    n = len(m)
    T = P
    f = 1
    for i in range(n):
        f = f^2 * g(T, T)
        T = T + T
        if int(m[i]) == 1:
            f = f * g(T, P)
            T = T + P
    return f

def eval_miller(P, Q):
    f = miller(n, P)
    (x1, y1) = Q.xy()
    return f(x = x1, y = y1)

def weil_pairing(P, Q, S):
    num = eval_miller(P, Q+S)/eval_miller(P,  S)
    den = eval_miller(Q, P-S)/eval_miller(Q, -S)
    return (num/den)

Then the following works as expected:

sage: e = weil_pairing(P, Q, S)
sage: print "e(P, Q) =", e
e(P, Q) = 242
sage: print "e(P, Q)^n =", e^n
e(P, Q)^n = 1
sage: P3 = P * 3
sage: Q4 = Q * 4
sage: e12 = weil_pairing(P3, Q4, S)
sage: print "[3]P =", P3.xy()
[3]P = (617, 5)
sage: print "[4]Q =", Q4.xy()
[4]Q = (121, 244)
sage: print "e([3]P, [4]Q) =", e12
e([3]P, [4]Q) = -119
sage: print "e(P, Q)^12 =", e^12
e(P, Q)^12 = -119

Minimal example for the segmentation fault

The following produces a segfault using Sage 7.3 or Sage 7.4 built from source on OS X 10.10.5 "Yosemite".

sage: F = GF(3)
sage: num, den = F(2)*x + F(1), x
sage: num/den
[1]    ... segmentation fault  ...