예제 #1
0
def _lagrange_coefficient(i, idxs):
    numerator = Scalar(1)
    denominator = Scalar(1)
    for j in idxs:
        if j != i:
            numerator *= j
            denominator *= j - i
    return numerator / denominator
예제 #2
0
def recover(indexed_shares):
    """ Takes EXACTLY t (idx, decrypted_share) tuples and performs Langrange interpolation to recover the secret S.
        The validity of the decrypted shares has to be verified prior to a call of this function.
    """
    idxs = [Scalar(idx) for idx, _ in indexed_shares]
    idx, share = indexed_shares[0]
    rec = share * _lagrange_coefficient(Scalar(idx), idxs)
    for idx, share in indexed_shares[1:]:
        rec += share * _lagrange_coefficient(Scalar(idx), idxs)
    return rec
예제 #3
0
def _random_codeword(num_nodes: int, recovery_threshold: int) -> List[Scalar]:
    f = Polynomial.random(num_nodes - recovery_threshold - 1)
    codeword = []
    for i in range(1, num_nodes + 1):
        # vi's could be precomputed given n and t
        vi = Scalar(1)
        for j in range(1, num_nodes + 1):
            if j != i:
                vi *= Scalar((i - j) % GROUP_ORDER)
        vi.invert()
        codeword.append(vi * f(i))
    return codeword
예제 #4
0
def test_share_verification_invalid_challenge():
    commitments, challenge, responses = proof.commitments, proof.challenge, proof.responses
    challenge = challenge + Scalar(1)
    assert not verify_shares(
        encrypted_shares,
        ShareCorrectnessProof(commitments, challenge, responses), public_keys,
        RECOVERY_THRESHOLD)
예제 #5
0
 def __call__(self, arg: int) -> Scalar:
     x = Scalar(arg)
     result = self.coeffs[0] + (self.coeffs[1] * x)
     x_pow = copy(x)
     for i in range(2, len(self.coeffs)):
         x_pow *= x
         result += self.coeffs[i] * x_pow
     return result
예제 #6
0
def test_dleq_invalid_challenge():
    α = Scalar.random()
    e, z = _DLEQ_prove(G, G * α, H, H * α, α)
    e += Scalar(1)
    assert not _DLEQ_verify(G, G * α, H, H * α, e, z)
예제 #7
0
def test_verify_invalid_secret():
    commitments = proof.commitments
    assert not verify_secret(secret + Scalar(1), commitments,
                             RECOVERY_THRESHOLD)
예제 #8
0
def test_multiply_inplace():
    X = copy(B)
    X *= Scalar(3)
    assert X == B + B + B
예제 #9
0
def test_multipy_flipped_order():
    assert B * Scalar(17) == Scalar(17) * B
예제 #10
0
def test_double_inplace():
    B2 = copy(B)
    B2 += B2
    assert B2 == B * Scalar(2)
예제 #11
0
def test_double_eq_times_2():
    assert B + B == B * Scalar(2)
예제 #12
0
def test_base_times_zero_fails():
    with pytest.raises(ValueError):
        Point.base_times(Scalar(0))
예제 #13
0
def test_multiply_by_zero_fails():
    with pytest.raises(ValueError):
        B * Scalar(0)
예제 #14
0
def test_base_multiply_by_one():
    assert B == Point.base_times(Scalar(1))