BigNumber (GMP)?

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

BigNumber (GMP)?

Postby urbanze » Tue Dec 19, 2017 11:25 pm

In the esp32 files, there are libraries about BIGNUMBER (GMP), such as MbedTLS which claims to support hardware acceleration up to 4096b.

I don't found any examples of using bignumb for esp32, does anyone know of an example with MbedTLS libraries or some other library?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: BigNumber (GMP)?

Postby ESP_Angus » Wed Dec 20, 2017 12:50 am

The mbedTLS big number library (mbedtls/bignum.h) is probably the best candidate.

I don't know of any generic examples using this code, but if you grep the mbedTLS library source you will find many examples of this API being used for RSA, EC ciphers, etc.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: BigNumber (GMP)?

Postby urbanze » Wed Dec 20, 2017 12:25 pm

I have not found any examples on the specific usage of BigNumber (mbed), however, in the bignumber.h file itself there is a help on the functions that allowed me to start.

Obviously something would have to go wrong, when I try to add the result of the accounts using bignum in a string, it returns me an internal library error (line 589 bignumber.c) What can I do? If someone understands the use of this library, you can pass an example or even add an example to the ESP-IDF :(

Note: error in "write_string", I tried several values in the last parameter of this function, some give "store prohibited" error or nothing happens. I would also like to know what to put in the last parameter.

Note2: I found this example, but it can't help me to understand better. (https://github.com/ARMmbed/mbedtls/blob ... mpi_demo.c)

Image (error and code): https://i.imgur.com/dpIYMY2.png

Image

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: BigNumber (GMP)?

Postby ESP_Angus » Wed Dec 20, 2017 11:32 pm

urbanze wrote: Note: error in "write_string", I tried several values in the last parameter of this function, some give "store prohibited" error or nothing happens. I would also like to know what to put in the last parameter.
The olen parameter (last parameter) can't be null. A hin that the StoreProhibted error is failing to write to a NULL pointer is that EXCVADDR (the address which lead to the exception) is 0x00000000. Try something like :

Code: Select all

int olen;
int r = mbedtls_write_string( ... , &olen);
printf("mbedtls_write_string result %d olen %d value %s\n", r, olen, gg);
and you should get a full picture of what's happening.

PS Please paste actual text into "code" tags (or on a site like pastebin.com) where possible, reading screenshots is quite fiddly.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: BigNumber (GMP)?

Postby urbanze » Mon Dec 25, 2017 4:08 pm

ESP_Angus wrote:
urbanze wrote: Note: error in "write_string", I tried several values in the last parameter of this function, some give "store prohibited" error or nothing happens. I would also like to know what to put in the last parameter.
The olen parameter (last parameter) can't be null. A hin that the StoreProhibted error is failing to write to a NULL pointer is that EXCVADDR (the address which lead to the exception) is 0x00000000. Try something like :

Code: Select all

int olen;
int r = mbedtls_write_string( ... , &olen);
printf("mbedtls_write_string result %d olen %d value %s\n", r, olen, gg);


and you should get a full picture of what's happening.

PS Please paste actual text into "code" tags (or on a site like pastebin.com) where possible, reading screenshots is quite fiddly.
The right instead of int olen is size_t olen, doing this worked properly.

Code:

Code: Select all

mbedtls_mpi x;
char bf[200];

extern "C" void app_main()
{
	initArduino();
	Serial.begin(115200);
	uint32_t a;
	size_t n = 0;

	mbedtls_mpi_init(&x);
	a = mbedtls_mpi_read_string(&x, 10, "18446744073709551616"); Serial.println(a);
	a = mbedtls_mpi_mul_int(&x, &x, 652);	Serial.println(a);
	

	a = mbedtls_mpi_write_string(&x, 16, bf, sizeof(bf)-1, &n);	Serial.println(a);
	
	for (int i = 0; i < 30; i++)
	{
		Serial.printf("%c", bf[i]);
	}
	
	esp_deep_sleep(300000000);
}

Note: the write_string only worked with base 16, when using base 10 it simply happens nothing, giving impression that the MCU has locked, will it be a bug?

I did the multiplication of 18446744073709551616 by 652 is it worked, see the result in the image: https://i.imgur.com/7fj81Gh.png
Image

Factorial 1-5010 test: https://i.imgur.com/8jdfHw0.png
Image
This really work :D

Who is online

Users browsing this forum: axellin, Bing [Bot], MicroController and 58 guests