diff --git a/Cargo.toml b/Cargo.toml index 1701b0f..507a951 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ csv = "1.3" debug_print = "1.0.0" linfa-datasets = { version = "0.7", features = ["iris", "winequality"] } rayon = "1.7.0" +serde_json = "1.0.114" +serde = { version = "1.0.114", features = ["derive"] } tfhe = { version = "0.5.1", features = ["boolean", "shortint", "integer"] } [features] @@ -28,3 +30,14 @@ path = "src/plain_lr.rs" name = "encrypted_lr" path = "src/encrypted_lr.rs" +[[bin]] +name = "encrypted_lr_dwt" +path = "src/encrypted_lr_dwt.rs" + +[[bin]] +name = "lut_test" +path = "src/lut_test.rs" + +[[bin]] +name = "lut_split_test" +path = "src/lut_split_test.rs" \ No newline at end of file diff --git a/lut16_quantized_lsb.json b/lut16_quantized_lsb.json new file mode 100644 index 0000000..7bc33f6 --- /dev/null +++ b/lut16_quantized_lsb.json @@ -0,0 +1,2050 @@ +[ + { + "key": 0, + "value": 73 + }, + { + "key": 1, + "value": 74 + }, + { + "key": 2, + "value": 75 + }, + { + "key": 3, + "value": 77 + }, + { + "key": 4, + "value": 78 + }, + { + "key": 5, + "value": 79 + }, + { + "key": 6, + "value": 80 + }, + { + "key": 7, + "value": 82 + }, + { + "key": 8, + "value": 83 + }, + { + "key": 9, + "value": 84 + }, + { + "key": 10, + "value": 85 + }, + { + "key": 11, + "value": 87 + }, + { + "key": 12, + "value": 88 + }, + { + "key": 13, + "value": 89 + }, + { + "key": 14, + "value": 91 + }, + { + "key": 15, + "value": 92 + }, + { + "key": 16, + "value": 94 + }, + { + "key": 17, + "value": 95 + }, + { + "key": 18, + "value": 97 + }, + { + "key": 19, + "value": 98 + }, + { + "key": 20, + "value": 100 + }, + { + "key": 21, + "value": 101 + }, + { + "key": 22, + "value": 103 + }, + { + "key": 23, + "value": 104 + }, + { + "key": 24, + "value": 106 + }, + { + "key": 25, + "value": 107 + }, + { + "key": 26, + "value": 109 + }, + { + "key": 27, + "value": 111 + }, + { + "key": 28, + "value": 112 + }, + { + "key": 29, + "value": 114 + }, + { + "key": 30, + "value": 116 + }, + { + "key": 31, + "value": 118 + }, + { + "key": 32, + "value": 120 + }, + { + "key": 33, + "value": 121 + }, + { + "key": 34, + "value": 123 + }, + { + "key": 35, + "value": 125 + }, + { + "key": 36, + "value": 127 + }, + { + "key": 37, + "value": 129 + }, + { + "key": 38, + "value": 131 + }, + { + "key": 39, + "value": 133 + }, + { + "key": 40, + "value": 135 + }, + { + "key": 41, + "value": 137 + }, + { + "key": 42, + "value": 139 + }, + { + "key": 43, + "value": 141 + }, + { + "key": 44, + "value": 143 + }, + { + "key": 45, + "value": 146 + }, + { + "key": 46, + "value": 148 + }, + { + "key": 47, + "value": 150 + }, + { + "key": 48, + "value": 152 + }, + { + "key": 49, + "value": 155 + }, + { + "key": 50, + "value": 157 + }, + { + "key": 51, + "value": 159 + }, + { + "key": 52, + "value": 162 + }, + { + "key": 53, + "value": 164 + }, + { + "key": 54, + "value": 167 + }, + { + "key": 55, + "value": 169 + }, + { + "key": 56, + "value": 172 + }, + { + "key": 57, + "value": 174 + }, + { + "key": 58, + "value": 177 + }, + { + "key": 59, + "value": 180 + }, + { + "key": 60, + "value": 183 + }, + { + "key": 61, + "value": 185 + }, + { + "key": 62, + "value": 188 + }, + { + "key": 63, + "value": 191 + }, + { + "key": 64, + "value": 194 + }, + { + "key": 65, + "value": 197 + }, + { + "key": 66, + "value": 200 + }, + { + "key": 67, + "value": 203 + }, + { + "key": 68, + "value": 206 + }, + { + "key": 69, + "value": 209 + }, + { + "key": 70, + "value": 212 + }, + { + "key": 71, + "value": 215 + }, + { + "key": 72, + "value": 218 + }, + { + "key": 73, + "value": 221 + }, + { + "key": 74, + "value": 225 + }, + { + "key": 75, + "value": 228 + }, + { + "key": 76, + "value": 232 + }, + { + "key": 77, + "value": 235 + }, + { + "key": 78, + "value": 238 + }, + { + "key": 79, + "value": 242 + }, + { + "key": 80, + "value": 246 + }, + { + "key": 81, + "value": 249 + }, + { + "key": 82, + "value": 253 + }, + { + "key": 83, + "value": 1 + }, + { + "key": 84, + "value": 4 + }, + { + "key": 85, + "value": 8 + }, + { + "key": 86, + "value": 12 + }, + { + "key": 87, + "value": 16 + }, + { + "key": 88, + "value": 20 + }, + { + "key": 89, + "value": 24 + }, + { + "key": 90, + "value": 28 + }, + { + "key": 91, + "value": 33 + }, + { + "key": 92, + "value": 37 + }, + { + "key": 93, + "value": 41 + }, + { + "key": 94, + "value": 45 + }, + { + "key": 95, + "value": 50 + }, + { + "key": 96, + "value": 54 + }, + { + "key": 97, + "value": 59 + }, + { + "key": 98, + "value": 63 + }, + { + "key": 99, + "value": 68 + }, + { + "key": 100, + "value": 73 + }, + { + "key": 101, + "value": 77 + }, + { + "key": 102, + "value": 82 + }, + { + "key": 103, + "value": 87 + }, + { + "key": 104, + "value": 92 + }, + { + "key": 105, + "value": 97 + }, + { + "key": 106, + "value": 102 + }, + { + "key": 107, + "value": 107 + }, + { + "key": 108, + "value": 113 + }, + { + "key": 109, + "value": 118 + }, + { + "key": 110, + "value": 123 + }, + { + "key": 111, + "value": 129 + }, + { + "key": 112, + "value": 134 + }, + { + "key": 113, + "value": 140 + }, + { + "key": 114, + "value": 145 + }, + { + "key": 115, + "value": 151 + }, + { + "key": 116, + "value": 157 + }, + { + "key": 117, + "value": 163 + }, + { + "key": 118, + "value": 168 + }, + { + "key": 119, + "value": 174 + }, + { + "key": 120, + "value": 181 + }, + { + "key": 121, + "value": 187 + }, + { + "key": 122, + "value": 193 + }, + { + "key": 123, + "value": 199 + }, + { + "key": 124, + "value": 206 + }, + { + "key": 125, + "value": 212 + }, + { + "key": 126, + "value": 218 + }, + { + "key": 127, + "value": 225 + }, + { + "key": 128, + "value": 232 + }, + { + "key": 129, + "value": 239 + }, + { + "key": 130, + "value": 245 + }, + { + "key": 131, + "value": 252 + }, + { + "key": 132, + "value": 3 + }, + { + "key": 133, + "value": 10 + }, + { + "key": 134, + "value": 18 + }, + { + "key": 135, + "value": 25 + }, + { + "key": 136, + "value": 32 + }, + { + "key": 137, + "value": 40 + }, + { + "key": 138, + "value": 47 + }, + { + "key": 139, + "value": 55 + }, + { + "key": 140, + "value": 62 + }, + { + "key": 141, + "value": 70 + }, + { + "key": 142, + "value": 78 + }, + { + "key": 143, + "value": 86 + }, + { + "key": 144, + "value": 94 + }, + { + "key": 145, + "value": 102 + }, + { + "key": 146, + "value": 110 + }, + { + "key": 147, + "value": 119 + }, + { + "key": 148, + "value": 127 + }, + { + "key": 149, + "value": 135 + }, + { + "key": 150, + "value": 144 + }, + { + "key": 151, + "value": 153 + }, + { + "key": 152, + "value": 161 + }, + { + "key": 153, + "value": 170 + }, + { + "key": 154, + "value": 179 + }, + { + "key": 155, + "value": 188 + }, + { + "key": 156, + "value": 197 + }, + { + "key": 157, + "value": 207 + }, + { + "key": 158, + "value": 216 + }, + { + "key": 159, + "value": 225 + }, + { + "key": 160, + "value": 235 + }, + { + "key": 161, + "value": 244 + }, + { + "key": 162, + "value": 254 + }, + { + "key": 163, + "value": 8 + }, + { + "key": 164, + "value": 18 + }, + { + "key": 165, + "value": 28 + }, + { + "key": 166, + "value": 38 + }, + { + "key": 167, + "value": 48 + }, + { + "key": 168, + "value": 58 + }, + { + "key": 169, + "value": 68 + }, + { + "key": 170, + "value": 79 + }, + { + "key": 171, + "value": 89 + }, + { + "key": 172, + "value": 100 + }, + { + "key": 173, + "value": 111 + }, + { + "key": 174, + "value": 122 + }, + { + "key": 175, + "value": 133 + }, + { + "key": 176, + "value": 144 + }, + { + "key": 177, + "value": 155 + }, + { + "key": 178, + "value": 166 + }, + { + "key": 179, + "value": 177 + }, + { + "key": 180, + "value": 189 + }, + { + "key": 181, + "value": 200 + }, + { + "key": 182, + "value": 212 + }, + { + "key": 183, + "value": 224 + }, + { + "key": 184, + "value": 235 + }, + { + "key": 185, + "value": 247 + }, + { + "key": 186, + "value": 3 + }, + { + "key": 187, + "value": 15 + }, + { + "key": 188, + "value": 27 + }, + { + "key": 189, + "value": 40 + }, + { + "key": 190, + "value": 52 + }, + { + "key": 191, + "value": 65 + }, + { + "key": 192, + "value": 77 + }, + { + "key": 193, + "value": 90 + }, + { + "key": 194, + "value": 102 + }, + { + "key": 195, + "value": 115 + }, + { + "key": 196, + "value": 128 + }, + { + "key": 197, + "value": 141 + }, + { + "key": 198, + "value": 154 + }, + { + "key": 199, + "value": 167 + }, + { + "key": 200, + "value": 181 + }, + { + "key": 201, + "value": 194 + }, + { + "key": 202, + "value": 207 + }, + { + "key": 203, + "value": 221 + }, + { + "key": 204, + "value": 234 + }, + { + "key": 205, + "value": 248 + }, + { + "key": 206, + "value": 6 + }, + { + "key": 207, + "value": 20 + }, + { + "key": 208, + "value": 34 + }, + { + "key": 209, + "value": 48 + }, + { + "key": 210, + "value": 62 + }, + { + "key": 211, + "value": 76 + }, + { + "key": 212, + "value": 90 + }, + { + "key": 213, + "value": 104 + }, + { + "key": 214, + "value": 119 + }, + { + "key": 215, + "value": 133 + }, + { + "key": 216, + "value": 148 + }, + { + "key": 217, + "value": 162 + }, + { + "key": 218, + "value": 177 + }, + { + "key": 219, + "value": 191 + }, + { + "key": 220, + "value": 206 + }, + { + "key": 221, + "value": 221 + }, + { + "key": 222, + "value": 236 + }, + { + "key": 223, + "value": 251 + }, + { + "key": 224, + "value": 10 + }, + { + "key": 225, + "value": 25 + }, + { + "key": 226, + "value": 40 + }, + { + "key": 227, + "value": 55 + }, + { + "key": 228, + "value": 71 + }, + { + "key": 229, + "value": 86 + }, + { + "key": 230, + "value": 101 + }, + { + "key": 231, + "value": 117 + }, + { + "key": 232, + "value": 132 + }, + { + "key": 233, + "value": 147 + }, + { + "key": 234, + "value": 163 + }, + { + "key": 235, + "value": 178 + }, + { + "key": 236, + "value": 194 + }, + { + "key": 237, + "value": 210 + }, + { + "key": 238, + "value": 225 + }, + { + "key": 239, + "value": 241 + }, + { + "key": 240, + "value": 1 + }, + { + "key": 241, + "value": 17 + }, + { + "key": 242, + "value": 32 + }, + { + "key": 243, + "value": 48 + }, + { + "key": 244, + "value": 64 + }, + { + "key": 245, + "value": 80 + }, + { + "key": 246, + "value": 96 + }, + { + "key": 247, + "value": 112 + }, + { + "key": 248, + "value": 128 + }, + { + "key": 249, + "value": 144 + }, + { + "key": 250, + "value": 160 + }, + { + "key": 251, + "value": 176 + }, + { + "key": 252, + "value": 192 + }, + { + "key": 253, + "value": 208 + }, + { + "key": 254, + "value": 224 + }, + { + "key": 255, + "value": 240 + }, + { + "key": 256, + "value": 0 + }, + { + "key": 257, + "value": 15 + }, + { + "key": 258, + "value": 31 + }, + { + "key": 259, + "value": 47 + }, + { + "key": 260, + "value": 63 + }, + { + "key": 261, + "value": 79 + }, + { + "key": 262, + "value": 95 + }, + { + "key": 263, + "value": 111 + }, + { + "key": 264, + "value": 127 + }, + { + "key": 265, + "value": 143 + }, + { + "key": 266, + "value": 159 + }, + { + "key": 267, + "value": 175 + }, + { + "key": 268, + "value": 191 + }, + { + "key": 269, + "value": 207 + }, + { + "key": 270, + "value": 223 + }, + { + "key": 271, + "value": 238 + }, + { + "key": 272, + "value": 254 + }, + { + "key": 273, + "value": 14 + }, + { + "key": 274, + "value": 30 + }, + { + "key": 275, + "value": 45 + }, + { + "key": 276, + "value": 61 + }, + { + "key": 277, + "value": 77 + }, + { + "key": 278, + "value": 92 + }, + { + "key": 279, + "value": 108 + }, + { + "key": 280, + "value": 123 + }, + { + "key": 281, + "value": 138 + }, + { + "key": 282, + "value": 154 + }, + { + "key": 283, + "value": 169 + }, + { + "key": 284, + "value": 184 + }, + { + "key": 285, + "value": 200 + }, + { + "key": 286, + "value": 215 + }, + { + "key": 287, + "value": 230 + }, + { + "key": 288, + "value": 245 + }, + { + "key": 289, + "value": 4 + }, + { + "key": 290, + "value": 19 + }, + { + "key": 291, + "value": 34 + }, + { + "key": 292, + "value": 49 + }, + { + "key": 293, + "value": 64 + }, + { + "key": 294, + "value": 78 + }, + { + "key": 295, + "value": 93 + }, + { + "key": 296, + "value": 107 + }, + { + "key": 297, + "value": 122 + }, + { + "key": 298, + "value": 136 + }, + { + "key": 299, + "value": 151 + }, + { + "key": 300, + "value": 165 + }, + { + "key": 301, + "value": 179 + }, + { + "key": 302, + "value": 193 + }, + { + "key": 303, + "value": 207 + }, + { + "key": 304, + "value": 221 + }, + { + "key": 305, + "value": 235 + }, + { + "key": 306, + "value": 249 + }, + { + "key": 307, + "value": 7 + }, + { + "key": 308, + "value": 21 + }, + { + "key": 309, + "value": 34 + }, + { + "key": 310, + "value": 48 + }, + { + "key": 311, + "value": 61 + }, + { + "key": 312, + "value": 74 + }, + { + "key": 313, + "value": 88 + }, + { + "key": 314, + "value": 101 + }, + { + "key": 315, + "value": 114 + }, + { + "key": 316, + "value": 127 + }, + { + "key": 317, + "value": 140 + }, + { + "key": 318, + "value": 153 + }, + { + "key": 319, + "value": 165 + }, + { + "key": 320, + "value": 178 + }, + { + "key": 321, + "value": 190 + }, + { + "key": 322, + "value": 203 + }, + { + "key": 323, + "value": 215 + }, + { + "key": 324, + "value": 228 + }, + { + "key": 325, + "value": 240 + }, + { + "key": 326, + "value": 252 + }, + { + "key": 327, + "value": 8 + }, + { + "key": 328, + "value": 20 + }, + { + "key": 329, + "value": 31 + }, + { + "key": 330, + "value": 43 + }, + { + "key": 331, + "value": 55 + }, + { + "key": 332, + "value": 66 + }, + { + "key": 333, + "value": 78 + }, + { + "key": 334, + "value": 89 + }, + { + "key": 335, + "value": 100 + }, + { + "key": 336, + "value": 111 + }, + { + "key": 337, + "value": 122 + }, + { + "key": 338, + "value": 133 + }, + { + "key": 339, + "value": 144 + }, + { + "key": 340, + "value": 155 + }, + { + "key": 341, + "value": 166 + }, + { + "key": 342, + "value": 176 + }, + { + "key": 343, + "value": 187 + }, + { + "key": 344, + "value": 197 + }, + { + "key": 345, + "value": 207 + }, + { + "key": 346, + "value": 217 + }, + { + "key": 347, + "value": 227 + }, + { + "key": 348, + "value": 237 + }, + { + "key": 349, + "value": 247 + }, + { + "key": 350, + "value": 1 + }, + { + "key": 351, + "value": 11 + }, + { + "key": 352, + "value": 20 + }, + { + "key": 353, + "value": 30 + }, + { + "key": 354, + "value": 39 + }, + { + "key": 355, + "value": 48 + }, + { + "key": 356, + "value": 58 + }, + { + "key": 357, + "value": 67 + }, + { + "key": 358, + "value": 76 + }, + { + "key": 359, + "value": 85 + }, + { + "key": 360, + "value": 94 + }, + { + "key": 361, + "value": 102 + }, + { + "key": 362, + "value": 111 + }, + { + "key": 363, + "value": 120 + }, + { + "key": 364, + "value": 128 + }, + { + "key": 365, + "value": 136 + }, + { + "key": 366, + "value": 145 + }, + { + "key": 367, + "value": 153 + }, + { + "key": 368, + "value": 161 + }, + { + "key": 369, + "value": 169 + }, + { + "key": 370, + "value": 177 + }, + { + "key": 371, + "value": 185 + }, + { + "key": 372, + "value": 193 + }, + { + "key": 373, + "value": 200 + }, + { + "key": 374, + "value": 208 + }, + { + "key": 375, + "value": 215 + }, + { + "key": 376, + "value": 223 + }, + { + "key": 377, + "value": 230 + }, + { + "key": 378, + "value": 237 + }, + { + "key": 379, + "value": 245 + }, + { + "key": 380, + "value": 252 + }, + { + "key": 381, + "value": 3 + }, + { + "key": 382, + "value": 10 + }, + { + "key": 383, + "value": 16 + }, + { + "key": 384, + "value": 23 + }, + { + "key": 385, + "value": 30 + }, + { + "key": 386, + "value": 37 + }, + { + "key": 387, + "value": 43 + }, + { + "key": 388, + "value": 49 + }, + { + "key": 389, + "value": 56 + }, + { + "key": 390, + "value": 62 + }, + { + "key": 391, + "value": 68 + }, + { + "key": 392, + "value": 74 + }, + { + "key": 393, + "value": 81 + }, + { + "key": 394, + "value": 87 + }, + { + "key": 395, + "value": 92 + }, + { + "key": 396, + "value": 98 + }, + { + "key": 397, + "value": 104 + }, + { + "key": 398, + "value": 110 + }, + { + "key": 399, + "value": 115 + }, + { + "key": 400, + "value": 121 + }, + { + "key": 401, + "value": 126 + }, + { + "key": 402, + "value": 132 + }, + { + "key": 403, + "value": 137 + }, + { + "key": 404, + "value": 142 + }, + { + "key": 405, + "value": 148 + }, + { + "key": 406, + "value": 153 + }, + { + "key": 407, + "value": 158 + }, + { + "key": 408, + "value": 163 + }, + { + "key": 409, + "value": 168 + }, + { + "key": 410, + "value": 173 + }, + { + "key": 411, + "value": 178 + }, + { + "key": 412, + "value": 182 + }, + { + "key": 413, + "value": 187 + }, + { + "key": 414, + "value": 192 + }, + { + "key": 415, + "value": 196 + }, + { + "key": 416, + "value": 201 + }, + { + "key": 417, + "value": 205 + }, + { + "key": 418, + "value": 210 + }, + { + "key": 419, + "value": 214 + }, + { + "key": 420, + "value": 218 + }, + { + "key": 421, + "value": 222 + }, + { + "key": 422, + "value": 227 + }, + { + "key": 423, + "value": 231 + }, + { + "key": 424, + "value": 235 + }, + { + "key": 425, + "value": 239 + }, + { + "key": 426, + "value": 243 + }, + { + "key": 427, + "value": 247 + }, + { + "key": 428, + "value": 251 + }, + { + "key": 429, + "value": 254 + }, + { + "key": 430, + "value": 2 + }, + { + "key": 431, + "value": 6 + }, + { + "key": 432, + "value": 9 + }, + { + "key": 433, + "value": 13 + }, + { + "key": 434, + "value": 17 + }, + { + "key": 435, + "value": 20 + }, + { + "key": 436, + "value": 23 + }, + { + "key": 437, + "value": 27 + }, + { + "key": 438, + "value": 30 + }, + { + "key": 439, + "value": 34 + }, + { + "key": 440, + "value": 37 + }, + { + "key": 441, + "value": 40 + }, + { + "key": 442, + "value": 43 + }, + { + "key": 443, + "value": 46 + }, + { + "key": 444, + "value": 49 + }, + { + "key": 445, + "value": 52 + }, + { + "key": 446, + "value": 55 + }, + { + "key": 447, + "value": 58 + }, + { + "key": 448, + "value": 61 + }, + { + "key": 449, + "value": 64 + }, + { + "key": 450, + "value": 67 + }, + { + "key": 451, + "value": 70 + }, + { + "key": 452, + "value": 72 + }, + { + "key": 453, + "value": 75 + }, + { + "key": 454, + "value": 78 + }, + { + "key": 455, + "value": 81 + }, + { + "key": 456, + "value": 83 + }, + { + "key": 457, + "value": 86 + }, + { + "key": 458, + "value": 88 + }, + { + "key": 459, + "value": 91 + }, + { + "key": 460, + "value": 93 + }, + { + "key": 461, + "value": 96 + }, + { + "key": 462, + "value": 98 + }, + { + "key": 463, + "value": 100 + }, + { + "key": 464, + "value": 103 + }, + { + "key": 465, + "value": 105 + }, + { + "key": 466, + "value": 107 + }, + { + "key": 467, + "value": 109 + }, + { + "key": 468, + "value": 112 + }, + { + "key": 469, + "value": 114 + }, + { + "key": 470, + "value": 116 + }, + { + "key": 471, + "value": 118 + }, + { + "key": 472, + "value": 120 + }, + { + "key": 473, + "value": 122 + }, + { + "key": 474, + "value": 124 + }, + { + "key": 475, + "value": 126 + }, + { + "key": 476, + "value": 128 + }, + { + "key": 477, + "value": 130 + }, + { + "key": 478, + "value": 132 + }, + { + "key": 479, + "value": 134 + }, + { + "key": 480, + "value": 135 + }, + { + "key": 481, + "value": 137 + }, + { + "key": 482, + "value": 139 + }, + { + "key": 483, + "value": 141 + }, + { + "key": 484, + "value": 143 + }, + { + "key": 485, + "value": 144 + }, + { + "key": 486, + "value": 146 + }, + { + "key": 487, + "value": 148 + }, + { + "key": 488, + "value": 149 + }, + { + "key": 489, + "value": 151 + }, + { + "key": 490, + "value": 152 + }, + { + "key": 491, + "value": 154 + }, + { + "key": 492, + "value": 155 + }, + { + "key": 493, + "value": 157 + }, + { + "key": 494, + "value": 158 + }, + { + "key": 495, + "value": 160 + }, + { + "key": 496, + "value": 161 + }, + { + "key": 497, + "value": 163 + }, + { + "key": 498, + "value": 164 + }, + { + "key": 499, + "value": 166 + }, + { + "key": 500, + "value": 167 + }, + { + "key": 501, + "value": 168 + }, + { + "key": 502, + "value": 170 + }, + { + "key": 503, + "value": 171 + }, + { + "key": 504, + "value": 172 + }, + { + "key": 505, + "value": 173 + }, + { + "key": 506, + "value": 175 + }, + { + "key": 507, + "value": 176 + }, + { + "key": 508, + "value": 177 + }, + { + "key": 509, + "value": 178 + }, + { + "key": 510, + "value": 180 + }, + { + "key": 511, + "value": 181 + } +] diff --git a/lut16_quantized_msb.json b/lut16_quantized_msb.json new file mode 100644 index 0000000..1aadb4a --- /dev/null +++ b/lut16_quantized_msb.json @@ -0,0 +1,2050 @@ +[ + { + "key": 0, + "value": 0 + }, + { + "key": 1, + "value": 0 + }, + { + "key": 2, + "value": 0 + }, + { + "key": 3, + "value": 0 + }, + { + "key": 4, + "value": 0 + }, + { + "key": 5, + "value": 0 + }, + { + "key": 6, + "value": 0 + }, + { + "key": 7, + "value": 0 + }, + { + "key": 8, + "value": 0 + }, + { + "key": 9, + "value": 0 + }, + { + "key": 10, + "value": 0 + }, + { + "key": 11, + "value": 0 + }, + { + "key": 12, + "value": 0 + }, + { + "key": 13, + "value": 0 + }, + { + "key": 14, + "value": 0 + }, + { + "key": 15, + "value": 0 + }, + { + "key": 16, + "value": 0 + }, + { + "key": 17, + "value": 0 + }, + { + "key": 18, + "value": 0 + }, + { + "key": 19, + "value": 0 + }, + { + "key": 20, + "value": 0 + }, + { + "key": 21, + "value": 0 + }, + { + "key": 22, + "value": 0 + }, + { + "key": 23, + "value": 0 + }, + { + "key": 24, + "value": 0 + }, + { + "key": 25, + "value": 0 + }, + { + "key": 26, + "value": 0 + }, + { + "key": 27, + "value": 0 + }, + { + "key": 28, + "value": 0 + }, + { + "key": 29, + "value": 0 + }, + { + "key": 30, + "value": 0 + }, + { + "key": 31, + "value": 0 + }, + { + "key": 32, + "value": 0 + }, + { + "key": 33, + "value": 0 + }, + { + "key": 34, + "value": 0 + }, + { + "key": 35, + "value": 0 + }, + { + "key": 36, + "value": 0 + }, + { + "key": 37, + "value": 0 + }, + { + "key": 38, + "value": 0 + }, + { + "key": 39, + "value": 0 + }, + { + "key": 40, + "value": 0 + }, + { + "key": 41, + "value": 0 + }, + { + "key": 42, + "value": 0 + }, + { + "key": 43, + "value": 0 + }, + { + "key": 44, + "value": 0 + }, + { + "key": 45, + "value": 0 + }, + { + "key": 46, + "value": 0 + }, + { + "key": 47, + "value": 0 + }, + { + "key": 48, + "value": 0 + }, + { + "key": 49, + "value": 0 + }, + { + "key": 50, + "value": 0 + }, + { + "key": 51, + "value": 0 + }, + { + "key": 52, + "value": 0 + }, + { + "key": 53, + "value": 0 + }, + { + "key": 54, + "value": 0 + }, + { + "key": 55, + "value": 0 + }, + { + "key": 56, + "value": 0 + }, + { + "key": 57, + "value": 0 + }, + { + "key": 58, + "value": 0 + }, + { + "key": 59, + "value": 0 + }, + { + "key": 60, + "value": 0 + }, + { + "key": 61, + "value": 0 + }, + { + "key": 62, + "value": 0 + }, + { + "key": 63, + "value": 0 + }, + { + "key": 64, + "value": 0 + }, + { + "key": 65, + "value": 0 + }, + { + "key": 66, + "value": 0 + }, + { + "key": 67, + "value": 0 + }, + { + "key": 68, + "value": 0 + }, + { + "key": 69, + "value": 0 + }, + { + "key": 70, + "value": 0 + }, + { + "key": 71, + "value": 0 + }, + { + "key": 72, + "value": 0 + }, + { + "key": 73, + "value": 0 + }, + { + "key": 74, + "value": 0 + }, + { + "key": 75, + "value": 0 + }, + { + "key": 76, + "value": 0 + }, + { + "key": 77, + "value": 0 + }, + { + "key": 78, + "value": 0 + }, + { + "key": 79, + "value": 0 + }, + { + "key": 80, + "value": 0 + }, + { + "key": 81, + "value": 0 + }, + { + "key": 82, + "value": 0 + }, + { + "key": 83, + "value": 1 + }, + { + "key": 84, + "value": 1 + }, + { + "key": 85, + "value": 1 + }, + { + "key": 86, + "value": 1 + }, + { + "key": 87, + "value": 1 + }, + { + "key": 88, + "value": 1 + }, + { + "key": 89, + "value": 1 + }, + { + "key": 90, + "value": 1 + }, + { + "key": 91, + "value": 1 + }, + { + "key": 92, + "value": 1 + }, + { + "key": 93, + "value": 1 + }, + { + "key": 94, + "value": 1 + }, + { + "key": 95, + "value": 1 + }, + { + "key": 96, + "value": 1 + }, + { + "key": 97, + "value": 1 + }, + { + "key": 98, + "value": 1 + }, + { + "key": 99, + "value": 1 + }, + { + "key": 100, + "value": 1 + }, + { + "key": 101, + "value": 1 + }, + { + "key": 102, + "value": 1 + }, + { + "key": 103, + "value": 1 + }, + { + "key": 104, + "value": 1 + }, + { + "key": 105, + "value": 1 + }, + { + "key": 106, + "value": 1 + }, + { + "key": 107, + "value": 1 + }, + { + "key": 108, + "value": 1 + }, + { + "key": 109, + "value": 1 + }, + { + "key": 110, + "value": 1 + }, + { + "key": 111, + "value": 1 + }, + { + "key": 112, + "value": 1 + }, + { + "key": 113, + "value": 1 + }, + { + "key": 114, + "value": 1 + }, + { + "key": 115, + "value": 1 + }, + { + "key": 116, + "value": 1 + }, + { + "key": 117, + "value": 1 + }, + { + "key": 118, + "value": 1 + }, + { + "key": 119, + "value": 1 + }, + { + "key": 120, + "value": 1 + }, + { + "key": 121, + "value": 1 + }, + { + "key": 122, + "value": 1 + }, + { + "key": 123, + "value": 1 + }, + { + "key": 124, + "value": 1 + }, + { + "key": 125, + "value": 1 + }, + { + "key": 126, + "value": 1 + }, + { + "key": 127, + "value": 1 + }, + { + "key": 128, + "value": 1 + }, + { + "key": 129, + "value": 1 + }, + { + "key": 130, + "value": 1 + }, + { + "key": 131, + "value": 1 + }, + { + "key": 132, + "value": 2 + }, + { + "key": 133, + "value": 2 + }, + { + "key": 134, + "value": 2 + }, + { + "key": 135, + "value": 2 + }, + { + "key": 136, + "value": 2 + }, + { + "key": 137, + "value": 2 + }, + { + "key": 138, + "value": 2 + }, + { + "key": 139, + "value": 2 + }, + { + "key": 140, + "value": 2 + }, + { + "key": 141, + "value": 2 + }, + { + "key": 142, + "value": 2 + }, + { + "key": 143, + "value": 2 + }, + { + "key": 144, + "value": 2 + }, + { + "key": 145, + "value": 2 + }, + { + "key": 146, + "value": 2 + }, + { + "key": 147, + "value": 2 + }, + { + "key": 148, + "value": 2 + }, + { + "key": 149, + "value": 2 + }, + { + "key": 150, + "value": 2 + }, + { + "key": 151, + "value": 2 + }, + { + "key": 152, + "value": 2 + }, + { + "key": 153, + "value": 2 + }, + { + "key": 154, + "value": 2 + }, + { + "key": 155, + "value": 2 + }, + { + "key": 156, + "value": 2 + }, + { + "key": 157, + "value": 2 + }, + { + "key": 158, + "value": 2 + }, + { + "key": 159, + "value": 2 + }, + { + "key": 160, + "value": 2 + }, + { + "key": 161, + "value": 2 + }, + { + "key": 162, + "value": 2 + }, + { + "key": 163, + "value": 3 + }, + { + "key": 164, + "value": 3 + }, + { + "key": 165, + "value": 3 + }, + { + "key": 166, + "value": 3 + }, + { + "key": 167, + "value": 3 + }, + { + "key": 168, + "value": 3 + }, + { + "key": 169, + "value": 3 + }, + { + "key": 170, + "value": 3 + }, + { + "key": 171, + "value": 3 + }, + { + "key": 172, + "value": 3 + }, + { + "key": 173, + "value": 3 + }, + { + "key": 174, + "value": 3 + }, + { + "key": 175, + "value": 3 + }, + { + "key": 176, + "value": 3 + }, + { + "key": 177, + "value": 3 + }, + { + "key": 178, + "value": 3 + }, + { + "key": 179, + "value": 3 + }, + { + "key": 180, + "value": 3 + }, + { + "key": 181, + "value": 3 + }, + { + "key": 182, + "value": 3 + }, + { + "key": 183, + "value": 3 + }, + { + "key": 184, + "value": 3 + }, + { + "key": 185, + "value": 3 + }, + { + "key": 186, + "value": 4 + }, + { + "key": 187, + "value": 4 + }, + { + "key": 188, + "value": 4 + }, + { + "key": 189, + "value": 4 + }, + { + "key": 190, + "value": 4 + }, + { + "key": 191, + "value": 4 + }, + { + "key": 192, + "value": 4 + }, + { + "key": 193, + "value": 4 + }, + { + "key": 194, + "value": 4 + }, + { + "key": 195, + "value": 4 + }, + { + "key": 196, + "value": 4 + }, + { + "key": 197, + "value": 4 + }, + { + "key": 198, + "value": 4 + }, + { + "key": 199, + "value": 4 + }, + { + "key": 200, + "value": 4 + }, + { + "key": 201, + "value": 4 + }, + { + "key": 202, + "value": 4 + }, + { + "key": 203, + "value": 4 + }, + { + "key": 204, + "value": 4 + }, + { + "key": 205, + "value": 4 + }, + { + "key": 206, + "value": 5 + }, + { + "key": 207, + "value": 5 + }, + { + "key": 208, + "value": 5 + }, + { + "key": 209, + "value": 5 + }, + { + "key": 210, + "value": 5 + }, + { + "key": 211, + "value": 5 + }, + { + "key": 212, + "value": 5 + }, + { + "key": 213, + "value": 5 + }, + { + "key": 214, + "value": 5 + }, + { + "key": 215, + "value": 5 + }, + { + "key": 216, + "value": 5 + }, + { + "key": 217, + "value": 5 + }, + { + "key": 218, + "value": 5 + }, + { + "key": 219, + "value": 5 + }, + { + "key": 220, + "value": 5 + }, + { + "key": 221, + "value": 5 + }, + { + "key": 222, + "value": 5 + }, + { + "key": 223, + "value": 5 + }, + { + "key": 224, + "value": 6 + }, + { + "key": 225, + "value": 6 + }, + { + "key": 226, + "value": 6 + }, + { + "key": 227, + "value": 6 + }, + { + "key": 228, + "value": 6 + }, + { + "key": 229, + "value": 6 + }, + { + "key": 230, + "value": 6 + }, + { + "key": 231, + "value": 6 + }, + { + "key": 232, + "value": 6 + }, + { + "key": 233, + "value": 6 + }, + { + "key": 234, + "value": 6 + }, + { + "key": 235, + "value": 6 + }, + { + "key": 236, + "value": 6 + }, + { + "key": 237, + "value": 6 + }, + { + "key": 238, + "value": 6 + }, + { + "key": 239, + "value": 6 + }, + { + "key": 240, + "value": 7 + }, + { + "key": 241, + "value": 7 + }, + { + "key": 242, + "value": 7 + }, + { + "key": 243, + "value": 7 + }, + { + "key": 244, + "value": 7 + }, + { + "key": 245, + "value": 7 + }, + { + "key": 246, + "value": 7 + }, + { + "key": 247, + "value": 7 + }, + { + "key": 248, + "value": 7 + }, + { + "key": 249, + "value": 7 + }, + { + "key": 250, + "value": 7 + }, + { + "key": 251, + "value": 7 + }, + { + "key": 252, + "value": 7 + }, + { + "key": 253, + "value": 7 + }, + { + "key": 254, + "value": 7 + }, + { + "key": 255, + "value": 7 + }, + { + "key": 256, + "value": 8 + }, + { + "key": 257, + "value": 8 + }, + { + "key": 258, + "value": 8 + }, + { + "key": 259, + "value": 8 + }, + { + "key": 260, + "value": 8 + }, + { + "key": 261, + "value": 8 + }, + { + "key": 262, + "value": 8 + }, + { + "key": 263, + "value": 8 + }, + { + "key": 264, + "value": 8 + }, + { + "key": 265, + "value": 8 + }, + { + "key": 266, + "value": 8 + }, + { + "key": 267, + "value": 8 + }, + { + "key": 268, + "value": 8 + }, + { + "key": 269, + "value": 8 + }, + { + "key": 270, + "value": 8 + }, + { + "key": 271, + "value": 8 + }, + { + "key": 272, + "value": 8 + }, + { + "key": 273, + "value": 9 + }, + { + "key": 274, + "value": 9 + }, + { + "key": 275, + "value": 9 + }, + { + "key": 276, + "value": 9 + }, + { + "key": 277, + "value": 9 + }, + { + "key": 278, + "value": 9 + }, + { + "key": 279, + "value": 9 + }, + { + "key": 280, + "value": 9 + }, + { + "key": 281, + "value": 9 + }, + { + "key": 282, + "value": 9 + }, + { + "key": 283, + "value": 9 + }, + { + "key": 284, + "value": 9 + }, + { + "key": 285, + "value": 9 + }, + { + "key": 286, + "value": 9 + }, + { + "key": 287, + "value": 9 + }, + { + "key": 288, + "value": 9 + }, + { + "key": 289, + "value": 10 + }, + { + "key": 290, + "value": 10 + }, + { + "key": 291, + "value": 10 + }, + { + "key": 292, + "value": 10 + }, + { + "key": 293, + "value": 10 + }, + { + "key": 294, + "value": 10 + }, + { + "key": 295, + "value": 10 + }, + { + "key": 296, + "value": 10 + }, + { + "key": 297, + "value": 10 + }, + { + "key": 298, + "value": 10 + }, + { + "key": 299, + "value": 10 + }, + { + "key": 300, + "value": 10 + }, + { + "key": 301, + "value": 10 + }, + { + "key": 302, + "value": 10 + }, + { + "key": 303, + "value": 10 + }, + { + "key": 304, + "value": 10 + }, + { + "key": 305, + "value": 10 + }, + { + "key": 306, + "value": 10 + }, + { + "key": 307, + "value": 11 + }, + { + "key": 308, + "value": 11 + }, + { + "key": 309, + "value": 11 + }, + { + "key": 310, + "value": 11 + }, + { + "key": 311, + "value": 11 + }, + { + "key": 312, + "value": 11 + }, + { + "key": 313, + "value": 11 + }, + { + "key": 314, + "value": 11 + }, + { + "key": 315, + "value": 11 + }, + { + "key": 316, + "value": 11 + }, + { + "key": 317, + "value": 11 + }, + { + "key": 318, + "value": 11 + }, + { + "key": 319, + "value": 11 + }, + { + "key": 320, + "value": 11 + }, + { + "key": 321, + "value": 11 + }, + { + "key": 322, + "value": 11 + }, + { + "key": 323, + "value": 11 + }, + { + "key": 324, + "value": 11 + }, + { + "key": 325, + "value": 11 + }, + { + "key": 326, + "value": 11 + }, + { + "key": 327, + "value": 12 + }, + { + "key": 328, + "value": 12 + }, + { + "key": 329, + "value": 12 + }, + { + "key": 330, + "value": 12 + }, + { + "key": 331, + "value": 12 + }, + { + "key": 332, + "value": 12 + }, + { + "key": 333, + "value": 12 + }, + { + "key": 334, + "value": 12 + }, + { + "key": 335, + "value": 12 + }, + { + "key": 336, + "value": 12 + }, + { + "key": 337, + "value": 12 + }, + { + "key": 338, + "value": 12 + }, + { + "key": 339, + "value": 12 + }, + { + "key": 340, + "value": 12 + }, + { + "key": 341, + "value": 12 + }, + { + "key": 342, + "value": 12 + }, + { + "key": 343, + "value": 12 + }, + { + "key": 344, + "value": 12 + }, + { + "key": 345, + "value": 12 + }, + { + "key": 346, + "value": 12 + }, + { + "key": 347, + "value": 12 + }, + { + "key": 348, + "value": 12 + }, + { + "key": 349, + "value": 12 + }, + { + "key": 350, + "value": 13 + }, + { + "key": 351, + "value": 13 + }, + { + "key": 352, + "value": 13 + }, + { + "key": 353, + "value": 13 + }, + { + "key": 354, + "value": 13 + }, + { + "key": 355, + "value": 13 + }, + { + "key": 356, + "value": 13 + }, + { + "key": 357, + "value": 13 + }, + { + "key": 358, + "value": 13 + }, + { + "key": 359, + "value": 13 + }, + { + "key": 360, + "value": 13 + }, + { + "key": 361, + "value": 13 + }, + { + "key": 362, + "value": 13 + }, + { + "key": 363, + "value": 13 + }, + { + "key": 364, + "value": 13 + }, + { + "key": 365, + "value": 13 + }, + { + "key": 366, + "value": 13 + }, + { + "key": 367, + "value": 13 + }, + { + "key": 368, + "value": 13 + }, + { + "key": 369, + "value": 13 + }, + { + "key": 370, + "value": 13 + }, + { + "key": 371, + "value": 13 + }, + { + "key": 372, + "value": 13 + }, + { + "key": 373, + "value": 13 + }, + { + "key": 374, + "value": 13 + }, + { + "key": 375, + "value": 13 + }, + { + "key": 376, + "value": 13 + }, + { + "key": 377, + "value": 13 + }, + { + "key": 378, + "value": 13 + }, + { + "key": 379, + "value": 13 + }, + { + "key": 380, + "value": 13 + }, + { + "key": 381, + "value": 14 + }, + { + "key": 382, + "value": 14 + }, + { + "key": 383, + "value": 14 + }, + { + "key": 384, + "value": 14 + }, + { + "key": 385, + "value": 14 + }, + { + "key": 386, + "value": 14 + }, + { + "key": 387, + "value": 14 + }, + { + "key": 388, + "value": 14 + }, + { + "key": 389, + "value": 14 + }, + { + "key": 390, + "value": 14 + }, + { + "key": 391, + "value": 14 + }, + { + "key": 392, + "value": 14 + }, + { + "key": 393, + "value": 14 + }, + { + "key": 394, + "value": 14 + }, + { + "key": 395, + "value": 14 + }, + { + "key": 396, + "value": 14 + }, + { + "key": 397, + "value": 14 + }, + { + "key": 398, + "value": 14 + }, + { + "key": 399, + "value": 14 + }, + { + "key": 400, + "value": 14 + }, + { + "key": 401, + "value": 14 + }, + { + "key": 402, + "value": 14 + }, + { + "key": 403, + "value": 14 + }, + { + "key": 404, + "value": 14 + }, + { + "key": 405, + "value": 14 + }, + { + "key": 406, + "value": 14 + }, + { + "key": 407, + "value": 14 + }, + { + "key": 408, + "value": 14 + }, + { + "key": 409, + "value": 14 + }, + { + "key": 410, + "value": 14 + }, + { + "key": 411, + "value": 14 + }, + { + "key": 412, + "value": 14 + }, + { + "key": 413, + "value": 14 + }, + { + "key": 414, + "value": 14 + }, + { + "key": 415, + "value": 14 + }, + { + "key": 416, + "value": 14 + }, + { + "key": 417, + "value": 14 + }, + { + "key": 418, + "value": 14 + }, + { + "key": 419, + "value": 14 + }, + { + "key": 420, + "value": 14 + }, + { + "key": 421, + "value": 14 + }, + { + "key": 422, + "value": 14 + }, + { + "key": 423, + "value": 14 + }, + { + "key": 424, + "value": 14 + }, + { + "key": 425, + "value": 14 + }, + { + "key": 426, + "value": 14 + }, + { + "key": 427, + "value": 14 + }, + { + "key": 428, + "value": 14 + }, + { + "key": 429, + "value": 14 + }, + { + "key": 430, + "value": 15 + }, + { + "key": 431, + "value": 15 + }, + { + "key": 432, + "value": 15 + }, + { + "key": 433, + "value": 15 + }, + { + "key": 434, + "value": 15 + }, + { + "key": 435, + "value": 15 + }, + { + "key": 436, + "value": 15 + }, + { + "key": 437, + "value": 15 + }, + { + "key": 438, + "value": 15 + }, + { + "key": 439, + "value": 15 + }, + { + "key": 440, + "value": 15 + }, + { + "key": 441, + "value": 15 + }, + { + "key": 442, + "value": 15 + }, + { + "key": 443, + "value": 15 + }, + { + "key": 444, + "value": 15 + }, + { + "key": 445, + "value": 15 + }, + { + "key": 446, + "value": 15 + }, + { + "key": 447, + "value": 15 + }, + { + "key": 448, + "value": 15 + }, + { + "key": 449, + "value": 15 + }, + { + "key": 450, + "value": 15 + }, + { + "key": 451, + "value": 15 + }, + { + "key": 452, + "value": 15 + }, + { + "key": 453, + "value": 15 + }, + { + "key": 454, + "value": 15 + }, + { + "key": 455, + "value": 15 + }, + { + "key": 456, + "value": 15 + }, + { + "key": 457, + "value": 15 + }, + { + "key": 458, + "value": 15 + }, + { + "key": 459, + "value": 15 + }, + { + "key": 460, + "value": 15 + }, + { + "key": 461, + "value": 15 + }, + { + "key": 462, + "value": 15 + }, + { + "key": 463, + "value": 15 + }, + { + "key": 464, + "value": 15 + }, + { + "key": 465, + "value": 15 + }, + { + "key": 466, + "value": 15 + }, + { + "key": 467, + "value": 15 + }, + { + "key": 468, + "value": 15 + }, + { + "key": 469, + "value": 15 + }, + { + "key": 470, + "value": 15 + }, + { + "key": 471, + "value": 15 + }, + { + "key": 472, + "value": 15 + }, + { + "key": 473, + "value": 15 + }, + { + "key": 474, + "value": 15 + }, + { + "key": 475, + "value": 15 + }, + { + "key": 476, + "value": 15 + }, + { + "key": 477, + "value": 15 + }, + { + "key": 478, + "value": 15 + }, + { + "key": 479, + "value": 15 + }, + { + "key": 480, + "value": 15 + }, + { + "key": 481, + "value": 15 + }, + { + "key": 482, + "value": 15 + }, + { + "key": 483, + "value": 15 + }, + { + "key": 484, + "value": 15 + }, + { + "key": 485, + "value": 15 + }, + { + "key": 486, + "value": 15 + }, + { + "key": 487, + "value": 15 + }, + { + "key": 488, + "value": 15 + }, + { + "key": 489, + "value": 15 + }, + { + "key": 490, + "value": 15 + }, + { + "key": 491, + "value": 15 + }, + { + "key": 492, + "value": 15 + }, + { + "key": 493, + "value": 15 + }, + { + "key": 494, + "value": 15 + }, + { + "key": 495, + "value": 15 + }, + { + "key": 496, + "value": 15 + }, + { + "key": 497, + "value": 15 + }, + { + "key": 498, + "value": 15 + }, + { + "key": 499, + "value": 15 + }, + { + "key": 500, + "value": 15 + }, + { + "key": 501, + "value": 15 + }, + { + "key": 502, + "value": 15 + }, + { + "key": 503, + "value": 15 + }, + { + "key": 504, + "value": 15 + }, + { + "key": 505, + "value": 15 + }, + { + "key": 506, + "value": 15 + }, + { + "key": 507, + "value": 15 + }, + { + "key": 508, + "value": 15 + }, + { + "key": 509, + "value": 15 + }, + { + "key": 510, + "value": 15 + }, + { + "key": 511, + "value": 15 + } +] diff --git a/sigmoid.py b/sigmoid.py new file mode 100644 index 0000000..0d5cb57 --- /dev/null +++ b/sigmoid.py @@ -0,0 +1,103 @@ +import numpy as np +import matplotlib.pyplot as plt +import pywt +import json + +def Sigmoid(x, mean=0, std=1): + return 1 / (1 + np.exp(-x)) + +def trunc(x, f): + return np.floor(x * 2**(-f)) + +def real_to_fixed(x, f): + x_fixed = np.floor(x * 2**f) + return x_fixed + +def fixed_to_real(x, f): + return x / 2**f + +def lut_gen(func, a, b, f, q): + """Create a Look Up Table for func. + a: lower boundary + b: upper boundary + f: precision + q: LUT size + """ + x = np.linspace(a, b-2**-q, (b-a)*2**q) + value = real_to_fixed(func(x), f) + return value + +def lut_eval(x, T): + y = x.copy() + for i, e in enumerate(x): + y[i] = T[int(e)] + return y + +a = -4 +b = 4 +f = 12 +q = 6 + +T = lut_gen(Sigmoid, a, b, f, q) +json_array = [] +for index, value in np.ndenumerate(T): + element = { + "key": index[0], + "value": int(value) & 0xFF, + } + json_array.append(element) +with open("lut16_quantized_lsb.json", 'w') as fout: + json_dumps_str = json.dumps(json_array, indent=4) + print(json_dumps_str, file=fout) + +json_array = [] +for index, value in np.ndenumerate(T): + element = { + "key": index[0], + "value": (int(value) >> 8) & 0xFF, + } + json_array.append(element) +with open("lut16_quantized_msb.json", 'w') as fout: + json_dumps_str = json.dumps(json_array, indent=4) + print(json_dumps_str, file=fout) + +print(f"LUT Size: {len(T)}") + +x = np.linspace(a, b-2**-f, (b-a)*2**f) +y = Sigmoid(x) +y_fixed = real_to_fixed(y, f) + +x_fixed = real_to_fixed(x, f) +x_trunced = trunc(x_fixed, f - q) +x_shifted = x_trunced - np.min(x_trunced) +y_lut = lut_eval(x_shifted, T) + +diff = abs(y_fixed - y_lut) +print(f"{max(diff)=}") +print(f"{sum(diff)/len(diff)=}") + +n = 7 + +coeffs = pywt.wavedec(y, 'haar', level=n) + +json_array = [] +for index, value in np.ndenumerate(coeffs[0]): + element = { + "key": index, + "value": value, + } + json_array.append(element) +with open("lut16.json", 'w') as fout: + json_dumps_str = json.dumps(json_array, indent=4) + print(json_dumps_str, file=fout) + +# Set detail coefficients at level m and higher to zero +for i in range(1, n+1): + coeffs[i] = np.zeros_like(coeffs[i]) +# Reconstruct the approximation +print(f"DWT LUT Size: {len(coeffs[0])}") +y_hat = real_to_fixed(pywt.waverec(coeffs, 'haar'), f) + +diff = abs(y_fixed - y_hat) +print(f"{max(diff)=}") +print(f"{sum(diff)/len(diff)=}") \ No newline at end of file diff --git a/src/common.rs b/src/common.rs index 4400fdf..e92a285 100644 --- a/src/common.rs +++ b/src/common.rs @@ -44,10 +44,7 @@ pub fn exponential(x: u64, input_precision: u8, output_precision: u8, bit_width: let x = to_signed(x, bit_width) as f64; let shift = (1u128 << input_precision) as f64; let exp = (x / shift).exp(); - let ret = (exp * ((1u128 << output_precision) as f64)) as u64; - // println!("\t exp {x:?} --> {:?}", &ret); - ret - // ((1.0 / (1.0 + exp)) * (1 << output_precision) as f64) as u64 + (exp * ((1u128 << output_precision) as f64)) as u64 } pub fn argmax(slice: &[T]) -> Option { @@ -83,13 +80,13 @@ pub fn quantize_weights_and_biases( .iter() .map(|row| { row.iter() - .map(|&w| quantize(w.into(), precision, bit_width)) + .map(|&w| quantize(w, precision, bit_width)) .collect::>() }) .collect::>(); let bias_int = biases .iter() - .map(|&w| quantize(w.into(), precision, bit_width)) + .map(|&w| quantize(w, precision, bit_width)) .collect::>(); (weights_int, bias_int) diff --git a/src/encrypted_lr.rs b/src/encrypted_lr.rs index aa46dfd..5baf405 100644 --- a/src/encrypted_lr.rs +++ b/src/encrypted_lr.rs @@ -84,7 +84,7 @@ fn main() { let mut prediction = server_key.create_trivial_radix(scaled_bias, nb_blocks.into()); for (s, &weight) in sample.iter_mut().zip(model.iter()) { - let mut d: u64 = client_key.decrypt(&s); + let mut d: u64 = client_key.decrypt(s); println!("s: {:?}", d); println!("weight: {:?}", weight); let ct_prod = server_key.smart_scalar_mul(s, weight); diff --git a/src/encrypted_lr_dwt.rs b/src/encrypted_lr_dwt.rs new file mode 100644 index 0000000..e05b829 --- /dev/null +++ b/src/encrypted_lr_dwt.rs @@ -0,0 +1,212 @@ +use std::{collections::HashMap, fs::File, time::Instant}; + +use fhe_lut::common::*; +use rayon::prelude::*; +use serde::{Deserialize, Serialize}; +use tfhe::{ + integer::{ + gen_keys_radix, wopbs::*, IntegerCiphertext, IntegerRadixCiphertext, RadixCiphertext, + }, + shortint::parameters::{ + parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, + PARAM_MESSAGE_2_CARRY_2_KS_PBS, + }, +}; + +#[derive(Debug, Serialize, Deserialize)] +struct KeyValue { + key: u64, + value: u64, +} + +fn eval_sigmoid(x: u64, sigmoid_map: &HashMap) -> u64 { + sigmoid_map[&x] +} + +fn main() { + let file = File::open("lut16_quantized_lsb.json").unwrap(); + let json = serde_json::from_reader(file).unwrap(); + let kv_lut: Vec = serde_json::from_value(json).unwrap(); + let mut lut_lsb: HashMap = HashMap::new(); + for entry in kv_lut { + lut_lsb.insert(entry.key, entry.value); + } + let file = File::open("lut16_quantized_msb.json").unwrap(); + let json = serde_json::from_reader(file).unwrap(); + let kv_lut: Vec = serde_json::from_value(json).unwrap(); + let mut lut_msb: HashMap = HashMap::new(); + for entry in kv_lut { + lut_msb.insert(entry.key, entry.value); + } + // ------- Client side ------- // + let bit_width = 16u8; + let precision = bit_width >> 2; + assert!(precision <= bit_width / 2); + + // Number of blocks per ciphertext + let nb_blocks = bit_width >> 2; + println!("Number of blocks: {:?}", nb_blocks); + + let start = Instant::now(); + // Generate radix keys + let (client_key, server_key) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, nb_blocks.into()); + + // Generate key for PBS (without padding) + let wopbs_key = WopbsKey::new_wopbs_key( + &client_key, + &server_key, + &WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, + ); + println!( + "Key generation done in {:?} sec.", + start.elapsed().as_secs_f64() + ); + + let (weights, biases) = load_weights_and_biases(); + let (weights_int, bias_int) = + quantize_weights_and_biases(&weights, &biases, precision, bit_width); + let (iris_dataset, targets) = prepare_iris_dataset(); + let num_features = iris_dataset[0].len(); + let (means, stds) = means_and_stds(&iris_dataset, num_features); + + let start = Instant::now(); + let mut encrypted_dataset: Vec> = iris_dataset + .par_iter() // Use par_iter() for parallel iteration + .map(|sample| { + sample + .par_iter() + .zip(means.par_iter().zip(stds.par_iter())) + .map(|(&s, (mean, std))| { + let quantized = quantize((s - mean) / std, precision, bit_width); + let mut lsb = client_key + .encrypt(quantized & (1 << ((nb_blocks << 1) - 1))) + .into_blocks(); // Get LSBs + let msb = client_key + .encrypt(quantized >> (nb_blocks << 1)) + .into_blocks(); // Get MSBs + lsb.extend(msb); + RadixCiphertext::from_blocks(lsb) + }) + .collect() + }) + .collect(); + println!( + "Encryption done in {:?} sec.", + start.elapsed().as_secs_f64() + ); + + // ------- Server side ------- // + + // Dummy encryption for building LUTs + let ct_dummy = client_key.encrypt(2_u64); + let mut ct_test = ct_dummy.clone(); + let ct_dummy = wopbs_key.keyswitch_to_wopbs_params(&server_key, &ct_dummy); + + // Build LUT for Sigmoid + let sigmoid_lut_lsb = + wopbs_key.generate_lut_radix(&ct_dummy, |x: u64| eval_sigmoid(x, &lut_lsb)); + let sigmoid_lut_msb = + wopbs_key.generate_lut_radix(&ct_dummy, |x: u64| eval_sigmoid(x, &lut_msb)); + + // FIXME: LUT TEST + let mut ct_dummy = wopbs_key.keyswitch_to_pbs_params(&ct_dummy); + ct_dummy = server_key.smart_mul(&mut ct_dummy, &mut ct_test); + let test: u64 = client_key.decrypt(&ct_dummy); + println!("Test Input: {:?}", &test); + println!("Expected activation (LSB): {:?}", &lut_lsb[&test]); + println!("Expected activation (MSB): {:?}", &lut_msb[&test]); + let ct_dummy = wopbs_key.keyswitch_to_wopbs_params(&server_key, &ct_dummy); + let activation_lsb = wopbs_key.wopbs(&ct_dummy, &sigmoid_lut_lsb); + let activation_lsb = wopbs_key.keyswitch_to_pbs_params(&activation_lsb); + let test: u64 = client_key.decrypt(&activation_lsb); + println!("Activation (LSB): {:?}", &test); + let activation_msb = wopbs_key.wopbs(&ct_dummy, &sigmoid_lut_msb); + let activation_msb = wopbs_key.keyswitch_to_pbs_params(&activation_msb); + let test: u64 = client_key.decrypt(&activation_msb); + println!("Activation (MSB): {:?}", &test); + + let encrypted_dataset_short = encrypted_dataset.get_mut(0..1).unwrap(); + let all_probabilities = encrypted_dataset_short + .iter_mut() + .enumerate() + .map(|(cnt, sample)| { + let start = Instant::now(); + let probabilities = weights_int + .iter() + .zip(bias_int.iter()) + // .par_iter() + // .zip(bias_int.par_iter()) + .map(|(model, &bias)| { + let scaled_bias = mul(1 << precision, bias, bit_width); + let mut prediction = + server_key.create_trivial_radix(scaled_bias, (nb_blocks << 1).into()); + for (s, &weight) in sample.iter_mut().zip(model.iter()) { + let ct_prod = server_key.smart_scalar_mul(s, weight); + prediction = server_key.unchecked_add(&ct_prod, &prediction); + } + let test: u64 = client_key.decrypt(&prediction); + println!("Original: {:?}", &test); + // Truncate + let prediction_blocks = &prediction.clone().into_blocks() + [(nb_blocks as usize)..((nb_blocks << 1) as usize)]; + let prediction_msb = RadixCiphertext::from_blocks(prediction_blocks.to_vec()); + // For some reason, the truncation is off by 1... + let prediction_msb = server_key.unchecked_scalar_add(&prediction_msb, 1); + let test: u64 = client_key.decrypt(&prediction_msb); + println!("Truncated: {:?}", &test); + println!("Expected activation (LSB): {:?}", &lut_lsb[&test]); + println!("Expected activation (MSB): {:?}", &lut_msb[&test]); + // Keyswitch and Bootstrap + prediction = wopbs_key.keyswitch_to_wopbs_params(&server_key, &prediction_msb); + let activation_lsb = wopbs_key.wopbs(&prediction, &sigmoid_lut_lsb); + let activation_lsb = wopbs_key.keyswitch_to_pbs_params(&activation_lsb); + let test: u64 = client_key.decrypt(&activation_lsb); + println!("Activation (LSB): {:?}", &test); + let activation_msb = wopbs_key.wopbs(&prediction, &sigmoid_lut_msb); + let activation_msb = wopbs_key.keyswitch_to_pbs_params(&activation_msb); + let test: u64 = client_key.decrypt(&activation_msb); + println!("Activation (MSB): {:?}", &test); + + // let (activation_lsb, activation_msb) = rayon::join( + // || { + // let activation_lsb = wopbs_key.wopbs(&prediction, &sigmoid_lut_lsb); + // wopbs_key.keyswitch_to_pbs_params(&activation_lsb) + // }, + // || { + // let activation_msb = wopbs_key.wopbs(&prediction, &sigmoid_lut_msb); + // wopbs_key.keyswitch_to_pbs_params(&activation_msb) + // } + // ); + let mut lsb_blocks = activation_lsb.clone().into_blocks(); + let msb_blocks = activation_msb.clone().into_blocks(); + lsb_blocks.extend(msb_blocks); + RadixCiphertext::from_blocks(lsb_blocks) + }) + .collect::>(); + println!( + "Finished inference #{:?} in {:?} sec.", + cnt, + start.elapsed().as_secs_f64() + ); + probabilities + }) + .collect::>(); + // } + + // ------- Client side ------- // + let mut total = 0; + for (num, (target, probabilities)) in targets.iter().zip(all_probabilities.iter()).enumerate() { + let ptxt_probabilities = probabilities + .iter() + .map(|p| client_key.decrypt(p)) + .collect::>(); + println!("{:?}", ptxt_probabilities); + let class = argmax(&ptxt_probabilities).unwrap(); + println!("[{}] predicted {:?}, target {:?}", num, class, target); + if class == *target { + total += 1; + } + } + let accuracy = (total as f32 / encrypted_dataset_short.len() as f32) * 100.0; + println!("Accuracy {accuracy}%"); +} diff --git a/src/lut_split_test.rs b/src/lut_split_test.rs new file mode 100644 index 0000000..08a3639 --- /dev/null +++ b/src/lut_split_test.rs @@ -0,0 +1,49 @@ +use std::time::Instant; + +use tfhe::{ + integer::{ + gen_keys_radix, wopbs::*, IntegerCiphertext, IntegerRadixCiphertext, RadixCiphertext, + }, + shortint::parameters::{ + parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, + PARAM_MESSAGE_2_CARRY_2_KS_PBS, + }, +}; + +fn main() { + let nb_block = 4; + let msg = 14; + let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, nb_block); + let wopbs_key = WopbsKey::new_wopbs_key(&cks, &sks, &WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS); + + let ct = cks.encrypt(msg); + let ct = wopbs_key.keyswitch_to_wopbs_params(&sks, &ct); + + let lut_lsb = wopbs_key.generate_lut_radix(&ct, |x| u64::pow(x, 2) % (1 << 4)); + let lut_msb = wopbs_key.generate_lut_radix(&ct, |x| (u64::pow(x, 2) >> 4) % (1 << 4)); + + let start = Instant::now(); + let (ct_res_lsb, ct_res_msb) = rayon::join( + || { + let ct_res_lsb = wopbs_key.wopbs(&ct, &lut_lsb); + wopbs_key.keyswitch_to_pbs_params(&ct_res_lsb) + }, + || { + let ct_res_msb = wopbs_key.wopbs(&ct, &lut_msb); + wopbs_key.keyswitch_to_pbs_params(&ct_res_msb) + }, + ); + + let mut lsb_blocks = ct_res_lsb.clone().into_blocks(); + let msb_blocks = ct_res_msb.clone().into_blocks(); + lsb_blocks.extend(msb_blocks); + let _ct_res = RadixCiphertext::from_blocks(lsb_blocks); + let duration = start.elapsed(); + println!("PBS time: {:?}", duration); + let res_lsb: u64 = cks.decrypt(&ct_res_lsb); + let res_msb: u64 = cks.decrypt(&ct_res_msb); + + assert_eq!(res_lsb, u64::pow(msg, 2) % (1 << 4)); + assert_eq!(res_msb, (u64::pow(msg, 2) >> 4) % (1 << 4)); + assert_eq!((res_msb << 4) + res_lsb, u64::pow(msg, 2)); +} diff --git a/src/lut_test.rs b/src/lut_test.rs new file mode 100644 index 0000000..7bef123 --- /dev/null +++ b/src/lut_test.rs @@ -0,0 +1,25 @@ +use std::time::Instant; + +use tfhe::{ + integer::{gen_keys_radix, wopbs::*}, + shortint::parameters::{ + parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, + PARAM_MESSAGE_2_CARRY_2_KS_PBS, + }, +}; + +fn main() { + let nb_block = 8; + let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, nb_block); + let wopbs_key = WopbsKey::new_wopbs_key(&cks, &sks, &WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS); + let ct = cks.encrypt(2_u64); + let ct = wopbs_key.keyswitch_to_wopbs_params(&sks, &ct); + let lut = wopbs_key.generate_lut_radix(&ct, |x| 5 + 2 * x); + let start = Instant::now(); + let ct_res = wopbs_key.wopbs(&ct, &lut); + let ct_res = wopbs_key.keyswitch_to_pbs_params(&ct_res); + let duration = start.elapsed(); + println!("PBS time: {:?}", duration); + let res: u64 = cks.decrypt(&ct_res); + assert_eq!(res, 9); +}