コード例 #1
0
def test_batchencoder(ctx):
    poly_modulus_degree = helper_poly_modulus_degree(ctx)
    enc_out = sealapi.Plaintext()

    testcase = [1, 2, 3, 4, 5]
    encoder = sealapi.BatchEncoder(ctx)
    encoder.encode(testcase, enc_out)
    assert enc_out.int_array().size() == poly_modulus_degree
    assert encoder.decode_uint64(enc_out)[:len(testcase)] == testcase
    assert encoder.decode_int64(enc_out)[:len(testcase)] == testcase

    test_str = "7FFx^3 + 1x^1 + 3"
    testcase = sealapi.Plaintext(test_str)
    encoder.encode(testcase)
    assert testcase.to_string() != test_str
    encoder.decode(testcase)
    assert testcase.to_string() == test_str
コード例 #2
0
def test_evaluator_mod_switch():
    poly_modulus_degree = 8192
    plain_modulus = 1032193

    ctx = helper_context_bfv(poly_modulus_degree, plain_modulus)

    batchenc = sealapi.BatchEncoder(ctx)
    keygen = sealapi.KeyGenerator(ctx)

    public_key = sealapi.PublicKey()
    keygen.create_public_key(public_key)
    secret_key = keygen.secret_key()

    decryptor = sealapi.Decryptor(ctx, secret_key)
    encryptor = sealapi.Encryptor(ctx, public_key)

    evaluator = sealapi.Evaluator(ctx)

    # cphertext mod switch to next
    expected_value = [1, 2, 3, 4, 5]
    plain = sealapi.Plaintext()
    batchenc.encode(expected_value, plain)

    out = sealapi.Plaintext()
    enc = sealapi.Ciphertext(ctx)

    encryptor.encrypt(plain, enc)

    before = decryptor.invariant_noise_budget(enc)
    evaluator.mod_switch_to_next_inplace(enc)
    after = decryptor.invariant_noise_budget(enc)
    assert before > after
    decryptor.decrypt(enc, out)
    assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value

    # ciphertext mod switch to next
    expected_value = [1, 2, 3, 4, 5]
    plain = sealapi.Plaintext()
    batchenc.encode(expected_value, plain)

    out = sealapi.Plaintext()
    enc = sealapi.Ciphertext(ctx)
    cout = sealapi.Ciphertext(ctx)

    encryptor.encrypt(plain, enc)

    before = decryptor.invariant_noise_budget(enc)
    evaluator.mod_switch_to_next(enc, cout)
    after = decryptor.invariant_noise_budget(cout)
    assert before > after
    decryptor.decrypt(cout, out)
    assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value

    # cphertext mod switch to inplace
    parms_id = ctx.last_parms_id()
    expected_value = [1, 2, 3, 4, 5]
    plain = sealapi.Plaintext()
    batchenc.encode(expected_value, plain)

    out = sealapi.Plaintext()
    enc = sealapi.Ciphertext(ctx)
    cout = sealapi.Ciphertext(ctx)

    encryptor.encrypt(plain, enc)

    before = decryptor.invariant_noise_budget(enc)
    evaluator.mod_switch_to_inplace(enc, parms_id)
    after = decryptor.invariant_noise_budget(enc)
    assert before > after
    decryptor.decrypt(enc, out)
    assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value
    assert enc.parms_id() == parms_id

    # ciphertext mod switch to
    parms_id = ctx.last_parms_id()
    expected_value = [1, 2, 3, 4, 5]
    plain = sealapi.Plaintext()
    batchenc.encode(expected_value, plain)

    out = sealapi.Plaintext()
    enc = sealapi.Ciphertext(ctx)
    cout = sealapi.Ciphertext(ctx)

    encryptor.encrypt(plain, enc)

    before = decryptor.invariant_noise_budget(enc)
    evaluator.mod_switch_to(enc, parms_id, cout)
    after = decryptor.invariant_noise_budget(cout)
    assert before > after
    decryptor.decrypt(cout, out)
    assert batchenc.decode_int64(out)[:len(expected_value)] == expected_value
    assert cout.parms_id() == parms_id

    pol_str = "1x^3 + 1x^1 + 3"
    # plaintext mod switch to next inplace
    plain = sealapi.Plaintext(pol_str)
    evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id())
    assert plain.is_ntt_form() is True
    evaluator.mod_switch_to_next_inplace(plain)
    assert plain.parms_id() != ctx.first_parms_id()

    # plaintext mod switch to next inplace failure
    plain = sealapi.Plaintext(pol_str)
    evaluator.transform_to_ntt_inplace(plain, ctx.last_parms_id())
    assert plain.is_ntt_form() is True
    with pytest.raises(BaseException):
        evaluator.mod_switch_to_next_inplace(plain)

    # plaintext mod switch to inplace
    plain = sealapi.Plaintext(pol_str)
    evaluator.transform_to_ntt_inplace(plain, ctx.first_parms_id())
    assert plain.is_ntt_form() is True
    evaluator.mod_switch_to_inplace(plain, ctx.last_parms_id())
    assert plain.parms_id() == ctx.last_parms_id()

    # plaintext mod switch to next
    plain = sealapi.Plaintext(pol_str)
    plain_out = sealapi.Plaintext()

    evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out)
    assert plain_out.is_ntt_form() is True

    plain_next = sealapi.Plaintext()
    evaluator.mod_switch_to_next(plain_out, plain_next)
    assert plain_out.parms_id() == ctx.first_parms_id()
    assert plain_next.parms_id() != ctx.first_parms_id()

    # plaintext mod switch to
    plain = sealapi.Plaintext(pol_str)
    plain_out = sealapi.Plaintext()

    evaluator.transform_to_ntt(plain, ctx.first_parms_id(), plain_out)
    assert plain_out.is_ntt_form() is True

    plain_next = sealapi.Plaintext()
    evaluator.mod_switch_to(plain_out, ctx.last_parms_id(), plain_next)
    assert plain_out.parms_id() == ctx.first_parms_id()
    assert plain_next.parms_id() == ctx.last_parms_id()
