Can anybody help me with leading zero of big integer?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Can anybody help me with leading zero of big integer?

zhouziyan
Hi,


I'm printing out the big number X of ECPoint with following code:

ECPoint point;

System.out.println("X:" + point.getXCoord().toBigInteger().toByteArray());


I got 33 bytes with a leading zero byte sometimes, like

X: 0 212 239 35 202 205 79 ...

What does the first byte 0 mean? Anybody could help me?


Many Thanks,

Ziyan


Reply | Threaded
Open this post in threaded view
|

Re: Can anybody help me with leading zero of big integer?

Matti Aarnio
Top bit of the leading byte is sign.
If the byte value of the first byte has the top bit set and the whole value is not negative, then it is encoded to a byte array with leading zero byte.


On 05/11/2017 06:41 PM, zhouziyan wrote:

> Hi,
>
>
> I'm printing out the big number X of ECPoint with following code:
>
> ECPoint point;
>
> System.out.println("X:" + point.getXCoord().toBigInteger().toByteArray());
>
>
> I got 33 bytes with a leading zero byte sometimes, like
>
> X: 0 212 239 35 202 205 79 ...
>
> What does the first byte 0 mean? Anybody could help me?
>
>
> Many Thanks,
>
> Ziyan
>
>


smime.p7s (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Can anybody help me with leading zero of big integer?

zhouziyan
Hi Matti,

On 2017/5/12 01:51, Matti Aarnio wrote:
> Top bit of the leading byte is sign.
> If the byte value of the first byte has the top bit set and the whole value is not negative, then it is encoded to a byte array with leading zero byte.
Very helpful to me. And if the BigInteger X of ECPoint could be negative
in bouncycastle? or always be positive?

Thanks,
Ziyan

>
>
> On 05/11/2017 06:41 PM, zhouziyan wrote:
>> Hi,
>>
>>
>> I'm printing out the big number X of ECPoint with following code:
>>
>> ECPoint point;
>>
>> System.out.println("X:" + point.getXCoord().toBigInteger().toByteArray());
>>
>>
>> I got 33 bytes with a leading zero byte sometimes, like
>>
>> X: 0 212 239 35 202 205 79 ...
>>
>> What does the first byte 0 mean? Anybody could help me?
>>
>>
>> Many Thanks,
>>
>> Ziyan
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Can anybody help me with leading zero of big integer?

Peter Dettman-3
On 12/05/2017 8:11 AM, zhouziyan wrote:
> Hi Matti,
>
> On 2017/5/12 01:51, Matti Aarnio wrote:
>> Top bit of the leading byte is sign.
>> If the byte value of the first byte has the top bit set and the whole
>> value is not negative, then it is encoded to a byte array with leading
>> zero byte.
> Very helpful to me. And if the BigInteger X of ECPoint could be negative
> in bouncycastle? or always be positive?

Point coordinates are always non-negative.

Note that point.getXCoord().getEncoded() probably already does what you
are trying to do.

Also ECPoint.getEncoded(boolean) for the SEC standard point encoding(s).

You might have to convert the point using point.normalize() if you are
trying to get the affine X coordinate, since points are usually in some
projective coordinate system.

Regards,
Pete Dettman


Reply | Threaded
Open this post in threaded view
|

Re: Can anybody help me with leading zero of big integer?

zhouziyan


On 2017/5/12 16:27, Peter Dettman wrote:

> On 12/05/2017 8:11 AM, zhouziyan wrote:
>> Hi Matti,
>>
>> On 2017/5/12 01:51, Matti Aarnio wrote:
>>> Top bit of the leading byte is sign.
>>> If the byte value of the first byte has the top bit set and the whole
>>> value is not negative, then it is encoded to a byte array with leading
>>> zero byte.
>> Very helpful to me. And if the BigInteger X of ECPoint could be negative
>> in bouncycastle? or always be positive?
> Point coordinates are always non-negative.
>
> Note that point.getXCoord().getEncoded() probably already does what you
> are trying to do.
>
> Also ECPoint.getEncoded(boolean) for the SEC standard point encoding(s).
>
> You might have to convert the point using point.normalize() if you are
> trying to get the affine X coordinate, since points are usually in some
> projective coordinate system.

Got it, thanks so much!

Thanks,
Ziyan
> Regards,
> Pete Dettman
>