factor.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:PaperImpl-2017-DirtyPeriodFinding 作者: Strilanc 项目源码 文件源码
def period_to_factors(base, modulus, period):
    half_period = period // 2
    x = pow(base, half_period, modulus)
    if x**2 % modulus != 1 or x == 1 or x == -1 % modulus:
        return None

    # (x + 1) * (x - 1) = x^2 - 1^2 = 1 - 1 = 0
    # therefore (x+1)*(x-1) * k = k * modulus
    # but (x+1) and (x-1) can't be multiples of modulus
    # therefore they contain factors

    factors_1 = fractions.gcd(x + 1, modulus)
    factors_2 = fractions.gcd(x - 1, modulus)

    assert factors_1 * factors_2 == modulus
    return sorted([factors_1, factors_2])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号