void extendedEuclidean(BigInteger m, BigInteger n,
		BigInteger &g, BigInteger &r, BigInteger &s) {
	if (&g == &r || &g == &s || &r == &s)
		throw "BigInteger extendedEuclidean: Outputs are aliased";
	BigInteger r1(1), s1(0), r2(0), s2(1), q;
	/* Invariants:
	 * r1*m(orig) + s1*n(orig) == m(current)
	 * r2*m(orig) + s2*n(orig) == n(current) */
	for (;;) {
		if (n.isZero()) {
			r = r1; s = s1; g = m;
			return;
		}
		// Subtract q times the second invariant from the first invariant.
		m.divideWithRemainder(n, q);
		r1 -= q*r2; s1 -= q*s2;

		if (m.isZero()) {
			r = r2; s = s2; g = n;
			return;
		}
		// Subtract q times the first invariant from the second invariant.
		n.divideWithRemainder(m, q);
		r2 -= q*r1; s2 -= q*s1;
	}
}
Exemplo n.º 2
0
	friend BigInteger gcd(BigInteger a, BigInteger b) {
		int powerOf2 = 0;
		a.sign = 1;
		b.sign = 1;
		while(!b.isZero() && !a.isZero()) {
			while(a.isEven() && b.isEven()) {
				a /= 2;
				b /= 2;
				++powerOf2;
			}
			while(a.isEven()) {
				a /= 2;
			}
			while(b.isEven()) {
				b /= 2;
			}
			a -= b;

			if(a.sign != 1){
				a.sign = 1;
				std::swap(a, b);
			}
		}
		return pow(BigInteger(2), powerOf2) * (a.isZero() ? b : a);
	}
Exemplo n.º 3
0
    String open (const BigInteger& inputChannels, const BigInteger& outputChannels,
                 double /* sampleRate */, int /* bufferSizeSamples */) override
    {
        if (client == nullptr)
        {
            lastError = "No JACK client running";
            return lastError;
        }

        lastError.clear();
        close();

        xruns = 0;
        juce::jack_set_process_callback (client, processCallback, this);
        juce::jack_set_port_connect_callback (client, portConnectCallback, this);
        juce::jack_on_shutdown (client, shutdownCallback, this);
        juce::jack_set_xrun_callback (client, xrunCallback, this);
        juce::jack_activate (client);
        deviceIsOpen = true;

        if (! inputChannels.isZero())
        {
            for (JackPortIterator i (client, true); i.next();)
            {
                if (inputChannels [i.index] && i.clientName == getName())
                {
                    int error = juce::jack_connect (client, i.ports[i.index], juce::jack_port_name ((jack_port_t*) inputPorts[i.index]));
                    if (error != 0)
                        JUCE_JACK_LOG ("Cannot connect input port " + String (i.index) + " (" + i.name + "), error " + String (error));
                }
            }
        }

        if (! outputChannels.isZero())
        {
            for (JackPortIterator i (client, false); i.next();)
            {
                if (outputChannels [i.index] && i.clientName == getName())
                {
                    int error = juce::jack_connect (client, juce::jack_port_name ((jack_port_t*) outputPorts[i.index]), i.ports[i.index]);
                    if (error != 0)
                        JUCE_JACK_LOG ("Cannot connect output port " + String (i.index) + " (" + i.name + "), error " + String (error));
                }
            }
        }

        updateActivePorts();

        return lastError;
    }
Exemplo n.º 4
0
bool RSAKey::applyToValue (BigInteger& value) const
{
    if (part1.isZero() || part2.isZero() || value <= 0)
    {
        jassertfalse;   // using an uninitialised key
        value.clear();
        return false;
    }

    BigInteger result;

    while (! value.isZero())
    {
        result *= part2;

        BigInteger remainder;
        value.divideBy (part2, remainder);

        remainder.exponentModulo (part1, part2);

        result += remainder;
    }

    value.swapWith (result);
    return true;
}
void
ExponentiationAlgorithm::compute(BigInteger& x, BigInteger& e, BigInteger& y, BigInteger m)
{ 
  BigInteger one(1);
  BigInteger two(2);
    
  if ((e % two) == one) {
    //    std::cout << "TRACE - odd " << x << " " << e << " " << y << " " << m  << std::endl;
    //    std::cout << "TRACE - check zero " << x .sign() << " " << y.sign() << std::endl;
    //    std::cout << "TRACE - (x * y) " << (x * y) << std::endl;
    y = (x * y);
    //    std::cout << "TRACE - y " << y << std::endl;
    e = e - one;
    //    std::cout << "TRACE - odd " << x << " " << e << " " << y << " " << m  << std::endl;
  } else {
    //    std::cout << "TRACE - even " << x << " " << e << " " << y << " " << m  << std::endl;
    x = (x * x);
    e = (e / two);
    //    std::cout << "TRACE - even " << x << " " << e << " " << y << " " << m  << std::endl;
  }

  if (!m.isZero()) {
    //    std::cout << "TRACE - !m.isZero " << x << " " << e << " " << y << " " << m  << std::endl;
    y = y % m;
    x = x % m;
    //    std::cout << "TRACE - !m.isZero " << x << " " << e << " " << y << " " << m  << std::endl;
  }
}// compute
BigInteger
ExponentiationAlgorithm::pow(BigInteger x, BigInteger e)
{
  BigInteger y(1);

  while (!e.isZero()) {
     compute(x, e, y, 0);
  }
   return y;
}// pow
BigInteger
ExponentiationAlgorithm::pow_mod(BigInteger x, BigInteger e, BigInteger m)
{
  //  std::cout << "TRACE - " << x << " " << e << " 1 " << m << std::endl;
  //  std::cout << "TRACE - x x.sign() " << x << " " << x.sign() << std::endl;

  BigInteger y(1);
  while (!e.isZero()) {
     compute(x, e, y, m);
     //     std::cout << "TRACE - " << x << " " << e << " " << y << " " << m  << std::endl;
  }
  return y;
}// pow_mod
Exemplo n.º 8
0
ECPoint ECCurve::multiplyPoint(BigInteger &k, ECPoint &p)
{
	BigInteger m = k;
	ECPointJacobian q = toJacobian(p);

	m = m % _q;

	ECPointJacobian r;

	while (!m.isZero()) {
		if (m.lsb()) {
			r = addJacobian(r, q);
		}
		m = m.rshift(1);
		q = doubleJacobian(q);
	}

	return toAffine(r);
}
Exemplo n.º 9
0
    //==============================================================================
    static XmlElement decryptXML (String hexData, RSAKey rsaPublicKey)
    {
        BigInteger val;
        val.parseString (hexData, 16);

        RSAKey key (rsaPublicKey);
        jassert (key.isValid());

        ScopedPointer<XmlElement> xml;

        if (! val.isZero())
        {
            key.applyToValue (val);

            const MemoryBlock mb (val.toMemoryBlock());

            if (CharPointer_UTF8::isValidString (static_cast<const char*> (mb.getData()), (int) mb.getSize()))
                xml = XmlDocument::parse (mb.toString());
        }

        return xml != nullptr ? *xml : XmlElement("key");
    }
