@@ -8,69 +8,73 @@ pub mod signers {
88 const SIGNING_KEY_LEN : usize = 32 ;
99
1010 macro_rules! impl_signature_trait {
11- ( $digest_alg_name: ident, $alias: ident, $sign_fn: ident, $verify_fn: ident) => {
12- #[ allow( non_camel_case_types) ]
13- #[ doc = concat!( "A signer using [`libcrux_sha2::" , stringify!( $digest_alg_name) , "`]." ) ]
14- pub type $alias = Signer <libcrux_sha2:: $digest_alg_name>;
11+ (
12+ $digest_alg_name: ident,
13+ $alias: ident,
14+ $sign_fn: ident,
15+ $verify_fn: ident
16+ ) => {
17+ #[ allow( non_camel_case_types) ]
18+ #[ doc = concat!( "A signer using [`libcrux_sha2::" , stringify!( $digest_alg_name) , "`]." ) ]
19+ pub type $alias = Signer <libcrux_sha2:: $digest_alg_name>;
1520
16- /// The [`arrayref`](libcrux_traits::signature::arrayref) version of the Sign trait.
17- impl arrayref:: Sign <SIGNING_KEY_LEN , SIG_LEN > for $alias {
18- /// The nonce needed for signing.
19- type SignAux <' a> = & ' a Nonce ;
20- type SigningKey <' a, const LEN : usize > = & ' a [ u8 ; SIGNING_KEY_LEN ] ;
21- /// Sign a payload using a provided signing key and `nonce`.
22- #[ inline( always) ]
23- fn sign(
24- payload: & [ u8 ] ,
25- signing_key: & [ u8 ; SIGNING_KEY_LEN ] ,
26- signature: & mut [ u8 ; SIG_LEN ] ,
27- nonce: & Nonce ,
28- ) -> Result <( ) , arrayref:: SignError > {
29- let result = libcrux_p256:: $sign_fn(
30- signature,
31- payload. len( ) . try_into( ) . map_err( |_| arrayref:: SignError :: InvalidPayloadLength ) ?,
32- payload,
33- signing_key,
34- & nonce. 0 ,
35- ) ;
36- if !result {
37- return Err ( arrayref:: SignError :: LibraryError ) ;
21+ /// The [`arrayref`](libcrux_traits::signature::arrayref) version of the Sign trait.
22+ impl arrayref:: Sign <SIGNING_KEY_LEN , SIG_LEN > for $alias {
23+ /// The nonce needed for signing.
24+ type SignAux <' a> = & ' a Nonce ;
25+ type SigningKey <' a, const LEN : usize > = & ' a [ u8 ; SIGNING_KEY_LEN ] ;
26+ /// Sign a payload using a provided signing key and `nonce`.
27+ #[ inline( always) ]
28+ fn sign(
29+ payload: & [ u8 ] ,
30+ signing_key: & [ u8 ; SIGNING_KEY_LEN ] ,
31+ signature: & mut [ u8 ; SIG_LEN ] ,
32+ nonce: & Nonce ,
33+ ) -> Result <( ) , arrayref:: SignError > {
34+ let result = libcrux_p256:: $sign_fn(
35+ signature,
36+ payload. len( ) . try_into( ) . map_err( |_| arrayref:: SignError :: InvalidPayloadLength ) ?,
37+ payload,
38+ signing_key,
39+ & nonce. 0 ,
40+ ) ;
41+ if !result {
42+ return Err ( arrayref:: SignError :: LibraryError ) ;
43+ }
44+ Ok ( ( ) )
3845 }
39- Ok ( ( ) )
4046 }
41- }
42-
43- /// The [`arrayref`](libcrux_traits::signature::arrayref) version of the Verify trait.
44- impl arrayref:: Verify <VERIFICATION_KEY_LEN , SIG_LEN > for $alias {
45- /// No auxiliary information is required for verification.
46- type VerifyAux <' a> = ( ) ;
47- #[ inline( always) ]
48- /// Verify a signature using a provided verification key.
49- fn verify(
50- payload: & [ u8 ] ,
51- verification_key: & [ u8 ; VERIFICATION_KEY_LEN ] ,
52- signature: & [ u8 ; SIG_LEN ] ,
53- _aux: ( ) ,
54- ) -> Result <( ) , arrayref:: VerifyError > {
5547
56- let result = libcrux_p256:: $verify_fn(
57- payload. len( ) . try_into( ) . map_err( |_| arrayref:: VerifyError :: InvalidPayloadLength ) ?,
58- payload,
59- verification_key,
60- <& [ u8 ; 32 ] >:: try_from( & signature[ 0 ..32 ] ) . unwrap( ) ,
61- <& [ u8 ; 32 ] >:: try_from( & signature[ 32 ..] ) . unwrap( ) ,
62- ) ;
63- if !result {
64- return Err ( arrayref:: VerifyError :: LibraryError ) ;
48+ /// The [`arrayref`](libcrux_traits::signature::arrayref) version of the Verify trait.
49+ impl arrayref:: Verify <VERIFICATION_KEY_LEN , SIG_LEN > for $alias {
50+ /// No auxiliary information is required for verification.
51+ type VerifyAux <' a> = ( ) ;
52+ #[ inline( always) ]
53+ /// Verify a signature using a provided verification key.
54+ fn verify(
55+ payload: & [ u8 ] ,
56+ verification_key: & [ u8 ; VERIFICATION_KEY_LEN ] ,
57+ signature: & [ u8 ; SIG_LEN ] ,
58+ _aux: ( ) ,
59+ ) -> Result <( ) , arrayref:: VerifyError > {
60+ let result = libcrux_p256:: $verify_fn(
61+ payload. len( ) . try_into( ) . map_err( |_| arrayref:: VerifyError :: InvalidPayloadLength ) ?,
62+ payload,
63+ verification_key,
64+ <& [ u8 ; 32 ] >:: try_from( & signature[ 0 ..32 ] ) . unwrap( ) ,
65+ <& [ u8 ; 32 ] >:: try_from( & signature[ 32 ..] ) . unwrap( ) ,
66+ ) ;
67+ if !result {
68+ return Err ( arrayref:: VerifyError :: LibraryError ) ;
69+ }
70+ Ok ( ( ) )
6571 }
66- Ok ( ( ) )
6772 }
68- }
69- libcrux_traits:: impl_signature_slice_trait!( $alias => SIGNING_KEY_LEN , SIG_LEN , & Nonce , nonce, & ' a [ u8 ; SIGNING_KEY_LEN ] ) ;
70- libcrux_traits:: impl_verify_slice_trait!( $alias => VERIFICATION_KEY_LEN , SIG_LEN , ( ) , _aux) ;
71- // TODO: owned and secrets traits not appearing in docs
72- } ;
73- }
73+ libcrux_traits:: impl_signature_slice_trait!( $alias => SIGNING_KEY_LEN , SIG_LEN , & Nonce , nonce, & ' a [ u8 ; SIGNING_KEY_LEN ] ) ;
74+ libcrux_traits:: impl_verify_slice_trait!( $alias => VERIFICATION_KEY_LEN , SIG_LEN , ( ) , _aux) ;
75+ // TODO: owned and secrets traits not appearing in docs
76+ } ;
77+ }
7478
7579 pub mod p256 {
7680 //! [`libcrux_traits::signature`] APIs for p256.
0 commit comments