1+ use bls12_381:: { G1Projective , G2Projective , Scalar , pairing, Gt } ;
2+ use ff:: Field ;
3+ use group:: { Group , Curve } ;
4+ use rand:: thread_rng;
5+
6+ pub struct ECAccumulator {
7+ pub sk : Scalar ,
8+ pub j : G2Projective ,
9+ pub alpha : G1Projective ,
10+ pub g1 : G1Projective ,
11+ pub g2 : G2Projective ,
12+ pub g : G1Projective ,
13+ pub h : G1Projective ,
14+ pub k : G1Projective ,
15+ pub z : G1Projective ,
16+ pub e_z_g2 : Gt ,
17+ pub e_z_j : Gt ,
18+ }
19+
20+ impl ECAccumulator {
21+ pub fn setup ( ) -> Self {
22+ let mut rng = thread_rng ( ) ;
23+ let sk = Scalar :: random ( & mut rng) ;
24+ let g1 = G1Projective :: generator ( ) ;
25+ let g2 = G2Projective :: generator ( ) ;
26+ let j = g2 * sk;
27+ let u0 = Scalar :: random ( & mut rng) ;
28+ let alpha = g1 * u0;
29+ let g = G1Projective :: random ( & mut rng) ;
30+ let h = G1Projective :: random ( & mut rng) ;
31+ let k = G1Projective :: random ( & mut rng) ;
32+ let z = G1Projective :: random ( & mut rng) ;
33+ let e_z_g2 = pairing ( & z. to_affine ( ) , & g2. to_affine ( ) ) ;
34+ let e_z_j = pairing ( & z. to_affine ( ) , & j. to_affine ( ) ) ;
35+
36+ ECAccumulator {
37+ sk,
38+ j,
39+ alpha,
40+ g1,
41+ g2,
42+ g,
43+ h,
44+ k,
45+ z,
46+ e_z_g2,
47+ e_z_j,
48+ }
49+ }
50+
51+ pub fn gen_witness ( & self , x : Scalar ) -> G1Projective {
52+ let x_sk_inv = ( x + self . sk ) . invert ( ) . expect ( "x + sk invertible" ) ;
53+ self . alpha * x_sk_inv
54+ }
55+
56+ pub fn del ( & self , x : Scalar ) -> ( G1Projective , Scalar ) {
57+ let delta = x + self . sk ;
58+ let inv = delta. invert ( ) . expect ( "x + sk invertible" ) ;
59+ let new_alpha = self . alpha * inv;
60+ ( new_alpha, delta)
61+ }
62+
63+ pub fn verify_witness ( & self , x : Scalar , witness : G1Projective ) -> bool {
64+ let lhs = pairing ( & self . alpha . to_affine ( ) , & self . g2 . to_affine ( ) ) ;
65+ let g2xj = self . g2 * x + self . j ;
66+ let rhs = pairing ( & witness. to_affine ( ) , & g2xj. to_affine ( ) ) ;
67+ lhs == rhs
68+ }
69+
70+ pub fn update_witness (
71+ & self ,
72+ old_witness : G1Projective ,
73+ x : Scalar ,
74+ delta : Scalar ,
75+ new_alpha : G1Projective ,
76+ ) -> G1Projective {
77+ let term1 = old_witness - new_alpha;
78+ let term2_inv = ( delta - x) . invert ( ) . expect ( "delta - x invertible" ) ;
79+ term1 * term2_inv
80+ }
81+ }
0 commit comments