Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > BigInteger enhancing

Reply
Thread Tools

BigInteger enhancing

 
 
Dmitriy Melnik
Guest
Posts: n/a
 
      02-22-2009
Hello again,

Standard BigInteger class lacks several useful methods used in
cryptography such as square rooting and square rooting modulo a prime.
Of course, it is possible to create static methods like this and use
them:

public class NewMethods {
public static BigInteger sqrt(BigInteger n) { ... }
public static BigInteger modSqrt(BigInteger n, BigInteger modulo)
{ ... }
}

But this solution does not look elegant.

How can I enhance BigInteger in the way that'll provide consistent
interface? I tried to write something like this:

public class BigIntegerExt extends BigInteger {
// constructors are omitted
public BigIntegerExt sqrt() { ... }
public BigIntegerExt modSqrt(BigInteger modulo) { ... }
}

But this still seems to be inconsistent. A lot of inherited methods
still return a BigInteger. So I end up with the mess of two classes
and constant casting. For example:

BigIntegerExt a = new BigIntegerExt("1");
BigIntegerExt b = new BigIntegerExt("2");
BigIntegerExt c = a.sqrt(); // Works
BigIntegerExt d = a.add(b); // Doesn't work; needs explicit casting
from BigInteger to BigIntegerExt which is unsafe

Is there another way to transparently add several methods to
BigInteger?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-22-2009
Dmitriy Melnik wrote:
> But this still seems to be inconsistent. A lot of inherited methods
> still return a BigInteger. So I end up with the mess of two classes
> and constant [?] casting.


Override those methods to return the subclass.

--
Lew
 
Reply With Quote
 
 
 
 
Dmitriy Melnik
Guest
Posts: n/a
 
      02-22-2009
On 22 , 23:58, Lew <(E-Mail Removed)> wrote:
> Dmitriy Melnik wrote:
> > But this still seems to be inconsistent. A lot of inherited methods
> > still return a BigInteger. So I end up with the mess of two classes
> > and constant [?] casting.

>
> Override those methods to return the subclass.


I thought about it but hoped there were an easier way. Thanks!

P.S. "Constant" means "all the time" My English's not good at times.
 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      02-22-2009
Dmitriy Melnik wrote:
> Standard BigInteger class lacks several useful methods used in
> cryptography such as square rooting and square rooting modulo a prime.
> Of course, it is possible to create static methods like this and use
> them:
>
> public class NewMethods {
> public static BigInteger sqrt(BigInteger n) { ... }
> public static BigInteger modSqrt(BigInteger n, BigInteger modulo)
> { ... }
> }
>
> But this solution does not look elegant.
>
> How can I enhance BigInteger in the way that'll provide consistent
> interface? I tried to write something like this:
>
> public class BigIntegerExt extends BigInteger {
> // constructors are omitted
> public BigIntegerExt sqrt() { ... }
> public BigIntegerExt modSqrt(BigInteger modulo) { ... }
> }
>
> But this still seems to be inconsistent. A lot of inherited methods
> still return a BigInteger. So I end up with the mess of two classes
> and constant casting. For example:
>
> BigIntegerExt a = new BigIntegerExt("1");
> BigIntegerExt b = new BigIntegerExt("2");
> BigIntegerExt c = a.sqrt(); // Works
> BigIntegerExt d = a.add(b); // Doesn't work; needs explicit casting
> from BigInteger to BigIntegerExt which is unsafe
>
> Is there another way to transparently add several methods to
> BigInteger?


Java does not support extension methods.

But do you really want it that way ?

I am assuming you are implementing something you have in Math.

You first construct combined with static import seems to much
better match Math syntax.

a.sqrt()
NewMethods.sqrt(a)
sqrt(a)

Arne


 
Reply With Quote
 
Dmitriy Melnik
Guest
Posts: n/a
 
      02-22-2009
On 23 ֬, 00:33, Arne Vajhj <(E-Mail Removed)> wrote:
> Dmitriy Melnik wrote:
> > Standard BigInteger class lacks several useful methods used in
> > cryptography such as square rooting and square rooting modulo a prime.
> > Of course, it is possible to create static methods like this and use
> > them:

>
> > public class NewMethods {
> > public static BigInteger sqrt(BigInteger n) { ... }
> > public static BigInteger modSqrt(BigInteger n, BigInteger modulo)
> > { ... }
> > }

>
> > But this solution does not look elegant.

>
> > How can I enhance BigInteger in the way that'll provide consistent
> > interface? I tried to write something like this:

>
> > public class BigIntegerExt extends BigInteger {
> > // constructors are omitted
> > public BigIntegerExt sqrt() { ... }
> > public BigIntegerExt modSqrt(BigInteger modulo) { ... }
> > }

>
> > But this still seems to be inconsistent. A lot of inherited methods
> > still return a BigInteger. So I end up with the mess of two classes
> > and constant casting. For example:

>
> > BigIntegerExt a = new BigIntegerExt("1");
> > BigIntegerExt b = new BigIntegerExt("2");
> > BigIntegerExt c = a.sqrt(); // Works
> > BigIntegerExt d = a.add(b); // Doesn't work; needs explicit casting
> > from BigInteger to BigIntegerExt which is unsafe

