BigInteger enhancing

Discussion in 'Java' started by Dmitriy Melnik, Feb 22, 2009.

  1. 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?
     
    Dmitriy Melnik, Feb 22, 2009
    #1
    1. Advertising

  2. Dmitriy Melnik

    Lew Guest

    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
     
    Lew, Feb 22, 2009
    #2
    1. Advertising

  3. On 22 ÆÅ×, 23:58, Lew <> 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.
     
    Dmitriy Melnik, Feb 22, 2009
    #3
  4. Dmitriy Melnik

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Feb 22, 2009
    #4
  5. On 23 ¬æ¬Ö¬Ó, 00:33, Arne Vajh©ªj <> 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.
     
    Dmitriy Melnik, Feb 22, 2009
    #5
  6. On 23 ¬æ¬Ö¬Ó, 00:33, Arne Vajh©ªj <> 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.
     
    Dmitriy Melnik, Feb 22, 2009
    #6
  7. Dmitriy Melnik wrote:
    > On 23 ¬æ¬Ö¬Ó, 00:33, Arne Vajh©ªj <> 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
     
    Arne Vajh©ªj, Feb 22, 2009
    #7
  8. On 23 фев, 00:46, Arne Vajhøj <> 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!
     
    Dmitriy Melnik, Feb 22, 2009
    #8
  9. Dmitriy Melnik

    blue indigo Guest

    On Sun, 22 Feb 2009 13:33:42 -0500, Arne Vajh¸j 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
     
    blue indigo, Feb 22, 2009
    #9
  10. Dmitriy Melnik

    Tom Anderson Guest

    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
     
    Tom Anderson, Feb 22, 2009
    #10
  11. Dmitriy Melnik

    Arne Vajh¸j Guest

    blue indigo wrote:
    > On Sun, 22 Feb 2009 13:33:42 -0500, Arne Vajh¸j 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.


    And even if there were consensus, then it would not help OP much - not
    today and considering that to my best knowledge the people active
    in JCP do not read this forum most likely not in the future either (*).

    Arne

    *) If they do then they are awfully quiet.
     
    Arne Vajh¸j, Feb 22, 2009
    #11
  12. On 23 ÆÅ×, 01:13, Tom Anderson <> wrote:
    >
    > 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.


    Thanks for GMP hint. I'm going to try it out.
     
    Dmitriy Melnik, Feb 22, 2009
    #12
  13. Dmitriy Melnik

    Lew Guest

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


    Arne Vajhøj wrote:
    > Java does not support extension methods.


    What do you mean by "extension methods"?

    --
    Lew
     
    Lew, Feb 22, 2009
    #13
  14. Lew wrote:
    > Dmitriy Melnik wrote:
    >>> Is there another way to transparently add several methods to
    >>> BigInteger?

    >
    > Arne Vajhøj wrote:
    >> Java does not support extension methods.

    >
    > What do you mean by "extension methods"?


    It is a C#/.NET feature where you can write
    a class Y that adds methods to class X.

    Arne
     
    Arne Vajhøj, Feb 22, 2009
    #14
  15. Arne Vajhøj wrote:
    > Lew wrote:
    >> Dmitriy Melnik wrote:
    >>>> Is there another way to transparently add several methods to
    >>>> BigInteger?

    >>
    >> Arne Vajhøj wrote:
    >>> Java does not support extension methods.

    >>
    >> What do you mean by "extension methods"?

    >
    > It is a C#/.NET feature where you can write
    > a class Y that adds methods to class X.


    The code below outputs 9.

    Arne

    ================================

    using System;

    namespace E
    {
    public class A
    {
    private int v;
    public int V { get { return v; } set { v = value; }}
    public A(int v)
    {
    this.v = v;
    }
    public void Add(int n)
    {
    v += n;
    }
    public override string ToString()
    {
    return v.ToString();
    }
    }
    public static class B
    {
    public static void Mul(this A o, int n)
    {
    o.V *= n;
    }
    }
    public class Program
    {
    public static void Main(string[] args)
    {
    A o = new A(2);
    o.Add(1);
    o.Mul(3);
    Console.WriteLine(o);
    Console.ReadKey();
    }
    }
    }
     
    Arne Vajhøj, Feb 22, 2009
    #15
  16. Dmitriy Melnik

    Lew Guest

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


    Thomas Pornin wrote:
    > You could always roll out your own class. Standard BigInteger happens
    > to be written in pure Java; source code is available through, for
    > instance, OpenJDK (and you can reuse it, provided that you comply
    > with the license, which is a kind of GPL-derivative). So you could
    > simple copy the BigInteger.java file, modify its package directive,
    > and alter it at will.


    If the OP is not willing to override BigInteger's methods with covariant
    methods in the subclass, it seems unlikely they'd go through the at least as
    large effort of replacing the class outright.

    If they were willing to go through that effort, they could also consider
    wrapping BigInteger in a class that delegates to appropriate methods.

    --
    Lew
     
    Lew, Feb 22, 2009
    #16
  17. Dmitriy Melnik

    Arne Vajhøj Guest

    Thomas Pornin wrote:
    > According to Dmitriy Melnik <>:
    >> Standard BigInteger class lacks several useful methods used in
    >> cryptography such as square rooting and square rooting modulo a prime.

    >
    > You could always roll out your own class. Standard BigInteger happens
    > to be written in pure Java; source code is available through, for
    > instance, OpenJDK (and you can reuse it, provided that you comply
    > with the license, which is a kind of GPL-derivative). So you could
    > simple copy the BigInteger.java file, modify its package directive,
    > and alter it at will.


    True.

    But I think it would be bad practice.

    Future changes to SUN BigInteger would need to be retrofitted
    into the forked code.

    Arne
     
    Arne Vajhøj, Feb 23, 2009
    #17
  18. On 23 ÆÅ×, 05:17, Lew <> wrote:
    >
    > If the OP is not willing to override BigInteger's methods with covariant
    > methods in the subclass, it seems unlikely they'd go through the at least as
    > large effort of replacing the class outright.
    >
    > If they were willing to go through that effort, they could also consider
    > wrapping BigInteger in a class that delegates to appropriate methods.


    I've already considered delegating. But my new BigInteger is to be
    used heavily. So invoking two methods instead of one could decrease
    the speed.
     
    Dmitriy Melnik, Feb 23, 2009
    #18
  19. Tom Anderson wrote:

    > 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.


    AFAIR GMP was also used with SUN's Java until it was replaced
    with a pure Java implementation with Java 1.2. So getting the
    sources of Java 1.1/1.0 should be possible as well if you
    want to reestablish the use of a C-Library instead of letting
    the Hotspot do its job.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Feb 23, 2009
    #19
  20. Dmitriy Melnik

    Lew Guest

    Dmitriy Melnik wrote:
    > I've already considered delegating. But my new BigInteger is to be
    > used heavily. So invoking two methods instead of one could decrease
    > the speed.


    By how much, exactly?

    What percentage of overall processing time would that be, exactly?

    --
    Lew
     
    Lew, Feb 23, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitriy Zakharov

    Enhancing ASP.NET Framework

    Dmitriy Zakharov, Sep 3, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    409
    Patrice
    Sep 6, 2004
  2. nick
    Replies:
    0
    Views:
    935
  3. nick
    Replies:
    1
    Views:
    32,075
    Eric Sosman
    Oct 26, 2004
  4. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    989
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  5. Replies:
    0
    Views:
    328
Loading...

Share This Page