@@ -299,13 +299,62 @@ async fn decrypt_bids(
299299 encrypted_bids : Vec < EncryptedBid > ,
300300 root_ibe_public_key_bytes : Vec < u8 > ,
301301) -> Vec < DecryptedBid > {
302+ let decrypted_values = decrypt_ciphertexts (
303+ lot_id. to_le_bytes ( ) . to_vec ( ) ,
304+ encrypted_bids
305+ . iter ( )
306+ . map ( |bid| bid. encrypted_amount . as_slice ( ) )
307+ . collect :: < Vec < _ > > ( ) ,
308+ root_ibe_public_key_bytes,
309+ )
310+ . await ;
311+
312+ let mut decrypted_bids = Vec :: with_capacity ( encrypted_bids. len ( ) ) ;
313+ for decrypted_value in decrypted_values {
314+ let decrypted_bid: Result < u128 , String > = decrypted_value
315+ . and_then ( |v| {
316+ v. as_slice ( )
317+ . try_into ( )
318+ . map_err ( |_| "failed to convert amount to u128" . to_string ( ) )
319+ } )
320+ . map ( u128:: from_le_bytes) ;
321+ decrypted_bids. push ( decrypted_bid) ;
322+ }
323+
324+ encrypted_bids
325+ . into_iter ( )
326+ . zip ( decrypted_bids. into_iter ( ) )
327+ . inspect ( |( encrypted_bid, decrypted_bid) | {
328+ if let Err ( e) = decrypted_bid {
329+ ic_cdk:: println!(
330+ "Failed to decrypt bid for lot id {lot_id} by {}: {e}" ,
331+ encrypted_bid. bidder
332+ ) ;
333+ }
334+ } )
335+ . filter_map ( |( encrypted_bid, decrypted_bid) | {
336+ decrypted_bid. ok ( ) . map ( |amount| DecryptedBid {
337+ amount,
338+ bidder : encrypted_bid. bidder ,
339+ } )
340+ } )
341+ . collect ( )
342+ }
343+
344+ /// In the canister, using the IBE key derived from the identity decrypt a vector of ciphertexts, which makes them public.
345+ /// Returns a vector, where each value is either a decrypted plaintext or an error message.
346+ async fn decrypt_ciphertexts (
347+ identity : Vec < u8 > ,
348+ encrypted_values : Vec < & [ u8 ] > ,
349+ root_ibe_public_key_bytes : Vec < u8 > ,
350+ ) -> Vec < Result < Vec < u8 > , String > > {
302351 let dummy_seed = vec ! [ 0 ; 32 ] ;
303352 let transport_secret_key = ic_vetkeys:: TransportSecretKey :: from_seed ( dummy_seed. clone ( ) )
304353 . expect ( "failed to create transport secret key" ) ;
305354
306355 let request = VetKDDeriveKeyRequest {
307356 context : DOMAIN_SEPARATOR . as_bytes ( ) . to_vec ( ) ,
308- input : lot_id . to_le_bytes ( ) . to_vec ( ) ,
357+ input : identity . clone ( ) ,
309358 key_id : key_id ( ) ,
310359 transport_public_key : transport_secret_key. public_key ( ) . to_vec ( ) ,
311360 } ;
@@ -326,43 +375,22 @@ async fn decrypt_bids(
326375 . decrypt_and_verify (
327376 & transport_secret_key,
328377 & root_ibe_public_key,
329- lot_id . to_le_bytes ( ) . as_ref ( ) ,
378+ identity . as_ref ( ) ,
330379 )
331380 . expect ( "failed to decrypt ibe key" ) ;
332381
333- let mut decrypted_bids = Vec :: new ( ) ;
334-
335- for encrypted_bid in encrypted_bids {
336- let decrypted_bid: Result < u128 , String > =
337- ic_vetkeys:: IbeCiphertext :: deserialize ( & encrypted_bid. encrypted_amount )
338- . map_err ( |e| format ! ( "failed to deserialize ibe ciphertext: {e}" ) )
339- . and_then ( |c| {
340- c. decrypt ( & ibe_decryption_key)
341- . map_err ( |_| "failed to decrypt ibe ciphertext" . to_string ( ) )
342- } )
343- . and_then ( |bytes| {
344- bytes
345- . as_slice ( )
346- . try_into ( )
347- . map_err ( |_| "failed to convert amount to u128" . to_string ( ) )
348- } )
349- . map ( u128:: from_le_bytes) ;
350- match decrypted_bid {
351- Ok ( amount) => {
352- decrypted_bids. push ( DecryptedBid {
353- amount,
354- bidder : encrypted_bid. bidder ,
355- } ) ;
356- }
357- Err ( e) => {
358- ic_cdk:: println!(
359- "Failed to decrypt bid for lot id {lot_id} by {}: {e}" ,
360- encrypted_bid. bidder
361- ) ;
362- }
363- }
382+ let mut decrypted_values = Vec :: new ( ) ;
383+
384+ for encrypted_value in encrypted_values. into_iter ( ) {
385+ let decrypted_value = ic_vetkeys:: IbeCiphertext :: deserialize ( encrypted_value)
386+ . map_err ( |e| format ! ( "failed to deserialize ibe ciphertext: {e}" ) )
387+ . and_then ( |c| {
388+ c. decrypt ( & ibe_decryption_key)
389+ . map_err ( |_| "failed to decrypt ibe ciphertext" . to_string ( ) )
390+ } ) ;
391+ decrypted_values. push ( decrypted_value) ;
364392 }
365- decrypted_bids
393+ decrypted_values
366394}
367395
368396fn is_authenticated ( ) -> Result < ( ) , String > {
0 commit comments