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
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
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
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)
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
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)
def test_verify_invalid_secret(): commitments = proof.commitments assert not verify_secret(secret + Scalar(1), commitments, RECOVERY_THRESHOLD)
def test_multiply_inplace(): X = copy(B) X *= Scalar(3) assert X == B + B + B
def test_multipy_flipped_order(): assert B * Scalar(17) == Scalar(17) * B
def test_double_inplace(): B2 = copy(B) B2 += B2 assert B2 == B * Scalar(2)
def test_double_eq_times_2(): assert B + B == B * Scalar(2)
def test_base_times_zero_fails(): with pytest.raises(ValueError): Point.base_times(Scalar(0))
def test_multiply_by_zero_fails(): with pytest.raises(ValueError): B * Scalar(0)
def test_base_multiply_by_one(): assert B == Point.base_times(Scalar(1))