5
5
pub mod aead;
6
6
pub mod fr;
7
7
use crate :: errors:: { IronfishError , IronfishErrorKind } ;
8
+ pub use ironfish_zkp:: hex:: { bytes_to_hex, hex_to_bytes, hex_to_vec_bytes} ;
8
9
9
10
/// Helper functions to convert pairing parts to bytes
10
11
///
@@ -16,8 +17,6 @@ use group::GroupEncoding;
16
17
17
18
use std:: io;
18
19
19
- const HEX_CHARS : & [ u8 ; 16 ] = b"0123456789abcdef" ;
20
-
21
20
pub ( crate ) fn read_scalar < F : PrimeField , R : io:: Read > ( mut reader : R ) -> Result < F , IronfishError > {
22
21
let mut fr_repr = F :: Repr :: default ( ) ;
23
22
reader. read_exact ( fr_repr. as_mut ( ) ) ?;
@@ -43,109 +42,3 @@ pub(crate) fn read_point_unchecked<G: GroupEncoding, R: io::Read>(
43
42
Option :: from ( G :: from_bytes_unchecked ( & point_repr) )
44
43
. ok_or_else ( || IronfishError :: new ( IronfishErrorKind :: InvalidData ) )
45
44
}
46
-
47
- /// Output the bytes as a hexadecimal String
48
- pub fn bytes_to_hex ( bytes : & [ u8 ] ) -> String {
49
- let mut hex: Vec < u8 > = vec ! [ 0 ; bytes. len( ) * 2 ] ;
50
-
51
- for ( i, b) in bytes. iter ( ) . enumerate ( ) {
52
- hex[ i * 2 ] = HEX_CHARS [ ( b >> 4 ) as usize ] ;
53
- hex[ i * 2 + 1 ] = HEX_CHARS [ ( b & 0x0f ) as usize ] ;
54
- }
55
-
56
- unsafe { String :: from_utf8_unchecked ( hex) }
57
- }
58
-
59
- /// Output the hexadecimal String as bytes
60
- pub fn hex_to_bytes < const SIZE : usize > ( hex : & str ) -> Result < [ u8 ; SIZE ] , IronfishError > {
61
- if hex. len ( ) != SIZE * 2 {
62
- return Err ( IronfishError :: new ( IronfishErrorKind :: InvalidData ) ) ;
63
- }
64
-
65
- let mut bytes = [ 0 ; SIZE ] ;
66
-
67
- let hex_iter = hex. as_bytes ( ) . chunks_exact ( 2 ) ;
68
-
69
- for ( i, hex) in hex_iter. enumerate ( ) {
70
- bytes[ i] = hex_to_u8 ( hex[ 0 ] ) ? << 4 | hex_to_u8 ( hex[ 1 ] ) ?;
71
- }
72
-
73
- Ok ( bytes)
74
- }
75
-
76
- pub fn hex_to_vec_bytes ( hex : & str ) -> Result < Vec < u8 > , IronfishError > {
77
- if hex. len ( ) % 2 != 0 {
78
- return Err ( IronfishError :: new ( IronfishErrorKind :: InvalidData ) ) ;
79
- }
80
-
81
- let mut bytes = Vec :: new ( ) ;
82
-
83
- let hex_iter = hex. as_bytes ( ) . chunks_exact ( 2 ) ;
84
-
85
- for hex in hex_iter {
86
- bytes. push ( hex_to_u8 ( hex[ 0 ] ) ? << 4 | hex_to_u8 ( hex[ 1 ] ) ?) ;
87
- }
88
-
89
- Ok ( bytes)
90
- }
91
-
92
- #[ inline]
93
- fn hex_to_u8 ( char : u8 ) -> Result < u8 , IronfishError > {
94
- match char {
95
- b'0' ..=b'9' => Ok ( char - b'0' ) ,
96
- b'a' ..=b'f' => Ok ( char - b'a' + 10 ) ,
97
- b'A' ..=b'F' => Ok ( char - b'A' + 10 ) ,
98
- _ => Err ( IronfishError :: new ( IronfishErrorKind :: InvalidData ) ) ,
99
- }
100
- }
101
-
102
- #[ cfg( test) ]
103
- mod test {
104
- use crate :: serializing:: { bytes_to_hex, hex_to_bytes, hex_to_vec_bytes} ;
105
-
106
- #[ test]
107
- fn test_hex_to_vec_bytes_valid ( ) {
108
- let hex = "A1B2C3" ;
109
- let expected_bytes = vec ! [ 161 , 178 , 195 ] ;
110
-
111
- let result = hex_to_vec_bytes ( hex) . expect ( "valid hex" ) ;
112
-
113
- assert_eq ! ( result, expected_bytes) ;
114
- }
115
-
116
- #[ test]
117
- fn test_hex_to_vec_bytes_invalid_char ( ) {
118
- let hex = "A1B2G3" ;
119
- hex_to_vec_bytes ( hex) . expect_err ( "invalid hex should throw an error" ) ;
120
- }
121
-
122
- #[ test]
123
- fn test_hex_to_vec_bytes_invalid_hex_with_odd_length ( ) {
124
- let hex = "A1B2C" ;
125
- hex_to_vec_bytes ( hex) . expect_err ( "invalid hex should throw an error" ) ;
126
- }
127
-
128
- #[ test]
129
- fn hex_serde ( ) {
130
- const HEX_STRING : & str = "68656C6C6F20776F726C6420616E64207374756666" ;
131
- const HEX_LOWER : & str = "68656c6c6f20776f726c6420616e64207374756666" ;
132
- const BYTE_LENGTH : usize = HEX_STRING . len ( ) / 2 ;
133
- // Same as above with the last character removed, which makes the hex
134
- // invalid as the length of a hex string must be divisible by 2
135
- const INVALID_HEX : & str = "68656C6C6F20776F726C6420616E6420737475666" ;
136
-
137
- hex_to_bytes :: < BYTE_LENGTH > ( INVALID_HEX ) . expect_err ( "invalid hex should throw an error" ) ;
138
-
139
- let bytes: [ u8 ; BYTE_LENGTH ] = hex_to_bytes ( HEX_STRING ) . expect ( "converts hex to bytes" ) ;
140
- let lower_bytes: [ u8 ; BYTE_LENGTH ] =
141
- hex_to_bytes ( HEX_STRING ) . expect ( "converts hex to bytes" ) ;
142
-
143
- assert_eq ! ( bytes, lower_bytes) ;
144
-
145
- let hex = bytes_to_hex ( & bytes) ;
146
- let lower_hex = bytes_to_hex ( & lower_bytes) ;
147
-
148
- assert_eq ! ( HEX_LOWER , hex) ;
149
- assert_eq ! ( HEX_LOWER , lower_hex) ;
150
- }
151
- }
0 commit comments