123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "BGN.hpp"
- BGN::BGN()
- {
- Scalar a1, b1, c1, d1, a2, b2, c2, d2;
-
- while (true)
- {
- a1.set_random();
- b1.set_random();
- c1.set_random();
- if (a1 != 0)
- {
- d1 = (b1 * c1 + Scalar(1)) / a1;
- break;
- }
- }
- while (true)
- {
- a2.set_random();
- b2.set_random();
- c2.set_random();
- if (a2 != 0)
- {
- d2 = (b2 * c2 + Scalar(1)) / a2;
- break;
- }
- }
- Scalar r1, r2, r3, r4;
- r1.set_random();
- r2.set_random();
- r3.set_random();
- r4.set_random();
- curvepoint_fp_t g_part1, g_part2, g_a1, g_b1;
- g_part1 = r1 * bn_curvegen;
- g_part2 = r2 * bn_curvegen;
- g_a1 = a1 * bn_curvegen;
- g_b1 = b1 * bn_curvegen;
- Bipoint<curvepoint_fp_t> full_g(g_part1, g_part2);
- Bipoint<curvepoint_fp_t> full_g1(g_a1, g_b1);
- twistpoint_fp2_t h_part1, h_part2, h_a2, h_b2;
- h_part1 = r3 * bn_twistgen;
- h_part2 = r4 * bn_twistgen;
- h_a2 = a2 * bn_twistgen;
- h_b2 = b2 * bn_twistgen;
- Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
- Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
- full_g.make_affine();
- full_g1.make_affine();
- full_h.make_affine();
- full_h1.make_affine();
-
- public_key.set(full_g, full_h, full_g1, full_h1);
- private_key.set(public_key, a1, b1, c1, d1, a2, b2, c2, d2);
- }
- void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const
- {
- public_key.encrypt(G_element, cleartext);
- }
- void BGN::encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
- {
- public_key.encrypt(H_element, cleartext);
- }
- void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
- {
- public_key.encrypt(G_element, H_element, cleartext);
- }
- Bipoint<curvepoint_fp_t> BGN::homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const
- {
- return public_key.homomorphic_addition(a, b);
- }
- Bipoint<twistpoint_fp2_t> BGN::homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
- {
- return public_key.homomorphic_addition(a, b);
- }
- Quadripoint BGN::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
- {
- return public_key.homomorphic_addition(a, b);
- }
- Quadripoint BGN::homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
- {
- return public_key.homomorphic_multiplication(a, b);
- }
- Scalar BGN::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const
- {
- return private_key.decrypt(ciphertext);
- }
- Scalar BGN::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const
- {
- return private_key.decrypt(ciphertext);
- }
- Scalar BGN::decrypt(const Quadripoint& ciphertext) const
- {
- return private_key.decrypt(ciphertext);
- }
- PublicKey BGN::get_public_key() const
- {
- return public_key;
- }
|