计算商而不跟踪余数的除法算法

时间:2019-06-03 07:28:07

标签: c algorithm cryptography division

作为一个业余项目,我正在尝试使用独立的C实现一些密码算法-也就是说,C的变体没有标准库函数(标准库类型和常量仍然可用),并且没有可选VLA(可变长度数组)等功能。

我要做的事情之一是为大整数(大小> 128位)实现一些功能。但是,此设置中的整数除法功能需要跟踪其当前形式的余数,并且由于我使用的是独立式环境,因此调用方必须为其提供空间。

是否可以在不依赖于余数的情况下并且可能使用位切片技术的情况下实现用于计算商的除法算法?使用调用递归将变量保留在堆栈上是可以接受的。

我们假定大整数的类型为bigint_t:

2019-06-02T04:27:51.2432150Z [command]C:\windows\system32\cmd.exe /D /S /C "C:\npm\prefix\npm.cmd run build:ssr"
2019-06-02T04:31:03.6579570Z FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
2019-06-02T04:31:03.6580213Z  1: 00007FF675FAF04A v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+5114
2019-06-02T04:31:03.6580271Z  2: 00007FF675F8A0C6 node::MakeCallback+4518
2019-06-02T04:31:03.6580376Z  3: 00007FF675F8AA30 node_module_register+2032
2019-06-02T04:31:03.6580421Z  4: 00007FF6762120EE v8::internal::FatalProcessOutOfMemory+846
2019-06-02T04:31:03.6580464Z  5: 00007FF67621201F v8::internal::FatalProcessOutOfMemory+639
2019-06-02T04:31:03.6580573Z  6: 00007FF676732BC4 v8::internal::Heap::MaxHeapGrowingFactor+9556
2019-06-02T04:31:03.6580676Z  7: 00007FF676729C46 v8::internal::ScavengeJob::operator=+24310
2019-06-02T04:31:03.6580721Z  8: 00007FF67672829C v8::internal::ScavengeJob::operator=+17740
2019-06-02T04:31:03.6580764Z  9: 00007FF676730F87 v8::internal::Heap::MaxHeapGrowingFactor+2327
2019-06-02T04:31:03.6580880Z 10: 00007FF676731006 v8::internal::Heap::MaxHeapGrowingFactor+2454
2019-06-02T04:31:03.6580923Z 11: 00007FF6762ECDB7 v8::internal::Factory::NewFillerObject+55
2019-06-02T04:31:03.6580965Z 12: 00007FF676382CC6 v8::internal::WasmJs::Install+29414
2019-06-02T04:31:03.6581005Z 13: 000002DF3095C5C1 
2019-06-02T04:31:03.6581101Z npm ERR! code ELIFECYCLE
2019-06-02T04:31:03.6581140Z npm ERR! errno 134
2019-06-02T04:31:03.6581185Z npm ERR! futek-ui@0.0.0 build:client-and-server-bundles: `ng build --prod && ng run FutekUI:server`
2019-06-02T04:31:03.6581285Z npm ERR! Exit status 134
2019-06-02T04:31:03.6581327Z npm ERR! 
2019-06-02T04:31:03.6581371Z npm ERR! Failed at the futek-ui@0.0.0 build:client-and-server-bundles script.
2019-06-02T04:31:03.6581417Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-06-02T04:31:03.6581502Z 
2019-06-02T04:31:03.6581545Z npm ERR! A complete log of this run can be found in:
2019-06-02T04:31:03.6582900Z npm ERR!     C:\npm\cache\_logs\2019-06-02T04_31_03_522Z-debug.log
2019-06-02T04:31:03.6582942Z npm ERR! code ELIFECYCLE
2019-06-02T04:31:03.6583053Z npm ERR! errno 134
2019-06-02T04:31:03.6583101Z npm ERR! futek-ui@0.0.0 build:ssr: `npm run build:client-and-server-bundles && npm run webpack:server`
2019-06-02T04:31:03.6583142Z npm ERR! Exit status 134
2019-06-02T04:31:03.6583233Z npm ERR! 
2019-06-02T04:31:03.6583275Z npm ERR! Failed at the futek-ui@0.0.0 build:ssr script.
2019-06-02T04:31:03.6583321Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-06-02T04:31:03.6583489Z 
2019-06-02T04:31:03.6583584Z npm ERR! A complete log of this run can be found in:
2019-06-02T04:31:03.6583613Z 
2019-06-02T04:31:03.6583938Z npm ERR!     C:\npm\cache\_logs\2019-06-02T04_31_03_584Z-debug.log
2019-06-02T04:31:03.6584033Z > futek-ui@0.0.0 build:ssr D:\a\1\s\FutekUI
2019-06-02T04:31:03.6584078Z > npm run build:client-and-server-bundles && npm run webpack:server
2019-06-02T04:31:03.6584115Z 
2019-06-02T04:31:03.6584140Z

2 个答案:

答案 0 :(得分:4)

我认为您担心其余部分会占用额外的空间,因为您无法调用malloc。

请注意,在正常的长除法样式实现期间,商随着分红缩小而变成余数时的长度增加。在每个阶段,商和余数加起来最多需要原始股息所需要的空间。

如果将股息剩余和商数保持在同一数组中,则多余的数字只需要几个变量即可。

答案 1 :(得分:3)

您可以使用二进制搜索。选择一个数字,然后将其乘以除数。如果结果太大,请减少数量;如果结果太小,请增加数量。控制增量/减量,使其趋向于指数为零。