0

I m learning C an I have ome issues.

First, i m using library gmp and I need to convert a mpzt to an unsigned char array.

I already lookup on documentation but I dont find anything. Maybe using mpz_get_str but I dont known how.

More over I have to convert 2 functions from GoLang to C but I have some trouble

Here is the two GoLang func:

    // paddedAppend appends the src byte slice to dst, returning the new slice.
// If the length of the source is smaller than the passed size, leading zero
// bytes are appended to the dst slice before appending src.
func paddedAppend(size uint, dst, src []byte) []byte {
  for i := 0; i < int(size)-len(src); i++ {
    dst = append(dst, 0)
  }
  return append(dst, src...)
}

func isOdd(a *big.Int) bool {
  return a.Bit(0) == 1
}

// SerializeUncompressed serializes a public key in a 65-byte uncompressed format.
func SerializeUncompressed(x *big.Int, y *big.Int) []byte {
  b := make([]byte, 0, PubKeyBytesLenUncompressed)
  b = append(b, pubkeyUncompressed)
  b = paddedAppend(32, b, x.Bytes())
  return paddedAppend(32, b, y.Bytes())
}

// SerializeCompressed serializes a public key in a 33-byte compressed format.
func SerializeCompressed(x *big.Int, y *big.Int) []byte {
  b := make([]byte, 0, PubKeyBytesLenCompressed)
  format := pubkeyCompressed
  if isOdd(y) {
    format |= 0x1
  }
  b = append(b, format)
  return paddedAppend(32, b, x.Bytes())
}

Every help will be appreciated ! Thanx in advance

jhd
  • 1
  • 1
    Are you converting to an ascii string, or an array of bytes? Those are very different things. If the latter you might want to look at mpz_import/mpz_export – Alcamtar Nov 27 '17 at 22:51
  • I m looking to convert an mpz to an unsigned char array (byte array equivalent in C) – jhd Nov 27 '17 at 22:56
  • Would browsing the page https://gmplib.org/manual/Integer-Import-and-Export.html#Integer-Import-and-Export help? Exporting seems to be what you want. – Rudy Velthuis Nov 28 '17 at 10:59
  • yes but i dont know how to export as unsigned char array :/ – jhd Nov 28 '17 at 11:49
  • Allocate the proper size and pass it as the `rop` argument. The function will fill the array with the requested data (be sure to pass the appropriate `order` and `endian` arguments, and perhaps `nails` too). – Rudy Velthuis Nov 28 '17 at 13:18

0 Answers0