{"id":24752711,"url":"https://github.com/adri326/mdc","last_synced_at":"2025-03-23T04:26:58.826Z","repository":{"id":109748709,"uuid":"478922023","full_name":"adri326/mdc","owner":"adri326","description":"Modular desk calculator, based on the gnu dc syntax","archived":false,"fork":false,"pushed_at":"2022-04-09T13:23:02.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T10:51:40.315Z","etag":null,"topics":["dc","modulus","rsa"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adri326.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-07T09:48:15.000Z","updated_at":"2023-11-14T01:28:59.000Z","dependencies_parsed_at":"2023-04-21T15:07:41.843Z","dependency_job_id":null,"html_url":"https://github.com/adri326/mdc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adri326%2Fmdc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adri326%2Fmdc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adri326%2Fmdc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adri326%2Fmdc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adri326","download_url":"https://codeload.github.com/adri326/mdc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245055734,"owners_count":20553735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dc","modulus","rsa"],"created_at":"2025-01-28T10:50:58.819Z","updated_at":"2025-03-23T04:26:58.807Z","avatar_url":"https://github.com/adri326.png","language":"C","readme":"# Modular desk calculator (mdc)\n\nA re-implementation of the [GNU program `dc`](https://linux.die.net/man/1/dc), in modular arithmetic.\n\nAs of right now, strings, arrays and macros aren't implemented.\n\nThe supported operations are:\n\n- `\u003cnumber\u003e`, `_\u003cnumber\u003e`: pushes the number `number` onto the stack. If it is preceded by `_`, pushes the negative value for it.\n- `m`: pops a value from the stack and sets it as the modulus for all subsequent operations; if set to 0 (default), then no modulus is applied. This number is referred to as `m`\n- `M`: pushes onto the stack the value of `m`\n- `%`: pops a number `x` from the stack and pushes back `x mod m`\n- `+`: pops two numbers `a` and `b` and pushes back `(a + b) mod m` (or `a + b` if `m = 0`)\n- `-`: pops two numbers `a` and `b` and pushes back `(a - b) mod m` (or `a - b` if `m = 0`)\n- `*`: pops two numbers `a` and `b` and pushes back `(a * b) mod m` (or `a * b` if `m = 0`)\n- `/`: pops two numbers `a` and `b` and pushes back `(a / b) mod m` (or `a // b` if `m = 0`); requires that `gcd(b, m)` divides `a`\n- `^`: pops two numbers `a` and `b` and pushes back `(a ^ b) mod m` (or `a ^ b` if `m = 0`; in that case, `b` must be less than `ULONG_MAX`)\n- `C`: pops two numbers `a` and `b` and pushes back `lcm(a, b)`\n- `G`: pops two numbers `a` and `b` and pushes back `gcd(a, b)`\n- `d`: duplicates the value on top of the stack\n- `z`: pushes on the stack the current length of the stack\n- `v`: pops a number `x` from the stack and computes the modular square root of `x`, see [Modular square root](https://www.rieselprime.de/ziki/Modular_square_root) for more information *(Note: composite moduli and moduli congruent to 1 mod 8 haven't been implemented yet.)*\n- `~`: pops a number `x` from the stack and pushes back `-x mod m` (or `m` if `m = 0`)\n- `f`: prints the entirety of the stack\n- `p`: prints the top value of the stack\n- `n`: pops a number from the stack and prints it, without a newline\n- `c`: clears the stack\n- `r`: swaps the position of the top two values of the stack\n- `s\u003cr\u003e`: pops `x` from the stack and stores in on the top of the register `\u003cr\u003e` (overriding the previous value)\n- `S\u003cr\u003e`: pops `x` from the stack and pushes it on top of the register `\u003cr\u003e`'s stack (shadowing the previous value)\n- `l\u003cr\u003e`: pushes on the stack the top value of the register `\u003cr\u003e`\n- `L\u003cr\u003e`: pops `x` from the stack of the register `\u003cr\u003e` and pushes it onto the stack (un-shadowing values below it)\n\nSpaces are optional, except between numbers.\nI really recommend reading through [`dc`'s documentation](https://linux.die.net/man/1/dc) if you aren't somewhat familiar with it already.\n\n## Installation\n\nTo compile and use this software, you will need `git`, GNU Make, a C compiler and the [GNU MP library](https://gmplib.org/manual/index) (`gmp` on Arch Linux).\n\nFirst, clone this repository, then compile it with `make`:\n\n```sh\ngit clone https://github.com/adri326/mdc\ncd mdc\nmake\n```\n\nThe final binary will be put in `build/mdc`.\nYou can copy it into a directory that is in your PATH to have the command `mdc` available everywhere.\n\n## Running\n\nSimply execute the `mdc` binary; it will then read input from `stdin` and print output to `stdout`.\n\nIf you aren't piping anything into `mdc`, then it will act as a kind of terminal; you may write and edit commands, and submit them by pressing `enter`.\nTo exit, press `Ctrl+D` to send the EoF character.\n\n## Example usage\n\n```sh\n31m # Set the modulus to 31\n10 # Push 10\n2* p # Prints 20\n2* p # Prints 40%31 = 9\n4* p # Prints (9*4) % 31 = 5\n```\n\nThe following expression computes `(23*(3*1+40))² - 2 mod 103`:\n\n```sh\n103m\n1 3* 40 + # (3*1 + 40)\n23* 2^ # (23*(3*1+40))²\n2- # (23*(3*1+40))² - 2\np # Prints 31\n```\n\nThe following encrypts and decrypts a message using RSA:\n\n```sh\n2010942103422233250095259520183 sp # p\n3503815992030544427564583819137 sq # q\nlplq*p sn # n\nlp1-lq1-Cm # Compute λ(n) and use it as modulo\n2 16^ 1+ d se # e = 2^16+1\n1r/p sd # d = e^-1 mod λ(n)\n\nlnm # Use n as modulo\n36762444129608 le ^p # Encode the message with (n, e); here the message is the decimal version of \"Hello!\"\nld ^p # Decode the message\n```\n\nFor comparison, here is how you would have to do it using vanilla `dc`:\n\n```sh\n2010942103422233250095259520183 sp # p\n3503815992030544427564583819137 sq # q\nlplq*p sn # n\nlp1- lq1- # Load p-1 and q-1\n[dSarLa%d0\u003ca]dsax+ # Compute gcd(p-1, q-1), thank you Wikipedia for the expression\nlp1- lq1- *r/ sl # Compute λ(n) and store it\n2 16^ 1+ d se # e = 2^16+1\nlldsm le dsa1sv[dsb~rsqlbrldlqlv*-lvsdsvd0\u003cx]dsxxldd[dlmr+]sx0\u003ex p sd # Computes d = e^-1 mod λ(n), thanks Rosetta stone for this expression\n\n36762444129608 le ln |p # Encode the message with (n, e); here the message is the decimal version of \"Hello!\"\nld ln |p # Decode the message\n```\n\n## Warranty\n\nThis software is provided as-is, there is no warranty, to the extent permitted by law.\nSee [LICENSE.md](./LICENSE.md) for more detail.\n\nThis software is not fit for cryptographic uses, use it at your own risk.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadri326%2Fmdc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadri326%2Fmdc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadri326%2Fmdc/lists"}