{"id":13632708,"url":"https://github.com/ZenGo-X/rust-elgamal","last_synced_at":"2025-04-18T05:33:01.808Z","repository":{"id":45605388,"uuid":"297580855","full_name":"ZenGo-X/rust-elgamal","owner":"ZenGo-X","description":"Rust implementation of ElGamal encryption ","archived":false,"fork":false,"pushed_at":"2024-02-02T07:55:09.000Z","size":82,"stargazers_count":14,"open_issues_count":6,"forks_count":6,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-08-01T22:54:13.182Z","etag":null,"topics":["cryptography"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZenGo-X.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":"2020-09-22T08:13:20.000Z","updated_at":"2023-09-13T03:31:58.000Z","dependencies_parsed_at":"2024-06-11T17:04:56.693Z","dependency_job_id":null,"html_url":"https://github.com/ZenGo-X/rust-elgamal","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/ZenGo-X%2Frust-elgamal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZenGo-X%2Frust-elgamal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZenGo-X%2Frust-elgamal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZenGo-X%2Frust-elgamal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZenGo-X","download_url":"https://codeload.github.com/ZenGo-X/rust-elgamal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223775244,"owners_count":17200480,"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"],"created_at":"2024-08-01T22:03:11.595Z","updated_at":"2024-11-09T01:30:43.095Z","avatar_url":"https://github.com/ZenGo-X.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# rust-elgamal\nSimple interface for ElGamal and Homomorphic-ElGamal cryptosystems. \n\n## Usage\n```rust\nuse curv::arithmetic::traits::Modulo;\nuse curv::arithmetic::traits::Samplable;\nuse curv::BigInt;\nuse elgamal::{\n    rfc7919_groups::SupportedGroups, ElGamal, ElGamalKeyPair, ElGamalPP, ElGamalPrivateKey,\n    ElGamalPublicKey,ExponentElGamal,ElGamalCiphertext,\n};\n\nfn main() {\n\n    // choose suitable field parameter, https://tools.ietf.org/html/rfc7919\n    let group_id = SupportedGroups::FFDHE2048;\n\n    let alice_pp = ElGamalPP::generate_from_rfc7919(group_id);\n\n    // create a public, secret keypair\n    let alice_key_pair = ElGamalKeyPair::generate(\u0026alice_pp);\n\n\n    // basic en/decryption roundtrip\n    let message = BigInt::from(13);\n    let cipher = ElGamal::encrypt(\u0026message, \u0026alice_key_pair.pk).unwrap();\n    let message_tag = ElGamal::decrypt(\u0026cipher, \u0026alice_key_pair.sk).unwrap();\n    println!(\"basic encryption: message: {}, decrypted: {}\", message, message_tag);\n\n\n    // homomorphic multiplication\n    let factor_1 = BigInt::from(13);\n    let factor_2 = BigInt::from(9);\n\n    let cipher = ElGamal::encrypt(\u0026factor_1, \u0026alice_key_pair.pk).unwrap();\n    let constant_cipher = ElGamal::encrypt(\u0026factor_2, \u0026alice_key_pair.pk).unwrap();\n\n    // homomorphic multiplication in cipher space\n    let product_cipher = ElGamal::mul(\u0026cipher, \u0026constant_cipher).unwrap();\n\n    // decrypt homomorphic product\n    let product_tag = ElGamal::decrypt(\u0026product_cipher, \u0026alice_key_pair.sk).unwrap();\n    println!(\" factor1: {} * factor 2: {} = {}; decrypted homomorphic product: {}\", factor_1, factor_2, \u0026factor_1 * \u0026factor_2, product_tag);\n\n\n    // homomorphic (pow) addition\n    // note to self: we now have (g^r, g^m * h^r) instead of (g^r, m * h^r)\n    // data set:\n    let data = vec![BigInt::from(1),BigInt::from(10), BigInt::from(100)];\n    let randomness = vec![BigInt::sample_below(\u0026alice_pp.q), BigInt::sample_below(\u0026alice_pp.q), BigInt::sample_below(\u0026alice_pp.q)];\n\n    // encrypt each data point \n    let mut ciphers: Vec\u003cElGamalCiphertext\u003e = Vec::new();\n    for (idx, number) in data.iter().enumerate() {\n        let c = ExponentElGamal::encrypt_from_predefined_randomness(\u0026number, \u0026alice_key_pair.pk, \u0026randomness[idx]).unwrap();\n        ciphers.push(c);\n    }\n\n    // finally, we add the data\n    let n = ciphers.len();\n    let mut addition_cipher: ElGamalCiphertext = ExponentElGamal::add(\u0026ciphers[0], \u0026ciphers[1]).unwrap();\n    for idx in 2..n {\n        addition_cipher = ExponentElGamal::add(\u0026ciphers[idx], \u0026addition_cipher).unwrap();\n    }\n\n    // and now we decrypt and due to the exponentiation we end up with g^m\n    let c_tag =  ExponentElGamal::decrypt_exp(\u0026addition_cipher, \u0026alice_key_pair.sk).unwrap();\n\n    // and we're super inefficiently brute-forcing g^i mod p to validate the raw sum\n    for i in 0..1_000_000 {\n        let res = BigInt::mod_pow(\u0026alice_key_pair.pk.pp.g, \u0026BigInt::from(i), \u0026alice_key_pair.pk.pp.p);\n        if res.eq(\u0026c_tag) {\n            println!(\"result: {}\", i);\n            break;\n        }\n    }\n\n}\n\n```\n## Tests  \n\nSeveral tests are included:  \n\n```rust\ncargo test --lib\n```  \n\nPlease note that the test for `generate_safe` is not part of the default test run due to the potentially long runtime. To run the expensive tests:  \n\n```rust\ncargo test --lib  -- --ignored\n```  \n\n## Benches  \n\nBenchmarks are also included:\n\n```rust\ncargo bench\n```  \n\nThe benchmarks are created by[criterion.rs](https://github.com/bheisler/criterion.rs) and the default reports include pretty cool plots, which are best with `gnuplot` installed, e.g., `brew install gnuplot`.  The benchmark reports can found in `../target/criterion/report` and `open index.html` should do.\n\nTo run the benches without plots, or with any of the other [criterion.rs options](https://bheisler.github.io/criterion.rs/book/user_guide/command_line_options.html), use  \n\n```bash\ncargo bench --bench elgamal_benches -- --noplot\n```  \n\nSee `benches/examples` for a full results set.  \n\n\n## Contact\n\nFeel free to [reach out](mailto:github@kzencorp.com) or join ZenGo X [Telegram](https://t.me/joinchat/ET1mddGXRoyCxZ-7) for discussions on code and research.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FZenGo-X%2Frust-elgamal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FZenGo-X%2Frust-elgamal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FZenGo-X%2Frust-elgamal/lists"}