JPBC库(基于配对的密码学)入门和避坑指南

视频地址:https://www.bilibili.com/video/BV1o5411Y77r/

1. JPBC简介

Java Pairing-Based Cryptography Library
对PBC库(C语言)的Java封装
常用于基于配对的密码学算法仿真(身份基加密、属性基加密等)

2. JPBC环境

JPBC: http://gas.dia.unisa.it/projects/jpbc/download.html
JDK: https://www.oracle.com/java/technologies/javase-jdk14-downloads.html
IntelliJ IDEA: https://www.jetbrains.com/idea/download/#section=windows

Eclipse: https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/2020-06/R/eclipse-java-2020-06-R-win32-x86_64.zip

3. JPBC使用

3.1 基本使用

  • 创建项目,添加jar包依赖
  • 引用库 (import,部分IDE支持自动导入)
  • 生成Ppairing实例(从文件导入或者自选参数生成)
  • 验证双线性 e ( g a , g b ) = e ( g , g ) a b e(g^a,g^b) = e(g,g)^{ab} e(ga,gb)=e(g,g)ab

测试代码

    // 一、基于特定椭圆曲线类型生成Pairing实例
    // 1.从文件导入椭圆曲线参数
    Pairing bp = PairingFactory.getPairing("a.properties");

    // 2.自定义曲线参数
    // int rBits = 160;
    // int qBits = 512;
    // TypeACurveGenerator pg = new TypeACurveGenerator(rBits, qBits);
    // PairingParameters pp = pg.generate();
    // Pairing bp = PairingFactory.getPairing(pp);

    // 二、选择群上的元素
    Field G1 = bp.getG1();
    Field Zr = bp.getZr();
    Element g = G1.newRandomElement().getImmutable();
    Element a = Zr.newRandomElement().getImmutable();
    Element b = Zr.newRandomElement().getImmutable();

    // 三、计算等式左半部分
    Element ga = g.powZn(a);
    Element gb = g.powZn(b);
    Element egg_ab = bp.pairing(ga,gb);

    // 四、计算等式右半部分
    Element egg = bp.pairing(g,g).getImmutable();
    Element ab = a.mul(b);
    Element egg_ab_p = egg.powZn(ab);

    if (egg_ab.isEqual(egg_ab_p)) {
        System.out.println("yes");
    }
    else {
        System.out.println("No");
    }

3.2 避坑指南(重要)

getImmutable()duplicate()

典型错误示例

    Pairing pairing = PairingFactory.getPairing("a.properties");
    Element g = pairing.getG1().newRandomElement();
    Element a = pairing.getZr().newRandomElement();
    System.out.println("g: " + g);
    Element ga = g.powZn(a); //the value "g" will also be changed to ga
    System.out.println("g: " + g);
    System.out.println("ga: " + ga);

避免方法

  • 定义g的时候使用 getImmutable() 方法
    Element g = pairing.getG1().newRandomElement().getImmutable();
  • 或者使用g的时候使用 duplicate() 方法
    Element ga = g.duplicate().powZn(a);
  • 30
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值