Line data Source code
1 : /** 2 : * The MIT License (MIT) 3 : * 4 : * Copyright (c) 2021 RSK Labs Ltd 5 : * 6 : * Permission is hereby granted, free of charge, to any person obtaining a copy 7 : * of this software and associated documentation files (the "Software"), to 8 : * deal in the Software without restriction, including without limitation the 9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 : * sell copies of the Software, and to permit persons to whom the Software is 11 : * furnished to do so, subject to the following conditions: 12 : * 13 : * The above copyright notice and this permission notice shall be included in 14 : * all copies or substantial portions of the Software. 15 : * 16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 : * IN THE SOFTWARE. 23 : */ 24 : 25 : #include "bigdigits_helper.h" 26 : 27 781 : void parse_bigint_be(const uint8_t* buf, 28 : uint16_t buf_size, 29 : DIGIT_T target[], 30 : uint16_t target_digits) { 31 : 32 781 : mpSetZero(target, target_digits); 33 781 : int j = 0, k = 0; 34 781 : DIGIT_T curr = 0; 35 16426 : for (int i = buf_size - 1; i >= 0; i--) { 36 15645 : curr = buf[i]; 37 15645 : target[j] |= (curr << (k * 8)); 38 15645 : if (++k == sizeof(DIGIT_T)) { 39 3833 : ++j; 40 3833 : k = 0; 41 : } 42 : } 43 781 : } 44 : 45 88 : void dump_bigint_be(uint8_t* buf, const DIGIT_T n[], const size_t digits) { 46 88 : int k = 0; 47 801 : for (int i = digits - 1; i >= 0; i--) { 48 713 : buf[k++] = (uint8_t)((n[i] & 0xff000000) >> 24); 49 713 : buf[k++] = (uint8_t)((n[i] & 0x00ff0000) >> 16); 50 713 : buf[k++] = (uint8_t)((n[i] & 0x0000ff00) >> 8); 51 713 : buf[k++] = (uint8_t)((n[i] & 0x000000ff) >> 0); 52 : } 53 88 : }