@@ -17,7 +17,7 @@ use derive_where::DeriveWhere;
17
17
use digest:: core_api:: BlockSizeUser ;
18
18
use digest:: { Digest , FixedOutputReset , Output } ;
19
19
use generic_array:: sequence:: Concat ;
20
- use generic_array:: typenum:: { U1 , U11 , U2 , U20 } ;
20
+ use generic_array:: typenum:: { U11 , U2 , U20 } ;
21
21
use generic_array:: GenericArray ;
22
22
use rand_core:: { CryptoRng , RngCore } ;
23
23
use subtle:: ConstantTimeEq ;
@@ -42,8 +42,17 @@ static STR_VOPRF: [u8; 8] = *b"VOPRF08-";
42
42
/// Determines the mode of operation (either base mode or verifiable mode)
43
43
#[ derive( Clone , Copy ) ]
44
44
enum Mode {
45
- Base = 0 ,
46
- Verifiable = 1 ,
45
+ Base ,
46
+ Verifiable ,
47
+ }
48
+
49
+ impl Mode {
50
+ fn to_u8 ( self ) -> u8 {
51
+ match self {
52
+ Mode :: Base => 0 ,
53
+ Mode :: Verifiable => 1 ,
54
+ }
55
+ }
47
56
}
48
57
49
58
////////////////////////////
@@ -418,7 +427,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> NonVerifiableServer
418
427
/// Corresponds to DeriveKeyPair() function from the VOPRF specification.
419
428
pub fn new_from_seed ( seed : & [ u8 ] ) -> Result < Self > {
420
429
let dst =
421
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Base ) ? ) ;
430
+ GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Base ) ) ;
422
431
let sk = G :: hash_to_scalar :: < H , _ , _ > ( Some ( seed) , dst) ?;
423
432
Ok ( Self {
424
433
sk,
@@ -443,11 +452,11 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> NonVerifiableServer
443
452
chain ! (
444
453
context,
445
454
STR_CONTEXT => |x| Some ( x. as_ref( ) ) ,
446
- get_context_string:: <G >( Mode :: Base ) ? => |x| Some ( x. as_slice( ) ) ,
455
+ get_context_string:: <G >( Mode :: Base ) => |x| Some ( x. as_slice( ) ) ,
447
456
Serialize :: <U2 >:: from( metadata. unwrap_or_default( ) ) ?,
448
457
) ;
449
458
let dst =
450
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Base ) ? ) ;
459
+ GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Base ) ) ;
451
460
let m = G :: hash_to_scalar :: < H , _ , _ > ( context, dst) ?;
452
461
let t = self . sk + & m;
453
462
let evaluation_element = blinded_element. value * & G :: scalar_invert ( & t) ;
@@ -486,7 +495,7 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableServer<G,
486
495
/// Corresponds to DeriveKeyPair() function from the VOPRF specification.
487
496
pub fn new_from_seed ( seed : & [ u8 ] ) -> Result < Self > {
488
497
let dst = GenericArray :: from ( STR_HASH_TO_SCALAR )
489
- . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
498
+ . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
490
499
let sk = G :: hash_to_scalar :: < H , _ , _ > ( Some ( seed) , dst) ?;
491
500
let pk = G :: base_point ( ) * & sk;
492
501
Ok ( Self {
@@ -581,11 +590,11 @@ impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableServer<G,
581
590
) -> Result < VerifiableServerBatchEvaluatePrepareResult < ' a , G , H , I > > {
582
591
chain ! ( context,
583
592
STR_CONTEXT => |x| Some ( x. as_ref( ) ) ,
584
- get_context_string:: <G >( Mode :: Verifiable ) ? => |x| Some ( x. as_slice( ) ) ,
593
+ get_context_string:: <G >( Mode :: Verifiable ) => |x| Some ( x. as_slice( ) ) ,
585
594
Serialize :: <U2 >:: from( metadata. unwrap_or_default( ) ) ?,
586
595
) ;
587
596
let dst = GenericArray :: from ( STR_HASH_TO_SCALAR )
588
- . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
597
+ . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
589
598
let m = G :: hash_to_scalar :: < H , _ , _ > ( context, dst) ?;
590
599
let t = self . sk + & m;
591
600
let evaluation_elements = blinded_elements
@@ -847,7 +856,7 @@ fn deterministic_blind_unchecked<G: Group, H: BlockSizeUser + Digest + FixedOutp
847
856
blind : & G :: Scalar ,
848
857
mode : Mode ,
849
858
) -> Result < G :: Elem > {
850
- let dst = GenericArray :: from ( STR_HASH_TO_GROUP ) . concat ( get_context_string :: < G > ( mode) ? ) ;
859
+ let dst = GenericArray :: from ( STR_HASH_TO_GROUP ) . concat ( get_context_string :: < G > ( mode) ) ;
851
860
let hashed_point = G :: hash_to_curve :: < H , _ > ( input, dst) ?;
852
861
Ok ( hashed_point * blind)
853
862
}
@@ -884,12 +893,12 @@ where
884
893
{
885
894
chain ! ( context,
886
895
STR_CONTEXT => |x| Some ( x. as_ref( ) ) ,
887
- get_context_string:: <G >( Mode :: Verifiable ) ? => |x| Some ( x. as_slice( ) ) ,
896
+ get_context_string:: <G >( Mode :: Verifiable ) => |x| Some ( x. as_slice( ) ) ,
888
897
Serialize :: <U2 >:: from( info) ?,
889
898
) ;
890
899
891
900
let dst =
892
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
901
+ GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
893
902
let m = G :: hash_to_scalar :: < H , _ , _ > ( context, dst) ?;
894
903
895
904
let g = G :: base_point ( ) ;
@@ -933,7 +942,7 @@ fn generate_proof<
933
942
let t3 = m * & r;
934
943
935
944
let challenge_dst =
936
- GenericArray :: from ( STR_CHALLENGE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
945
+ GenericArray :: from ( STR_CHALLENGE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
937
946
chain ! (
938
947
h2_input,
939
948
Serialize :: <U2 , _>:: from_owned( G :: to_arr( b) ) ?,
@@ -945,7 +954,7 @@ fn generate_proof<
945
954
) ;
946
955
947
956
let hash_to_scalar_dst =
948
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
957
+ GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
949
958
950
959
let c_scalar = G :: hash_to_scalar :: < H , _ , _ > ( h2_input, hash_to_scalar_dst) ?;
951
960
let s_scalar = r - & ( c_scalar * & k) ;
@@ -970,7 +979,7 @@ fn verify_proof<G: Group, H: BlockSizeUser + Digest + FixedOutputReset>(
970
979
let t3 = ( m * & proof. s_scalar ) + & ( z * & proof. c_scalar ) ;
971
980
972
981
let challenge_dst =
973
- GenericArray :: from ( STR_CHALLENGE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
982
+ GenericArray :: from ( STR_CHALLENGE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
974
983
chain ! (
975
984
h2_input,
976
985
Serialize :: <U2 , _>:: from_owned( G :: to_arr( b) ) ?,
@@ -982,7 +991,7 @@ fn verify_proof<G: Group, H: BlockSizeUser + Digest + FixedOutputReset>(
982
991
) ;
983
992
984
993
let hash_to_scalar_dst =
985
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
994
+ GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
986
995
let c = G :: hash_to_scalar :: < H , _ , _ > ( h2_input, hash_to_scalar_dst) ?;
987
996
988
997
match c. ct_eq ( & proof. c_scalar ) . into ( ) {
@@ -1007,7 +1016,7 @@ fn finalize_after_unblind<
1007
1016
info : & ' a [ u8 ] ,
1008
1017
mode : Mode ,
1009
1018
) -> Result < FinalizeAfterUnblindResult < G , H , I , IE > > {
1010
- let finalize_dst = GenericArray :: from ( STR_FINALIZE ) . concat ( get_context_string :: < G > ( mode) ? ) ;
1019
+ let finalize_dst = GenericArray :: from ( STR_FINALIZE ) . concat ( get_context_string :: < G > ( mode) ) ;
1011
1020
1012
1021
Ok ( inputs_and_unblinded_elements
1013
1022
// To make a return type possible, we have to convert to a `fn` pointer,
@@ -1038,9 +1047,9 @@ fn compute_composites<G: Group, H: BlockSizeUser + Digest + FixedOutputReset>(
1038
1047
return Err ( Error :: MismatchedLengthsForCompositeInputs ) ;
1039
1048
}
1040
1049
1041
- let seed_dst = GenericArray :: from ( STR_SEED ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
1050
+ let seed_dst = GenericArray :: from ( STR_SEED ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
1042
1051
let composite_dst =
1043
- GenericArray :: from ( STR_COMPOSITE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
1052
+ GenericArray :: from ( STR_COMPOSITE ) . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
1044
1053
1045
1054
chain ! (
1046
1055
h1_input,
@@ -1063,7 +1072,7 @@ fn compute_composites<G: Group, H: BlockSizeUser + Digest + FixedOutputReset>(
1063
1072
Serialize :: <U2 , _>:: from_owned( composite_dst) ?,
1064
1073
) ;
1065
1074
let dst = GenericArray :: from ( STR_HASH_TO_SCALAR )
1066
- . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ? ) ;
1075
+ . concat ( get_context_string :: < G > ( Mode :: Verifiable ) ) ;
1067
1076
let di = G :: hash_to_scalar :: < H , _ , _ > ( h2_input, dst) ?;
1068
1077
m = c. value * & di + & m;
1069
1078
z = match k_option {
@@ -1082,10 +1091,10 @@ fn compute_composites<G: Group, H: BlockSizeUser + Digest + FixedOutputReset>(
1082
1091
1083
1092
/// Generates the contextString parameter as defined in
1084
1093
/// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-08.html>
1085
- fn get_context_string < G : Group > ( mode : Mode ) -> Result < GenericArray < u8 , U11 > > {
1086
- Ok ( GenericArray :: from ( STR_VOPRF )
1087
- . concat ( i2osp :: < U1 > ( mode as usize ) ? )
1088
- . concat ( i2osp :: < U2 > ( G :: SUITE_ID ) ? ) )
1094
+ fn get_context_string < G : Group > ( mode : Mode ) -> GenericArray < u8 , U11 > {
1095
+ GenericArray :: from ( STR_VOPRF )
1096
+ . concat ( [ mode. to_u8 ( ) ] . into ( ) )
1097
+ . concat ( G :: SUITE_ID . to_be_bytes ( ) . into ( ) )
1089
1098
}
1090
1099
1091
1100
///////////
@@ -1113,18 +1122,16 @@ mod tests {
1113
1122
info : & [ u8 ] ,
1114
1123
mode : Mode ,
1115
1124
) -> Output < H > {
1116
- let dst =
1117
- GenericArray :: from ( STR_HASH_TO_GROUP ) . concat ( get_context_string :: < G > ( mode) . unwrap ( ) ) ;
1125
+ let dst = GenericArray :: from ( STR_HASH_TO_GROUP ) . concat ( get_context_string :: < G > ( mode) ) ;
1118
1126
let point = G :: hash_to_curve :: < H , _ > ( input, dst) . unwrap ( ) ;
1119
1127
1120
1128
chain ! ( context,
1121
1129
STR_CONTEXT => |x| Some ( x. as_ref( ) ) ,
1122
- get_context_string:: <G >( mode) . unwrap ( ) => |x| Some ( x. as_slice( ) ) ,
1130
+ get_context_string:: <G >( mode) => |x| Some ( x. as_slice( ) ) ,
1123
1131
Serialize :: <U2 >:: from( info) . unwrap( ) ,
1124
1132
) ;
1125
1133
1126
- let dst =
1127
- GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( mode) . unwrap ( ) ) ;
1134
+ let dst = GenericArray :: from ( STR_HASH_TO_SCALAR ) . concat ( get_context_string :: < G > ( mode) ) ;
1128
1135
let m = G :: hash_to_scalar :: < H , _ , _ > ( context, dst) . unwrap ( ) ;
1129
1136
1130
1137
let res = point * & G :: scalar_invert ( & ( key + & m) ) ;
@@ -1315,8 +1322,7 @@ mod tests {
1315
1322
)
1316
1323
. unwrap ( ) ;
1317
1324
1318
- let dst = GenericArray :: from ( STR_HASH_TO_GROUP )
1319
- . concat ( get_context_string :: < G > ( Mode :: Base ) . unwrap ( ) ) ;
1325
+ let dst = GenericArray :: from ( STR_HASH_TO_GROUP ) . concat ( get_context_string :: < G > ( Mode :: Base ) ) ;
1320
1326
let point = G :: hash_to_curve :: < H , _ > ( & input, dst) . unwrap ( ) ;
1321
1327
let res2 = finalize_after_unblind :: < G , H , _ , _ > (
1322
1328
Some ( ( input. as_ref ( ) , point) ) . into_iter ( ) ,
0 commit comments