问题描述
我正在使用libgcrypt函数gcry_prime_check
来测试数字3
是否为质数.事实证明,根据我的功能3不是素数.我在做什么错了?
I am using a libgcrypt function gcry_prime_check
to test if the number 3
is a prime number. It turns out 3 is not a prime number according to my the function. What am i doing wrong?
这是我的代码
#include <gcrypt.h>
#include <stdio.h>
int main(void)
{
gcry_mpi_t cript_prime;
gcry_error_t err;
char buffer[8] = {0};
char number[8] = {0};
printf("%s\n", gcry_check_version ( NULL ) );
gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 );
cript_prime = gcry_mpi_new(16);
strcpy(number,"3");
gcry_mpi_scan(&cript_prime,GCRYMPI_FMT_USG,number,sizeof(number),NULL);
gcry_mpi_print(GCRYMPI_FMT_USG,buffer,sizeof(buffer),NULL,cript_prime);
printf("The number tested is: %s\n",buffer);
err = gcry_prime_check(cript_prime,4);
if(err)
{
printf("%s\n",gcry_strerror(err));
}
gcry_mpi_release(cript_prime);
return 0;
}
这是输出
1.4.4
The number tested is: 3
Number is not prime
此外,有关使用libgcrypt的良好教程的链接将是一个很大的收获.谢谢
Also, a link for a good tutorial on using libgcrypt would be a big bonus. Thanks
好的,我设法使用gcry_prime_generate
生成素数,并将该值复制到number
中.事实证明,它没有通过首要检查.但是,当您直接将prime生成的mpi输出传递给prime检查函数时,它就会通过!
Alright, I managed to generate a prime number using gcry_prime_generate
and copied the value into number
. Turns out it failed the prime check. But when you directly the pass the mpi output from prime generate to the prime check function ... it passes!!
推荐答案
这是一个错误.像大多数素数测试一样,它会先以较小的素数测试除数,然后再进行更昂贵的测试.不幸的是,如果您的素数是这些值之一,它被报告为可整除的(本身)-因此是复合的.
It's a bug. Like most prime tests, it tests for divisibility by small prime values before moving onto more expensive tests. Unfortunately, if your prime is one of these values, it is reported as being divisible (by itself) - and therefore composite.
由于libgcrypt的重点是密码应用程序,所以这么小的素数没有实用程序.虽然草率马虎,并且不需要花费很多时间进行纠正.
Since the focus of libgcrypt is cryptographic applications, such a small prime has no utility. It's sloppy though, and wouldn't take much to rectify.
这篇关于C Libgcrypt:无法使用libgcrypt检查数字是否为素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!