简体   繁体   English

有没有办法对 Rust 中的 BigInt 进行右位移位?

[英]Is there a way to do a right bit-shift on a BigInt in Rust?

I get this error when attempting to do >> or >>= on a BigInt:尝试对 BigInt 执行 >> 或 >>= 时出现此错误:

no implementation for `BigInt >> BigInt

using the num_bigint::BigInt library使用num_bigint::BigInt

Edit: More Context:编辑:更多上下文:

I am rewriting this program https://www.geeksforgeeks.org/how-to-generate-large-prime-numbers-for-rsa-algorithm/ from python/c++ into rust however I will focus on the python implementation as it is written to handle 1024 bit prime numbers which are extremely big.我正在重写这个程序https://www.geeksforgeeks.org/how-to-generate-large-prime-numbers-for-rsa-algorithm/从 python/c++ 到 rust 但是我将专注于 python 实现编写用于处理非常大的 1024 位素数。

In the code we run the Miller Rabin Primality test which includes shifting EC: (prime-candidate - 1) to the right by 1 if we find that EC % 2 == 0. As I mentioned in the python implementation EC can be an incredibly large integer.在代码中,我们运行 Miller Rabin 素数测试,其中包括如果我们发现 EC % 2 == 0,则将 EC: (prime-candidate - 1) 向右移动 1。正如我在 python 实现中提到的,EC 可以是一个令人难以置信的大 integer。

It would be convenient to be able to use the same operator in rust, if that is not possible can someone suggest an alternative?能够在 rust 中使用相同的运营商会很方便,如果那不可能,有人可以建议替代方案吗?

According to the documentation for the num-bigint crate, the BigInt struct does implement the Shr trait for the right-shift operator, just not when the shift amount is itself a BigInt .根据num-bigint crate 的文档BigInt结构确实为右移运算符实现了Shr特性,只是当移位量本身是一个BigInt时没有。 If you convert the shift amount to a standard integer type (eg i64 ) then it should work.如果您将偏移量转换为标准 integer 类型(例如i64 ),那么它应该可以工作。

It is unlikely you would ever want to shift by an amount greater than i64::MAX , but if you do need this, then the correct result is going to be zero (because no computer has 2^60 bytes of memory), so you can write a simple implementation which checks for that case.您不太可能想要移动大于i64::MAX的量,但如果您确实需要这个,那么正确的结果将是零(因为没有计算机有 2^60 字节的内存),所以您可以编写一个简单的实现来检查这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM