|
| 1 | +import { beforeAll, describe, expect, test } from 'vitest' |
| 2 | +import MainModuleFactory, { |
| 3 | + type BatchEncoder, |
| 4 | + type Decryptor, |
| 5 | + type EncryptionParameters, |
| 6 | + type Encryptor, |
| 7 | + type Evaluator, |
| 8 | + type KeyGenerator, |
| 9 | + type MainModule, |
| 10 | + type SEALContext |
| 11 | +} from '../index_allows' |
| 12 | + |
| 13 | +let seal: MainModule |
| 14 | +let encParms: EncryptionParameters |
| 15 | +let context: SEALContext |
| 16 | +let keygen: KeyGenerator |
| 17 | +let encryptor: Encryptor |
| 18 | +let evaluator: Evaluator |
| 19 | +let decryptor: Decryptor |
| 20 | +let encoder: BatchEncoder |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + seal = await MainModuleFactory() |
| 24 | + |
| 25 | + const scheme = seal.SchemeType.bfv |
| 26 | + const polyModulusDegree = 1024 |
| 27 | + const sec = seal.SecLevelType.tc128 |
| 28 | + |
| 29 | + const coeffModulus = seal.CoeffModulus.Create( |
| 30 | + polyModulusDegree, |
| 31 | + Int32Array.from([27]) |
| 32 | + ) |
| 33 | + const plainModulus = seal.PlainModulus.Batching(polyModulusDegree, 20) |
| 34 | + |
| 35 | + encParms = new seal.EncryptionParameters(scheme) |
| 36 | + encParms.setPolyModulusDegree(polyModulusDegree) |
| 37 | + encParms.setCoeffModulus(coeffModulus) |
| 38 | + encParms.setPlainModulus(plainModulus) |
| 39 | + |
| 40 | + context = new seal.SEALContext(encParms, true, sec) |
| 41 | + |
| 42 | + keygen = new seal.KeyGenerator(context) |
| 43 | + const pub = keygen.createPublicKey() |
| 44 | + const secKey = keygen.secretKey() |
| 45 | + |
| 46 | + encryptor = new seal.Encryptor(context, pub) |
| 47 | + evaluator = new seal.Evaluator(context) |
| 48 | + decryptor = new seal.Decryptor(context, secKey) |
| 49 | + encoder = new seal.BatchEncoder(context) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('Ciphertext (allows)', () => { |
| 53 | + test('It allows transparent ciphertext and decrypts to zeros', () => { |
| 54 | + const ct = new seal.Ciphertext() |
| 55 | + encryptor.encryptZero(ct) |
| 56 | + |
| 57 | + evaluator.subInplace(ct, ct) |
| 58 | + const plain = new seal.Plaintext() |
| 59 | + decryptor.decrypt(ct, plain) |
| 60 | + |
| 61 | + const arr = encoder.decodeBigUint64(plain) as BigUint64Array |
| 62 | + expect(arr).toEqual( |
| 63 | + BigUint64Array.from({ length: encoder.slotCount() }, _ => BigInt(0)) |
| 64 | + ) |
| 65 | + }) |
| 66 | +}) |
0 commit comments