From a84a9313f019661941c9304dc7aab6ac5736ffa5 Mon Sep 17 00:00:00 2001 From: Robin Hundt <24554122+robinhundt@users.noreply.github.com> Date: Mon, 5 Feb 2024 13:42:26 +0100 Subject: [PATCH] SEEC: Use low-depth adder in mixed-gmw --- Cargo.lock | 7 + crates/seec-channel/src/tcp.rs | 3 + crates/seec/Cargo.toml | 1 + crates/seec/circuits/int_add16_depth.bristol | 128 ++++ crates/seec/circuits/int_add32_depth.bristol | 304 ++++++++ crates/seec/circuits/int_add64_depth.bristol | 704 +++++++++++++++++++ crates/seec/circuits/int_add8_depth.bristol | 52 ++ crates/seec/src/circuit/base_circuit.rs | 26 +- crates/seec/src/circuit/mod.rs | 17 +- crates/seec/src/executor.rs | 18 +- crates/seec/src/mul_triple/storage.rs | 3 + crates/seec/src/parse/fuse/mod.rs | 13 +- crates/seec/src/private_test_utils.rs | 20 +- crates/seec/src/protocols/aby2.rs | 3 +- crates/seec/src/protocols/mixed_gmw.rs | 67 +- crates/seec/src/protocols/tensor_aby2.rs | 3 +- 16 files changed, 1339 insertions(+), 30 deletions(-) create mode 100644 crates/seec/circuits/int_add16_depth.bristol create mode 100644 crates/seec/circuits/int_add32_depth.bristol create mode 100644 crates/seec/circuits/int_add64_depth.bristol create mode 100644 crates/seec/circuits/int_add8_depth.bristol diff --git a/Cargo.lock b/Cargo.lock index 5b06e9f..740b4bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2045,6 +2045,7 @@ dependencies = [ "tracing-appender", "tracing-subscriber", "typemap", + "typemap_rev", "zappot", ] @@ -2528,6 +2529,12 @@ dependencies = [ "unsafe-any", ] +[[package]] +name = "typemap_rev" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74b08b0c1257381af16a5c3605254d529d3e7e109f3c62befc5d168968192998" + [[package]] name = "typenum" version = "1.17.0" diff --git a/crates/seec-channel/src/tcp.rs b/crates/seec-channel/src/tcp.rs index 8381208..80a7b92 100644 --- a/crates/seec-channel/src/tcp.rs +++ b/crates/seec-channel/src/tcp.rs @@ -134,11 +134,14 @@ mod tests { use crate::tcp::new_local_pair; use remoc::codec; use remoc::rch::mpsc::channel; + use std::time::Duration; #[tokio::test] async fn establish_connection() { let (ch1, ch2) = new_local_pair::<()>(None).await.unwrap(); + // Sleep to ensure values have been actually sent and counters are correct + tokio::time::sleep(Duration::from_millis(10)).await; let (_tx1, bytes_written1, _rx1, bytes_read1) = ch1; let (_tx2, bytes_written2, _rx2, bytes_read2) = ch2; assert_eq!(bytes_written1.get(), bytes_read2.get()); diff --git a/crates/seec/Cargo.toml b/crates/seec/Cargo.toml index c9df1e1..32db71e 100644 --- a/crates/seec/Cargo.toml +++ b/crates/seec/Cargo.toml @@ -68,6 +68,7 @@ seec-channel = { path = "../seec-channel" } remoc = { workspace = true } zappot = { path = "../zappot" } typemap = "0.3.3" +typemap_rev = "0.3.0" seec-bitmatrix = { path = "../seec-bitmatrix" } either = "1.10.0" flatbuffers = "23.5.26" diff --git a/crates/seec/circuits/int_add16_depth.bristol b/crates/seec/circuits/int_add16_depth.bristol new file mode 100644 index 0000000..2a95757 --- /dev/null +++ b/crates/seec/circuits/int_add16_depth.bristol @@ -0,0 +1,128 @@ +125 157 +16 16 16 + +2 1 0 16 141 XOR +2 1 1 17 32 XOR +2 1 0 16 33 AND +2 1 32 33 142 XOR +2 1 2 18 34 XOR +2 1 1 17 35 AND +2 1 32 33 36 AND +2 1 35 36 37 XOR +2 1 34 37 143 XOR +2 1 3 19 38 XOR +2 1 2 18 39 AND +2 1 34 35 40 AND +2 1 39 40 41 XOR +2 1 34 32 42 AND +2 1 42 33 43 AND +2 1 41 43 44 XOR +2 1 38 44 144 XOR +2 1 4 20 45 XOR +2 1 3 19 46 AND +2 1 38 44 47 AND +2 1 46 47 48 XOR +2 1 45 48 145 XOR +2 1 5 21 49 XOR +2 1 4 20 50 AND +2 1 45 46 51 AND +2 1 50 51 52 XOR +2 1 45 38 53 AND +2 1 53 44 54 AND +2 1 52 54 55 XOR +2 1 49 55 146 XOR +2 1 6 22 56 XOR +2 1 5 21 57 AND +2 1 49 52 58 AND +2 1 57 58 59 XOR +2 1 49 53 60 AND +2 1 60 44 61 AND +2 1 59 61 62 XOR +2 1 56 62 147 XOR +2 1 7 23 63 XOR +2 1 6 22 64 AND +2 1 56 57 65 AND +2 1 64 65 66 XOR +2 1 56 49 67 AND +2 1 67 52 68 AND +2 1 66 68 69 XOR +2 1 67 53 70 AND +2 1 70 44 71 AND +2 1 69 71 72 XOR +2 1 63 72 148 XOR +2 1 8 24 73 XOR +2 1 7 23 74 AND +2 1 63 72 75 AND +2 1 74 75 76 XOR +2 1 73 76 149 XOR +2 1 9 25 77 XOR +2 1 8 24 78 AND +2 1 73 74 79 AND +2 1 78 79 80 XOR +2 1 73 63 81 AND +2 1 81 72 82 AND +2 1 80 82 83 XOR +2 1 77 83 150 XOR +2 1 10 26 84 XOR +2 1 9 25 85 AND +2 1 77 80 86 AND +2 1 85 86 87 XOR +2 1 77 81 88 AND +2 1 88 72 89 AND +2 1 87 89 90 XOR +2 1 84 90 151 XOR +2 1 11 27 91 XOR +2 1 10 26 92 AND +2 1 84 85 93 AND +2 1 92 93 94 XOR +2 1 84 77 95 AND +2 1 95 80 96 AND +2 1 94 96 97 XOR +2 1 95 81 98 AND +2 1 98 72 99 AND +2 1 97 99 100 XOR +2 1 91 100 152 XOR +2 1 12 28 101 XOR +2 1 11 27 102 AND +2 1 91 97 103 AND +2 1 102 103 104 XOR +2 1 91 98 105 AND +2 1 105 72 106 AND +2 1 104 106 107 XOR +2 1 101 107 153 XOR +2 1 13 29 108 XOR +2 1 12 28 109 AND +2 1 101 102 110 AND +2 1 109 110 111 XOR +2 1 101 91 112 AND +2 1 112 97 113 AND +2 1 111 113 114 XOR +2 1 112 98 115 AND +2 1 115 72 116 AND +2 1 114 116 117 XOR +2 1 108 117 154 XOR +2 1 14 30 118 XOR +2 1 13 29 119 AND +2 1 108 111 120 AND +2 1 119 120 121 XOR +2 1 108 112 122 AND +2 1 122 97 123 AND +2 1 121 123 124 XOR +2 1 122 98 125 AND +2 1 125 72 126 AND +2 1 124 126 127 XOR +2 1 118 127 155 XOR +2 1 15 31 128 XOR +2 1 14 30 129 AND +2 1 118 119 130 AND +2 1 129 130 131 XOR +2 1 118 108 132 AND +2 1 132 111 133 AND +2 1 131 133 134 XOR +2 1 132 112 135 AND +2 1 135 97 136 AND +2 1 134 136 137 XOR +2 1 135 98 138 AND +2 1 138 72 139 AND +2 1 137 139 140 XOR +2 1 128 140 156 XOR diff --git a/crates/seec/circuits/int_add32_depth.bristol b/crates/seec/circuits/int_add32_depth.bristol new file mode 100644 index 0000000..de5c4ef --- /dev/null +++ b/crates/seec/circuits/int_add32_depth.bristol @@ -0,0 +1,304 @@ +301 365 +32 32 32 + +2 1 0 32 333 XOR +2 1 1 33 64 XOR +2 1 0 32 65 AND +2 1 64 65 334 XOR +2 1 2 34 66 XOR +2 1 1 33 67 AND +2 1 64 65 68 AND +2 1 67 68 69 XOR +2 1 66 69 335 XOR +2 1 3 35 70 XOR +2 1 2 34 71 AND +2 1 66 67 72 AND +2 1 71 72 73 XOR +2 1 66 64 74 AND +2 1 74 65 75 AND +2 1 73 75 76 XOR +2 1 70 76 336 XOR +2 1 4 36 77 XOR +2 1 3 35 78 AND +2 1 70 76 79 AND +2 1 78 79 80 XOR +2 1 77 80 337 XOR +2 1 5 37 81 XOR +2 1 4 36 82 AND +2 1 77 78 83 AND +2 1 82 83 84 XOR +2 1 77 70 85 AND +2 1 85 76 86 AND +2 1 84 86 87 XOR +2 1 81 87 338 XOR +2 1 6 38 88 XOR +2 1 5 37 89 AND +2 1 81 84 90 AND +2 1 89 90 91 XOR +2 1 81 85 92 AND +2 1 92 76 93 AND +2 1 91 93 94 XOR +2 1 88 94 339 XOR +2 1 7 39 95 XOR +2 1 6 38 96 AND +2 1 88 89 97 AND +2 1 96 97 98 XOR +2 1 88 81 99 AND +2 1 99 84 100 AND +2 1 98 100 101 XOR +2 1 99 85 102 AND +2 1 102 76 103 AND +2 1 101 103 104 XOR +2 1 95 104 340 XOR +2 1 8 40 105 XOR +2 1 7 39 106 AND +2 1 95 104 107 AND +2 1 106 107 108 XOR +2 1 105 108 341 XOR +2 1 9 41 109 XOR +2 1 8 40 110 AND +2 1 105 106 111 AND +2 1 110 111 112 XOR +2 1 105 95 113 AND +2 1 113 104 114 AND +2 1 112 114 115 XOR +2 1 109 115 342 XOR +2 1 10 42 116 XOR +2 1 9 41 117 AND +2 1 109 112 118 AND +2 1 117 118 119 XOR +2 1 109 113 120 AND +2 1 120 104 121 AND +2 1 119 121 122 XOR +2 1 116 122 343 XOR +2 1 11 43 123 XOR +2 1 10 42 124 AND +2 1 116 117 125 AND +2 1 124 125 126 XOR +2 1 116 109 127 AND +2 1 127 112 128 AND +2 1 126 128 129 XOR +2 1 127 113 130 AND +2 1 130 104 131 AND +2 1 129 131 132 XOR +2 1 123 132 344 XOR +2 1 12 44 133 XOR +2 1 11 43 134 AND +2 1 123 129 135 AND +2 1 134 135 136 XOR +2 1 123 130 137 AND +2 1 137 104 138 AND +2 1 136 138 139 XOR +2 1 133 139 345 XOR +2 1 13 45 140 XOR +2 1 12 44 141 AND +2 1 133 134 142 AND +2 1 141 142 143 XOR +2 1 133 123 144 AND +2 1 144 129 145 AND +2 1 143 145 146 XOR +2 1 144 130 147 AND +2 1 147 104 148 AND +2 1 146 148 149 XOR +2 1 140 149 346 XOR +2 1 14 46 150 XOR +2 1 13 45 151 AND +2 1 140 143 152 AND +2 1 151 152 153 XOR +2 1 140 144 154 AND +2 1 154 129 155 AND +2 1 153 155 156 XOR +2 1 154 130 157 AND +2 1 157 104 158 AND +2 1 156 158 159 XOR +2 1 150 159 347 XOR +2 1 15 47 160 XOR +2 1 14 46 161 AND +2 1 150 151 162 AND +2 1 161 162 163 XOR +2 1 150 140 164 AND +2 1 164 143 165 AND +2 1 163 165 166 XOR +2 1 164 144 167 AND +2 1 167 129 168 AND +2 1 166 168 169 XOR +2 1 167 130 170 AND +2 1 170 104 171 AND +2 1 169 171 172 XOR +2 1 160 172 348 XOR +2 1 16 48 173 XOR +2 1 15 47 174 AND +2 1 160 172 175 AND +2 1 174 175 176 XOR +2 1 173 176 349 XOR +2 1 17 49 177 XOR +2 1 16 48 178 AND +2 1 173 174 179 AND +2 1 178 179 180 XOR +2 1 173 160 181 AND +2 1 181 172 182 AND +2 1 180 182 183 XOR +2 1 177 183 350 XOR +2 1 18 50 184 XOR +2 1 17 49 185 AND +2 1 177 180 186 AND +2 1 185 186 187 XOR +2 1 177 181 188 AND +2 1 188 172 189 AND +2 1 187 189 190 XOR +2 1 184 190 351 XOR +2 1 19 51 191 XOR +2 1 18 50 192 AND +2 1 184 185 193 AND +2 1 192 193 194 XOR +2 1 184 177 195 AND +2 1 195 180 196 AND +2 1 194 196 197 XOR +2 1 195 181 198 AND +2 1 198 172 199 AND +2 1 197 199 200 XOR +2 1 191 200 352 XOR +2 1 20 52 201 XOR +2 1 19 51 202 AND +2 1 191 197 203 AND +2 1 202 203 204 XOR +2 1 191 198 205 AND +2 1 205 172 206 AND +2 1 204 206 207 XOR +2 1 201 207 353 XOR +2 1 21 53 208 XOR +2 1 20 52 209 AND +2 1 201 202 210 AND +2 1 209 210 211 XOR +2 1 201 191 212 AND +2 1 212 197 213 AND +2 1 211 213 214 XOR +2 1 212 198 215 AND +2 1 215 172 216 AND +2 1 214 216 217 XOR +2 1 208 217 354 XOR +2 1 22 54 218 XOR +2 1 21 53 219 AND +2 1 208 211 220 AND +2 1 219 220 221 XOR +2 1 208 212 222 AND +2 1 222 197 223 AND +2 1 221 223 224 XOR +2 1 222 198 225 AND +2 1 225 172 226 AND +2 1 224 226 227 XOR +2 1 218 227 355 XOR +2 1 23 55 228 XOR +2 1 22 54 229 AND +2 1 218 219 230 AND +2 1 229 230 231 XOR +2 1 218 208 232 AND +2 1 232 211 233 AND +2 1 231 233 234 XOR +2 1 232 212 235 AND +2 1 235 197 236 AND +2 1 234 236 237 XOR +2 1 235 198 238 AND +2 1 238 172 239 AND +2 1 237 239 240 XOR +2 1 228 240 356 XOR +2 1 24 56 241 XOR +2 1 23 55 242 AND +2 1 228 237 243 AND +2 1 242 243 244 XOR +2 1 228 238 245 AND +2 1 245 172 246 AND +2 1 244 246 247 XOR +2 1 241 247 357 XOR +2 1 25 57 248 XOR +2 1 24 56 249 AND +2 1 241 242 250 AND +2 1 249 250 251 XOR +2 1 241 228 252 AND +2 1 252 237 253 AND +2 1 251 253 254 XOR +2 1 252 238 255 AND +2 1 255 172 256 AND +2 1 254 256 257 XOR +2 1 248 257 358 XOR +2 1 26 58 258 XOR +2 1 25 57 259 AND +2 1 248 251 260 AND +2 1 259 260 261 XOR +2 1 248 252 262 AND +2 1 262 237 263 AND +2 1 261 263 264 XOR +2 1 262 238 265 AND +2 1 265 172 266 AND +2 1 264 266 267 XOR +2 1 258 267 359 XOR +2 1 27 59 268 XOR +2 1 26 58 269 AND +2 1 258 259 270 AND +2 1 269 270 271 XOR +2 1 258 248 272 AND +2 1 272 251 273 AND +2 1 271 273 274 XOR +2 1 272 252 275 AND +2 1 275 237 276 AND +2 1 274 276 277 XOR +2 1 275 238 278 AND +2 1 278 172 279 AND +2 1 277 279 280 XOR +2 1 268 280 360 XOR +2 1 28 60 281 XOR +2 1 27 59 282 AND +2 1 268 274 283 AND +2 1 282 283 284 XOR +2 1 268 275 285 AND +2 1 285 237 286 AND +2 1 284 286 287 XOR +2 1 285 238 288 AND +2 1 288 172 289 AND +2 1 287 289 290 XOR +2 1 281 290 361 XOR +2 1 29 61 291 XOR +2 1 28 60 292 AND +2 1 281 282 293 AND +2 1 292 293 294 XOR +2 1 281 268 295 AND +2 1 295 274 296 AND +2 1 294 296 297 XOR +2 1 295 275 298 AND +2 1 298 237 299 AND +2 1 297 299 300 XOR +2 1 298 238 301 AND +2 1 301 172 302 AND +2 1 300 302 303 XOR +2 1 291 303 362 XOR +2 1 30 62 304 XOR +2 1 29 61 305 AND +2 1 291 294 306 AND +2 1 305 306 307 XOR +2 1 291 295 308 AND +2 1 308 274 309 AND +2 1 307 309 310 XOR +2 1 308 275 311 AND +2 1 311 237 312 AND +2 1 310 312 313 XOR +2 1 311 238 314 AND +2 1 314 172 315 AND +2 1 313 315 316 XOR +2 1 304 316 363 XOR +2 1 31 63 317 XOR +2 1 30 62 318 AND +2 1 304 305 319 AND +2 1 318 319 320 XOR +2 1 304 291 321 AND +2 1 321 294 322 AND +2 1 320 322 323 XOR +2 1 321 295 324 AND +2 1 324 274 325 AND +2 1 323 325 326 XOR +2 1 324 275 327 AND +2 1 327 237 328 AND +2 1 326 328 329 XOR +2 1 327 238 330 AND +2 1 330 172 331 AND +2 1 329 331 332 XOR +2 1 317 332 364 XOR diff --git a/crates/seec/circuits/int_add64_depth.bristol b/crates/seec/circuits/int_add64_depth.bristol new file mode 100644 index 0000000..80dd95b --- /dev/null +++ b/crates/seec/circuits/int_add64_depth.bristol @@ -0,0 +1,704 @@ +701 829 +64 64 64 + +2 1 0 64 765 XOR +2 1 1 65 128 XOR +2 1 0 64 129 AND +2 1 128 129 766 XOR +2 1 2 66 130 XOR +2 1 1 65 131 AND +2 1 128 129 132 AND +2 1 131 132 133 XOR +2 1 130 133 767 XOR +2 1 3 67 134 XOR +2 1 2 66 135 AND +2 1 130 131 136 AND +2 1 135 136 137 XOR +2 1 130 128 138 AND +2 1 138 129 139 AND +2 1 137 139 140 XOR +2 1 134 140 768 XOR +2 1 4 68 141 XOR +2 1 3 67 142 AND +2 1 134 140 143 AND +2 1 142 143 144 XOR +2 1 141 144 769 XOR +2 1 5 69 145 XOR +2 1 4 68 146 AND +2 1 141 142 147 AND +2 1 146 147 148 XOR +2 1 141 134 149 AND +2 1 149 140 150 AND +2 1 148 150 151 XOR +2 1 145 151 770 XOR +2 1 6 70 152 XOR +2 1 5 69 153 AND +2 1 145 148 154 AND +2 1 153 154 155 XOR +2 1 145 149 156 AND +2 1 156 140 157 AND +2 1 155 157 158 XOR +2 1 152 158 771 XOR +2 1 7 71 159 XOR +2 1 6 70 160 AND +2 1 152 153 161 AND +2 1 160 161 162 XOR +2 1 152 145 163 AND +2 1 163 148 164 AND +2 1 162 164 165 XOR +2 1 163 149 166 AND +2 1 166 140 167 AND +2 1 165 167 168 XOR +2 1 159 168 772 XOR +2 1 8 72 169 XOR +2 1 7 71 170 AND +2 1 159 168 171 AND +2 1 170 171 172 XOR +2 1 169 172 773 XOR +2 1 9 73 173 XOR +2 1 8 72 174 AND +2 1 169 170 175 AND +2 1 174 175 176 XOR +2 1 169 159 177 AND +2 1 177 168 178 AND +2 1 176 178 179 XOR +2 1 173 179 774 XOR +2 1 10 74 180 XOR +2 1 9 73 181 AND +2 1 173 176 182 AND +2 1 181 182 183 XOR +2 1 173 177 184 AND +2 1 184 168 185 AND +2 1 183 185 186 XOR +2 1 180 186 775 XOR +2 1 11 75 187 XOR +2 1 10 74 188 AND +2 1 180 181 189 AND +2 1 188 189 190 XOR +2 1 180 173 191 AND +2 1 191 176 192 AND +2 1 190 192 193 XOR +2 1 191 177 194 AND +2 1 194 168 195 AND +2 1 193 195 196 XOR +2 1 187 196 776 XOR +2 1 12 76 197 XOR +2 1 11 75 198 AND +2 1 187 193 199 AND +2 1 198 199 200 XOR +2 1 187 194 201 AND +2 1 201 168 202 AND +2 1 200 202 203 XOR +2 1 197 203 777 XOR +2 1 13 77 204 XOR +2 1 12 76 205 AND +2 1 197 198 206 AND +2 1 205 206 207 XOR +2 1 197 187 208 AND +2 1 208 193 209 AND +2 1 207 209 210 XOR +2 1 208 194 211 AND +2 1 211 168 212 AND +2 1 210 212 213 XOR +2 1 204 213 778 XOR +2 1 14 78 214 XOR +2 1 13 77 215 AND +2 1 204 207 216 AND +2 1 215 216 217 XOR +2 1 204 208 218 AND +2 1 218 193 219 AND +2 1 217 219 220 XOR +2 1 218 194 221 AND +2 1 221 168 222 AND +2 1 220 222 223 XOR +2 1 214 223 779 XOR +2 1 15 79 224 XOR +2 1 14 78 225 AND +2 1 214 215 226 AND +2 1 225 226 227 XOR +2 1 214 204 228 AND +2 1 228 207 229 AND +2 1 227 229 230 XOR +2 1 228 208 231 AND +2 1 231 193 232 AND +2 1 230 232 233 XOR +2 1 231 194 234 AND +2 1 234 168 235 AND +2 1 233 235 236 XOR +2 1 224 236 780 XOR +2 1 16 80 237 XOR +2 1 15 79 238 AND +2 1 224 236 239 AND +2 1 238 239 240 XOR +2 1 237 240 781 XOR +2 1 17 81 241 XOR +2 1 16 80 242 AND +2 1 237 238 243 AND +2 1 242 243 244 XOR +2 1 237 224 245 AND +2 1 245 236 246 AND +2 1 244 246 247 XOR +2 1 241 247 782 XOR +2 1 18 82 248 XOR +2 1 17 81 249 AND +2 1 241 244 250 AND +2 1 249 250 251 XOR +2 1 241 245 252 AND +2 1 252 236 253 AND +2 1 251 253 254 XOR +2 1 248 254 783 XOR +2 1 19 83 255 XOR +2 1 18 82 256 AND +2 1 248 249 257 AND +2 1 256 257 258 XOR +2 1 248 241 259 AND +2 1 259 244 260 AND +2 1 258 260 261 XOR +2 1 259 245 262 AND +2 1 262 236 263 AND +2 1 261 263 264 XOR +2 1 255 264 784 XOR +2 1 20 84 265 XOR +2 1 19 83 266 AND +2 1 255 261 267 AND +2 1 266 267 268 XOR +2 1 255 262 269 AND +2 1 269 236 270 AND +2 1 268 270 271 XOR +2 1 265 271 785 XOR +2 1 21 85 272 XOR +2 1 20 84 273 AND +2 1 265 266 274 AND +2 1 273 274 275 XOR +2 1 265 255 276 AND +2 1 276 261 277 AND +2 1 275 277 278 XOR +2 1 276 262 279 AND +2 1 279 236 280 AND +2 1 278 280 281 XOR +2 1 272 281 786 XOR +2 1 22 86 282 XOR +2 1 21 85 283 AND +2 1 272 275 284 AND +2 1 283 284 285 XOR +2 1 272 276 286 AND +2 1 286 261 287 AND +2 1 285 287 288 XOR +2 1 286 262 289 AND +2 1 289 236 290 AND +2 1 288 290 291 XOR +2 1 282 291 787 XOR +2 1 23 87 292 XOR +2 1 22 86 293 AND +2 1 282 283 294 AND +2 1 293 294 295 XOR +2 1 282 272 296 AND +2 1 296 275 297 AND +2 1 295 297 298 XOR +2 1 296 276 299 AND +2 1 299 261 300 AND +2 1 298 300 301 XOR +2 1 299 262 302 AND +2 1 302 236 303 AND +2 1 301 303 304 XOR +2 1 292 304 788 XOR +2 1 24 88 305 XOR +2 1 23 87 306 AND +2 1 292 301 307 AND +2 1 306 307 308 XOR +2 1 292 302 309 AND +2 1 309 236 310 AND +2 1 308 310 311 XOR +2 1 305 311 789 XOR +2 1 25 89 312 XOR +2 1 24 88 313 AND +2 1 305 306 314 AND +2 1 313 314 315 XOR +2 1 305 292 316 AND +2 1 316 301 317 AND +2 1 315 317 318 XOR +2 1 316 302 319 AND +2 1 319 236 320 AND +2 1 318 320 321 XOR +2 1 312 321 790 XOR +2 1 26 90 322 XOR +2 1 25 89 323 AND +2 1 312 315 324 AND +2 1 323 324 325 XOR +2 1 312 316 326 AND +2 1 326 301 327 AND +2 1 325 327 328 XOR +2 1 326 302 329 AND +2 1 329 236 330 AND +2 1 328 330 331 XOR +2 1 322 331 791 XOR +2 1 27 91 332 XOR +2 1 26 90 333 AND +2 1 322 323 334 AND +2 1 333 334 335 XOR +2 1 322 312 336 AND +2 1 336 315 337 AND +2 1 335 337 338 XOR +2 1 336 316 339 AND +2 1 339 301 340 AND +2 1 338 340 341 XOR +2 1 339 302 342 AND +2 1 342 236 343 AND +2 1 341 343 344 XOR +2 1 332 344 792 XOR +2 1 28 92 345 XOR +2 1 27 91 346 AND +2 1 332 338 347 AND +2 1 346 347 348 XOR +2 1 332 339 349 AND +2 1 349 301 350 AND +2 1 348 350 351 XOR +2 1 349 302 352 AND +2 1 352 236 353 AND +2 1 351 353 354 XOR +2 1 345 354 793 XOR +2 1 29 93 355 XOR +2 1 28 92 356 AND +2 1 345 346 357 AND +2 1 356 357 358 XOR +2 1 345 332 359 AND +2 1 359 338 360 AND +2 1 358 360 361 XOR +2 1 359 339 362 AND +2 1 362 301 363 AND +2 1 361 363 364 XOR +2 1 362 302 365 AND +2 1 365 236 366 AND +2 1 364 366 367 XOR +2 1 355 367 794 XOR +2 1 30 94 368 XOR +2 1 29 93 369 AND +2 1 355 358 370 AND +2 1 369 370 371 XOR +2 1 355 359 372 AND +2 1 372 338 373 AND +2 1 371 373 374 XOR +2 1 372 339 375 AND +2 1 375 301 376 AND +2 1 374 376 377 XOR +2 1 375 302 378 AND +2 1 378 236 379 AND +2 1 377 379 380 XOR +2 1 368 380 795 XOR +2 1 31 95 381 XOR +2 1 30 94 382 AND +2 1 368 369 383 AND +2 1 382 383 384 XOR +2 1 368 355 385 AND +2 1 385 358 386 AND +2 1 384 386 387 XOR +2 1 385 359 388 AND +2 1 388 338 389 AND +2 1 387 389 390 XOR +2 1 388 339 391 AND +2 1 391 301 392 AND +2 1 390 392 393 XOR +2 1 391 302 394 AND +2 1 394 236 395 AND +2 1 393 395 396 XOR +2 1 381 396 796 XOR +2 1 32 96 397 XOR +2 1 31 95 398 AND +2 1 381 396 399 AND +2 1 398 399 400 XOR +2 1 397 400 797 XOR +2 1 33 97 401 XOR +2 1 32 96 402 AND +2 1 397 398 403 AND +2 1 402 403 404 XOR +2 1 397 381 405 AND +2 1 405 396 406 AND +2 1 404 406 407 XOR +2 1 401 407 798 XOR +2 1 34 98 408 XOR +2 1 33 97 409 AND +2 1 401 404 410 AND +2 1 409 410 411 XOR +2 1 401 405 412 AND +2 1 412 396 413 AND +2 1 411 413 414 XOR +2 1 408 414 799 XOR +2 1 35 99 415 XOR +2 1 34 98 416 AND +2 1 408 409 417 AND +2 1 416 417 418 XOR +2 1 408 401 419 AND +2 1 419 404 420 AND +2 1 418 420 421 XOR +2 1 419 405 422 AND +2 1 422 396 423 AND +2 1 421 423 424 XOR +2 1 415 424 800 XOR +2 1 36 100 425 XOR +2 1 35 99 426 AND +2 1 415 421 427 AND +2 1 426 427 428 XOR +2 1 415 422 429 AND +2 1 429 396 430 AND +2 1 428 430 431 XOR +2 1 425 431 801 XOR +2 1 37 101 432 XOR +2 1 36 100 433 AND +2 1 425 426 434 AND +2 1 433 434 435 XOR +2 1 425 415 436 AND +2 1 436 421 437 AND +2 1 435 437 438 XOR +2 1 436 422 439 AND +2 1 439 396 440 AND +2 1 438 440 441 XOR +2 1 432 441 802 XOR +2 1 38 102 442 XOR +2 1 37 101 443 AND +2 1 432 435 444 AND +2 1 443 444 445 XOR +2 1 432 436 446 AND +2 1 446 421 447 AND +2 1 445 447 448 XOR +2 1 446 422 449 AND +2 1 449 396 450 AND +2 1 448 450 451 XOR +2 1 442 451 803 XOR +2 1 39 103 452 XOR +2 1 38 102 453 AND +2 1 442 443 454 AND +2 1 453 454 455 XOR +2 1 442 432 456 AND +2 1 456 435 457 AND +2 1 455 457 458 XOR +2 1 456 436 459 AND +2 1 459 421 460 AND +2 1 458 460 461 XOR +2 1 459 422 462 AND +2 1 462 396 463 AND +2 1 461 463 464 XOR +2 1 452 464 804 XOR +2 1 40 104 465 XOR +2 1 39 103 466 AND +2 1 452 461 467 AND +2 1 466 467 468 XOR +2 1 452 462 469 AND +2 1 469 396 470 AND +2 1 468 470 471 XOR +2 1 465 471 805 XOR +2 1 41 105 472 XOR +2 1 40 104 473 AND +2 1 465 466 474 AND +2 1 473 474 475 XOR +2 1 465 452 476 AND +2 1 476 461 477 AND +2 1 475 477 478 XOR +2 1 476 462 479 AND +2 1 479 396 480 AND +2 1 478 480 481 XOR +2 1 472 481 806 XOR +2 1 42 106 482 XOR +2 1 41 105 483 AND +2 1 472 475 484 AND +2 1 483 484 485 XOR +2 1 472 476 486 AND +2 1 486 461 487 AND +2 1 485 487 488 XOR +2 1 486 462 489 AND +2 1 489 396 490 AND +2 1 488 490 491 XOR +2 1 482 491 807 XOR +2 1 43 107 492 XOR +2 1 42 106 493 AND +2 1 482 483 494 AND +2 1 493 494 495 XOR +2 1 482 472 496 AND +2 1 496 475 497 AND +2 1 495 497 498 XOR +2 1 496 476 499 AND +2 1 499 461 500 AND +2 1 498 500 501 XOR +2 1 499 462 502 AND +2 1 502 396 503 AND +2 1 501 503 504 XOR +2 1 492 504 808 XOR +2 1 44 108 505 XOR +2 1 43 107 506 AND +2 1 492 498 507 AND +2 1 506 507 508 XOR +2 1 492 499 509 AND +2 1 509 461 510 AND +2 1 508 510 511 XOR +2 1 509 462 512 AND +2 1 512 396 513 AND +2 1 511 513 514 XOR +2 1 505 514 809 XOR +2 1 45 109 515 XOR +2 1 44 108 516 AND +2 1 505 506 517 AND +2 1 516 517 518 XOR +2 1 505 492 519 AND +2 1 519 498 520 AND +2 1 518 520 521 XOR +2 1 519 499 522 AND +2 1 522 461 523 AND +2 1 521 523 524 XOR +2 1 522 462 525 AND +2 1 525 396 526 AND +2 1 524 526 527 XOR +2 1 515 527 810 XOR +2 1 46 110 528 XOR +2 1 45 109 529 AND +2 1 515 518 530 AND +2 1 529 530 531 XOR +2 1 515 519 532 AND +2 1 532 498 533 AND +2 1 531 533 534 XOR +2 1 532 499 535 AND +2 1 535 461 536 AND +2 1 534 536 537 XOR +2 1 535 462 538 AND +2 1 538 396 539 AND +2 1 537 539 540 XOR +2 1 528 540 811 XOR +2 1 47 111 541 XOR +2 1 46 110 542 AND +2 1 528 529 543 AND +2 1 542 543 544 XOR +2 1 528 515 545 AND +2 1 545 518 546 AND +2 1 544 546 547 XOR +2 1 545 519 548 AND +2 1 548 498 549 AND +2 1 547 549 550 XOR +2 1 548 499 551 AND +2 1 551 461 552 AND +2 1 550 552 553 XOR +2 1 551 462 554 AND +2 1 554 396 555 AND +2 1 553 555 556 XOR +2 1 541 556 812 XOR +2 1 48 112 557 XOR +2 1 47 111 558 AND +2 1 541 553 559 AND +2 1 558 559 560 XOR +2 1 541 554 561 AND +2 1 561 396 562 AND +2 1 560 562 563 XOR +2 1 557 563 813 XOR +2 1 49 113 564 XOR +2 1 48 112 565 AND +2 1 557 558 566 AND +2 1 565 566 567 XOR +2 1 557 541 568 AND +2 1 568 553 569 AND +2 1 567 569 570 XOR +2 1 568 554 571 AND +2 1 571 396 572 AND +2 1 570 572 573 XOR +2 1 564 573 814 XOR +2 1 50 114 574 XOR +2 1 49 113 575 AND +2 1 564 567 576 AND +2 1 575 576 577 XOR +2 1 564 568 578 AND +2 1 578 553 579 AND +2 1 577 579 580 XOR +2 1 578 554 581 AND +2 1 581 396 582 AND +2 1 580 582 583 XOR +2 1 574 583 815 XOR +2 1 51 115 584 XOR +2 1 50 114 585 AND +2 1 574 575 586 AND +2 1 585 586 587 XOR +2 1 574 564 588 AND +2 1 588 567 589 AND +2 1 587 589 590 XOR +2 1 588 568 591 AND +2 1 591 553 592 AND +2 1 590 592 593 XOR +2 1 591 554 594 AND +2 1 594 396 595 AND +2 1 593 595 596 XOR +2 1 584 596 816 XOR +2 1 52 116 597 XOR +2 1 51 115 598 AND +2 1 584 590 599 AND +2 1 598 599 600 XOR +2 1 584 591 601 AND +2 1 601 553 602 AND +2 1 600 602 603 XOR +2 1 601 554 604 AND +2 1 604 396 605 AND +2 1 603 605 606 XOR +2 1 597 606 817 XOR +2 1 53 117 607 XOR +2 1 52 116 608 AND +2 1 597 598 609 AND +2 1 608 609 610 XOR +2 1 597 584 611 AND +2 1 611 590 612 AND +2 1 610 612 613 XOR +2 1 611 591 614 AND +2 1 614 553 615 AND +2 1 613 615 616 XOR +2 1 614 554 617 AND +2 1 617 396 618 AND +2 1 616 618 619 XOR +2 1 607 619 818 XOR +2 1 54 118 620 XOR +2 1 53 117 621 AND +2 1 607 610 622 AND +2 1 621 622 623 XOR +2 1 607 611 624 AND +2 1 624 590 625 AND +2 1 623 625 626 XOR +2 1 624 591 627 AND +2 1 627 553 628 AND +2 1 626 628 629 XOR +2 1 627 554 630 AND +2 1 630 396 631 AND +2 1 629 631 632 XOR +2 1 620 632 819 XOR +2 1 55 119 633 XOR +2 1 54 118 634 AND +2 1 620 621 635 AND +2 1 634 635 636 XOR +2 1 620 607 637 AND +2 1 637 610 638 AND +2 1 636 638 639 XOR +2 1 637 611 640 AND +2 1 640 590 641 AND +2 1 639 641 642 XOR +2 1 640 591 643 AND +2 1 643 553 644 AND +2 1 642 644 645 XOR +2 1 643 554 646 AND +2 1 646 396 647 AND +2 1 645 647 648 XOR +2 1 633 648 820 XOR +2 1 56 120 649 XOR +2 1 55 119 650 AND +2 1 633 642 651 AND +2 1 650 651 652 XOR +2 1 633 643 653 AND +2 1 653 553 654 AND +2 1 652 654 655 XOR +2 1 653 554 656 AND +2 1 656 396 657 AND +2 1 655 657 658 XOR +2 1 649 658 821 XOR +2 1 57 121 659 XOR +2 1 56 120 660 AND +2 1 649 650 661 AND +2 1 660 661 662 XOR +2 1 649 633 663 AND +2 1 663 642 664 AND +2 1 662 664 665 XOR +2 1 663 643 666 AND +2 1 666 553 667 AND +2 1 665 667 668 XOR +2 1 666 554 669 AND +2 1 669 396 670 AND +2 1 668 670 671 XOR +2 1 659 671 822 XOR +2 1 58 122 672 XOR +2 1 57 121 673 AND +2 1 659 662 674 AND +2 1 673 674 675 XOR +2 1 659 663 676 AND +2 1 676 642 677 AND +2 1 675 677 678 XOR +2 1 676 643 679 AND +2 1 679 553 680 AND +2 1 678 680 681 XOR +2 1 679 554 682 AND +2 1 682 396 683 AND +2 1 681 683 684 XOR +2 1 672 684 823 XOR +2 1 59 123 685 XOR +2 1 58 122 686 AND +2 1 672 673 687 AND +2 1 686 687 688 XOR +2 1 672 659 689 AND +2 1 689 662 690 AND +2 1 688 690 691 XOR +2 1 689 663 692 AND +2 1 692 642 693 AND +2 1 691 693 694 XOR +2 1 692 643 695 AND +2 1 695 553 696 AND +2 1 694 696 697 XOR +2 1 695 554 698 AND +2 1 698 396 699 AND +2 1 697 699 700 XOR +2 1 685 700 824 XOR +2 1 60 124 701 XOR +2 1 59 123 702 AND +2 1 685 691 703 AND +2 1 702 703 704 XOR +2 1 685 692 705 AND +2 1 705 642 706 AND +2 1 704 706 707 XOR +2 1 705 643 708 AND +2 1 708 553 709 AND +2 1 707 709 710 XOR +2 1 708 554 711 AND +2 1 711 396 712 AND +2 1 710 712 713 XOR +2 1 701 713 825 XOR +2 1 61 125 714 XOR +2 1 60 124 715 AND +2 1 701 702 716 AND +2 1 715 716 717 XOR +2 1 701 685 718 AND +2 1 718 691 719 AND +2 1 717 719 720 XOR +2 1 718 692 721 AND +2 1 721 642 722 AND +2 1 720 722 723 XOR +2 1 721 643 724 AND +2 1 724 553 725 AND +2 1 723 725 726 XOR +2 1 724 554 727 AND +2 1 727 396 728 AND +2 1 726 728 729 XOR +2 1 714 729 826 XOR +2 1 62 126 730 XOR +2 1 61 125 731 AND +2 1 714 717 732 AND +2 1 731 732 733 XOR +2 1 714 718 734 AND +2 1 734 691 735 AND +2 1 733 735 736 XOR +2 1 734 692 737 AND +2 1 737 642 738 AND +2 1 736 738 739 XOR +2 1 737 643 740 AND +2 1 740 553 741 AND +2 1 739 741 742 XOR +2 1 740 554 743 AND +2 1 743 396 744 AND +2 1 742 744 745 XOR +2 1 730 745 827 XOR +2 1 63 127 746 XOR +2 1 62 126 747 AND +2 1 730 731 748 AND +2 1 747 748 749 XOR +2 1 730 714 750 AND +2 1 750 717 751 AND +2 1 749 751 752 XOR +2 1 750 718 753 AND +2 1 753 691 754 AND +2 1 752 754 755 XOR +2 1 753 692 756 AND +2 1 756 642 757 AND +2 1 755 757 758 XOR +2 1 756 643 759 AND +2 1 759 553 760 AND +2 1 758 760 761 XOR +2 1 759 554 762 AND +2 1 762 396 763 AND +2 1 761 763 764 XOR +2 1 746 764 828 XOR diff --git a/crates/seec/circuits/int_add8_depth.bristol b/crates/seec/circuits/int_add8_depth.bristol new file mode 100644 index 0000000..775db71 --- /dev/null +++ b/crates/seec/circuits/int_add8_depth.bristol @@ -0,0 +1,52 @@ +49 65 +8 8 8 + +2 1 0 8 57 XOR +2 1 1 9 16 XOR +2 1 0 8 17 AND +2 1 16 17 58 XOR +2 1 2 10 18 XOR +2 1 1 9 19 AND +2 1 16 17 20 AND +2 1 19 20 21 XOR +2 1 18 21 59 XOR +2 1 3 11 22 XOR +2 1 2 10 23 AND +2 1 18 19 24 AND +2 1 23 24 25 XOR +2 1 18 16 26 AND +2 1 26 17 27 AND +2 1 25 27 28 XOR +2 1 22 28 60 XOR +2 1 4 12 29 XOR +2 1 3 11 30 AND +2 1 22 28 31 AND +2 1 30 31 32 XOR +2 1 29 32 61 XOR +2 1 5 13 33 XOR +2 1 4 12 34 AND +2 1 29 30 35 AND +2 1 34 35 36 XOR +2 1 29 22 37 AND +2 1 37 28 38 AND +2 1 36 38 39 XOR +2 1 33 39 62 XOR +2 1 6 14 40 XOR +2 1 5 13 41 AND +2 1 33 36 42 AND +2 1 41 42 43 XOR +2 1 33 37 44 AND +2 1 44 28 45 AND +2 1 43 45 46 XOR +2 1 40 46 63 XOR +2 1 7 15 47 XOR +2 1 6 14 48 AND +2 1 40 41 49 AND +2 1 48 49 50 XOR +2 1 40 33 51 AND +2 1 51 36 52 AND +2 1 50 52 53 XOR +2 1 51 37 54 AND +2 1 54 28 55 AND +2 1 53 55 56 XOR +2 1 47 56 64 XOR diff --git a/crates/seec/src/circuit/base_circuit.rs b/crates/seec/src/circuit/base_circuit.rs index 482f2de..e2f43c1 100644 --- a/crates/seec/src/circuit/base_circuit.rs +++ b/crates/seec/src/circuit/base_circuit.rs @@ -55,6 +55,8 @@ pub enum BaseGate { /// Connects a sub circuit to the main circuit and selects the i'th individual value from /// the SIMD output ConnectToMainFromSimd((D, u32)), + /// Identity gate, simply outputs its input + Identity, Constant(T), Debug, } @@ -117,7 +119,7 @@ impl BaseCircuit { BaseGate::SubCircuitInput(_) | BaseGate::ConnectToMain(_) | BaseGate::ConnectToMainFromSimd(_) => self.sub_circuit_input_gates.push(gate_id), - BaseGate::Debug => (/* nothing special to do */), + BaseGate::Debug | BaseGate::Identity => (/* nothing special to do */), } } if gate.is_interactive() { @@ -223,6 +225,14 @@ impl BaseCircuit { ); let mut gate_id_map = vec![GateId::default(); circuit.gate_count()]; for (gate, id) in circuit.iter() { + // Map ScInput/Output gates to identity gates. Otherwise, we can't add_sub_circuit a circuit + // which itself was built using add_sub_circuit + let gate = match gate.as_base_gate() { + Some(BaseGate::SubCircuitInput(_) | BaseGate::SubCircuitOutput(_)) => { + G::wrap_base_gate(BaseGate::Identity) + } + _ => gate, + }; let new_id = self.add_gate(gate); gate_id_map[id.as_usize()] = new_id; for parent in circuit.parent_gates(id) { @@ -309,7 +319,7 @@ where Share: Clone, G: Gate + From> + for<'a> From<&'a bristol::Gate>, { - #[tracing::instrument(skip(bristol))] + #[tracing::instrument(skip(bristol), ret)] pub fn from_bristol(bristol: bristol::Circuit, load: Load) -> Result { info!( "Converting bristol circuit with header: {:?}", @@ -464,7 +474,8 @@ impl Gate for BaseGate { | Self::Input(_) | Self::SubCircuitInput(_) | Self::SubCircuitOutput(_) - | Self::ConnectToMain(_) => inputs + | Self::ConnectToMain(_) + | Self::Identity => inputs .next() .unwrap_or_else(|| panic!("Empty input for {self:?}")), Self::ConnectToMainFromSimd(_) => { @@ -488,12 +499,9 @@ impl Gate for BaseGate { | BaseGate::Input(_) | BaseGate::ConnectToMain(_) | BaseGate::SubCircuitInput(_) - | BaseGate::ConnectToMainFromSimd(_) => { - inputs.next().expect("Missing input to {self:?}").clone() - } - BaseGate::SubCircuitOutput(_) => { - inputs.next().expect("Missing input to {self:?}").clone() - } + | BaseGate::SubCircuitOutput(_) + | BaseGate::ConnectToMainFromSimd(_) + | BaseGate::Identity => inputs.next().expect("Missing input to {self:?}").clone(), BaseGate::Constant(_constant) => { todo!("SimdShare from constant") } diff --git a/crates/seec/src/circuit/mod.rs b/crates/seec/src/circuit/mod.rs index d412620..ba26b46 100644 --- a/crates/seec/src/circuit/mod.rs +++ b/crates/seec/src/circuit/mod.rs @@ -1,6 +1,6 @@ //! Circuit builder and executable circuit types. +use crate::protocols::Gate; use crate::SubCircuitGate; -pub use builder::SubCircCache; use bytemuck::Pod; use either::Either; use num_integer::Integer; @@ -18,9 +18,20 @@ pub mod dyn_layers; pub mod static_layers; pub use crate::protocols::boolean_gmw::BooleanGate; -use crate::protocols::Gate; pub use base_circuit::{BaseCircuit, GateId}; -pub use builder::{CircuitBuilder, SharedCircuit}; +pub use builder::{CircuitBuilder, SharedCircuit, SubCircCache}; + +macro_rules! circ_path { + ($file:expr) => { + include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/circuits/", $file)) + }; +} +// TODO the plain text of these bristol circuits is currently always embedded into the binary +// maybe we can find a better solution for this, or toggle this via a feature +pub const BRISTOL_ADD_8: &str = circ_path!("int_add8_depth.bristol"); +pub const BRISTOL_ADD_16: &str = circ_path!("int_add16_depth.bristol"); +pub const BRISTOL_ADD_32: &str = circ_path!("int_add32_depth.bristol"); +pub const BRISTOL_ADD_64: &str = circ_path!("int_add64_depth.bristol"); pub type CircuitId = u32; diff --git a/crates/seec/src/executor.rs b/crates/seec/src/executor.rs index ebcfdbf..56bf30e 100644 --- a/crates/seec/src/executor.rs +++ b/crates/seec/src/executor.rs @@ -139,7 +139,12 @@ where receiver: &mut Receiver>, ) -> Result, ExecutorError> { info!("Executing circuit"); - debug!(?inputs); + if inputs.should_debug() { + debug!(?inputs); + } else { + debug!("Inputs too large for debug printing. Use trace if necessary."); + trace!(?inputs); + } let now = Instant::now(); let inp_len = match &inputs { Input::Scalar(shares) => shares.len(), @@ -527,6 +532,17 @@ impl Input { Input::Simd(shares) => Some(shares), } } + + fn should_debug(&self) -> bool + where + Shares: ShareStorage, + { + match self { + Input::Scalar(shares) if shares.len() <= 1024 => true, + Input::Simd(simd_shares) if simd_shares.len() <= 256 => true, + _ => false, + } + } } impl GateOutputs { diff --git a/crates/seec/src/mul_triple/storage.rs b/crates/seec/src/mul_triple/storage.rs index 82ca858..43d9b66 100644 --- a/crates/seec/src/mul_triple/storage.rs +++ b/crates/seec/src/mul_triple/storage.rs @@ -19,6 +19,7 @@ pub struct MTStorage { file: F, write_batch_size: usize, stored_mts: MulTriples, + #[cfg(feature = "bench-api")] /// can only be set when feature bench-api is enabled insecure_loop_file: bool, } @@ -36,6 +37,7 @@ where file: BufWriter::new(file), write_batch_size: DEFAULT_BATCH_SIZE, stored_mts: Default::default(), + #[cfg(feature = "bench-api")] insecure_loop_file: false, }) } @@ -50,6 +52,7 @@ where file, write_batch_size: DEFAULT_BATCH_SIZE, stored_mts: Default::default(), + #[cfg(feature = "bench-api")] insecure_loop_file: false, } } diff --git a/crates/seec/src/parse/fuse/mod.rs b/crates/seec/src/parse/fuse/mod.rs index 82706db..a30ca0f 100644 --- a/crates/seec/src/parse/fuse/mod.rs +++ b/crates/seec/src/parse/fuse/mod.rs @@ -13,6 +13,7 @@ use rand::distributions::{Distribution, Standard}; use std::path::Path; use std::{fs, io}; use thiserror::Error; +use tracing::{debug, instrument}; #[allow(unused_imports, dead_code, clippy::all)] #[rustfmt::skip] @@ -124,6 +125,7 @@ where Ok(self.builder.into_circuit()) } + #[instrument(level = "debug", skip(self, circ), fields(name = circ.name()))] fn add_fuse_sub_circ( &mut self, circ: CircuitTable<'_>, @@ -200,7 +202,11 @@ where match prim_op { PO::Split => { assert_eq!(1, inputs.len(), "Expecting 1 input for Split gate"); - let b_shares = mixed_gmw::a2b(bc, inputs[0]); + let mut b_shares = mixed_gmw::a2b(bc, inputs[0]); + // Insert identity gates so that output of a2b has contiguous gate ids + for sh in &mut b_shares { + *sh = bc.add_wired_gate(Base(BaseGate::Identity), &[*sh]); + } // we return the first one, if we encounter a node with an input offset, we // can retrieve this gate_id from the map and add the offset b_shares[0] @@ -239,6 +245,7 @@ where main_inputs[0] } PO::CallSubcircuit if self.call_mode == CallMode::InlineCircuits => { + debug!(name = node.subcircuit_name(), "CallSubcircuit"); let circ = self .sc_map .get( @@ -333,7 +340,7 @@ mod tests { .convert_module(mod_table) .expect("Fuse conversion"); let ec = ExecutableCircuit::DynLayers(circ); - let inputs: Vec = ChaChaRng::seed_from_u64(4242) + let inputs: Vec = ChaChaRng::seed_from_u64(46456315) .sample_iter(Standard) .take(ec.input_count()) .collect(); @@ -345,7 +352,7 @@ mod tests { .await .unwrap(); // TODO, this is very likely not the correct output - let exp = vec![i32::MAX as u32; 10]; + let exp = vec![u32::MAX; 10]; let exp = MixedShareStorage::Arith(exp); assert_eq!(out, exp) } diff --git a/crates/seec/src/private_test_utils.rs b/crates/seec/src/private_test_utils.rs index 7bdc6a4..d4eba27 100644 --- a/crates/seec/src/private_test_utils.rs +++ b/crates/seec/src/private_test_utils.rs @@ -3,6 +3,7 @@ //! This module is activated by the "_integration_tests" feature and should not be used by //! downstream code. It can change in any version. use std::convert::Infallible; +use std::env; use std::fmt::Debug; use std::path::Path; @@ -13,14 +14,17 @@ use bitvec::prelude::BitSlice; use bitvec::vec; use bitvec::view::BitViewSized; use itertools::Itertools; +use once_cell::sync::Lazy; +use parking_lot::Mutex; use rand::distributions::Standard; use rand::prelude::Distribution; use rand::rngs::ThreadRng; -use rand::thread_rng; +use rand::{thread_rng, Rng, SeedableRng}; +use rand_chacha::ChaCha8Rng; use seec_channel::sub_channel; use tokio::task::spawn_blocking; use tokio::time::Instant; -use tracing::info; +use tracing::{debug, info}; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; @@ -142,7 +146,17 @@ macro_rules! impl_into_shares { for $typ { fn into_shares(self) -> (MixedShareStorage<$typ>, MixedShareStorage<$typ>) { - let [a, b] = AdditiveSharing::new(thread_rng()).share(vec![self]); + static RNG: Lazy> = Lazy::new(|| { + let seed = match env::var("RNG_SEED") { + Ok(seed) => seed.parse().expect("failed to parse RNG_SEED env var as u64"), + Err(_) => thread_rng().gen() + }; + debug!(seed, "Input sharing rng seed"); + Mutex::new(ChaCha8Rng::seed_from_u64(seed)) + }); + let mut rng = RNG.lock(); + // let [a, b] = AdditiveSharing::new(ChaCha8Rng::seed_from_u64(65432)).share(vec![self]); + let [a, b] = AdditiveSharing::new(&mut *rng).share(vec![self]); (MixedShareStorage::Arith(a), MixedShareStorage::Arith(b)) } } diff --git a/crates/seec/src/protocols/aby2.rs b/crates/seec/src/protocols/aby2.rs index c9458d7..f74457a 100644 --- a/crates/seec/src/protocols/aby2.rs +++ b/crates/seec/src/protocols/aby2.rs @@ -234,7 +234,8 @@ impl BooleanGate { | BaseGate::SubCircuitInput(_) | BaseGate::SubCircuitOutput(_) | BaseGate::ConnectToMain(_) - | BaseGate::Debug => inputs.next().expect("Empty input"), + | BaseGate::Debug + | BaseGate::Identity => inputs.next().expect("Empty input"), BaseGate::Constant(_) => todo!(), BaseGate::ConnectToMainFromSimd(_) => { unimplemented!("SIMD currently not supported for ABY2") diff --git a/crates/seec/src/protocols/mixed_gmw.rs b/crates/seec/src/protocols/mixed_gmw.rs index abdb499..91275cc 100644 --- a/crates/seec/src/protocols/mixed_gmw.rs +++ b/crates/seec/src/protocols/mixed_gmw.rs @@ -1,4 +1,4 @@ -use crate::circuit::base_circuit::BaseGate; +use crate::circuit::base_circuit::{BaseGate, Load}; use crate::circuit::{BaseCircuit, ExecutableCircuit, GateIdx}; use crate::common::BitVec; use crate::executor::{GateOutputs, Input}; @@ -9,11 +9,13 @@ use crate::protocols::{ arithmetic_gmw, boolean_gmw, Gate, Protocol, Ring, ScalarDim, SetupStorage, Share, ShareStorage, Sharing, }; -use crate::GateId; +use crate::{bristol, circuit, GateId}; use async_trait::async_trait; use bitvec::array::BitArray; use bitvec::order::Lsb0; use bitvec::view::BitViewSized; +use once_cell::sync::Lazy; +use parking_lot::Mutex; use rand::distributions::Standard; use rand::prelude::Distribution; use rand::{random, Rng, SeedableRng}; @@ -22,7 +24,8 @@ use serde::{Deserialize, Serialize}; use std::convert::Infallible; use std::marker::PhantomData; use std::{iter, mem}; -use tracing::trace; +use tracing::{instrument, trace}; +use typemap_rev::{TypeMap, TypeMapKey}; #[derive(Clone, Debug, Default, Hash, Eq, PartialEq)] pub struct MixedGmw(PhantomData); @@ -679,6 +682,22 @@ impl Gate for MixedGate { } } +impl From<&bristol::Gate> for MixedGate { + fn from(gate: &bristol::Gate) -> Self { + match gate { + bristol::Gate::And(_) => MixedGate::Bool(boolean_gmw::BooleanGate::And), + bristol::Gate::Xor(_) => MixedGate::Bool(boolean_gmw::BooleanGate::Xor), + bristol::Gate::Inv(_) => MixedGate::Bool(boolean_gmw::BooleanGate::Inv), + } + } +} + +impl From>> for MixedGate { + fn from(value: BaseGate>) -> Self { + Self::Base(value) + } +} + #[derive(Debug)] pub struct MixedSharing { bool: B, @@ -733,10 +752,40 @@ pub fn a2b(bc: &mut BaseCircuit>, a: GateId) -> Vec( + bc: &mut BaseCircuit>, + a: &[GateId], + b: &[GateId], +) -> Vec { + // We can't have a generic static, so we resort to a lazy typemap to cache + // the addition circuits + static ADDERS: Lazy> = Lazy::new(Mutex::default); + struct Key(PhantomData); + impl TypeMapKey for Key { + type Value = BaseCircuit>; + } + assert_eq!(R::BITS, a.len(), "Wrong number of inputs"); + assert_eq!(R::BITS, b.len(), "Wrong number of inputs"); + let mut guard = ADDERS.lock(); + let adder = guard.entry::>().or_insert_with(|| { + let bristol = match R::BITS { + 8 => circuit::BRISTOL_ADD_8, + 16 => circuit::BRISTOL_ADD_16, + 32 => circuit::BRISTOL_ADD_32, + 64 => circuit::BRISTOL_ADD_64, + other => panic!("Unsupported bit size {other}"), + }; + let bristol = bristol::circuit(bristol).expect("Unable to parse stored bristol circuit"); + BaseCircuit::from_bristol(bristol, Load::SubCircuit) + .expect("Unable to convert to BaseCircuit") + }); + bc.add_sub_circuit(adder, a.iter().chain(b).copied()) } +#[instrument(level = "debug", ret, skip_all)] fn basic_add( bc: &mut BaseCircuit>, a: &[GateId], @@ -775,7 +824,7 @@ fn basic_add( #[cfg(test)] mod tests { - use super::basic_add; + use super::depth_optimized_add; use crate::circuit::base_circuit::BaseGate; use crate::circuit::{BaseCircuit, DefaultIdx, ExecutableCircuit}; use crate::private_test_utils::{execute_circuit, init_tracing, TestChannel, ToBool}; @@ -834,7 +883,7 @@ mod tests { } #[tokio::test] - async fn basic_add_test() -> anyhow::Result<()> { + async fn low_depth_add_test() -> anyhow::Result<()> { let _g = init_tracing(); let mut bc = BaseCircuit::, _>::new(); @@ -844,7 +893,7 @@ mod tests { let inp2: Vec<_> = (0..8) .map(|_| bc.add_gate(MixedGate::Base(BaseGate::Input(ScalarDim)))) .collect(); - let added = basic_add(&mut bc, &inp1, &inp2); + let added = depth_optimized_add(&mut bc, &inp1, &inp2); for g in added { bc.add_wired_gate(MixedGate::Base(BaseGate::Output(ScalarDim)), &[g]); } @@ -929,7 +978,7 @@ mod tests { let mul = bc.add_wired_gate(MixedGate::Arith(ArithmeticGate::Mul), &[ainp1, ainp2]); let mul_b = a2b(&mut bc, mul); - let added = basic_add(&mut bc, &binps, &mul_b); + let added = depth_optimized_add(&mut bc, &binps, &mul_b); let res_a = bc.add_wired_gate(MixedGate::Conv(ConvGate::B2A), &added); bc.add_wired_gate(MixedGate::Base(BaseGate::Output(ScalarDim)), &[res_a]); diff --git a/crates/seec/src/protocols/tensor_aby2.rs b/crates/seec/src/protocols/tensor_aby2.rs index a81a3d1..9c4d0bd 100644 --- a/crates/seec/src/protocols/tensor_aby2.rs +++ b/crates/seec/src/protocols/tensor_aby2.rs @@ -345,7 +345,8 @@ impl BooleanGate { | BaseGate::SubCircuitInput(_) | BaseGate::SubCircuitOutput(_) | BaseGate::ConnectToMain(_) - | BaseGate::Debug => inputs.next().expect("Empty input"), + | BaseGate::Debug + | BaseGate::Identity => inputs.next().expect("Empty input"), BaseGate::Constant(_) => todo!(), BaseGate::ConnectToMainFromSimd(_) => { panic!("No SIMD support for BoolTensorAby2")