Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
math / math-pow-modOpenRuns on Liberas

Modular exponentiation

base^exponent mod modulus as the least non-negative residue, 0 ≤ result < modulus: a negative base counts as its residue, so (−2)^3 mod 5 = 2, and x^0 mod m = 1 mod m (0 when m = 1, 0^0 = 1). Each input is an optional - then 1 to 500 ASCII digits (leading zeros allowed; nothing else). The result is value(text) in canonical decimal (no leading zeros). Errors are invalid(reason), in this order: base, exponent, then modulus, each not in the syntax → syntax, over 500 digits → too-long (each checked for syntax before length); modulus 0 → division-by-zero; modulus < 0 or exponent < 0 → out-of-range.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init math-pow-mod --lang rust
cd math-pow-mod && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)32 lines›
math-pow-mod.wit
package root:component;

world root {
  import liberas:math/types@0.1.0;

  export liberas:math/modular-power@0.1.0;
}
package liberas:math@0.1.0 {
  interface types {
    enum math-error {
      syntax,
      too-long,
      division-by-zero,
      too-large,
      out-of-range,
      not-finite,
      empty,
      too-deep,
    }

    variant number-outcome {
      value(string),
      invalid(math-error),
    }
  }
  interface modular-power {
    use types.{number-outcome};

    pow-mod: func(base: string, exponent: string, modulus: string) -> number-outcome;
  }
}
Test vectors15›
math-pow-mod.cases.json
[
  {
    "args": {
      "base": "4",
      "modulus": "497",
      "exponent": "13"
    }
  },
  {
    "args": {
      "base": "-2",
      "modulus": "5",
      "exponent": "3"
    }
  },
  {
    "args": {
      "base": "2",
      "modulus": "1",
      "exponent": "0"
    }
  },
  {
    "args": {
      "base": "0",
      "modulus": "7",
      "exponent": "0"
    }
  },
  {
    "args": {
      "base": "3",
      "modulus": "1000000007",
      "exponent": "1000000000000000000000000000000"
    }
  },
  {
    "args": {
      "base": "65537",
      "modulus": "4294967297",
      "exponent": "65537"
    }
  },
  {
    "args": {
      "base": "78475461027818321663018174383590578446156314367905957531775706795766621721982803109884333780295096813439205854580158237587206361065809070495796398214948157668357515892981212650412317415156168296971139685780911353986580769780234937873723983258286387354060602235909303094681745708473461969490285918109773471520312976306224801449608193113627820997462486218051461538776585451835324272163922548873089940571932850783485902917167074720801330249518985288293047771084065454981134193068610065992336075342191683",
      "modulus": "50977518845117117247312472027876863491159617645089504198617765486505106664507350725299410346230880945557106461588159543400343219932140692299155693967623975281487532179573152101781847898750762057578321297952456297953653433635134420158422853188823422650590811355884674235528909981281027481992667753836456111484553470613263618959122998209803481109546053289314076971833404194988900852893439862204018405051770461856901540177735382168032939736509073505246196593090694977385400053002470215296991843932775777",
      "exponent": "32179968393443203851686470061159151746459947953436188085529757107347681537129130341674220635248304747975007378553457156026713860806414544026817928317308093874217526453132092682750377904354396815175280879454691861250756834513007686436128750015160564956320720775736915449768466434022683670124779598863139828011797974920235725492646023028786312644957653287604651614263020867064886132890673116662040663838518372959124118637333620273290805953527438125269245342006993197459406210046530465223886318822359531"
    }
  },
  {
    "args": {
      "base": "78475461027818321663018174383590578446156314367905957531775706795766621721982803109884333780295096813439205854580158237587206361065809070495796398214948157668357515892981212650412317415156168296971139685780911353986580769780234937873723983258286387354060602235909303094681745708473461969490285918109773471520312976306224801449608193113627820997462486218051461538776585451835324272163922548873089940571932850783485902917167074720801330249518985288293047771084065454981134193068610065992336075342191683",
      "modulus": "20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
      "exponent": "28575650252771911193403779033494412755707477492623423110865718386268225837485220154395990411762286978689109160002709494184630159103736155973348772357392952184947083493418167529708277694106173249470818195505565133700904306822107514422371800840430935404270868680825439566830543524969411588575695962040324360143866576490738960567553972981824149565696400709417492337640181337924124427070563453349048542220932166607584432517102469662849101583089612480754658258094471557606704840063910520773112232187196913"
    }
  },
  {
    "args": {
      "base": "123456789",
      "modulus": "-1",
      "exponent": "987654321"
    }
  },
  {
    "args": {
      "base": "2",
      "modulus": "5",
      "exponent": "-1"
    }
  },
  {
    "args": {
      "base": "2",
      "modulus": "0",
      "exponent": "10"
    }
  },
  {
    "args": {
      "base": "2",
      "modulus": "-0",
      "exponent": "10"
    }
  },
  {
    "args": {
      "base": "x",
      "modulus": "0",
      "exponent": "1"
    }
  },
  {
    "args": {
      "base": "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
      "modulus": "5",
      "exponent": "1"
    }
  },
  {
    "args": {
      "base": "1",
      "modulus": "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
      "exponent": "1"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 15 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 1,200,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.