Homomorphic Encryption example compilation error

I'm trying to run the Using Homomorphic Encryption example from the announcement blog post in a playground and am running into an Ambiguous use of 'decrypt(using:)' error on the line

let decrypted = try ciphertext.decrypt(using: secretKey)

as well as a (possible downstream) error Cannot infer contextual base in reference to member 'coefficient' on the line just below:

let decoded: [UInt64] = try decrypted.decode(format: .coefficient)

I'm guessing the example is missing some qualification on the decrypt call's return type but I can't quite figure out what I'd put there to help the compiler. It's not Bfv<UInt64>.CoeffPlaintext by the looks of it.

Am I missing something obvious or is the example out of date perhaps?

1 Like

It is!

The example works fine for me, but not in a playground, where I see what you do.

I haven't gotten a playground to work for several years, and consider them abandoned.

Mmm, neither

let decrypted: Bfv<UInt64>.CoeffPlaintext = try ciphertext.decrypt(using: secretKey)

nor

let decrypted = try ciphertext.decrypt(using: secretKey) as Bfv<UInt64>.CoeffPlaintext

help to resolve the ambiguity. Is there another way to force the correct overload that I could try?

I'll try sticking it in a test as well, this was just the easiest way to try this. (And it usually works quite reliably, I do this routinely for packages I'm trying out).

Thanks for taking a look!

1 Like

I've heard you talk about it on the podcast (of which I am a regular listener—thanks for doing it!), and wish I had as good of a general experience with them as you do. I really liked playgrounds from 2014 to about 2018, used them daily, and wish they would be considered as worthy of support as they seemed to be back then.

We have made a few API changes to resolve the ambiguous references, but that hasn't been tagged in a release yet. Can you try depending on

.package(
    url: "https://github.com/apple/swift-homomorphic-encryption",
    branch: "main"),

?

For reference, the blog post example is similar to this snippet: swift-homomorphic-encryption/Snippets/HomomorphicEncryption/BasicsSnippet.swift at 780e3215cb48a5c681286d8f41d8a982bc313ccf · apple/swift-homomorphic-encryption · GitHub, which can be run with swift run BasicsSnippet.

3 Likes

The snippet does indeed compile and run in my playground based off of main. Thanks a lot for the pointer, @fboemer !

2 Likes

I love to hear that someone's listening in on our ramblings, thank you :slight_smile:

I suspect part of it is that I've learned to step around the problems with playgrounds over the years. There's always the ritual of closing and reopening them when dependencies aren't building as well as trimming down code but on the whole I'm still able to get quite far with playgrounds.

One of the main things I do to make them more stable is move chunks of code into files in Sources. It gives better compiler errors (sometimes it's the only way to get compiler errors) and it'll run much faster.

1 Like