11//! Adapters for OT types.
22
3+ use std:: io;
4+
35use bitvec:: { order:: Lsb0 , vec:: BitVec } ;
46use cryprot_core:: { Block , buf:: Buf } ;
57use cryprot_net:: ConnectionError ;
68use futures:: { SinkExt , StreamExt } ;
79use subtle:: ConditionallySelectable ;
10+ use thiserror:: Error ;
811use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
912
1013use crate :: {
@@ -38,8 +41,20 @@ impl<P: SemiHonest> SemiHonest for ChosenChoice<P> {}
3841// TODO is there something I can cite that this holds?
3942impl < P : Malicious > Malicious for ChosenChoice < P > { }
4043
44+ #[ derive( Error , Debug ) ]
45+ pub enum Error < E > {
46+ #[ error( "unable to perform R-OTs" ) ]
47+ Rot ( E ) ,
48+ #[ error( "error in sending correction values for C-OT" ) ]
49+ Correction ( io:: Error ) ,
50+ #[ error( "expected correction values but receiver is closed" ) ]
51+ MissingCorrection ,
52+ #[ error( "connection error to peer" ) ]
53+ Connecion ( #[ from] ConnectionError ) ,
54+ }
55+
4156impl < R : RandChoiceRotReceiver > RotReceiver for ChosenChoice < R > {
42- type Error = R :: Error ;
57+ type Error = Error < R :: Error > ;
4358
4459 async fn receive_into (
4560 & mut self ,
@@ -50,30 +65,33 @@ impl<R: RandChoiceRotReceiver> RotReceiver for ChosenChoice<R> {
5065 . 0
5166 . rand_choice_receive_into ( ots)
5267 . await
53- . map_err ( |_| ( ) )
54- . unwrap ( ) ;
68+ . map_err ( Error :: Rot ) ?;
5569 for ( c1, c2) in rand_choices. iter_mut ( ) . zip ( choices) {
5670 * c1 ^= * c2;
5771 }
5872 let mut bv: BitVec < u8 , Lsb0 > = BitVec :: with_capacity ( choices. len ( ) ) ;
5973 bv. extend ( rand_choices. iter ( ) . map ( |c| c. unwrap_u8 ( ) != 0 ) ) ;
6074
61- let ( mut tx, _) = self . connection ( ) . stream ( ) . await . unwrap ( ) ;
62- tx. send ( bv) . await . unwrap ( ) ;
75+ let ( mut tx, _) = self . connection ( ) . stream ( ) . await ? ;
76+ tx. send ( bv) . await . map_err ( Error :: Correction ) ? ;
6377 Ok ( ( ) )
6478 }
6579}
6680
6781impl < S : RotSender + RandChoiceRotSender + Send > RotSender for ChosenChoice < S > {
68- type Error = S :: Error ;
82+ type Error = Error < S :: Error > ;
6983
7084 async fn send_into (
7185 & mut self ,
7286 ots : & mut impl cryprot_core:: buf:: Buf < [ cryprot_core:: Block ; 2 ] > ,
7387 ) -> Result < ( ) , Self :: Error > {
74- self . 0 . send_into ( ots) . await . map_err ( |_| ( ) ) . unwrap ( ) ;
75- let ( _, mut rx) = self . connection ( ) . stream ( ) . await . unwrap ( ) ;
76- let correction: BitVec < u8 , Lsb0 > = rx. next ( ) . await . unwrap ( ) . unwrap ( ) ;
88+ self . 0 . send_into ( ots) . await . map_err ( Error :: Rot ) ?;
89+ let ( _, mut rx) = self . connection ( ) . stream ( ) . await ?;
90+ let correction: BitVec < u8 , Lsb0 > = rx
91+ . next ( )
92+ . await
93+ . ok_or ( Error :: MissingCorrection ) ?
94+ . map_err ( Error :: Correction ) ?;
7795
7896 for ( ots, c_bit) in ots. iter_mut ( ) . zip ( correction) {
7997 let tmp = * ots;
0 commit comments