Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:04:02+00:00 2026-06-13T19:04:02+00:00

I’m trying to implement some Montgomery math code for an encryption assignment; I have

  • 0

I’m trying to implement some Montgomery math code for an encryption assignment; I have most of it working except for the step where I need to compare each individual bit in the BigInteger to One.

The math algorithm I’ve been using , from Step 4 is

for i = k - 1 down to 0 do
xBar = MonPro(xBar, xBar)
if e[i] = 1 then xBar = MonPro(MBar, xBar)

i is just an index, k is the number of bits in the BIgInteger, and b is the exponent. My attempt is:

for(int i = n.bitLength() - 1; i > 0 ; i--) {
    xBar = monPro(xBar, xBar, r, nPrime, n);
    if (n.testBit(i) == true)
         xBar = monPro(mBar, xBar, r, nPrime, n);
}
return monPro(xBar, BigINteger.ONE, r, nPrime, n); 

monPro is a helper function that is definitely working. Likewise, the steps omitted before that code snippet definitely work. So the main question is, how do I bitwise iterate through a BigInteger? Unless I’m wrong about e[i], see below.

I’ve done an enormous amount of reading on this over the last while and resources are scant with regards to actual implementations. Some of the math papers published on the subject are purely mystifying.

I’m also unsure about is the e[i] above. In the book, it’s e with a little i below it, the same way Log2 generally has the 2 written as a small number below it. Can anyone clarify?

I’ve tried casting the BigInteger to a base 2 string and making a char array out of and doing the compare to 1 on it. I also tried just BigInteger.toString(2) to make a base 2 string and cycle through that using charAt[i] == 1.

I’m certain that all the steps above the e[i] are correct because I’ve checked them with a lot of different values.

If I’m off the track on the E[i] aspect then could someone explain what it actually means? And if not could anyone point out any errors or slight direction?

This is a homework assignment so please don’t list any code beyond snippets.

Any direction or advice would be very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:04:05+00:00Added an answer on June 13, 2026 at 7:04 pm
    for(int i = n.bitLength() - 1; i > 0 ; i--) { ... }
    

    This will miss bit 0 (because when i==0 the loop terminates.)
    Try using >= instead of >:

    for(int i = n.bitLength() - 1; i >= 0 ; i--) { ... }
    

    Alternatively, if you are using Java 7 and you know that n is positive, then you can convert it to a BitSet and iterate through its ‘1’ bits:

    static void showBitsOf(BigInteger n) {
        if (n.compareTo(BigInteger.ZERO) < 0) {
            throw new IllegalArgumentException("n must not be negative");
        }
        BitSet bs = BitSet.valueOf(n.toByteArray());
        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
            System.out.println(i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.