private bool IsQuadResModProduct( uint Prime ) { // Euler's Criterion: Integer Exponent = new Integer(); Integer Result = new Integer(); Integer Modulus = new Integer(); Exponent.SetFromULong( Prime ); IntMath.SubtractULong( Exponent, 1 ); Exponent.ShiftRight( 1 ); // Divide by 2. Result.Copy( Product ); Modulus.SetFromULong( Prime ); IntMath.IntMathNew.ModularPower( Result, Exponent, Modulus, false ); if( Result.IsOne() ) return true; else return false; // Result should be Prime - 1. }
// Factor 2 is along the right side diagonal. internal void SetFactor2( Integer UseBigIntToSet ) { Integer BigIntToSet = new Integer(); BigIntToSet.Copy( UseBigIntToSet ); if( (BigIntToSet.GetD( 0 ) & 1 ) != 1 ) throw( new Exception( "Factor2 can't be even." )); uint ToSet = MultArray[0].OneLine[0]; ToSet = SetMult1( ToSet ); ToSet = SetMult2( ToSet ); ToSet = SetMult( ToSet ); ToSet = SetAccumOut( ToSet ); ToSet = ClearCarryOut( ToSet ); MultArray[0].OneLine[0] = ToSet; // Factor2 has to be odd so Mult2 is set to // 1 all the way across the top. SetMult2AtRow( 0 ); BigIntToSet.ShiftRight( 1 ); if( BigIntToSet.IsZero()) throw( new Exception( "Factor2 can't be 1." )); // 107 = 64 + 32 + 8 + 2 + 1 int Where = 1; for( int Row = 1; Row < MultArraySize; Row++ ) { ToSet = MultArray[Row].OneLine[0]; if( (BigIntToSet.GetD( 0 ) & 1 ) == 1 ) ToSet = SetMult2( ToSet ); else ToSet = ClearMult2( ToSet ); MultArray[Row].OneLine[0] = ToSet; Where = Row; BigIntToSet.ShiftRight( 1 ); if( BigIntToSet.IsZero()) break; } // At the last 1 bit. // ToSet = MultArray[Where].OneLine[0]; for( int Row = Where + 1; Row < MultArraySize; Row++ ) { ToSet = MultArray[Row].OneLine[0]; ToSet = ClearMult2( ToSet ); MultArray[Row].OneLine[0] = ToSet; } }
// Product is along the right side diagonal. internal void SetProduct( Integer UseBigIntToSet ) { Integer BigIntToSet = new Integer(); BigIntToSet.Copy( UseBigIntToSet ); // Primes: 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, // 71, 73, 79, 83, 89, 97, 101, 103, 107 if( (BigIntToSet.GetD( 0 ) & 1 ) != 1 ) throw( new Exception( "Product can't be even." )); int Where = 0; uint ToSet = 0; for( int Row = 0; Row < MultArraySize; Row++ ) { ToSet = MultArray[Row].OneLine[0]; if( (BigIntToSet.GetD( 0 ) & 1 ) == 1 ) ToSet = SetAccumOut( ToSet ); else ToSet = ClearAccumOut( ToSet ); MultArray[Row].OneLine[0] = ToSet; Where = Row; BigIntToSet.ShiftRight( 1 ); if( BigIntToSet.IsZero()) break; } ProductBitIndex = Where; int HowMany = ProductBitIndex + 1; MForm.ShowStatus( "There were " + HowMany.ToString() + " bits in the product." ); // At the last 1 bit. ToSet = MultArray[Where].OneLine[0]; ToSet = ClearCarryOut( ToSet ); MultArray[Where].OneLine[0] = ToSet; // The ProductBits can be: // Factor1Bits + Factor2Bits // or it can be: // Factor1Bits + Factor2Bits + 1 // ProductBits - Factor1Bits = Factor2Bits ( + 1 or not) int Factor1Bits = GetHighestMult1BitIndex() + 1; int ProductBits = (Where + 1); // Factor2Bits might be this or it might be one // less than this. int MaximumFactor2Bits = ProductBits - Factor1Bits; // Factor2BitIndex = Factor2Bits - 1; // Test if the top bit of Factor2 is in the right place. // ToSet = MultArray[Factor2BitIndex].OneLine[0]; // ToSet = SetMult( ToSet ); // MultArray[Factor2BitIndex].OneLine[0] = ToSet; for( int Row = MaximumFactor2Bits + 1; Row < MultArraySize; Row++ ) { ToSet = MultArray[Row].OneLine[0]; ToSet = ClearMult2( ToSet ); MultArray[Row].OneLine[0] = ToSet; } for( int Row = Where + 1; Row < MultArraySize; Row++ ) { ToSet = MultArray[Row].OneLine[0]; ToSet = ClearMult2( ToSet ); ToSet = ClearMult( ToSet ); ToSet = ClearAccumOut( ToSet ); ToSet = ClearCarryOut( ToSet ); MultArray[Row].OneLine[0] = ToSet; } }
// Factor1 is along the top. internal void SetFactor1( Integer UseBigIntToSet ) { Integer BigIntToSet = new Integer(); BigIntToSet.Copy( UseBigIntToSet ); if( (BigIntToSet.GetD( 0 ) & 1 ) != 1 ) throw( new Exception( "Factor1 can't be even." )); uint ToSet = MultArray[0].OneLine[0]; ToSet = SetMult1( ToSet ); ToSet = SetMult2( ToSet ); ToSet = SetMult( ToSet ); ToSet = SetAccumOut( ToSet ); ToSet = ClearCarryOut( ToSet ); MultArray[0].OneLine[0] = ToSet; // Factor1 has to be odd so Mult1 is set to // 1 all the way down the side. SetMult1AtColumn( 0 ); BigIntToSet.ShiftRight( 1 ); if( BigIntToSet.IsZero()) throw( new Exception( "Factor1 can't be 1." )); // 107 = 64 + 32 + 8 + 2 + 1 int Where = 1; for( int Column = 1; Column < MultArraySize; Column++ ) { ToSet = MultArray[0].OneLine[Column]; if( (BigIntToSet.GetD( 0 ) & 1 ) == 1 ) ToSet = SetMult1( ToSet ); else ToSet = ClearMult1( ToSet ); MultArray[0].OneLine[Column] = ToSet; Where = Column; BigIntToSet.ShiftRight( 1 ); if( BigIntToSet.IsZero()) break; } // At the last 1 bit. // ToSet = MultArray[0].OneLine[Where]; for( int Column = Where + 1; Column < MultArraySize; Column++ ) { ToSet = MultArray[0].OneLine[Column]; ToSet = ClearMult1( ToSet ); ToSet = ClearMult( ToSet ); ToSet = ClearAccumOut( ToSet ); ToSet = ClearCarryOut( ToSet ); MultArray[0].OneLine[Column] = ToSet; } }