Convert from big endian array to gmp bignum ?

Aaron Boxer boxerab at
Thu May 26 13:19:57 UTC 2016

I have the following code that I am converting from openssl to nettle and

      BN_bin2bn(xkey, key_size, &bn_xkey);
      BN_bin2bn(sha_buf, SHA1_DIGEST_SIZE, &bn_x_n);
      mpz_add(bn_xkey,bn_xkey, bn_one);            // xkey += 1
      mpz_add(bn_tmp, bn_xkey, bn_x_n);       // xkey += x
      mpz_mod(bn_xkey, bn_tmp, c_2powb);  // xkey = xkey mod (2^b)

      memset(xkey, 0, xkey_len);
      ui32_t bn_buf_len = BN_get_num_bytes(bn_xkey);
      ui32_t idx = ( bn_buf_len < key_size ) ? key_size - bn_buf_len : 0;
      BN_bn2bin(&bn_xkey, &xkey[idx]);

What is the best way of getting the number of bytes in a gmp bignum,
and also importing from and exporting to a big endian array of bytes ?

Thanks so much,

More information about the gmp-discuss mailing list