椭圆曲线基点标量多重复制JPair库

时间:2017-11-21 14:01:56

标签: java multiplication elliptic-curve scalar

我必须将两个场元素(f1和f2)与使用Weierstrass方程定义的椭圆曲线上的基点P相乘,即y2 = x3 + ax + b。我知道在标量乘法中,为了得到结果,这个点被添加了很多次。我有一个问题,如果标量乘法((f1 + f2)* P)等于(f1 * P + f2 * P)?我已经在Jpair库中编写了代码,但我得到了不同的观点。而且,当我使用BigInt数字时,我得到同样的观点。有人能告诉我为什么在进行标量乘法时我会得到两个不同点?

import java.util.Random;

import uk.ac.ic.doc.jpair.api.FieldElement;
import uk.ac.ic.doc.jpair.pairing.BigInt;
import uk.ac.ic.doc.jpair.pairing.EllipticCurve;
import uk.ac.ic.doc.jpair.pairing.Fp;
import uk.ac.ic.doc.jpair.pairing.Point;


public class testCode {

    public static void main(String [] args)
    {

        Random rnd = new Random();
        //initialise the finite field Fp given a prime number p
        BigInt p = new BigInt("8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791",10);
        Fp field = new Fp(p);

        //System.out.println("order" +field.getOrder());

        //initialise the elliptic curve. The curve can be specified given the field and the coefficient a and b 
        //of the Weierstrass equation: 
        //y2 = x3 +ax +b
        //For example, in the case of the supersingular curve //y2 = x3 + x, a = 1 b = 0 ,  
        EllipticCurve ec = new EllipticCurve (field,BigInt.ONE,BigInt.ZERO);


        //You also need to know the group order r and the cofactor
        BigInt r = new BigInt("730750818665451621361119245571504901405976559617",10);
        BigInt cof = new BigInt("12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776",10);

        /*------------------------------------------------BEGIN SETUP ---------------------------------------------------------*/
        Point ecBasePoint = ec.getBasePoint(rnd, r, cof);

        // scALAR MULITPLICATION
        Point p1 = ec.multiply(ecBasePoint, BigInt.valueOf(2));
        Point p2 = ec.multiply(ecBasePoint, BigInt.valueOf(3));
        Point p3 = ec.add(p1, p2); 
        Point p4 = ec.multiply(ecBasePoint, BigInt.valueOf(5));

        if(p4.equals(p3))
        {
            System.out.println("P3 equals P4");

        }
        else{System.out.println("P3 NOT equals P4");}

        // pick two random elements in  Zp
        FieldElement f1 = field.randomElement(rnd);
        FieldElement f2 = field.randomElement(rnd);

        Point p5 = ec.multiply(ecBasePoint, (BigInt) f1);
        Point p6 = ec.multiply(ecBasePoint, (BigInt) f2);
        Point p7 = ec.add(p5, p6);
        Point p8 = ec.multiply(ecBasePoint, (BigInt) field.add(f1, f2));
        if(p8.equals(p7))
        {
            System.out.println("P7 equals P8");

        }
        else{System.out.println("P7 NOT equals P8");}
    }
}

--------------新代码-----

import java.util.Random;
import uk.ac.ic.doc.jpair.api.FieldElement;
import uk.ac.ic.doc.jpair.pairing.BigInt;
import uk.ac.ic.doc.jpair.pairing.EllipticCurve;
import uk.ac.ic.doc.jpair.pairing.Fp;
import uk.ac.ic.doc.jpair.pairing.Point;
import uk.ac.ic.doc.jpair.ibeNew.Util;
/*
 * ECC CPABE Scheme coding
 * 
 * */
public class eccThird {

    static final String charSet ="UTF-8";

