1
+ import { Field } from '@noble/curves/abstract/modular' ;
2
+ import { bls12_381 } from '@noble/curves/bls12-381' ;
3
+
4
+ const PrimeFieldModulus = 52435875175126190479447740508185965837690552500527637822603658699938581184513n ;
5
+ const Fr = Field ( PrimeFieldModulus , 255 ) ;
6
+ const G1 = bls12_381 . G1
7
+ const basePoint = G1 . ProjectivePoint ;
8
+
9
+ // class Poly {
10
+ // coeff:bigint[] = []
11
+
12
+ // constructor(coeff:bigint[]){
13
+ // this.coeff = coeff
14
+ // }
15
+
16
+ // static constant(coeff: bigint){
17
+ // return new Poly([coeff])
18
+ // }
19
+
20
+ // static interpolate(dataPoints:[bigint,bigint][]){
21
+ // if(dataPoints.length == 0){
22
+ // return Poly.constant(0n)
23
+ // }
24
+
25
+ // let poly = new Poly([dataPoints[0][1]]);
26
+ // let minusS0 = dataPoints[0][0];
27
+ // minusS0 = Fr.neg(minusS0);
28
+ // console.log(dataPoints[1][1].toString(16));
29
+ // let base = new Poly([minusS0, 1n]);
30
+
31
+ // for(const [x, y] of dataPoints.slice(1)){
32
+ // let diff = y;
33
+ // console.log("diff", diff.toString(16));
34
+ // diff = Fr.sub(diff, poly.evaluate(x));
35
+ // let base_val = base.evaluate(x);
36
+ // diff = Fr.mul(diff, Fr.inv(base_val));
37
+ // base.mulFr(diff);
38
+ // poly.add(base);
39
+ // base.mul(new Poly([Fr.neg(x), 1n]));
40
+ // }
41
+ // return poly;
42
+ // }
43
+
44
+ // add(rhs: Poly){
45
+ // const len = this.coeff.length;
46
+ // const rhs_len = rhs.coeff.length;
47
+
48
+ // if(rhs_len > len){
49
+ // const fillFr = new Array(rhs_len - len).fill(0n);
50
+ // this.coeff = this.coeff.concat(fillFr);
51
+ // }
52
+
53
+ // this.coeff = this.coeff.map((coeff, index) => {
54
+ // return Fr.add(coeff,rhs.coeff[index])
55
+ // })
56
+
57
+ // this.removeZeroes();
58
+ // }
59
+
60
+ // mul(rhs:Poly){
61
+ // if(this.isZero() || rhs.isZero()){
62
+ // return Poly.constant(0n)
63
+ // }
64
+ // const len = this.coeff.length + rhs.coeff.length - 1;
65
+ // let coeff = new Array(len).fill(0n);
66
+ // let tmp = 0n;
67
+ // let i, j = 0;
68
+ // for(i = 0; i < this.coeff.length; i++){
69
+ // for(j = 0; j < rhs.coeff.length; j++){
70
+ // tmp = Fr.mul(this.coeff[i], rhs.coeff[j]);
71
+ // coeff[i+j] = Fr.add(coeff[i+j], tmp);
72
+ // }
73
+ // }
74
+ // this.coeff = coeff;
75
+ // }
76
+
77
+ // mulFr(rhs:bigint){
78
+ // if(rhs === 0n){
79
+ // this.coeff = [0n];
80
+ // } else {
81
+ // this.coeff = this.coeff.map((coeff) => {
82
+ // return Fr.mul(coeff, rhs)
83
+ // });
84
+ // }
85
+ // }
86
+
87
+ // evaluate(x: bigint){
88
+ // let result = this.coeff[this.coeff.length-1];
89
+ // for(let i = this.coeff.length-2; i >= 0; i--){
90
+ // result = Fr.add(Fr.mul(result, x),this.coeff[i]);
91
+ // }
92
+ // return result
93
+ // }
94
+
95
+ // removeZeroes(){
96
+ // for(let i = this.coeff.length-1; i >= 0; i--){
97
+ // if(this.coeff[i] === 0n){
98
+ // this.coeff.pop()
99
+ // } else {
100
+ // break;
101
+ // }
102
+ // }
103
+ // }
104
+
105
+ // isZero(){
106
+ // return !this.coeff.some(coeff => !(coeff === 0n))
107
+ // }
108
+
109
+ // }
110
+ export const interpolate = ( dataPoints :[ bigint , bigint ] [ ] , index : bigint ) => {
111
+ let result = 0n ; // term result
112
+ for ( let i = 0 ; i < dataPoints . length ; i ++ ) {
113
+ let term = dataPoints [ i ] [ 1 ] ; // y value
114
+ for ( let j = 0 ; j < dataPoints . length ; j ++ ) {
115
+ if ( i != j ) {
116
+ const diff_index = Fr . sub ( index , dataPoints [ j ] [ 0 ] ) ;
117
+ const diff_i = Fr . sub ( dataPoints [ i ] [ 0 ] , dataPoints [ j ] [ 0 ] ) ;
118
+ term = Fr . mul ( term , diff_index ) ;
119
+ term = Fr . div ( term , diff_i ) ;
120
+ }
121
+ }
122
+ result = Fr . add ( result , term ) ;
123
+ }
124
+ return result ;
125
+ }
126
+
127
+ export function getPairFromSharesAndIndexes ( dataPoints :[ bigint , bigint ] [ ] ) {
128
+ const privateKey = interpolate ( dataPoints , 0n ) ;
129
+ const pubKey = basePoint . BASE . multiply ( privateKey ) . toHex ( ) ;
130
+ return { privKey :privateKey . toString ( 16 ) , pubKey} ;
131
+ }
132
+
133
+ // (()=>{
134
+ // let shares = ["6f569904269aef285688a23a4991ba590b9fe4471b0a2d64ec0cca311b6bd78a",
135
+ // "0fa02b2b0a69946bc83ee96d0df6e6656d3d87306b29872ae638e559c30d63e0",
136
+ // "1eb6fbab0706e58e5659eca4276d38253d9ee615858a93c6c7952141a3367485"];
137
+ // let indexes = ["01", "02", "03"];
138
+ // const shareAndIndexes:[bigint,bigint][] = shares.map((share, index) =>
139
+ // [BigInt(parseInt(indexes[index], 16)), BigInt("0x" + share)]);
140
+ // const poly = Poly.interpolate(shareAndIndexes.slice(0,3));
141
+ // console.log(getPairFromSharesAndIndexes(shareAndIndexes));
142
+ // console.log(poly.coeff.map(coeff => coeff.toString(16)));
143
+ // const secret = poly.evaluate(0n);
144
+ // const pubkeyKey = basePoint.BASE.multiply(secret);
145
+ // console.log(pubkeyKey.toHex());
146
+ // console.log(secret.toString(16));
147
+ // // privateKey: 55fef690085ffb339ac366fbc6fa03f5714ab553952fce76d910cfc9ac51cf81
148
+ // // pubkey: 92ccef9a7fe47e1df29d24aca9e11d00d2405a2459431349398eed46bb411115026ddb984a09b0de9cf59d1f66e5cbb7
149
+ // })()
0 commit comments