-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (54 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import BN from 'bn.js';
import {bn2array,arraySize} from './lib/bn2array.js';
export * from "./lib/lwesetup.js";
function typeCheckBN(plainBigNum){
if(!BN.isBN(plainBigNum)){
throw new Error('Type Error: the input is not BN type');
}
}
export function LWEencrypt(encryptor,plainBigNum,encoder){
typeCheckBN(plainBigNum);
var plainNumArray = bn2array(plainBigNum);
const plainNumArrayEncode = encoder.encode(
Uint32Array.from(plainNumArray)
)
return encryptor.encrypt(plainNumArrayEncode);
}
export function LWEaddMatrix(evaluator,cipherTexMatrix1,cipherTexMatrix2,seal){
var cipherTexArray = [];
for (let i=0;i<arraySize;i++){
let cipherText = seal.CipherText();
evaluator.add(cipherTexMatrix1[i], cipherTexMatrix2[i], cipherText)
cipherTexArray.push(cipherText);
}
return {typeinfo:"cipherTexArray", contents:cipherTexArray};
}
export function LWEsmul(evaluator,encoder,cipherTex1,plainTexBN,seal){
const plainArray = bn2array(plainTexBN);
var cipherTexArray = [];
for (let i=0;i<arraySize;i++){
let array0 = [];
for (let j=0;j<arraySize;j++){
array0.push(plainArray[i]);
}
let plainTexArray = Uint32Array.from(array0);
let plainTexArrayEncoded =encoder.encode(plainTexArray);
let cipherText = seal.CipherText();
evaluator.multiplyPlain(cipherTex1, plainTexArrayEncoded, cipherText);
cipherTexArray.push(cipherText);
}
return {typeinfo:"cipherTextMetrix", contents:cipherTexArray};
}
export function decryptMatrixToBN(decryptor,matrix,encoder){
var bn0 = new BN(0);
for (let i=0;i<arraySize;i++){
let intArray0 = decryptor.decrypt(matrix[i]);
let intArray = encoder.decode(intArray0);
for (let j=0;j<arraySize;j++){
let bn = new BN(intArray[j]);
let bnMul = bn.mul(new BN("1".concat("00".repeat(i+j))));
bn0 = bn0.add(bnMul);
}
}
return bn0;
}