Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # HG changeset patch
- # Parent c8a83ff13320911abb1e77a9149959f28c4c4e9d
- Add COMB method
- diff -r c8a83ff13320 Makefile
- --- a/Makefile Mon Feb 14 14:13:04 2011 +0200
- +++ b/Makefile Thu Feb 17 14:03:07 2011 +0200
- @@ -13,7 +13,7 @@
- AFFINE_ZZ_P := utils.o ec.o ec_defaults.o ec_compress.o
- AFFINE_GF2X := utils.o ec.o ec_defaults.o ec_compress.o
- -PROJ_GF2X := ec.o
- +PROJ_GF2X := ec.o ec_comb.o
- HASHES := rmd160.o sha512.o sha1.o
- GENERIC := octet.o hash.o mgf.o convhex.o
- diff -r c8a83ff13320 include/ec/GF2X/projective/ec.hpp
- --- a/include/ec/GF2X/projective/ec.hpp Mon Feb 14 14:13:04 2011 +0200
- +++ b/include/ec/GF2X/projective/ec.hpp Thu Feb 17 14:03:07 2011 +0200
- @@ -8,6 +8,9 @@
- #include <memory>
- #include "ec/GF2X/affine/ec.hpp"
- +#include "ec/GF2X/projective/ec_precomputations.hpp"
- +#include "ec/GF2X/projective/ec_comb.hpp"
- +
- namespace ECGF2X
- {
- @@ -27,20 +30,16 @@
- GF2X Z;
- const EC & __EC;
- -
- +
- bool __isZeroPoint;
- - bool __isPrecomputed;
- -
- - /* ------ FIXED POINT PRECOMPUTATION DATA ------- */
- - /* TODO: Make precomputations duplications in copy constructor */
- - EC_Point ** __precomputations;
- - int __precomputations_window;
- - const unsigned char * __point_as_byte[2048];
- - unsigned int __point_byte_size;
- - /* ---------------------------------------------- */
- +
- + EC_Point_Precomputations * __precomputations;
- public:
- -
- +
- + /* Create point from point and it's precomputations */
- + EC_Point(const EC_Point & Point,
- + EC_Point_Precomputations_Logic * Precomputations);
- EC_Point(const GF2X &X, const GF2X &Y, const GF2X &Z, const EC & __EC); // Projective
- EC_Point(const EC_Point & Point); // Same point in same field
- @@ -53,13 +52,17 @@
- public:
- bool precompute(void);
- + inline bool isPrecomputed() const
- + { if (__precomputations)
- + return __precomputations->isReady();
- + else
- + return false; }
- - inline bool isPrecomputed() const
- - { return __isPrecomputed; }
- -
- -
- inline bool isZero() const
- { return __isZeroPoint; }
- +
- + inline void setZero()
- + { __isZeroPoint = true; }
- EC_Point & operator= (const EC_Point & Y);
- EC_Point operator+ (const EC_Point & Y) const;
- @@ -110,11 +113,16 @@
- class EC : public Affine::EC
- {
- - const EC_Point G; // Base point in GF2X/Projective
- -
- + EC_Point G; // Base point in GF2X/Projective
- + // Can be precomputed
- +
- + const Affine::EC_Point G_a;
- +
- public:
- inline const EC_Point & getBasePoint() const
- { return G; }
- + inline const Affine::EC_Point & getAffineBasePoint() const
- + { return G_a; }
- public:
- /* This EC curve class exists only for precomputations.
- diff -r c8a83ff13320 include/ec/GF2X/projective/ec_comb.hpp
- --- /dev/null Thu Jan 01 00:00:00 1970 +0000
- +++ b/include/ec/GF2X/projective/ec_comb.hpp Thu Feb 17 14:03:07 2011 +0200
- @@ -0,0 +1,52 @@
- +#pragma once
- +
- +#include <NTL/GF2X.h>
- +#include <NTL/ZZ.h>
- +#include <NTL/ZZ_p.h>
- +
- +#include <ostream>
- +#include <memory>
- +
- +#include "ec/GF2X/affine/ec.hpp"
- +
- +namespace ECGF2X
- +{
- + namespace Projective
- + {
- + using NTL::GF2X;
- + using NTL::GF2XModulus;
- + using NTL::ZZ;
- +
- + class EC_Point_Precomputations_Comb : public EC_Point_Precomputations_Logic
- + {
- + const long __precomputations_window;
- + const long __precomputations_elements;
- +
- + Affine::EC_Point ** __precomputations;
- +
- + const long __precomputations_max_size;
- + const long __precomputations_portion;
- +
- + private:
- + EC_Point_Precomputations_Comb(EC_Point_Precomputations_Comb & Source);
- +
- + public:
- + EC_Point_Precomputations_Comb(); /* Empty precomputations. Totaly useless */
- + EC_Point_Precomputations_Comb(const EC_Point & Source);
- + ~EC_Point_Precomputations_Comb();
- +
- + public:
- + void Multiply(EC_Point & P,
- + const ZZ & Y) const;
- + inline bool isReady(void)
- + { return true; }
- +
- +
- + private:
- + long getMulPortions(void) const;
- + const Affine::EC_Point & getPrecomputedForMul(const ZZ & scalar,
- + const long portion) const;
- + };
- + }
- +}
- +
- diff -r c8a83ff13320 include/ec/GF2X/projective/ec_comb_index.hpp
- --- /dev/null Thu Jan 01 00:00:00 1970 +0000
- +++ b/include/ec/GF2X/projective/ec_comb_index.hpp Thu Feb 17 14:03:07 2011 +0200
- @@ -0,0 +1,254 @@
- +#pragma once
- +
- +static const long comb_window = 8;
- +static const long comb_idx[] =
- +{
- + 3, 2, 1,
- + 5, 4, 1,
- + 6, 4, 2,
- + 7, 4, 3,
- + 9, 8, 1,
- + 10, 8, 2,
- + 11, 8, 3,
- + 12, 8, 4,
- + 13, 8, 5,
- + 14, 8, 6,
- + 15, 8, 7,
- + 17, 16, 1,
- + 18, 16, 2,
- + 19, 16, 3,
- + 20, 16, 4,
- + 21, 16, 5,
- + 22, 16, 6,
- + 23, 16, 7,
- + 24, 16, 8,
- + 25, 16, 9,
- + 26, 16, 10,
- + 27, 16, 11,
- + 28, 16, 12,
- + 29, 16, 13,
- + 30, 16, 14,
- + 31, 16, 15,
- + 33, 32, 1,
- + 34, 32, 2,
- + 35, 32, 3,
- + 36, 32, 4,
- + 37, 32, 5,
- + 38, 32, 6,
- + 39, 32, 7,
- + 40, 32, 8,
- + 41, 32, 9,
- + 42, 32, 10,
- + 43, 32, 11,
- + 44, 32, 12,
- + 45, 32, 13,
- + 46, 32, 14,
- + 47, 32, 15,
- + 48, 32, 16,
- + 49, 32, 17,
- + 50, 32, 18,
- + 51, 32, 19,
- + 52, 32, 20,
- + 53, 32, 21,
- + 54, 32, 22,
- + 55, 32, 23,
- + 56, 32, 24,
- + 57, 32, 25,
- + 58, 32, 26,
- + 59, 32, 27,
- + 60, 32, 28,
- + 61, 32, 29,
- + 62, 32, 30,
- + 63, 32, 31,
- + 65, 64, 1,
- + 66, 64, 2,
- + 67, 64, 3,
- + 68, 64, 4,
- + 69, 64, 5,
- + 70, 64, 6,
- + 71, 64, 7,
- + 72, 64, 8,
- + 73, 64, 9,
- + 74, 64, 10,
- + 75, 64, 11,
- + 76, 64, 12,
- + 77, 64, 13,
- + 78, 64, 14,
- + 79, 64, 15,
- + 80, 64, 16,
- + 81, 64, 17,
- + 82, 64, 18,
- + 83, 64, 19,
- + 84, 64, 20,
- + 85, 64, 21,
- + 86, 64, 22,
- + 87, 64, 23,
- + 88, 64, 24,
- + 89, 64, 25,
- + 90, 64, 26,
- + 91, 64, 27,
- + 92, 64, 28,
- + 93, 64, 29,
- + 94, 64, 30,
- + 95, 64, 31,
- + 96, 64, 32,
- + 97, 64, 33,
- + 98, 64, 34,
- + 99, 64, 35,
- + 100, 64, 36,
- + 101, 64, 37,
- + 102, 64, 38,
- + 103, 64, 39,
- + 104, 64, 40,
- + 105, 64, 41,
- + 106, 64, 42,
- + 107, 64, 43,
- + 108, 64, 44,
- + 109, 64, 45,
- + 110, 64, 46,
- + 111, 64, 47,
- + 112, 64, 48,
- + 113, 64, 49,
- + 114, 64, 50,
- + 115, 64, 51,
- + 116, 64, 52,
- + 117, 64, 53,
- + 118, 64, 54,
- + 119, 64, 55,
- + 120, 64, 56,
- + 121, 64, 57,
- + 122, 64, 58,
- + 123, 64, 59,
- + 124, 64, 60,
- + 125, 64, 61,
- + 126, 64, 62,
- + 127, 64, 63,
- + 129, 128, 1,
- + 130, 128, 2,
- + 131, 128, 3,
- + 132, 128, 4,
- + 133, 128, 5,
- + 134, 128, 6,
- + 135, 128, 7,
- + 136, 128, 8,
- + 137, 128, 9,
- + 138, 128, 10,
- + 139, 128, 11,
- + 140, 128, 12,
- + 141, 128, 13,
- + 142, 128, 14,
- + 143, 128, 15,
- + 144, 128, 16,
- + 145, 128, 17,
- + 146, 128, 18,
- + 147, 128, 19,
- + 148, 128, 20,
- + 149, 128, 21,
- + 150, 128, 22,
- + 151, 128, 23,
- + 152, 128, 24,
- + 153, 128, 25,
- + 154, 128, 26,
- + 155, 128, 27,
- + 156, 128, 28,
- + 157, 128, 29,
- + 158, 128, 30,
- + 159, 128, 31,
- + 160, 128, 32,
- + 161, 128, 33,
- + 162, 128, 34,
- + 163, 128, 35,
- + 164, 128, 36,
- + 165, 128, 37,
- + 166, 128, 38,
- + 167, 128, 39,
- + 168, 128, 40,
- + 169, 128, 41,
- + 170, 128, 42,
- + 171, 128, 43,
- + 172, 128, 44,
- + 173, 128, 45,
- + 174, 128, 46,
- + 175, 128, 47,
- + 176, 128, 48,
- + 177, 128, 49,
- + 178, 128, 50,
- + 179, 128, 51,
- + 180, 128, 52,
- + 181, 128, 53,
- + 182, 128, 54,
- + 183, 128, 55,
- + 184, 128, 56,
- + 185, 128, 57,
- + 186, 128, 58,
- + 187, 128, 59,
- + 188, 128, 60,
- + 189, 128, 61,
- + 190, 128, 62,
- + 191, 128, 63,
- + 192, 128, 64,
- + 193, 128, 65,
- + 194, 128, 66,
- + 195, 128, 67,
- + 196, 128, 68,
- + 197, 128, 69,
- + 198, 128, 70,
- + 199, 128, 71,
- + 200, 128, 72,
- + 201, 128, 73,
- + 202, 128, 74,
- + 203, 128, 75,
- + 204, 128, 76,
- + 205, 128, 77,
- + 206, 128, 78,
- + 207, 128, 79,
- + 208, 128, 80,
- + 209, 128, 81,
- + 210, 128, 82,
- + 211, 128, 83,
- + 212, 128, 84,
- + 213, 128, 85,
- + 214, 128, 86,
- + 215, 128, 87,
- + 216, 128, 88,
- + 217, 128, 89,
- + 218, 128, 90,
- + 219, 128, 91,
- + 220, 128, 92,
- + 221, 128, 93,
- + 222, 128, 94,
- + 223, 128, 95,
- + 224, 128, 96,
- + 225, 128, 97,
- + 226, 128, 98,
- + 227, 128, 99,
- + 228, 128, 100,
- + 229, 128, 101,
- + 230, 128, 102,
- + 231, 128, 103,
- + 232, 128, 104,
- + 233, 128, 105,
- + 234, 128, 106,
- + 235, 128, 107,
- + 236, 128, 108,
- + 237, 128, 109,
- + 238, 128, 110,
- + 239, 128, 111,
- + 240, 128, 112,
- + 241, 128, 113,
- + 242, 128, 114,
- + 243, 128, 115,
- + 244, 128, 116,
- + 245, 128, 117,
- + 246, 128, 118,
- + 247, 128, 119,
- + 248, 128, 120,
- + 249, 128, 121,
- + 250, 128, 122,
- + 251, 128, 123,
- + 252, 128, 124,
- + 253, 128, 125,
- + 254, 128, 126,
- + 255, 128, 127,
- + 257, 256, 1
- +};
- diff -r c8a83ff13320 include/ec/GF2X/projective/ec_precomputations.hpp
- --- /dev/null Thu Jan 01 00:00:00 1970 +0000
- +++ b/include/ec/GF2X/projective/ec_precomputations.hpp Thu Feb 17 14:03:07 2011 +0200
- @@ -0,0 +1,88 @@
- +#pragma once
- +
- +#include <NTL/GF2X.h>
- +#include <NTL/ZZ.h>
- +#include <NTL/ZZ_p.h>
- +
- +#include <ostream>
- +#include <memory>
- +
- +#include "ec/GF2X/affine/ec.hpp"
- +
- +namespace ECGF2X
- +{
- + namespace Projective
- + {
- + using NTL::GF2X;
- + using NTL::GF2XModulus;
- + using NTL::ZZ;
- +
- + class EC;
- + class EC_Point;
- +
- + class EC_Point_Precomputations_Logic
- + {
- + public:
- + EC_Point_Precomputations_Logic() {};
- + virtual ~EC_Point_Precomputations_Logic() {};
- + virtual bool isReady(void) = 0;
- + virtual void Multiply(EC_Point & P,
- + const ZZ & Y) const = 0;
- + };
- +
- + class EC_Point_Precomputations
- + {
- + private:
- + long * __ref_counter;
- + const bool __can_be_used;
- +
- + EC_Point_Precomputations_Logic * __logic;
- +
- + protected:
- + inline long * get(void)
- + { std::cout << "GET" << std::endl; (* __ref_counter) ++ ; return __ref_counter; }
- +
- + inline long * drop (void)
- + { std::cout << "DROP" << std::endl; (* __ref_counter) -- ; return __ref_counter; }
- +
- + inline bool isLastUser (void)
- + { return ( * __ref_counter) == 1; }
- +
- + public:
- + EC_Point_Precomputations * operator= (EC_Point_Precomputations * Source)
- + {
- + std::cout << "operator= on precomputations" << std::endl;
- + Source->get(); return Source; }
- +
- + public:
- + bool isReady(void) const
- + { return __can_be_used; }
- +
- + private:
- + EC_Point_Precomputations();
- +
- + public:
- + EC_Point_Precomputations(EC_Point_Precomputations_Logic * Logic)
- + : __ref_counter(new long(0)),
- + __can_be_used(true),
- + __logic(Logic) {}
- +
- + EC_Point_Precomputations(EC_Point_Precomputations & Source)
- + : __ref_counter(Source.get()),
- + __can_be_used(true),
- + __logic(Source.__logic) {}
- +
- + ~EC_Point_Precomputations() { drop(); if (isLastUser()) delete __logic; }
- +
- + public:
- + void Multiply(EC_Point & P,
- + const ZZ & Y) const
- + {
- + if (__logic)
- + __logic->Multiply(P, Y);
- + else throw;
- + }
- +
- + };
- + }
- +}
- diff -r c8a83ff13320 src/ec/GF2X/projective/ec.cpp
- --- a/src/ec/GF2X/projective/ec.cpp Mon Feb 14 14:13:04 2011 +0200
- +++ b/src/ec/GF2X/projective/ec.cpp Thu Feb 17 14:03:07 2011 +0200
- @@ -1,4 +1,5 @@
- #include "ec/GF2X/projective/ec.hpp"
- +#include "ec/GF2X/projective/ec_comb_index.hpp"
- #include "ec/GF2X/affine/ec.hpp"
- #include "ec/GF2X/affine/utils.hpp"
- @@ -46,36 +47,72 @@
- Lopez_Dahab_Mixed_Addition(EC_Point & P1,
- const Affine::EC_Point & P2)
- {
- + std::cout << "MIXED ADDITION" << std::endl;
- +
- + const GF2X & a = P1.getEC().getA();
- + const GF2X & b = P1.getEC().getB();
- const GF2XModulus & P = P1.getEC().getModulus();
- - const GF2X & a = P1.getEC().getA();
- - /* -------------------------------------- */
- - const GF2X Z2 = SqrMod(P1.Z, P);
- - const GF2X aZ2 = MulMod(a, Z2, P);
- - /* -------------------------------------- */
- - const GF2X A = MulMod(P2.getY(), Z2, P) + P1.Y;
- - const GF2X B = MulMod(P2.getX(), P1.Z, P) + P1.X;
- - const GF2X C = MulMod(P1.Z, B, P);
- + const GF2X Z2 = SqrMod(P1.Z, P);
- + const GF2X aZ2 = IsOne(a) ? Z2 : MulMod(a, Z2, P);
- +
- + /* ------------------------------------- */
- +
- + const GF2X & X_2 = P2.getX();
- + const GF2X & Y_2 = P2.getY();
- + const GF2X & Z_1 = P1.getZ();
- + const GF2X & X_1 = P1.getX();
- + const GF2X & Y_1 = P1.getY();
- +
- + /* ------------------------------------- */
- +
- + const GF2X A = MulMod(Y_2, Z2, P) + Y_1;
- + const GF2X B = MulMod(X_2, Z_1, P) + X_1;
- +
- + if (IsZero(B))
- + {
- + std::cout << "Try to double point" << std::endl;
- +
- + P1.X = X_2;
- + P1.Y = Y_2;
- + P1.Z = GF2X(0, 1);
- +
- + Lopez_Dahab_Double(P1.X,
- + P1.Y,
- + P1.Z,
- + a,
- + b,
- + P);
- +
- + // std::cout << "X3: " << P1.X << std::endl;
- + // std::cout << "Y3: " << P1.Y << std::endl;
- + // std::cout << "Z3: " << P1.Z << std::endl;
- +
- + return;
- + }
- +
- + const GF2X C = MulMod(Z_1, B, P);
- const GF2X D = MulMod(SqrMod(B, P),
- - C + aZ2,
- + (C + aZ2),
- P);
- - const GF2X E = MulMod(A, C, P);
- -
- - SqrMod(P1.Z, C, P);
- - /* P1.Z == Z_3 */
- - add(P1.X, SqrMod(A, P), D);
- - P1.X += E;
- - /* P1.X == X_3 */
- + const GF2X Z_3 = SqrMod(C, P);
- + const GF2X E = MulMod(A, C, P);
- + const GF2X X_3 = SqrMod(A, P) + D + E;
- + const GF2X F = MulMod(X_2, Z_3, P) + X_3;
- + const GF2X G = MulMod(X_2 + Y_2,
- + SqrMod(Z_3, P),
- + P);
- + const GF2X Y_3 = MulMod(E+Z_3, F, P) + G;
- - const GF2X F = MulMod(P2.getX(), P1.Z, P) + P1.X;
- - const GF2X G = MulMod(SqrMod(P1.Z, P),
- - (P2.getX() + P2.getY()),
- - P);
- -
- - P1.Y = MulMod(E + P1.Z, F, P) + G;
- + // std::cout << "X3: " << X_3 << std::endl;
- + // std::cout << "Y3: " << Y_3 << std::endl;
- + // std::cout << "Z3: " << Z_3 << std::endl;
- - /* P1 == P3 */
- + P1.X = X_3;
- + P1.Y = Y_3;
- + P1.Z = Z_3;
- }
- +
- /* LNCS. 2000 / 1977, 10.1.1.75.402 */
- inline void
- Lopez_Dahab_Addition(EC_Point & P1,
- @@ -120,32 +157,38 @@
- {
- R += S;
- }
- -
- +
- S += S;
- }
- P = R;
- }
- +
- }
- Projective::EC_Point
- toProjective(const Affine::EC_Point & Point,
- const Projective::EC & EC)
- {
- -
- - return EC.create(Point.getX(), Point.getY(),
- - GF2X(0, 1));
- + if (Point.isZero())
- + return EC.create();
- + else
- + return EC.create(Point.getX(), Point.getY(),
- + GF2X(0, 1));
- }
- -
- +
- Affine::EC_Point
- toAffine(const Projective::EC_Point & Point,
- const Affine::EC & EC)
- {
- +
- + std::cout << "toAffine" << std::endl;
- + std::cout << "Z: " << Point.getZ() << std::endl;
- +
- const GF2X & P = EC.getModulus();
- const GF2X iZ = InvMod(Point.getZ(), P);
- -
- return EC.create(MulMod(Point.getX(), iZ, P),
- MulMod(Point.getY(), SqrMod(iZ, P), P));
- }
- @@ -160,32 +203,65 @@
- Y(Point.Y),
- Z(Point.Z),
- __EC(Point.__EC),
- - __isZeroPoint(Point.__isZeroPoint),
- - __isPrecomputed(false),
- - __precomputations(NULL),
- - __precomputations_window(0)
- -{}
- + __isZeroPoint(Point.__isZeroPoint)
- +{
- + __precomputations = Point.__precomputations ?
- + ( Point.__precomputations->isReady() ?
- + Point.__precomputations : NULL) : NULL;
- +
- + std::cout << "Just Copy" << std::endl;
- +}
- EC_Point::EC_Point(const EC_Point & Point, bool isZero)
- : X(isZero ? GF2X() : Point.X),
- Y(isZero ? GF2X() : Point.Y),
- Z(isZero ? GF2X() : Point.Z),
- __EC(Point.__EC),
- - __isZeroPoint(isZero),
- - __isPrecomputed(false)
- + __isZeroPoint(isZero)
- +{
- + if (! isZero && Point.__precomputations)
- + {
- + __precomputations = Point.__precomputations->isReady() ?
- + Point.__precomputations : NULL;
- + }
- +
- +}
- -{}
- -
- +EC_Point::EC_Point(const EC_Point & Point, EC_Point_Precomputations_Logic * comp)
- + : X(Point.X),
- + Y(Point.Y),
- + Z(Point.Z),
- + __EC(Point.__EC),
- + __isZeroPoint(Point.__isZeroPoint)
- +{
- + if (comp->isReady())
- + {
- + __precomputations = new EC_Point_Precomputations(comp);
- + }
- +
- +
- + std::cout << "Copy + Attach precomputations" << std::endl;
- + std::cout << "__precomputations = " << __precomputations << std::endl;
- +}
- EC_Point::EC_Point(const GF2X &X,
- const GF2X &Y,
- const GF2X &Z, const EC & __EC)
- : X(X), Y(Y), Z(Z), __EC(__EC),
- __isZeroPoint(false),
- - __isPrecomputed(false)
- + __precomputations(NULL)
- {
- +
- + std::cout << X << std::endl;
- + std::cout << Y << std::endl;
- + std::cout << Z << std::endl;
- + std::cout << "IS ON CURVE? " << std::endl;
- +
- if (! _IsOnCurve())
- + {
- throw;
- + }
- +
- }
- EC_Point::EC_Point(const EC & __EC)
- @@ -194,11 +270,18 @@
- Z(GF2X()),
- __EC(__EC),
- __isZeroPoint(true),
- - __isPrecomputed(false)
- + __precomputations(NULL)
- {}
- EC_Point::~EC_Point()
- -{}
- +{
- + if (__precomputations)
- + {
- + std::cout << "__precomputations at: " << __precomputations << std::endl;
- + delete __precomputations;
- + }
- +
- +}
- /* Lopez - Dahab */
- @@ -246,6 +329,11 @@
- this->Y = Y.getY();
- this->Z = Y.getZ();
- this->__isZeroPoint = false;
- + this->__precomputations = Y.__precomputations ?
- + ( Y.__precomputations->isReady() ?
- + Y.__precomputations : NULL)
- + : NULL;
- +
- }
- return *this;
- @@ -294,6 +382,8 @@
- __retval+= _Y;
- + delete __retval.__precomputations;
- +
- return __retval;
- }
- @@ -311,7 +401,15 @@
- }
- /* Doubling couldn't be */
- - Lopez_Dahab_Mixed_Addition(*this, _Y);
- + if ((IsOne(__EC.getA())) || (IsZero(__EC.getA())))
- + Lopez_Dahab_Mixed_Addition(*this, _Y);
- + else
- + {
- + /* FIXME ADD GENERIC ADDITIONS */
- + abort();
- +
- + }
- +
- return;
- }
- @@ -320,42 +418,36 @@
- {
- if (isPrecomputed())
- {
- + std::cout << "PRECOMPUTATIONS VERSION" << std::endl;
- + __precomputations->Multiply(*this, Y);
- }
- else
- + {
- + std::cout << "STANDARD VERSION" << std::endl;
- Right_To_Left_Multiplication(*this, Y);
- + }
- +
- return;
- }
- EC_Point EC_Point::operator* (const ZZ & Y) const
- {
- EC_Point __retval(*this);
- +
- + __retval*= Y;
- - __retval*= Y;
- + delete __retval.__precomputations;
- return __retval;
- }
- -bool EC_Point::precompute(void)
- -{
- - __precomputations_window = 1 * sizeof(char) * 8;
- -
- - __precomputations = new EC_Point *[1 << __precomputations_window];
- -
- - for (unsigned int i = 0; i < (1 << __precomputations_window); i++)
- - {
- - char bytes[4096]; // FIXME
- - }
- -
- -
- - return true;
- -}
- -
- -
- /* ----------------------- EC ---------------------------------- */
- EC::EC(const Affine::EC & EC)
- - : Affine::EC(EC), G(toProjective(EC.getBasePoint(), *this))
- + : Affine::EC(EC),
- + G(toProjective(EC.getBasePoint(), *this)),
- + G_a(EC.getBasePoint())
- {}
- EC::~EC()
- diff -r c8a83ff13320 src/ec/GF2X/projective/ec_comb.cpp
- --- /dev/null Thu Jan 01 00:00:00 1970 +0000
- +++ b/src/ec/GF2X/projective/ec_comb.cpp Thu Feb 17 14:03:07 2011 +0200
- @@ -0,0 +1,154 @@
- +#include "ec/GF2X/projective/ec.hpp"
- +#include "ec/GF2X/projective/ec_comb_index.hpp"
- +
- +#include "ec/GF2X/affine/ec.hpp"
- +#include "ec/GF2X/affine/utils.hpp"
- +
- +using namespace ECGF2X;
- +using namespace ECGF2X::Projective;
- +
- +static inline Affine::EC_Point wdouble(const Affine::EC_Point & input,
- + long window)
- +{
- + Affine::EC_Point r(input);
- + std::cout << "wdouble(" << window << ")" << std::endl;
- +
- + for (long i=0; i<window; i++)
- + {
- + r += r;
- + }
- +
- + return r;
- +}
- +
- +EC_Point_Precomputations_Comb::EC_Point_Precomputations_Comb()
- + : __precomputations_window(0),
- + __precomputations_elements(0),
- + __precomputations(NULL),
- + __precomputations_max_size(0),
- + __precomputations_portion(0)
- +{}
- +
- +EC_Point_Precomputations_Comb::EC_Point_Precomputations_Comb(const EC_Point & Source)
- + : __precomputations_window(comb_window),
- + __precomputations_elements(1 << __precomputations_window),
- + __precomputations(new Affine::EC_Point * [ __precomputations_elements ]),
- + __precomputations_max_size(((NumBits(Source.getEC().getModulus()) +
- + comb_window - 1) / comb_window) *
- + comb_window),
- + __precomputations_portion(__precomputations_max_size / __precomputations_window)
- +{
- + Affine::EC_Point asAffine(toAffine(Source, Source.getEC()));
- +
- + /* Fill 0, 1, 2 */
- + /* -------------------------------------------------------- */
- + __precomputations[0] = new Affine::EC_Point(asAffine.getEC()); // Zero
- + __precomputations[1] = new Affine::EC_Point(asAffine); // This point
- + /* ----------L----------------------------------------- */
- +
- + Affine::EC_Point * prev = __precomputations[1];
- +
- + for (long i = 1; i<__precomputations_window; i++)
- + {
- + __precomputations[1<<i] =
- + new Affine::EC_Point(
- + wdouble(*prev,
- + __precomputations_portion));
- +
- + prev = __precomputations[1<<i];
- + }
- +
- + for (long i = 0; i<__precomputations_elements - __precomputations_window - 1; i++)
- + {
- + long t_idx = comb_idx[i*3];
- + long f_idx = comb_idx[i*3 + 1];
- + long s_idx = comb_idx[i*3 + 2];
- + // std::cout << "B: " << t_idx << " = " << f_idx << " + " << s_idx << std::endl;
- +
- + __precomputations[t_idx] = new Affine::EC_Point(*__precomputations[f_idx] +
- + *__precomputations[s_idx]);
- +
- + // std::cout << "P: " << t_idx << " :: " << *__precomputations_own[t_idx] << std::endl;
- +
- + }
- +}
- +
- +EC_Point_Precomputations_Comb::~EC_Point_Precomputations_Comb()
- +{
- +
- + std::cout << "DELETE PRECOMPUTATIONS" << std::endl;
- +
- + for (long i = 0; i< __precomputations_elements; i++)
- + {
- + delete __precomputations[i];
- +
- + __precomputations[i] = NULL;
- + }
- +
- + delete [] __precomputations;
- +}
- +
- +long EC_Point_Precomputations_Comb::getMulPortions(void) const
- +{
- + return __precomputations_portion;
- +}
- +
- +
- +/* Buffer -- NTL -- Big endian */
- +static inline long comb_build_index(const ZZ & Y,
- + const long window,
- + const long portions,
- + const long iteration)
- +{
- + long index = 0x0;
- +
- + for (long i = 0; i<window; i++)
- + {
- + index |=
- + bit(Y, i*portions + iteration) << i;
- + // std::cout << "Get bit: " << ( i*portions + iteration ) << std::endl;
- + }
- +
- + // std::cout << "idx: " << index << std::endl;
- + return index;
- +}
- +
- +
- +const Affine::EC_Point &
- +EC_Point_Precomputations_Comb::getPrecomputedForMul(const ZZ & scalar,
- + const long portion) const
- +{
- + if (NumBits(scalar) > __precomputations_max_size)
- + {
- + std::cout << "Couldn't use precomputations!" << std::endl;
- +
- + throw;
- + }
- +
- + return * __precomputations[ comb_build_index(scalar,
- + __precomputations_window,
- + __precomputations_portion,
- + __precomputations_portion - portion - 1) ];
- +}
- +
- +
- +/* TODO: Check, that Multiply compatible with point ? */
- +void
- +EC_Point_Precomputations_Comb::Multiply(EC_Point & P,
- + const ZZ & Y) const
- +{
- + std::cout << "USING MIXED COMBO" << std::endl;
- +
- + P.setZero();
- +
- + for (long i = 0;
- + i < getMulPortions();
- + i++)
- + {
- + P += P;
- + P += getPrecomputedForMul(Y, i);
- + }
- +
- + std::cout << "Is P On curve? " << P._IsOnCurve() << std::endl;
- +}
- +
- diff -r c8a83ff13320 src/examples/basis.cpp
- --- a/src/examples/basis.cpp Mon Feb 14 14:13:04 2011 +0200
- +++ b/src/examples/basis.cpp Thu Feb 17 14:03:07 2011 +0200
- @@ -23,8 +23,12 @@
- const EC_Point G = EC.getBasePoint();
- const Projective::EC_Point G_p = EC_p.getBasePoint();
- + ZZ seed;
- + seed += time(NULL);
- + SetSeed(seed);
- +
- +
- ZZ k;
- -
- EC.generate_random(k);
- cout << "Original: " << G << endl;
- @@ -32,5 +36,19 @@
- cout << "O*k: " << G * k << endl;
- cout << "P*k => O: " << toAffine( G_p * k, EC ) << endl;
- + cout << "Try to precompute" << endl;
- +
- + Projective::EC_Point_Precomputations_Comb comb(G_p);
- +
- + Projective::EC_Point G_pp(G_p, & comb);
- +
- + cout << "Try to multiply" << endl;
- +
- + cout << "Is computations ready? " << comb.isReady() << endl;
- +
- + cout << "Is basepoint powered with precomputations? " << G_pp.isPrecomputed() << endl;
- +
- + cout << "PP*k => O:" << toAffine(G_pp * k, EC) << endl;
- +
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement