Struct seec::protocols::boolean_gmw::XorSharing
source · pub struct XorSharing<R: CryptoRng + Rng> { /* private fields */ }
Implementations§
source§impl<R: CryptoRng + Rng> XorSharing<R>
impl<R: CryptoRng + Rng> XorSharing<R>
sourcepub fn new(rng: R) -> Self
pub fn new(rng: R) -> Self
Examples found in repository?
crates/seec/examples/aes_cbc.rs (line 153)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
async fn execute(args: &ExecuteArgs) -> Result<()> {
let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
0 => seec_channel::tcp::listen(args.server).await?,
1 => seec_channel::tcp::connect(args.server).await?,
illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
};
let (mut main_channel, executor_channel) =
sub_channels_for!(&mut sender, &mut receiver, 8, Msg, Message<BooleanGmw>).await?;
let mut sharing = XorSharing::new(ChaChaRng::from_rng(thread_rng()).context("Sharing RNG")?);
let ot_channel = if args.insecure_setup.not() {
let sub_channel = sub_channel_with(
&mut main_channel.0,
&mut main_channel.1,
128,
Msg::OtChannel,
|msg| match msg {
Msg::OtChannel(sender) => Some(sender),
_ => None,
},
)
.await
.context("failed to create ot sub_channel")?;
Some(sub_channel)
} else {
None
};
match args.id {
0 => {
let key: [u8; 16] = hex::decode(&args.key)
.context("Decoding key")?
.try_into()
.ok()
.context("Key must be 16 bytes long")?;
let key: [usize; 2] = bytemuck::cast(key);
// Apparently, the AES circuit wants it's arguments in Msb format. However,
// the executor currently expects arguments to be in a BitVec with Lsb order.
// The following changes the order of the bits manyally
// TODO the following is likely wrong with the change to BitVec<usize>
let msb_key: BitVec<usize> = key.iter().map(|bytes| bytes.reverse_bits()).collect();
let iv = thread_rng().gen::<[usize; 2]>();
let msb_iv: BitVec<usize> = iv.iter().map(|bytes| bytes.reverse_bits()).collect();
let [key_share0, key_share1] = sharing.share(msb_key);
let [iv_share0, iv_share1] = sharing.share(msb_iv);
main_channel
.0
.send(Msg::ShareIvKey {
iv: iv_share1,
key: key_share1,
})
.await?;
let Msg::ShareInput(input_share) = main_channel
.1
.recv()
.await?
.ok_or(anyhow::anyhow!("Remote closed"))?
else {
anyhow::bail!("Received wrong message. Expected ShareInput")
};
let out = encrypt(
args,
executor_channel,
ot_channel,
&input_share,
&key_share0,
&iv_share0,
)
.await?;
main_channel
.0
.send(Msg::ReconstructAesCiphertext(out.clone()))
.await?;
if args.validate {
main_channel.0.send(Msg::PlainIvKey { iv, key }).await?;
}
// Try to recv Ack but ignore errors
let _ = main_channel.1.recv().await;
info!(
bytes_written = bytes_written.get(),
bytes_read = bytes_read.get(),
);
}
1 => {
let (data, padded_data) = get_data(args).context("Loading data to encrypt")?;
let mut padded_data_usize = vec![0_usize; padded_data.len() / mem::size_of::<usize>()];
bytemuck::cast_slice_mut(&mut padded_data_usize).clone_from_slice(&padded_data);
let padded_file_data = BitVec::from_vec(padded_data_usize);
let [input_share0, input_share1] = sharing.share(padded_file_data);
main_channel.0.send(Msg::ShareInput(input_share1)).await?;
let Msg::ShareIvKey {
iv: iv_share,
key: key_share,
} = main_channel
.1
.recv()
.await
.context("Receiving IvKeyShare")?
.ok_or(anyhow::anyhow!("Remote closed"))?
else {
anyhow::bail!("Received wrong message. Expected IvKeyShare")
};
let out = encrypt(
args,
executor_channel,
ot_channel,
&input_share0,
&key_share,
&iv_share,
)
.await?;
let Msg::ReconstructAesCiphertext(shared_out) = main_channel
.1
.recv()
.await
.context("Receiving ciphertext share")?
.ok_or(anyhow::anyhow!("Remote closed"))?
else {
anyhow::bail!("Received wrong message. Expected IvKeyShare")
};
let ciphertext = match (out, shared_out) {
(Output::Scalar(out), Output::Scalar(shared_out)) => {
XorSharing::<ThreadRng>::reconstruct([out, shared_out])
}
(Output::Simd(out), Output::Simd(shared_out)) => out
.into_iter()
.zip(shared_out)
.flat_map(|(a, b)| XorSharing::<ThreadRng>::reconstruct([a, b]))
.collect(),
_ => unreachable!("Non compatible output"),
};
if args.validate {
let Msg::PlainIvKey { iv, key } = main_channel
.1
.recv()
.await
.context("Reconstructing Iv/Key")?
.ok_or(anyhow::anyhow!("Remote closed"))?
else {
anyhow::bail!("Received wrong message. Expected ReconstructIvKey")
};
validate(iv, key, &data, &ciphertext)?;
}
main_channel.0.send(Msg::Ack).await?;
let encoded = hex::encode(bytemuck::cast_slice(ciphertext.as_raw_slice()));
info!(
bytes_written = bytes_written.get(),
bytes_read = bytes_read.get(),
ciphertext = encoded
);
}
_ => unreachable!(),
};
Ok(())
}
Trait Implementations§
Auto Trait Implementations§
impl<R> Freeze for XorSharing<R>where
R: Freeze,
impl<R> RefUnwindSafe for XorSharing<R>where
R: RefUnwindSafe,
impl<R> Send for XorSharing<R>where
R: Send,
impl<R> Sync for XorSharing<R>where
R: Sync,
impl<R> Unpin for XorSharing<R>where
R: Unpin,
impl<R> UnwindSafe for XorSharing<R>where
R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
Causes
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
Causes
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
Causes
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
Causes
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
Causes
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
Causes
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
Causes
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
Causes
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
Formats each item in a sequence. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Pipes by value. This is generally the method you want to use. Read more
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
Borrows
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
Mutably borrows
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
Borrows
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
Mutably borrows
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
Borrows
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Immutable access to the
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
Mutable access to the
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
Immutable access to the
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
Mutable access to the
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Immutable access to the
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Mutable access to the
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
Calls
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
Calls
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
Calls
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
Calls
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
Calls
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
Calls
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
Calls
.tap_deref()
only in debug builds, and is erased in release
builds.