>
> > Is there another way to transparently add several methods to
> > BigInteger?

>
> Java does not support extension methods.
>
> But do you really want it that way ?
>
> I am assuming you are implementing something you have in Math.
>
> You first construct combined with static import seems to much
> better match Math syntax.
>
> a.sqrt()
> NewMethods.sqrt(a)
> sqrt(a)


I tried to mimic native BigInteger interface. But now I can see the
required amount of work is not worth the result.
 
Reply With Quote
 
Dmitriy Melnik
Guest
Posts: n/a
 
      02-22-2009
On 23 ֬, 00:33, Arne Vajhj <(E-Mail Removed)> wrote:
>
> Java does not support extension methods.
>
> But do you really want it that way ?
>
> I am assuming you are implementing something you have in Math.
>
> You first construct combined with static import seems to much
> better match Math syntax.
>
> a.sqrt()
> NewMethods.sqrt(a)
> sqrt(a)


I tried to mimic native BigInteger interface. But now I can see the
required amount of work is not worth the result.
 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      02-22-2009
Dmitriy Melnik wrote:
> On 23 ֬, 00:33, Arne Vajhj <(E-Mail Removed)> wrote:
>> Dmitriy Melnik wrote:
>>> Standard BigInteger class lacks several useful methods used in
>>> cryptography such as square rooting and square rooting modulo a prime.
>>> Of course, it is possible to create static methods like this and use
>>> them:
>>> public class NewMethods {
>>> public static BigInteger sqrt(BigInteger n) { ... }
>>> public static BigInteger modSqrt(BigInteger n, BigInteger modulo)
>>> { ... }
>>> }
>>> But this solution does not look elegant.
>>> How can I enhance BigInteger in the way that'll provide consistent
>>> interface? I tried to write something like this:
>>> public class BigIntegerExt extends BigInteger {
>>> // constructors are omitted
>>> public BigIntegerExt sqrt() { ... }
>>> public BigIntegerExt modSqrt(BigInteger modulo) { ... }
>>> }
>>> But this still seems to be inconsistent. A lot of inherited methods
>>> still return a BigInteger. So I end up with the mess of two classes
>>> and constant casting. For example:
>>> BigIntegerExt a = new BigIntegerExt("1");
>>> BigIntegerExt b = new BigIntegerExt("2");
>>> BigIntegerExt c = a.sqrt(); // Works
>>> BigIntegerExt d = a.add(b); // Doesn't work; needs explicit casting
>>> from BigInteger to BigIntegerExt which is unsafe
>>> Is there another way to transparently add several methods to
>>> BigInteger?

>> Java does not support extension methods.
>>
>> But do you really want it that way ?
>>
>> I am assuming you are implementing something you have in Math.
>>
>> You first construct combined with static import seems to much
>> better match Math syntax.
>>
>> a.sqrt()
>> NewMethods.sqrt(a)
>> sqrt(a)

>
> I tried to mimic native BigInteger interface. But now I can see the
> required amount of work is not worth the result.


That interface/syntax is not very convenient.

You will be better off with static import.

Arne
 
Reply With Quote
 
Dmitriy Melnik
Guest
Posts: n/a
 
      02-22-2009
On 23 фев, 00:46, Arne Vajhøj <(E-Mail Removed)> wrote:
> That interface/syntax is not very convenient.
>
> You will be better off with static import.


Now I understood it too. Besides it will save me some time. Thanks!
 
Reply With Quote
 
blue indigo
Guest
Posts: n/a
 
      02-22-2009
On Sun, 22 Feb 2009 13:33:42 -0500, Arne Vajhj wrote:

> Dmitriy Melnik wrote:
>> Is there another way to transparently add several methods to
>> BigInteger?

>
> Java does not support extension methods.


There was a big discussion about adding such support, a while back. I
didn't participate. No consensus emerged that I can recall.

--
blue indigo
UA Telecom since 1987
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      02-22-2009
On Sun, 22 Feb 2009, Dmitriy Melnik wrote:

> Standard BigInteger class lacks several useful methods used in
> cryptography such as square rooting and square rooting modulo a prime.


Another option could be to use a different bignum class that does have the
methods you need. I could have sworn that there was an IBM one that was
faster than Sun's, but i can't find it. The GMP package is very fast, and
has all sorts of methods useful for cryptography, but is written in C -
you'd have to write a wrapper using JNI, or else copy the java-GMP bridge
used in the Kaffe project.

tom

--
Crazy week so far, which at one point involved spewing down the inside
of my jeans! -- D
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enhancing reception of Edimax EW-7126 [?] Eugene F. Wireless Networking 7 08-05-2005 06:58 PM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 1 10-26-2004 02:45 PM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 0 10-26-2004 08:18 AM
Enhancing ASP.NET Framework Dmitriy Zakharov ASP .Net 9 09-06-2004 08:34 AM
Enhancing skies Nod Digital Photography 2 07-17-2003 10:28 PM



Advertisments