forked from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64.cairo
279 lines (260 loc) · 6.77 KB
/
base64.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use alexandria_data_structures::array_ext::ArrayTraitExt;
use alexandria_math::BitShift;
use core::num::traits::Bounded;
pub trait Encoder<T> {
fn encode(data: T) -> Array<u8>;
}
pub trait Decoder<T> {
fn decode(data: T) -> Array<u8>;
}
pub impl Base64Encoder of Encoder<Array<u8>> {
fn encode(data: Array<u8>) -> Array<u8> {
let mut char_set = get_base64_char_set();
char_set.append('+');
char_set.append('/');
encode_u8_array(data, char_set.span())
}
}
pub impl Base64UrlEncoder of Encoder<Array<u8>> {
fn encode(data: Array<u8>) -> Array<u8> {
let mut char_set = get_base64_char_set();
char_set.append('-');
char_set.append('_');
encode_u8_array(data, char_set.span())
}
}
pub impl Base64FeltEncoder of Encoder<felt252> {
fn encode(data: felt252) -> Array<u8> {
let mut char_set = get_base64_char_set();
char_set.append('+');
char_set.append('/');
encode_felt(data, char_set.span())
}
}
pub impl Base64UrlFeltEncoder of Encoder<felt252> {
fn encode(data: felt252) -> Array<u8> {
let mut char_set = get_base64_char_set();
char_set.append('-');
char_set.append('_');
encode_felt(data, char_set.span())
}
}
pub fn encode_u8_array(mut bytes: Array<u8>, base64_chars: Span<u8>) -> Array<u8> {
let mut result = array![];
if bytes.len() == 0 {
return result;
}
let mut p: u8 = 0;
let c = bytes.len() % 3;
if c == 1 {
p = 2;
bytes.append(0_u8);
bytes.append(0_u8);
} else if c == 2 {
p = 1;
bytes.append(0_u8);
}
let mut i = 0;
let bytes_len = bytes.len();
let last_iteration = bytes_len - 3;
while (i != bytes_len) {
let n: u32 = (*bytes[i]).into()
* 65536 | (*bytes[i + 1]).into()
* 256 | (*bytes[i + 2]).into();
let e1 = (n / 262144) & 63;
let e2 = (n / 4096) & 63;
let e3 = (n / 64) & 63;
let e4 = n & 63;
result.append(*base64_chars[e1]);
result.append(*base64_chars[e2]);
if i == last_iteration {
if p == 2 {
result.append('=');
result.append('=');
} else if p == 1 {
result.append(*base64_chars[e3]);
result.append('=');
} else {
result.append(*base64_chars[e3]);
result.append(*base64_chars[e4]);
}
} else {
result.append(*base64_chars[e3]);
result.append(*base64_chars[e4]);
}
i += 3;
};
result
}
pub fn encode_felt(self: felt252, base64_chars: Span<u8>) -> Array<u8> {
let mut result = array![];
let mut num: u256 = self.into();
if num != 0 {
let (quotient, remainder) = DivRem::div_rem(num, 65536_u256.try_into().unwrap());
// Safe since 'remainder' is always less than 65536 (2^16),
// which is within the range of usize (less than 2^32).
let remainder: usize = remainder.try_into().unwrap();
let r3 = (remainder / 1024) & 63;
let r2 = (remainder / 16) & 63;
let r1 = (remainder * 4) & 63;
result.append(*base64_chars[r1]);
result.append(*base64_chars[r2]);
result.append(*base64_chars[r3]);
num = quotient;
}
while (num != 0) {
let (quotient, remainder) = DivRem::div_rem(num, 16777216_u256.try_into().unwrap());
// Safe since 'remainder' is always less than 16777216 (2^24),
// which is within the range of usize (less than 2^32).
let remainder: usize = remainder.try_into().unwrap();
let r4 = remainder / 262144;
let r3 = (remainder / 4096) & 63;
let r2 = (remainder / 64) & 63;
let r1 = remainder & 63;
result.append(*base64_chars[r1]);
result.append(*base64_chars[r2]);
result.append(*base64_chars[r3]);
result.append(*base64_chars[r4]);
num = quotient;
};
while (result.len() < 43) {
result.append('A');
};
result = result.reversed();
result.append('=');
result
}
pub impl Base64Decoder of Decoder<Array<u8>> {
fn decode(data: Array<u8>) -> Array<u8> {
inner_decode(data)
}
}
pub impl Base64UrlDecoder of Decoder<Array<u8>> {
fn decode(mut data: Array<u8>) -> Array<u8> {
inner_decode(data)
}
}
fn inner_decode(data: Array<u8>) -> Array<u8> {
let mut result = array![];
let mut p = 0_u8;
if data.len() > 0 {
if *data[data.len() - 1] == '=' {
p += 1;
}
if *data[data.len() - 2] == '=' {
p += 1;
}
decode_loop(p, data, 0, ref result);
}
result
}
fn decode_loop(p: u8, data: Array<u8>, d: usize, ref result: Array<u8>) {
if (d >= data.len()) {
return;
}
let x: u128 = BitShift::shl((get_base64_value(*data[d]).into()), 18)
| BitShift::shl((get_base64_value(*data[d + 1])).into(), 12)
| BitShift::shl((get_base64_value(*data[d + 2])).into(), 6)
| (get_base64_value(*data[d + 3])).into();
let mut i: u8 = (BitShift::shr(x, 16) & Bounded::<u8>::MAX.into()).try_into().unwrap();
result.append(i);
i = (BitShift::shr(x, 8) & Bounded::<u8>::MAX.into()).try_into().unwrap();
if d + 4 >= data.len() && p == 2 {
return;
}
result.append(i);
i = (x & Bounded::<u8>::MAX.into()).try_into().unwrap();
if d + 4 >= data.len() && p == 1 {
return;
}
result.append(i);
decode_loop(p, data, d + 4, ref result);
}
fn get_base64_value(x: u8) -> u8 {
if (x == '+') {
62
} else if (x == '-') {
62
} else if (x == '/') {
63
} else if (x <= '9') {
(x - '0') + 52
} else if (x == '=') {
0
} else if (x <= 'Z') {
(x - 'A') + 0
} else if (x == '_') {
63
} else if (x <= 'z') {
(x - 'a') + 26
} else {
0
}
}
fn get_base64_char_set() -> Array<u8> {
let mut result = array![
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'
];
result
}