{"id":24057240,"url":"https://github.com/marvin9/tonelli-shanks-algorithm","last_synced_at":"2025-02-26T12:16:57.713Z","repository":{"id":161544012,"uuid":"321768595","full_name":"Marvin9/tonelli-shanks-algorithm","owner":"Marvin9","description":"sqrt of n modulo p","archived":false,"fork":false,"pushed_at":"2020-12-16T07:06:48.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-09T05:31:26.195Z","etag":null,"topics":["algorithm","number-theory","tonelli-shanks"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marvin9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-12-15T19:27:42.000Z","updated_at":"2023-03-04T09:23:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"35a3e7f6-9fd3-4f6d-a241-b28b9c654003","html_url":"https://github.com/Marvin9/tonelli-shanks-algorithm","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/Marvin9%2Ftonelli-shanks-algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marvin9%2Ftonelli-shanks-algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marvin9%2Ftonelli-shanks-algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marvin9%2Ftonelli-shanks-algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marvin9","download_url":"https://codeload.github.com/Marvin9/tonelli-shanks-algorithm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240849012,"owners_count":19867617,"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":["algorithm","number-theory","tonelli-shanks"],"created_at":"2025-01-09T05:29:23.545Z","updated_at":"2025-02-26T12:16:57.692Z","avatar_url":"https://github.com/Marvin9.png","language":"Go","readme":"[This](https://discuss.codechef.com/t/lcasqrt-editorial/82141) problem on codechef motivated me to implement [tonelli-shanks](https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm#Speed_of_the_algorithm).\n\n```\nGiven non-zero integar n and prime number p, It finds R such that,\n\n(R)^2 congruence n (mod p)\n```\n\n[Algorithm](https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm#The_algorithm)\n\n## Algorithm in nutshell:\n\n\u003e Input: n, p\n\u003e Goal: R, where (R)^2 ≡ n (mod p); If solution exists else -1\n\n1. Check if solution exists using Euler's criterion. Return -1 if not.\n\n2. Find ```Q and S``` such that ```p - 1 = Q * (2)^S```\n\u003e divide p - 1 by 2 until it's modulo 2 is not equal to 0. Keep count number of division, store it as ```S```, remaining value (of ```p-1```) is ```Q```.\n\n3. Find smallest ```z``` which is quadratic non-residue.\n\n\u003e continue ```i = 2 to (p-1)``` until euler's criterion (i, p) is ```1```. Store ```z = i``` at end.\n\n4. Define some variables\n```bash\nm = S\nc = (z)^Q\nt = (n)^Q\nR = (n)^((Q+1)/2)\n```\n\n\u003e Note: Make sure all operations don't go beyond ```p```. For example, (3)^3 and ```p = 10``` =\u003e ans = ```[[[[(1 * 3)%10]*3]%10]*3]%10```\n\n5. Infinite loop,\n\n- if `t = 0`, return `0`\n- if `t = 1`, return `R`\n- For i = ```(1 to m - 1)```, such ```(t)^((2)^i) % p = 1```\n\u003e Note: Operations must not go beyond ```p```. \n- `b = (c)^((2)^(M - i - 1))`\n- `M = i`\n- `c = (b)^2 % p`\n- `t = t * (b)^2 % p`\n- `R = (R * b) % p`","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvin9%2Ftonelli-shanks-algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarvin9%2Ftonelli-shanks-algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvin9%2Ftonelli-shanks-algorithm/lists"}