void CBC_PDF417HighLevelEncoder::encodeNumeric(CFX_WideString msg,
                                               int32_t startpos,
                                               int32_t count,
                                               CFX_WideString& sb) {
  int32_t idx = 0;
  BigInteger num900 = 900;
  while (idx < count) {
    CFX_WideString tmp;
    int32_t len = 44 < count - idx ? 44 : count - idx;
    CFX_ByteString part =
        ((FX_WCHAR)'1' + msg.Mid(startpos + idx, len)).UTF8Encode();
    BigInteger bigint = stringToBigInteger(part.c_str());
    do {
      int32_t c = (bigint % num900).toInt();
      tmp += (FX_WCHAR)(c);
      bigint = bigint / num900;
    } while (!bigint.isZero());
    for (int32_t i = tmp.GetLength() - 1; i >= 0; i--) {
      sb += tmp.GetAt(i);
    }
    idx += len;
  }
}
Exemplo n.º 11
0
    void bigSieve (const BigInteger& base, const int numBits, BigInteger& result,
                   const BigInteger& smallSieve, const int smallSieveSize)
    {
        jassert (! base[0]); // must be even!

        result.setBit (numBits);
        result.clearBit (numBits);  // to enlarge the array

        int index = smallSieve.findNextClearBit (0);

        do
        {
            const int prime = (index << 1) + 1;

            BigInteger r (base), remainder;
            r.divideBy (prime, remainder);

            int i = prime - remainder.getBitRangeAsInt (0, 32);

            if (r.isZero())
                i += prime;

            if ((i & 1) == 0)
                i += prime;

            i = (i - 1) >> 1;

            while (i < numBits)
            {
                result.setBit (i);
                i += prime;
            }

            index = smallSieve.findNextClearBit (index + 1);
        }
        while (index < smallSieveSize);
    }
Exemplo n.º 12
0
    String open (const BigInteger& inputChannels, const BigInteger& outputChannels,
                 double /* sampleRate */, int /* bufferSizeSamples */)
    {
        if (client == nullptr)
        {
            lastError = "No JACK client running";
            return lastError;
        }

        lastError = String::empty;
        close();

        juce::jack_set_process_callback (client, processCallback, this);
        juce::jack_set_port_connect_callback (client, portConnectCallback, this);
        juce::jack_on_shutdown (client, shutdownCallback, this);
        juce::jack_activate (client);
        isOpen_ = true;

        if (! inputChannels.isZero())
        {
            if (const char** const ports = getJackPorts (client, true))
            {
                const int numInputChannels = inputChannels.getHighestBit() + 1;

                for (int i = 0; i < numInputChannels; ++i)
                {
                    const String portName (ports[i]);

                    if (inputChannels[i] && portName.upToFirstOccurrenceOf (":", false, false) == getName())
                    {
                        int error = juce::jack_connect (client, ports[i], juce::jack_port_name ((jack_port_t*) inputPorts[i]));
                        if (error != 0)
                            jack_Log ("Cannot connect input port " + String (i) + " (" + String (ports[i]) + "), error " + String (error));
                    }
                }

                free (ports);
            }
        }

        if (! outputChannels.isZero())
        {
            if (const char** const ports = getJackPorts (client, false))
            {
                const int numOutputChannels = outputChannels.getHighestBit() + 1;

                for (int i = 0; i < numOutputChannels; ++i)
                {
                    const String portName (ports[i]);

                    if (outputChannels[i] && portName.upToFirstOccurrenceOf (":", false, false) == getName())
                    {
                        int error = juce::jack_connect (client, juce::jack_port_name ((jack_port_t*) outputPorts[i]), ports[i]);
                        if (error != 0)
                            jack_Log ("Cannot connect output port " + String (i) + " (" + String (ports[i]) + "), error " + String (error));
                    }
                }

                free (ports);
            }
        }

        return lastError;
    }