Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ecckrypt;
- import java.math.BigInteger;
- /**
- *
- * @author Student
- */
- class PECC
- {
- BigInteger x,y,a,b,q;
- PECC(BigInteger x, BigInteger y, BigInteger a, BigInteger b, BigInteger q)
- {
- this.a = a;
- this.b = b;
- this.x = x;
- this.y = y;
- this.q = q;
- }
- }
- public class ECCKRYPT {
- /**
- * @param args the command line arguments
- */
- public static BigInteger ODW(BigInteger m, BigInteger n)
- {
- BigInteger a, ap, s, sp, t, tp, r, q;
- BigInteger L1 = new BigInteger("1"), L0=new BigInteger("0");
- a = m;
- ap = n;
- s = L1;
- sp=L0;
- t=L0;
- tp = L1;
- while(ap.compareTo(L0) !=0)
- {
- q = a.divide(ap);
- r = a; a=ap; ap=r.subtract(q.multiply(ap));
- r = s; s=sp; sp=r.subtract(q.multiply(sp));
- r = t; t=tp; tp=r.subtract(q.multiply(tp));
- }
- if (t.compareTo(L0)>0) r=t;
- else r=t.add(n);
- return r;
- }
- public static PECC DOD(PECC P1, PECC P2)
- {
- BigInteger x1, x2, x3, y1, y2, y3, lam, q, dwa, trzy;
- x1 = P1.x;
- x2 = P2.x;
- x3 = new BigInteger("0");
- y1 = P1.y;
- y2 = P2.y;
- y3 = new BigInteger("0");
- dwa = new BigInteger("2");
- trzy = new BigInteger("3");
- q = P1.q;
- if (x1.compareTo(x2)!=0)
- {
- lam = x2.subtract(x1);
- lam = ODW(lam, P1.q);
- lam = lam.multiply(y2.subtract(y1));
- lam = lam.mod(q);
- x3 = lam.multiply(lam);
- x3 = (x3.subtract(x1)).subtract(x2);
- x3 = x3.mod(q);
- y3 = (x1.subtract(x3)).multiply(lam);
- y3 = y3.subtract(y1);
- y3 = y3.mod(q);
- }
- else
- {
- if (y1.compareTo(y2)==0)
- {
- lam = ODW(dwa.multiply(y1), q);
- BigInteger r;
- r = trzy.multiply(x1.multiply(x1));
- r = r.add(P1.a);
- lam = lam.multiply(r);
- x3 = lam.multiply(lam);
- x3 = x3.subtract(dwa.multiply(x1));
- x3 = x3.mod(q);
- y3 = (x1.subtract(x3)).multiply(lam);
- y3 = y3.subtract(y1);
- y3 = y3.mod(q);
- }
- }
- return new PECC (x3, y3, P1.a, P1.b, q);
- }
- public static PECC MNOZENIE(BigInteger k, PECC P)
- {
- BigInteger i = new BigInteger("1");
- BigInteger jeden = i;
- PECC R = P;
- while(i.compareTo(k) < 0)
- {
- R = DOD(R, P);
- i.add(jeden);
- }
- return R;
- }
- public static BigInteger SLegendrea(BigInteger a, BigInteger p) {
- BigInteger jeden = new BigInteger("1");
- BigInteger dwa = new BigInteger("2");
- return (a.modPow((p.subtract(jeden)).divide(dwa),p));
- }
- public static BigInteger pierw(BigInteger r, BigInteger q){
- BigInteger L1 = new BigInteger("1");
- BigInteger L2 = new BigInteger("2");
- BigInteger r1 = r.divide(L2);
- while((r1.multiply(r1)).compareTo(r)>0)
- r1.subtract(L1);
- return r1;
- }
- void Wyswietlanie (PECC P)
- {
- System.out.print("( "+P.x+" , "+P.y+" )");
- }
- public static void main(String[] args) {
- BigInteger p, q, a, b, L1, L2, L10, x, r1, r2, y;
- int n = 177;
- p = new BigInteger("3");
- p = p.nextProbablePrime();
- L2 = new BigInteger("2");
- L1 = new BigInteger("1");
- L10 = new BigInteger("10");
- q = p.pow(n);
- a = L1;
- b = L2;
- b = b.nextProbablePrime();
- while (b.compareTo(q)>0)
- {
- b = b.divide(L2);
- b = b.nextProbablePrime();
- }
- System.out.println("Rownanie krzywej eleptycznej:\n" +
- "(y^2=x^3+x"+b+") mod "+q);
- do {
- x = q.divide(L10);
- r1 = ((x.pow(3)).add(x)).add(b);
- r1 = r1.mod(q);
- r2 = SLegendrea(r1,q);
- } while (r2.compareTo(L1)!=0);
- y = pierw(r1,q);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement