Coder Social home page Coder Social logo

Comments (6)

Metaception avatar Metaception commented on June 25, 2024

If I may ask, how much of this functionality is working right?
I am in need of a BigInt library as well, but there was none to be found.

I am attempting to make one myself, but I am unfamiliar with these algorithms so progress has been very slow.

I have tested the powMod, multMod, and mod in the online Solidity compiler and they are not producing the expected result.
Am I using them incorrectly?

from cypherpoker.

monicanagent avatar monicanagent commented on June 25, 2024

I've put off working on this library indefinitely as I've discovered that I can achieve similar results using Solidity's mulmod. I stopped working on powMod which currently appears to be experiencing overflows (https://github.com/monicanagent/cypherpoker/blob/master/ethereum/solidity/BigInt.sol), although the conversion to/from BigInt and basic math functions seem to work fine.

If you're looking to achieve modular exponentiation within 256 bits then the following function works well (all of my testing has been successful):

function modExp(uint256 base, uint256 exp, uint256 mod) internal returns (uint256 result)  {
   result = 1;
   for (uint count = 1; count <= exp; count *= 2) {
      if (exp & count != 0)
         result = mulmod(result, base, mod);
      base = mulmod(base, base, mod);
   }
}

This function runs through at most "exp" loops so it's fairly efficient but it should be noted that at 256 loops "count" resets back to 0, effectively causing an infinite loop. That means that you can only use values that are 255 bits or shorter. With the following modifications you may be able to squeeze in all 256 bits but I haven't tested this yet:

function modExp(uint256 base, uint256 exp, uint256 mod) internal returns (uint256 result)  {
   result = 1;
   for (uint count = 1; count <= exp; (count = (count*2)-1)) {
      if (exp & (count+1) != 0)
         result = mulmod(result, base, mod);
      base = mulmod(base, base, mod);
   }
}

As long as you can work with modulo values that are within the (2^256)-1 range you can use practically unlimited exponent sizes simply by calling the modExp function repeatedly. For example, using the exponents a and b:

(((m^a) mod P)^b) mod P) = (m^ab) mod P

If each of our exponents is smaller than (2^256)-1 then we can chain our results to produce the same result as if we'd done one calculation using a single composite exponent, ab, effectively increasing our exponent to any arbitrary size (as long as we can provide enough gas to cover the calculations). You can find more details here

from cypherpoker.

Metaception avatar Metaception commented on June 25, 2024

I was hoping to work with 1024-bits for the base, exponential, and modulo values.
Bummer :(

Though for my application, only mulmod is needed. PowMod would have been nice, but it not necessary.

So I was wondering if mod and mulmod was working?
Mod gave me erroneous results and mulmod caused the online compiler to crash.

Great work though, I am hoping to implement the more efficient algorithms from this library and Leemon's JS library that this one is based off.

P.S. Here is my current work, it is pretty bad though.

EDIT: Link fixed

from cypherpoker.

riordant avatar riordant commented on June 25, 2024

Hey, I know this is an old thread but I built out a full bigint library for Solidity: https://github.com/zcoinofficial/solidity-BigNumber/

from cypherpoker.

monicanagent avatar monicanagent commented on June 25, 2024

This looks great! Thanks for sharing it; I'll definitely have a further look into your big number contracts.

from cypherpoker.

riordant avatar riordant commented on June 25, 2024

No problem, I intend to get it added to EthPM and Consensys live-libs soon, and to get it properly audited. It's pretty well tested as is

from cypherpoker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.