JPBC库应用之BLS签名
视频地址 https://www.bilibili.com/video/BV1jA41147vt/
BLS签名简单介绍
Initialization
- 生成pairing参数 < G 1 , G T , Z r , g , e G_1, G_T, Z_r, g, e G1,GT,Zr,g,e>
- 选取随机数 x ∈ Z r x \in Z_r x∈Zr 作为私钥
- 计算对应的公钥 g x g^x gx
Singing
签名者拥有的数据包括消息 m m m,私钥 x x x,以及公开的pairing参数
签名者将消息 m m m 的哈希值(哈希算法可以自定义,签名者和验证者一致)映射为一个 G 1 G_1 G1 上的群元素 h h h,并利用私钥计算签名 σ = h x \sigma = h^x σ=hx
Verifying
验证者拥有的数据包括 m , σ , g x m, \sigma, g^x m,σ,gx,以及公开的pairing参数
验证者利用公钥验证签名如下
e ( σ , g ) = ? e ( h , g x ) e(\sigma, g) \overset{?}{=} e(h, g^x) e(σ,g)=?e(h,gx) 即 e ( h x , g ) = ? e ( h , g x ) e(h^x, g)\overset{?}{=}e(h, g^x) e(hx,g)=?e(h,gx)
BLS签名算法实现
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
public class BLS {
public static void main(String[] args){
// Initialization
Pairing bp = PairingFactory.getPairing("a.properties");
Field G1 = bp.getG1();
Field Zr = bp.getZr();
Element g = G1.newRandomElement();
Element x = Zr.newRandomElement();
Element g_x = g.duplicate().powZn(x);
//Signing
String m = "message";
byte[] m_hash = Integer.toString(m.hashCode()).getBytes();
Element h = G1.newElementFromHash(m_hash, 0, m_hash.length);
Element sig = h.duplicate().powZn(x);
//Verification
Element pl = bp.pairing(g, sig);
Element pr = bp.pairing(h, g_x);
if (pl.isEqual(pr))
System.out.println("Yes");
else
System.out.println("No");
}
}