1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 * The license is detailed in the file LICENSE.md, and applies to this file.
15 *
16 * Written by Nir Drucker and Shay Gueron
17 * AWS Cryptographic Algorithms Group.
18 * (ndrucker@amazon.com, gueron@amazon.com)
19 */
20
21 #include "decode.h"
22 #include "utilities.h"
23
24 // Convert a sequence of uint8_t elements which fully uses all 8-bits of
25 // an uint8_t element to a sequence of uint8_t which uses just a single
26 // bit per byte (either 0 or 1).
27 EXTERNC void
convert_to_redundant_rep(OUT uint8_t * out,IN const uint8_t * in,IN const uint64_t len)28 convert_to_redundant_rep(OUT uint8_t *out,
29 IN const uint8_t *in,
30 IN const uint64_t len)
31 {
32 uint8_t tmp;
33 for(uint32_t i = 0; i < (len / 8); i++)
34 {
35 tmp = in[i];
36 for(uint8_t j = 0; j < 8; j++)
37 {
38 out[8 * i + j] |= (tmp & 0x1);
39 tmp >>= 1;
40 }
41 }
42
43 // Convert the reminder
44 tmp = in[len / 8];
45 for(uint32_t j = 8 * (len / 8); j < len; j++)
46 {
47 out[j] |= (tmp & 0x1);
48 tmp >>= 1;
49 }
50 }
51
52 EXTERNC uint64_t
count_ones(IN const uint8_t * in,IN const uint32_t len)53 count_ones(IN const uint8_t *in, IN const uint32_t len)
54 {
55 uint64_t acc = 0;
56 for(uint32_t i = 0; i < len; i++)
57 {
58 acc += __builtin_popcount(in[i]);
59 }
60
61 return acc;
62 }
63