    public static void main(String [] args)
    {

        Random rnd = new Random();
        //initialise the finite field Fp given a prime number p
        BigInt p = new BigInt("8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791",10);
        Fp field = new Fp(p);

        System.out.println("order" +field.getOrder());

        //initialise the elliptic curve. The curve can be specified given the field and the coefficient a and b 
        //of the Weierstrass equation: 
        //y2 = x3 +ax +b
        //For example, in the case of the supersingular curve //y2 = x3 + x, a = 1 b = 0 ,  
        EllipticCurve ec = new EllipticCurve (field,BigInt.ONE,BigInt.ZERO);


        //You also need to know the group order r and the cofactor
        BigInt r = new BigInt("730750818665451621361119245571504901405976559617",10);
        BigInt cof = new BigInt("12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776",10);

        // attribute universe = U = {"attribute1","attribute2","attribute3","attribute4"}
        // Obtaining hashes of attributes
        String attribute1 ="Attribute1";
        String attribute2 ="Attribute2";
        String attribute3 ="Attribute3";
        String attribute4 ="Attribute4";

        byte[] bAttribute1 = null;
        byte[] bAttribute2 = null;
        byte[] bAttribute3 = null;
        byte[] bAttribute4 = null;


        try {
            bAttribute1 = attribute1.getBytes(charSet);
            bAttribute2 = attribute2.getBytes(charSet);
            bAttribute3 = attribute3.getBytes(charSet);
            bAttribute4 = attribute4.getBytes(charSet);

        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("Byte Attribute 1" +bAttribute1);

        // Hashes attribute bytes to field elements
        FieldElement attribute1H4 =Util.hashToField(bAttribute1, field);
        FieldElement attribute2H4 =Util.hashToField(bAttribute2, field);
        FieldElement attribute3H4 =Util.hashToField(bAttribute3, field);
        FieldElement attribute4H4 =Util.hashToField(bAttribute4, field);        


        int numberAttributes = 5;

        /*------------------------------------------------BEGIN SETUP ---------------------------------------------------------*/
        Point ecBasePoint = ec.getBasePoint(rnd, r, cof);
        // pick three random private keys alpa, k1, and k2 in Zp
        FieldElement alphaSK = field.randomElement(rnd);
        FieldElement k1SK = field.randomElement(rnd);
        FieldElement k2SK = field.randomElement(rnd);

        // Generate public key components P_i, U_i, and  V_i for all attributes


        // generate alpha^i components

        FieldElement alphaSKZero  = field.pow(alphaSK, BigInt.ZERO);
        FieldElement alphaSKOne   =  field.pow(alphaSK, BigInt.ONE);
        FieldElement alphaSKTwo   = field.pow(alphaSK, BigInt.valueOf(2));
        FieldElement alphaSKThree =  field.pow(alphaSK, BigInt.valueOf(3));
        FieldElement alphaSKFour  =  field.pow(alphaSK, BigInt.valueOf(4));
        FieldElement alphaSKFive =  field.pow(alphaSK, BigInt.valueOf(5));



        // Generate P_i components
        Point PiZero = ec.multiply(ecBasePoint, (BigInt) alphaSKZero);
        Point PiOne = ec.multiply(ecBasePoint, (BigInt) alphaSKOne);
        Point PiTwo = ec.multiply(ecBasePoint, (BigInt) alphaSKTwo);
        Point PiThree = ec.multiply(ecBasePoint, (BigInt) alphaSKThree);
        Point PiFour = ec.multiply(ecBasePoint, (BigInt) alphaSKFour);
        Point PiFive = ec.multiply(ecBasePoint, alphaSKFive);

        // Generate U_i components
        Point UiZero = ec.multiply(PiZero, (BigInt) k1SK);
        Point UiOne = ec.multiply(PiOne, (BigInt) k1SK);
        Point UiTwo = ec.multiply(PiTwo, (BigInt) k1SK);
        Point UiThree = ec.multiply(PiThree, (BigInt) k1SK);
        Point UiFour = ec.multiply(PiFour, (BigInt) k1SK);
        Point UiFive = ec.multiply(PiFive, (BigInt)k1SK);


        // Generate V_i components
        Point ViZero = ec.multiply(PiZero, (BigInt) k2SK);
        Point ViOne = ec.multiply(PiOne, (BigInt) k2SK);
        Point ViTwo = ec.multiply(PiTwo, (BigInt) k2SK);
        Point ViThree = ec.multiply(PiThree, (BigInt) k2SK);
        Point ViFour = ec.multiply(PiFour, (BigInt) k2SK);
        Point ViFive = ec.multiply(PiFive, (BigInt) k2SK);  


        //System.out.println("Alpha alphaSKTwo = "+ PiZero);

    /*------------------------------------------------ END SETUP ---------------------------------------------------------*/

    /*------------------------------------------------ BEGIN KEYGEN ---------------------------------------------------------*/
        // generating KeyGen Polynomial
        FieldElement keyGenPoly = field.add(alphaSK, attribute3H4);
        // two random numbers in Zp field
        FieldElement ru = field.randomElement(rnd);
        FieldElement tu = field.randomElement(rnd);
        if(ru.equals(tu))
        {
            System.out.println("ru and tu are same");
        }
        else { System.out.println("ru and tu are not same"); }

        //compute su
        FieldElement suTemp1 = field.multiply(k2SK, ru);
        FieldElement suTemp2 = field.subtract(field.inverse(keyGenPoly),suTemp1);
        FieldElement su = field.multiply(field.inverse(k1SK),suTemp2);
        //computing u1 and u2
        FieldElement u1Temp = field.multiply(k1SK, tu);
        FieldElement u1 = field.add(ru, u1Temp);

        FieldElement u2Temp = field.multiply(k2SK, tu);
        FieldElement u2 = field.subtract(su, u2Temp);

        //System.out.println("KeyGen Polynomial "+ u1);

        // checking if field.inverse(keyGenPoly) = k1su + k2ru

        FieldElement test1 = field.add(field.multiply(k1SK, su), field.multiply(k2SK, ru));

        if(test1.equals(field.inverse(keyGenPoly)))
        {
            /*System.out.println("Poly Inverse = " + field.inverse(keyGenPoly));
            System.out.println("Condition = " + test1); */
            System.out.println("KeyGen Condition Satisfied ");

        }


    /*------------------------------------------------ END KEYGEN ---------------------------------------------------------*/
}

}

0 个答案:

没有答案