| 1 | /* |
| 2 | * Class for simple, arbitrary size unsigned integer math. |
| 3 | * Note that some method implementations might lack features that weren't required so far! |
| 4 | * |
| 5 | * Copyright (C) 2008 - Olaf Freyer |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Affero General Public License |
| 9 | * as published by the Free Software Foundation; either version 3 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 19 | #include "bigint.h"cxx . . $ `<file>/src/common/bigint.h`/ |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <cstdint> |
| 24 | #include <cstdio> |
| 25 | #include <cstdlib> |
| 26 | #include <cstring> |
| 27 | #include <iostream> |
| 28 | #include <stdexcept> |
| 29 | #include <sstream> |
| 30 | |
| 31 | #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/ |
| 32 | |
| 33 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 34 | |
| 35 | namespace pvpgncxx . . $ pvpgn/ |
| 36 | { |
| 37 | |
| 38 | static const char small_digitscxx . . $ pvpgn/small_digits.[] = "0123456789abcdef"; |
| 39 | |
| 40 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).() |
| 41 | { |
| 42 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = 1; |
| 43 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 44 | segmentcxx . . $ pvpgn/BigInt#segment.[0] = 0; |
| 45 | } |
| 46 | |
| 47 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(ac50b47256ae7414).(stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# input) |
| 48 | { |
| 49 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = 1; |
| 50 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 51 | segmentcxx . . $ pvpgn/BigInt#segment.[0] = input; |
| 52 | } |
| 53 | |
| 54 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(ed53e3bbd3fa2f69).(stdcxx . . $ std/::uint16_tcxx . . $ std/uint16_t# input) |
| 55 | { |
| 56 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = 1; |
| 57 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 58 | segmentcxx . . $ pvpgn/BigInt#segment.[0] = input; |
| 59 | } |
| 60 | |
| 61 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(cb2f890c2fdad230).(stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# input) |
| 62 | { |
| 63 | #ifndef HAVE_UINT64_T |
| 64 | int i; |
| 65 | #endif |
| 66 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = sizeof(stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#) / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 67 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 68 | #ifdef HAVE_UINT64_T |
| 69 | segment[0] = input; |
| 70 | #else |
| 71 | for (i = 0; i < segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++){ |
| 72 | segmentcxx . . $ pvpgn/BigInt#segment.[i] = input & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 73 | input >>= bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 74 | } |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(2c44cd906ae4e829).(stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t# input) |
| 79 | { |
| 80 | int i; |
| 81 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = sizeof(stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#) / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 82 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 83 | for (i = 0; i < segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++){ |
| 84 | segmentcxx . . $ pvpgn/BigInt#segment.[i] = input & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 85 | input >>= bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).(const BigIntcxx . . $ pvpgn/BigInt#& input) |
| 90 | :segment_countcxx . . $ pvpgn/BigInt#segment_count.(input.segment_countcxx . . $ pvpgn/BigInt#segment_count.) |
| 91 | { |
| 92 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 93 | stdcxx . . $ std/::memcpy(segmentcxx . . $ pvpgn/BigInt#segment., input.segmentcxx . . $ pvpgn/BigInt#segment., segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 94 | } |
| 95 | |
| 96 | BigIntcxx . . $ pvpgn/BigInt#& |
| 97 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625).=(const BigIntcxx . . $ pvpgn/BigInt#& input) |
| 98 | { |
| 99 | if (&input != this) { |
| 100 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = input.segment_countcxx . . $ pvpgn/BigInt#segment_count.; |
| 101 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(segment, segment_count * sizeof(bigint_base)); |
| 102 | stdcxx . . $ std/::memcpy(segmentcxx . . $ pvpgn/BigInt#segment., input.segmentcxx . . $ pvpgn/BigInt#segment., segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 103 | } |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | BigIntcxx . . $ pvpgn/BigInt#::BigIntcxx . . $ pvpgn/BigInt#BigInt(a7352a5524aaaab2).(unsigned char const * input, int input_size, int blockSize, bool bigEndian) |
| 108 | { |
| 109 | int i, j; |
| 110 | unsigned char *in; |
| 111 | unsigned char *inPointer; |
| 112 | |
| 113 | if (bigEndian){ |
| 114 | in = (unsigned char*)input; |
| 115 | inPointer = (unsigned char*)in; |
| 116 | } |
| 117 | else { |
| 118 | in = (unsigned char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(input_size); |
| 119 | inPointer = (unsigned char*)in + input_size - 1; |
| 120 | if (blockSize == 1) |
| 121 | stdcxx . . $ std/::memcpy(in, input, input_size); |
| 122 | else { |
| 123 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(blockSize % 2 == 0); |
| 124 | for (i = 0; i < input_size; i += blockSize) |
| 125 | { |
| 126 | for (j = 0; j < blockSize / 2; j++) |
| 127 | { |
| 128 | in[i + j] = input[i + blockSize - (j + 1)]; |
| 129 | in[i + blockSize - (j + 1)] = input[i + j]; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | segment_countcxx . . $ pvpgn/BigInt#segment_count. = input_size / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 136 | if (input_size % sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)) |
| 137 | segment_countcxx . . $ pvpgn/BigInt#segment_count.++; |
| 138 | segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(segment_count * sizeof(bigint_base)); |
| 139 | stdcxx . . $ std/::memset(segmentcxx . . $ pvpgn/BigInt#segment., 0, segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 140 | |
| 141 | |
| 142 | for (i = input_size - 1; i >= 0; i--) |
| 143 | { |
| 144 | j = i / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 145 | segmentcxx . . $ pvpgn/BigInt#segment.[j] <<= 8; |
| 146 | if (bigEndian) |
| 147 | segmentcxx . . $ pvpgn/BigInt#segment.[j] += *(inPointer++); |
| 148 | else |
| 149 | segmentcxx . . $ pvpgn/BigInt#segment.[j] += *(inPointer--); |
| 150 | } |
| 151 | |
| 152 | if (!bigEndian) |
| 153 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(in); |
| 154 | } |
| 155 | |
| 156 | BigIntcxx . . $ pvpgn/BigInt#::~cxx . . $ pvpgn/BigInt#`~BigInt`(ced63f7c635d850d).BigIntcxx . . $ pvpgn/BigInt#() throw() |
| 157 | { |
| 158 | if (segmentcxx . . $ pvpgn/BigInt#segment.) |
| 159 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(segment); |
| 160 | } |
| 161 | |
| 162 | bool |
| 163 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator==`(1196d6b4fe21bab6).== (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 164 | { |
| 165 | int i; |
| 166 | bool result; |
| 167 | |
| 168 | result = (segment_countcxx . . $ pvpgn/BigInt#segment_count. == right.segment_countcxx . . $ pvpgn/BigInt#segment_count.); |
| 169 | for (i = 0; (i < segment_countcxx . . $ pvpgn/BigInt#segment_count.) && result; i++) |
| 170 | result = result && (segmentcxx . . $ pvpgn/BigInt#segment.[i] == right.segmentcxx . . $ pvpgn/BigInt#segment.[i]); |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | bool |
| 176 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator<`(1196d6b4fe21bab6).< (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 177 | { |
| 178 | if (segment_countcxx . . $ pvpgn/BigInt#segment_count. < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) { |
| 179 | return true; |
| 180 | } |
| 181 | else if (segment_countcxx . . $ pvpgn/BigInt#segment_count. > right.segment_countcxx . . $ pvpgn/BigInt#segment_count.){ |
| 182 | return false; |
| 183 | } |
| 184 | else { |
| 185 | int i; |
| 186 | for (i = segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; i >= 0; i--) { |
| 187 | if (segmentcxx . . $ pvpgn/BigInt#segment.[i]<right.segmentcxx . . $ pvpgn/BigInt#segment.[i]) |
| 188 | return true; |
| 189 | else if (segmentcxx . . $ pvpgn/BigInt#segment.[i]>right.segmentcxx . . $ pvpgn/BigInt#segment.[i]) |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | bool |
| 197 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator>`(1196d6b4fe21bab6).> (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 198 | { |
| 199 | if (segment_countcxx . . $ pvpgn/BigInt#segment_count. > right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) { |
| 200 | return true; |
| 201 | } |
| 202 | else if (segment_countcxx . . $ pvpgn/BigInt#segment_count. < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.){ |
| 203 | return false; |
| 204 | } |
| 205 | else { |
| 206 | int i; |
| 207 | for (i = segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; i >= 0; i--) { |
| 208 | if (segmentcxx . . $ pvpgn/BigInt#segment.[i]>right.segmentcxx . . $ pvpgn/BigInt#segment.[i]) |
| 209 | return true; |
| 210 | else if (segmentcxx . . $ pvpgn/BigInt#segment.[i] < right.segmentcxx . . $ pvpgn/BigInt#segment.[i]) |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | BigIntcxx . . $ pvpgn/BigInt# |
| 219 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#operator+(aad5a6d0d695e555).+ (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 220 | { |
| 221 | int i, max_segment_count; |
| 222 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# sum; |
| 223 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# lhs, rhs; |
| 224 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# carry = 0; |
| 225 | BigIntcxx . . $ pvpgn/BigInt# resultcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 226 | |
| 227 | max_segment_count = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(segment_countcxx . . $ pvpgn/BigInt#segment_count., right.segment_countcxx . . $ pvpgn/BigInt#segment_count.); |
| 228 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = max_segment_count + 1; |
| 229 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 230 | |
| 231 | for (i = 0; i < max_segment_count; i++) |
| 232 | { |
| 233 | lhs = (i < segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? segmentcxx . . $ pvpgn/BigInt#segment.[i] : 0; |
| 234 | rhs = (i<right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? right.segmentcxx . . $ pvpgn/BigInt#segment.[i] : 0; |
| 235 | sum = lhs + rhs + carry; |
| 236 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = sum & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 237 | carry = sum >> bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 238 | } |
| 239 | |
| 240 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = carry; |
| 241 | |
| 242 | for (max_segment_count = result.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; max_segment_count>0; max_segment_count--) { |
| 243 | if (result.segmentcxx . . $ pvpgn/BigInt#segment.[max_segment_count] != 0) |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | if (result.segment_countcxx . . $ pvpgn/BigInt#segment_count. != max_segment_count + 1) |
| 248 | { |
| 249 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = max_segment_count + 1; |
| 250 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 251 | } |
| 252 | |
| 253 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 254 | } |
| 255 | |
| 256 | BigIntcxx . . $ pvpgn/BigInt# |
| 257 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555).- (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 258 | { |
| 259 | int i, max_segment_count; |
| 260 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# diff; |
| 261 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# lhs, rhs; |
| 262 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# carry = 0; |
| 263 | BigIntcxx . . $ pvpgn/BigInt# resultcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 264 | |
| 265 | // Currently we only implement unsigned math |
| 266 | // so we can either throw exception or simply return 0. |
| 267 | if (!(this->operator>(right))) |
| 268 | { |
| 269 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 270 | } |
| 271 | |
| 272 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = segment_countcxx . . $ pvpgn/BigInt#segment_count.; |
| 273 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 274 | for (i = 0; i < result.segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++) |
| 275 | { |
| 276 | lhs = (i < segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? segmentcxx . . $ pvpgn/BigInt#segment.[i] : 0; |
| 277 | rhs = (i < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? right.segmentcxx . . $ pvpgn/BigInt#segment.[i] : 0; |
| 278 | rhs += carry; |
| 279 | if (lhs < rhs) |
| 280 | { |
| 281 | diff = (bigint_extended_carrycxx . . $ `src/common/bigint.h:60:9`! + lhs) - rhs; |
| 282 | carry = 1; |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | diff = (lhs)-rhs; |
| 287 | carry = 0; |
| 288 | } |
| 289 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = diff; |
| 290 | } |
| 291 | |
| 292 | for (max_segment_count = result.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; max_segment_count > 0; max_segment_count--) { |
| 293 | if (result.segmentcxx . . $ pvpgn/BigInt#segment.[max_segment_count] != 0) |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | if (result.segment_countcxx . . $ pvpgn/BigInt#segment_count. != max_segment_count + 1) |
| 298 | { |
| 299 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = max_segment_count + 1; |
| 300 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 301 | } |
| 302 | |
| 303 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 304 | } |
| 305 | |
| 306 | BigIntcxx . . $ pvpgn/BigInt# |
| 307 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555).* (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 308 | { |
| 309 | int i, j, index, max_segment_count; |
| 310 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# prod, sum; |
| 311 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# lhs, rhs; |
| 312 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# carry = 0; |
| 313 | BigIntcxx . . $ pvpgn/BigInt# resultcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 314 | |
| 315 | if ((segment_countcxx . . $ pvpgn/BigInt#segment_count. == 1 && segmentcxx . . $ pvpgn/BigInt#segment.[0] == 0) || (right.segment_countcxx . . $ pvpgn/BigInt#segment_count. == 1 && right.segmentcxx . . $ pvpgn/BigInt#segment.[0] == 0)) |
| 316 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 317 | |
| 318 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = segment_countcxx . . $ pvpgn/BigInt#segment_count. + right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; |
| 319 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 320 | stdcxx . . $ std/::memset(result.segmentcxx . . $ pvpgn/BigInt#segment., 0, result.segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 321 | for (i = 0; i < segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++) |
| 322 | { |
| 323 | for (j = 0; j < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; j++) |
| 324 | { |
| 325 | lhs = (i < segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? segmentcxx . . $ pvpgn/BigInt#segment.[i] : 0; |
| 326 | rhs = (j < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) ? right.segmentcxx . . $ pvpgn/BigInt#segment.[j] : 0; |
| 327 | prod = lhs * rhs; |
| 328 | index = i + j; |
| 329 | sum = result.segmentcxx . . $ pvpgn/BigInt#segment.[index] + prod + carry; |
| 330 | result.segmentcxx . . $ pvpgn/BigInt#segment.[index] = sum & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 331 | carry = sum >> bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 332 | } |
| 333 | |
| 334 | if (carry) { |
| 335 | index = i + j; |
| 336 | sum = result.segmentcxx . . $ pvpgn/BigInt#segment.[index] + carry; |
| 337 | result.segmentcxx . . $ pvpgn/BigInt#segment.[index] = sum & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 338 | carry = sum >> bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i + j - 1] += carry; |
| 343 | |
| 344 | for (max_segment_count = result.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; max_segment_count > 0; max_segment_count--) { |
| 345 | if (result.segmentcxx . . $ pvpgn/BigInt#segment.[max_segment_count] != 0) |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | if (result.segment_countcxx . . $ pvpgn/BigInt#segment_count. != max_segment_count + 1) |
| 350 | { |
| 351 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = max_segment_count + 1; |
| 352 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 353 | } |
| 354 | |
| 355 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 356 | } |
| 357 | |
| 358 | BigIntcxx . . $ pvpgn/BigInt# |
| 359 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator/`(aad5a6d0d695e555)./ (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 360 | { |
| 361 | int i, j, max_segment_count; |
| 362 | BigIntcxx . . $ pvpgn/BigInt# quotientcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 363 | BigIntcxx . . $ pvpgn/BigInt# remaindercxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 364 | BigIntcxx . . $ pvpgn/BigInt# mcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 365 | BigIntcxx . . $ pvpgn/BigInt# qcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 366 | BigIntcxx . . $ pvpgn/BigInt# pcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 367 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# n, d, qest; |
| 368 | |
| 369 | |
| 370 | if (this->operator<(right)){ |
| 371 | return quotientcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 372 | } |
| 373 | |
| 374 | quotient.segment_countcxx . . $ pvpgn/BigInt#segment_count. = (segment_countcxx . . $ pvpgn/BigInt#segment_count. - right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) + 1; |
| 375 | quotient.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(quotient.segment, quotient.segment_count*sizeof(bigint_base)); |
| 376 | stdcxx . . $ std/::memset(quotient.segmentcxx . . $ pvpgn/BigInt#segment., 0, quotient.segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 377 | |
| 378 | remainder.segment_countcxx . . $ pvpgn/BigInt#segment_count. = right.segment_countcxx . . $ pvpgn/BigInt#segment_count. + 1; |
| 379 | remainder.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(remainder.segment, remainder.segment_count*sizeof(bigint_base)); |
| 380 | stdcxx . . $ std/::memset(remainder.segmentcxx . . $ pvpgn/BigInt#segment., 0, remainder.segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 381 | |
| 382 | for (j = 0; j < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; j++){ |
| 383 | remainder.segmentcxx . . $ pvpgn/BigInt#segment.[j] = segmentcxx . . $ pvpgn/BigInt#segment.[(segment_countcxx . . $ pvpgn/BigInt#segment_count. - right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) + j]; |
| 384 | } |
| 385 | |
| 386 | n = 0; |
| 387 | for (i = segment_countcxx . . $ pvpgn/BigInt#segment_count.; i >= right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; i--){ |
| 388 | //now do some "educated guessing" |
| 389 | //qest=n/q |
| 390 | //calculate qest*right |
| 391 | // adjust by +/-1 until in target range |
| 392 | n = remainder.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count.]; |
| 393 | n <<= bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 394 | n += remainder.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1]; |
| 395 | d = right.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1]; |
| 396 | |
| 397 | qest = n / d; |
| 398 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). BigIntcxx . . $ pvpgn/BigInt#(qest) *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). right; |
| 399 | while (1) { |
| 400 | if (p ==cxx . . $ pvpgn/BigInt#`operator==`(1196d6b4fe21bab6). remainder) { |
| 401 | break; |
| 402 | } |
| 403 | else if (p <cxx . . $ pvpgn/BigInt#`operator<`(1196d6b4fe21bab6). remainder) { |
| 404 | if ((remainder -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). p) >cxx . . $ pvpgn/BigInt#`operator>`(1196d6b4fe21bab6). right) { |
| 405 | qest++; |
| 406 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). p +cxx . . $ pvpgn/BigInt#operator+(aad5a6d0d695e555). right; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | else { |
| 414 | qest--; |
| 415 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). p -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). right; |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | quotient.segmentcxx . . $ pvpgn/BigInt#segment.[i - right.segment_countcxx . . $ pvpgn/BigInt#segment_count.] = qest & bigint_base_maskcxx . . $ `src/common/bigint.h:59:9`!; |
| 420 | remainder =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). remainder -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). p; |
| 421 | |
| 422 | if (i > right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) { |
| 423 | remainder =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). remainder <<cxx . . $ pvpgn/BigInt#`operator<<`(5e64dd19ad388b84). sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 424 | remainder.segmentcxx . . $ pvpgn/BigInt#segment.[0] = segmentcxx . . $ pvpgn/BigInt#segment.[i - (right.segment_countcxx . . $ pvpgn/BigInt#segment_count. + 1)]; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | for (max_segment_count = quotient.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; max_segment_count > 0; max_segment_count--) { |
| 429 | if (quotient.segmentcxx . . $ pvpgn/BigInt#segment.[max_segment_count] != 0) |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | if (quotient.segment_countcxx . . $ pvpgn/BigInt#segment_count. != max_segment_count + 1) |
| 434 | { |
| 435 | quotient.segment_countcxx . . $ pvpgn/BigInt#segment_count. = max_segment_count + 1; |
| 436 | quotient.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(quotient.segment, quotient.segment_count * sizeof(bigint_base)); |
| 437 | } |
| 438 | |
| 439 | return quotientcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 440 | } |
| 441 | |
| 442 | BigIntcxx . . $ pvpgn/BigInt# |
| 443 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator%`(aad5a6d0d695e555).% (const BigIntcxx . . $ pvpgn/BigInt#& right) const |
| 444 | { |
| 445 | int i, j; |
| 446 | BigIntcxx . . $ pvpgn/BigInt# remaindercxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 447 | BigIntcxx . . $ pvpgn/BigInt# mcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 448 | BigIntcxx . . $ pvpgn/BigInt# qcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 449 | BigIntcxx . . $ pvpgn/BigInt# pcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 450 | bigint_extendedcxx . . $ pvpgn/BigInt#bigint_extended# n, d, qest; |
| 451 | |
| 452 | |
| 453 | if (this->operator<(right)){ |
| 454 | return *cxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).this; |
| 455 | } |
| 456 | |
| 457 | remainder.segment_countcxx . . $ pvpgn/BigInt#segment_count. = right.segment_countcxx . . $ pvpgn/BigInt#segment_count. + 1; |
| 458 | remainder.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(remainder.segment, remainder.segment_count*sizeof(bigint_base)); |
| 459 | stdcxx . . $ std/::memset(remainder.segmentcxx . . $ pvpgn/BigInt#segment., 0, remainder.segment_countcxx . . $ pvpgn/BigInt#segment_count. * sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)); |
| 460 | |
| 461 | for (j = 0; j < right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; j++){ |
| 462 | remainder.segmentcxx . . $ pvpgn/BigInt#segment.[j] = segmentcxx . . $ pvpgn/BigInt#segment.[(segment_countcxx . . $ pvpgn/BigInt#segment_count. - right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) + j]; |
| 463 | } |
| 464 | |
| 465 | n = 0; |
| 466 | for (i = segment_countcxx . . $ pvpgn/BigInt#segment_count.; i >= right.segment_countcxx . . $ pvpgn/BigInt#segment_count.; i--){ |
| 467 | //now do some "educated guessing" |
| 468 | //qest=n/q |
| 469 | //calculate qest*right |
| 470 | // adjust by +/-1 until in target range |
| 471 | n = remainder.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count.]; |
| 472 | n <<= bigint_base_bitcountcxx . . $ `src/common/bigint.h:61:9`!; |
| 473 | n += remainder.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1]; |
| 474 | d = right.segmentcxx . . $ pvpgn/BigInt#segment.[right.segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1]; |
| 475 | |
| 476 | qest = n / d; |
| 477 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). BigIntcxx . . $ pvpgn/BigInt#(qest) *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). right; |
| 478 | while (1) { |
| 479 | if (p ==cxx . . $ pvpgn/BigInt#`operator==`(1196d6b4fe21bab6). remainder) { |
| 480 | break; |
| 481 | } |
| 482 | else if (p <cxx . . $ pvpgn/BigInt#`operator<`(1196d6b4fe21bab6). remainder) { |
| 483 | if ((remainder -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). p) >cxx . . $ pvpgn/BigInt#`operator>`(1196d6b4fe21bab6). right) { |
| 484 | qest++; |
| 485 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). p +cxx . . $ pvpgn/BigInt#operator+(aad5a6d0d695e555). right; |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | else { |
| 493 | qest--; |
| 494 | p =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). p -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). right; |
| 495 | } |
| 496 | }; |
| 497 | |
| 498 | |
| 499 | remainder =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). remainder -cxx . . $ pvpgn/BigInt#operator-(aad5a6d0d695e555). p; |
| 500 | |
| 501 | if (i > right.segment_countcxx . . $ pvpgn/BigInt#segment_count.) { |
| 502 | remainder =cxx . . $ pvpgn/BigInt#`operator=`(edfbb244da9a4625). remainder <<cxx . . $ pvpgn/BigInt#`operator<<`(5e64dd19ad388b84). sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 503 | remainder.segmentcxx . . $ pvpgn/BigInt#segment.[0] = segmentcxx . . $ pvpgn/BigInt#segment.[i - (right.segment_countcxx . . $ pvpgn/BigInt#segment_count. + 1)]; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return remaindercxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 508 | } |
| 509 | |
| 510 | BigIntcxx . . $ pvpgn/BigInt# |
| 511 | BigIntcxx . . $ pvpgn/BigInt#::operatorcxx . . $ pvpgn/BigInt#`operator<<`(5e64dd19ad388b84).<< (int bytesToShift) const { |
| 512 | int i; |
| 513 | BigIntcxx . . $ pvpgn/BigInt# resultcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 514 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(bytesToShift >= 0); |
| 515 | //currently we only implement (and need) segmentwise shifting |
| 516 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(bytesToShift%sizeof(bigint_base) == 0); |
| 517 | |
| 518 | if (bytesToShift == 0) |
| 519 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 520 | |
| 521 | int segmentsToShift = bytesToShift / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 522 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = segment_countcxx . . $ pvpgn/BigInt#segment_count. + segmentsToShift; |
| 523 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 524 | |
| 525 | for (i = segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1; i >= 0; i--) { |
| 526 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i + segmentsToShift] = segmentcxx . . $ pvpgn/BigInt#segment.[i]; |
| 527 | } |
| 528 | |
| 529 | for (i = 0; i < segmentsToShift; i++) { |
| 530 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = 0; |
| 531 | } |
| 532 | |
| 533 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 534 | } |
| 535 | |
| 536 | BigIntcxx . . $ pvpgn/BigInt# |
| 537 | BigIntcxx . . $ pvpgn/BigInt#::randomcxx . . $ pvpgn/BigInt#random(1a9faf48955cd7cf).(int size) |
| 538 | { |
| 539 | BigIntcxx . . $ pvpgn/BigInt# resultcxx . . $ pvpgn/BigInt#BigInt(49f6e7a06ebc5aa8).; |
| 540 | int i, j; |
| 541 | unsigned int r; |
| 542 | |
| 543 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(size > 0); |
| 544 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(size%sizeof(bigint_base) == 0); |
| 545 | |
| 546 | result.segment_countcxx . . $ pvpgn/BigInt#segment_count. = size / sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); |
| 547 | result.segmentcxx . . $ pvpgn/BigInt#segment. = (bigint_basecxx . . $ pvpgn/BigInt#bigint_base#*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result.segment, result.segment_count * sizeof(bigint_base)); |
| 548 | |
| 549 | for (i = 0; i < result.segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++){ |
| 550 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = 0; |
| 551 | for (j = 0; j < sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); j += sizeof(unsigned int)){ |
| 552 | r = rand(); |
| 553 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] <<= (sizeof(unsigned int)); |
| 554 | result.segmentcxx . . $ pvpgn/BigInt#segment.[i] = result.segmentcxx . . $ pvpgn/BigInt#segment.[i] + r; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return resultcxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).; |
| 559 | } |
| 560 | |
| 561 | BigIntcxx . . $ pvpgn/BigInt# |
| 562 | BigIntcxx . . $ pvpgn/BigInt#::powmcxx . . $ pvpgn/BigInt#powm(b174fc4de7f7a52e).(const BigIntcxx . . $ pvpgn/BigInt#& exp, const BigIntcxx . . $ pvpgn/BigInt#& mod) const |
| 563 | { |
| 564 | if (exp.segment_countcxx . . $ pvpgn/BigInt#segment_count. == 1) |
| 565 | { |
| 566 | if (exp.segmentcxx . . $ pvpgn/BigInt#segment.[0] == 0x02) |
| 567 | { |
| 568 | return (*this *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). *this) %cxx . . $ pvpgn/BigInt#`operator%`(aad5a6d0d695e555). mod; |
| 569 | } |
| 570 | else if (exp.segmentcxx . . $ pvpgn/BigInt#segment.[0] == 0x01) |
| 571 | { |
| 572 | return *cxx . . $ pvpgn/BigInt#BigInt(b36b8e5b36868922).this; |
| 573 | } |
| 574 | else if (exp.segmentcxx . . $ pvpgn/BigInt#segment.[0] == 0x00) |
| 575 | { |
| 576 | return BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t#)0x01); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | //trying a divide&conquer approach |
| 581 | if (exp.segmentcxx . . $ pvpgn/BigInt#segment.[0] % 2 == 0) |
| 582 | { |
| 583 | // exp is even |
| 584 | BigIntcxx . . $ pvpgn/BigInt# half = exp /cxx . . $ pvpgn/BigInt#`operator/`(aad5a6d0d695e555). (BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t#)0x02)); |
| 585 | BigIntcxx . . $ pvpgn/BigInt# halfpow = this->powmcxx . . $ pvpgn/BigInt#powm(b174fc4de7f7a52e).(half, mod); |
| 586 | return (halfpow *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). halfpow) %cxx . . $ pvpgn/BigInt#`operator%`(aad5a6d0d695e555). mod; |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | // exp is odd |
| 591 | BigIntcxx . . $ pvpgn/BigInt# half = exp /cxx . . $ pvpgn/BigInt#`operator/`(aad5a6d0d695e555). (BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t#)0x02)); |
| 592 | BigIntcxx . . $ pvpgn/BigInt# halfpow = this->powmcxx . . $ pvpgn/BigInt#powm(b174fc4de7f7a52e).(half, mod); |
| 593 | return (((halfpow *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). halfpow) %cxx . . $ pvpgn/BigInt#`operator%`(aad5a6d0d695e555). mod) *cxx . . $ pvpgn/BigInt#`operator*`(aad5a6d0d695e555). *this) %cxx . . $ pvpgn/BigInt#`operator%`(aad5a6d0d695e555). mod; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | unsigned char* |
| 598 | BigIntcxx . . $ pvpgn/BigInt#::getDatacxx . . $ pvpgn/BigInt#getData(7fde0967c2cfd879).(int byteCount, int blockSize, bool bigEndian) const |
| 599 | { |
| 600 | unsigned char* result; |
| 601 | |
| 602 | result = (unsigned char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(byteCount); |
| 603 | getDatacxx . . $ pvpgn/BigInt#getData(e8a9d04a1645988e).(result, byteCount, blockSize, bigEndian); |
| 604 | |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | void |
| 609 | BigIntcxx . . $ pvpgn/BigInt#::getDatacxx . . $ pvpgn/BigInt#getData(e8a9d04a1645988e).(unsigned char* out, int byteCount, int blockSize, bool bigEndian) const |
| 610 | { |
| 611 | int i, j; |
| 612 | unsigned char* pos; |
| 613 | |
| 614 | stdcxx . . $ std/::memset(out, 0, byteCount); |
| 615 | pos = out + (byteCount - 1); |
| 616 | |
| 617 | for (i = 0; i < segment_countcxx . . $ pvpgn/BigInt#segment_count.; i++) |
| 618 | { |
| 619 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base# data; |
| 620 | data = segmentcxx . . $ pvpgn/BigInt#segment.[segment_countcxx . . $ pvpgn/BigInt#segment_count. - (i + 1)]; |
| 621 | for (j = 0; j < sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#); j++) |
| 622 | { |
| 623 | if (pos < out) |
| 624 | break; |
| 625 | *pos = data & 0xff; |
| 626 | |
| 627 | pos--; |
| 628 | data >>= 8; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (!bigEndian && blockSize>1) { |
| 633 | unsigned char val; |
| 634 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(blockSize % 2 == 0); |
| 635 | for (i = 0; i < byteCount; i += blockSize) |
| 636 | { |
| 637 | for (j = 0; j < blockSize / 2; j++) |
| 638 | { |
| 639 | val = out[i + j]; |
| 640 | out[i + j] = out[i + blockSize - (j + 1)]; |
| 641 | out[i + blockSize - (j + 1)] = val; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | stdcxx . . $ std/::stringcxx . . $ std/string# |
| 648 | BigIntcxx . . $ pvpgn/BigInt#::toHexStringcxx . . $ pvpgn/BigInt#toHexString(219584694fe2b248).() const |
| 649 | { |
| 650 | int i; |
| 651 | char data; |
| 652 | stdcxx . . $ std/::ostringstreamcxx . . $ std/ostringstream# resultcxx . . $ std/__cxx11/basic_ostringstream#basic_ostringstream(49f6e7a06ebc5aa8).; |
| 653 | bigint_basecxx . . $ pvpgn/BigInt#bigint_base#* src; |
| 654 | |
| 655 | // handle most significant segment |
| 656 | src = &segmentcxx . . $ pvpgn/BigInt#segment.[segment_countcxx . . $ pvpgn/BigInt#segment_count. - 1]; |
| 657 | if (segment_countcxx . . $ pvpgn/BigInt#segment_count. == 1 && *src == 0) |
| 658 | { |
| 659 | result <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "00"; |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | int sum = 0; |
| 664 | for (i = sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)-1; i >= 0; i--) |
| 665 | { |
| 666 | data = ((*src) & (0xff << (i * 8))) >> (i * 8); |
| 667 | sum += data; |
| 668 | if (sum) |
| 669 | { |
| 670 | result <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). small_digitscxx . . $ pvpgn/small_digits.[(data & 0xf0) >> 4]; |
| 671 | result <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). small_digitscxx . . $ pvpgn/small_digits.[(data & 0x0f)]; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | // handle rest |
| 676 | for (src = &segmentcxx . . $ pvpgn/BigInt#segment.[segment_countcxx . . $ pvpgn/BigInt#segment_count. - 2]; src >= segmentcxx . . $ pvpgn/BigInt#segment.; src--) |
| 677 | { |
| 678 | for (i = sizeof(bigint_basecxx . . $ pvpgn/BigInt#bigint_base#)-1; i >= 0; i--) |
| 679 | { |
| 680 | data = ((*src) & (0xff << (i * 8))) >> (i * 8); |
| 681 | result <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). small_digitscxx . . $ pvpgn/small_digits.[(data & 0xf0) >> 4]; |
| 682 | result <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). small_digitscxx . . $ pvpgn/small_digits.[(data & 0x0f)]; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return result.strcxx . . $ std/__cxx11/basic_ostringstream#str(d33e1a6fd36255f7).(); |
| 688 | |
| 689 | } |
| 690 | |
| 691 | } |