点标量乘法python 2.7 信息泄露

point scalar multiplication python 2.7 information leak

我正在尝试实现一个点标量 double 并添加不会泄漏的乘法函数,并且任何可能在恒定时间内执行的信息。不过,不泄露信息才是最重要的。

我想请人帮我解决一下下面的代码,以免泄露信息。 我注意到对于不同的标量大小,函数需要不同的时间来计算这是不安全的。

有什么帮助吗?请

def point_scalar_multiplication_double_and_add(a, b, p, x, y, scalar):
"""
Implement Point multiplication with a scalar:
    r * (x, y) = (x, y) + ... + (x, y)    (r times)

Reminder of Double and Multiply algorithm: r * P
    Q = Inf
    for i = 0 to num_bits(P)-1
        if bit i of P == 1 then
            Q = Q + P
        P = 2 * P
    return Q
"""
Q = (None, None)
P = (x, y)
binary = bin(scalar)

for i in range(scalar.num_bits()):

    if binary[scalar.num_bits()-i+1] == '1':
        Q = point_add(a, b, p, Q[0], Q[1], P[0], P[1])
        #print Q
        pass
    P = point_double(a, b, p, P[0],P[1])
    pass
return Q

试试这个。我 hard-coded 位计数以避免基于标量值的泄漏,并尝试平衡指令,无论是否采取行动。

我找不到 'num_bits' 文档,所以我假设您的标量是某种对象。请将 NUM_BITS 常量调整为标量值的最大大小(至少是您打算使用的最大值)。

我确实在网上找到了一些提到 numbits 的地方,它指的是 active 位的数量,如果你使用它,它往往会泄漏数据(例如,标量(1).num_bits() != 标量(1000).num_bits()).

已编辑:

def point_scalar_multiplication_double_and_add(a, b, p, x, y, scalar):
    """
    Implement Point multiplication with a scalar:
        r * (x, y) = (x, y) + ... + (x, y)    (r times)

    Reminder of Double and Multiply algorithm: r * P
        Q = Inf
        for i = 0 to num_bits(P)-1
            if bit i of P == 1 then
                Q = Q + P
            P = 2 * P
        return Q
    """
    Q = (None, None)
    Q2 = Q
    P = (x, y)
    binary = bin(scalar)
    binary = binary[2:]   # get rid of 0b

    NUM_BITS = 64
    # pre-pad binary with 0s - 1010 becomes 0000000...00001010
    binary = '0' * (NUM_BITS - len(binary)) + binary

    # reverse binary and iterate over bits
    for b in binary[::-1]:
        Q2 = point_add(a, b, p, Q[0], Q[1], P[0], P[1])
        if b == '1':
            Q = Q2
        else:
            Q2 = Q  # Useless, but balances instruction count

        P = point_double(a, b, p, P[0],P[1])

    return Q