{"id":13648847,"url":"https://github.com/rkm0959/Inequality_Solving_with_CVP","last_synced_at":"2025-04-22T11:33:20.113Z","repository":{"id":134500391,"uuid":"323675686","full_name":"rkm0959/Inequality_Solving_with_CVP","owner":"rkm0959","description":"CVP \"trick\" for CTF challenges","archived":false,"fork":false,"pushed_at":"2023-06-19T16:27:46.000Z","size":60,"stargazers_count":123,"open_issues_count":0,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-09T23:36:21.812Z","etag":null,"topics":["cryptography","ctf"],"latest_commit_sha":null,"homepage":"","language":"Sage","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/rkm0959.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}},"created_at":"2020-12-22T16:18:57.000Z","updated_at":"2024-10-08T04:18:29.000Z","dependencies_parsed_at":"2023-06-16T14:15:49.306Z","dependency_job_id":null,"html_url":"https://github.com/rkm0959/Inequality_Solving_with_CVP","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/rkm0959%2FInequality_Solving_with_CVP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkm0959%2FInequality_Solving_with_CVP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkm0959%2FInequality_Solving_with_CVP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkm0959%2FInequality_Solving_with_CVP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rkm0959","download_url":"https://codeload.github.com/rkm0959/Inequality_Solving_with_CVP/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250232557,"owners_count":21396662,"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":["cryptography","ctf"],"created_at":"2024-08-02T01:04:36.323Z","updated_at":"2025-04-22T11:33:19.871Z","avatar_url":"https://github.com/rkm0959.png","language":"Sage","funding_links":[],"categories":["Sage"],"sub_categories":[],"readme":"# Inequality Solving with CVP\r\n\r\nA special case of this problem has another algorithm : check the \"Special Case\" folder for details\r\n\r\nA full writeup on this toolkit (in Korean) will hopefully be posted for SAMSUNG Software Membership blog.\r\n\r\nhttp://www.secmem.org/blog/2021/03/15/Inequality_Solving_with_CVP/\r\n\r\n## How to use\r\n\r\nThe solve function has four inputs, matrix ``mat``, lower/upper bounds ``lb, ub``, and a ``weight``.\r\n\r\nAssume ``mat`` is an ``n x m`` integer matrix. This means there are ``n`` variables and ``m`` inequalities.\r\n\r\nEach column of the ``mat`` represents a linear combination of the ``n`` variables. \r\n\r\nEach entry of ``lb, ub`` denotes a lower/upper bound to that linear combination.\r\n\r\nOf course, we require the length of ``lb, ub`` to be ``m``. \r\n\r\n``weight`` is a variable that you do *NOT* have to initialize. It will be explained later.\r\n\r\n`result` is the result of the CVP \r\n\r\n`applied_weights` is the applied weights during the weighting process (see below)\r\n\r\n`fin` is the actual value of the variables, recovered when `n = m` and vectors are linearly independent\r\n\r\nWe also have a heuristic for number of solutions for the inequality. This is a good way to decide if this method is feasible. For some notes on this topic, check out [Mystiz's writeup on Example Challenge 5.](https://mystiz.hk/posts/2021-02-28-aeroctf/)\r\n\r\n## The reasoning behind the algorithm\r\n\r\n**Warning : the stuff I say here are not mathematically precise. It's based on intuition**\r\n\r\nBasically what the algorithm does, is to build a lattice with the given matrix and find a closest vector (with Babai's algorithm) to ``(lb + vb) / 2``. However, there is one more twist to the algorithm.\r\n\r\nThe reason we hope that CVP will solve our problem is basically as follows\r\n\r\n- CVP will try to minimize ``||x - (lb + vb) / 2||`` where ``x`` is in our lattice\r\n- Usually, that *implies* trying to minimize ``|x_i - (lb_i + ub_i) / 2|`` for each `i`\r\n- Therefore, it will try to keep `|x_i - (lb_i + ub_i) / 2|` below `|(ub_i - lb_i) / 2|`!\r\n\r\nHowever, there's a case where this reasoning fails. \r\n\r\n- Assume we have an instance with`lb = [0, 0]`, `ub = [10 ** 300, 1]`\r\n- Does the CVP algorithm \"respect\" the bound `lb_2 = 0, ub_2 = 1`?\r\n- CVP algorithm will ignore it to keep the first entry close to `(10 ** 300) / 2` as possible\r\n\r\nTo do this, we have to *scale* our inequalities so `ub_i - lb_i` becomes of similar size. \r\n\r\n- This can be done by multiplying an entire column, as well as `lb_i` and `ub_i`\r\n- What if `lb_i = ub_i`? Then, we have to multiply a `super large integer` to that column.\r\n- This `super large integer` is the `weight` in the input. \r\n- The default `weight` is what I think is \"super large\", but you can definitely change it :)\r\n\r\n\r\n## Further Comments\r\n\r\n- **Babai's Algorithm implementation is NOT MINE - read solver.sage for details**\r\n- I have included some example challenges I have solved using this technique.\r\n- You can also break truncated LCG with this idea.\r\n- This method does not work *that* well with low density 0/1 knapsack - CJ LOSS is much better.\r\n- The *scaling* method (obviously) increases the runtime of the LLL.\r\n- It seems like sometimes SVP gives better results than CVP...\r\n- If failed, it's a good idea to try a different scaling by observing the failed output.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkm0959%2FInequality_Solving_with_CVP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frkm0959%2FInequality_Solving_with_CVP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkm0959%2FInequality_Solving_with_CVP/lists"}