Exemplo n.º 1
0
def test_intencoder(ctx):
    encoder = sealapi.IntegerEncoder(ctx)

    enc = encoder.encode(5)
    assert enc.to_string() == "1x^2 + 1"
    assert enc.int_array()[2] == 1
    assert encoder.decode_uint32(enc) == 5
    assert encoder.decode_int32(enc) == 5
    assert encoder.decode_uint64(enc) == 5
    assert encoder.decode_int32(enc) == 5

    enc = sealapi.Plaintext()
    encoder.encode(7, enc)
    assert enc.to_string() == "1x^2 + 1x^1 + 1"

    big = sealapi.BigUInt("5555555555")
    enc = encoder.encode(big)
    assert enc.int_array()[0] == 1
    assert encoder.decode_biguint(enc) == 0x5555555555

    enc = sealapi.Plaintext()
    encoder.encode(big + 1, enc)
    assert encoder.decode_biguint(enc) == 0x5555555556
Exemplo n.º 2
0
def test_decryptor():
    poly_modulus_degree = 4096
    plain_modulus = 1024
    ctx = helper_context_bfv(poly_modulus_degree, plain_modulus)

    keygen = sealapi.KeyGenerator(ctx)
    intenc = sealapi.IntegerEncoder(ctx)
    public_key = keygen.public_key()
    secret_key = keygen.secret_key()

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

    expected_value = 1234
    plaintext = intenc.encode(expected_value)

    ciphertext = sealapi.Ciphertext(ctx)
    encryptor.encrypt(plaintext, ciphertext)
    plaintext_out = sealapi.Plaintext()

    assert decryptor.invariant_noise_budget(ciphertext) > 0

    decryptor.decrypt(ciphertext, plaintext_out)
    assert intenc.decode_int64(plaintext_out) == expected_value
Exemplo n.º 3
0
def test_encryptor_bfv():
    poly_modulus_degree = 4096
    plain_modulus = 1024
    ctx = helper_context_bfv(poly_modulus_degree, plain_modulus)

    keygen = sealapi.KeyGenerator(ctx)
    intenc = sealapi.IntegerEncoder(ctx)
    public_key = keygen.public_key()
    secret_key = keygen.secret_key()

    decryptor = sealapi.Decryptor(ctx, secret_key)

    expected_value = 1234
    plaintext = intenc.encode(expected_value)

    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 intenc.decode_int64(plaintext_out) == expected_value

        tmp = NamedTemporaryFile()
        serial = encryptor.encrypt_symmetric(plaintext)
        serial.save(tmp.name)
        assert Path(tmp.name).stat().st_size > 0

        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 intenc.decode_int64(plaintext_out) == 0

        tmp = NamedTemporaryFile()
        serial = encryptor.encrypt_zero_symmetric()
        serial.save(tmp.name)
        assert Path(tmp.name).stat().st_size > 0
        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 intenc.decode_int64(plaintext_out) == 0

        tmp = NamedTemporaryFile()
        serial = encryptor.encrypt_zero_symmetric(ctx.last_parms_id())
        serial.save(tmp.name)
        assert Path(tmp.name).stat().st_size > 0

    def _test_encryptor_pk_setup(encryptor):
        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt(plaintext, ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert intenc.decode_int64(plaintext_out) == expected_value
        plaintext_out.set_zero()

        ciphertext = sealapi.Ciphertext(ctx)
        encryptor.encrypt_zero(ciphertext)
        plaintext_out = sealapi.Plaintext()
        decryptor.decrypt(ciphertext, plaintext_out)
        assert intenc.decode_int64(plaintext_out) == 0
        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 intenc.decode_int64(plaintext_out) == 0
        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)
Exemplo n.º 4
0
def test_evaluator_mod_switch():
    scheme = sealapi.SCHEME_TYPE.BFV
    parms = sealapi.EncryptionParameters(scheme)
    parms.set_poly_modulus_degree(128)
    parms.set_plain_modulus(1 << 6)
    coeff = sealapi.CoeffModulus.Create(128, [30, 30, 30])
    parms.set_coeff_modulus(coeff)
    ctx = sealapi.SEALContext.Create(parms, True, sealapi.SEC_LEVEL_TYPE.NONE)

    intenc = sealapi.IntegerEncoder(ctx)
    keygen = sealapi.KeyGenerator(ctx)
    public_key = keygen.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 = 1234
    plain = intenc.encode(expected_value)
    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 intenc.decode_int64(out) == expected_value

    # ciphertext mod switch to next
    plain = intenc.encode(expected_value)
    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 intenc.decode_int64(out) == expected_value

    # cphertext mod switch to inplace
    parms_id = ctx.last_parms_id()
    plain = intenc.encode(expected_value)
    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 intenc.decode_int64(out) == expected_value
    assert enc.parms_id() == parms_id

    # ciphertext mod switch to
    parms_id = ctx.last_parms_id()
    plain = intenc.encode(expected_value)
    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 intenc.decode_int64(out) == 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()