コード例 #3
0
def test_evaluator_rotate_bfv():
    parms = sealapi.EncryptionParameters(sealapi.SCHEME_TYPE.BFV)
    parms.set_poly_modulus_degree(8)
    parms.set_plain_modulus(257)

    coeff = sealapi.CoeffModulus.Create(8, [40, 40])
    parms.set_coeff_modulus(coeff)
    ctx = sealapi.SEALContext(parms, False, sealapi.SEC_LEVEL_TYPE.NONE)

    keygen = sealapi.KeyGenerator(ctx)
    galois_keys = sealapi.GaloisKeys()
    keygen.create_galois_keys(galois_keys)
    public_key = sealapi.PublicKey()
    keygen.create_public_key(public_key)

    decryptor = sealapi.Decryptor(ctx, keygen.secret_key())
    encryptor = sealapi.Encryptor(ctx, public_key)

    evaluator = sealapi.Evaluator(ctx)
    encoder = sealapi.BatchEncoder(ctx)

    testcase = [1, 2, 3, 4, 5, 6, 7, 8]
    # Input
    # 1, 2, 3, 4,
    # 5, 6, 7, 8

    plain = sealapi.Plaintext()
    encoder.encode(testcase, plain)

    encrypted = sealapi.Ciphertext(ctx)
    encryptor.encrypt(plain, encrypted)

    evaluator.rotate_columns_inplace(encrypted, galois_keys)
    decryptor.decrypt(encrypted, plain)
    # Rotate columns
    # 5, 6, 7, 8,
    # 1, 2, 3, 4

    assert encoder.decode_int64(plain) == [5, 6, 7, 8, 1, 2, 3, 4]

    evaluator.rotate_rows_inplace(encrypted, -1, galois_keys)
    decryptor.decrypt(encrypted, plain)
    # Shift rows -1
    # 8, 5, 6, 7,
    # 4, 1, 2, 3,
    assert encoder.decode_int64(plain) == [8, 5, 6, 7, 4, 1, 2, 3]

    cout = sealapi.Ciphertext(ctx)
    evaluator.rotate_rows(encrypted, 2, galois_keys, cout)
    decryptor.decrypt(cout, plain)
    # Shift rows +2
    # 6, 7, 8, 5,
    # 2, 3, 4, 1,
    assert encoder.decode_int64(plain) == [6, 7, 8, 5, 2, 3, 4, 1]

    evaluator.rotate_columns(cout, galois_keys, encrypted)
    decryptor.decrypt(encrypted, plain)
    # Rotate columns
    # 2, 3, 4, 1,
    # 6, 7, 8, 5,
    assert encoder.decode_int64(plain) == [2, 3, 4, 1, 6, 7, 8, 5]
コード例 #4
0
def test_encryptor_bfv():
    batch = [1, 2, 3, 4, 5]
    poly_modulus_degree = 8192
    plain_modulus = 1032193

    ctx = helper_context_bfv(poly_modulus_degree, plain_modulus)

    keygen = sealapi.KeyGenerator(ctx)
    batchenc = sealapi.BatchEncoder(ctx)

    public_key = sealapi.PublicKey()
    keygen.create_public_key(public_key)

    secret_key = keygen.secret_key()

    decryptor = sealapi.Decryptor(ctx, secret_key)

    plaintext = sealapi.Plaintext()
    batchenc.encode(batch, plaintext)

    def _test_encryptor_symmetric_setup(encryptor):
        # encrypt symmetric
        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_symmetric(plaintext, ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == batch
        plaintext_out.set_zero()

        def save_load(path):
            serial = encryptor.encrypt_symmetric(plaintext)
            serial.save(path)
            assert Path(path).stat().st_size > 0

        tmp_file(save_load)
        plaintext_out.set_zero()

        # zero symmetric
        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_zero_symmetric(ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch)
        plaintext_out.set_zero()

        # zero symmetric parms_id
        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_zero_symmetric(ctx.last_parms_id(), ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch)
        plaintext_out.set_zero()

    def _test_encryptor_pk_setup(encryptor):
        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt(plaintext, ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == batch
        plaintext_out.set_zero()

        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_zero(ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch)
        plaintext_out.set_zero()

        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_zero(ctx.last_parms_id(), ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert batchenc.decode_int64(plaintext_out)[: len(batch)] == [0] * len(batch)
        plaintext_out.set_zero()

    encryptor = sealapi.Encryptor(ctx, public_key)
    _test_encryptor_pk_setup(encryptor)

    encryptor = sealapi.Encryptor(ctx, public_key, secret_key)
    _test_encryptor_symmetric_setup(encryptor)
    _test_encryptor_pk_setup(encryptor)

    encryptor = sealapi.Encryptor(ctx, secret_key)
    _test_encryptor_symmetric_setup(encryptor)

    encryptor = sealapi.Encryptor(ctx, public_key)
    encryptor.set_secret_key(secret_key)
    _test_encryptor_pk_setup(encryptor)
    _test_encryptor_symmetric_setup(encryptor)

    encryptor = sealapi.Encryptor(ctx, secret_key)
    encryptor.set_public_key(public_key)
    _test_encryptor_pk_setup(encryptor)
    _test_encryptor_symmetric_setup(encryptor)