-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoly-commitment.mjs
164 lines (122 loc) · 3.92 KB
/
poly-commitment.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { bls12_381 } from '@noble/curves/bls12-381'
import * as polynomials from './polynomials.mjs'
import { getRandomElement } from './random.mjs'
const field = bls12_381.fields.Fr
// For now, just find powers of a random secret in the most simple way
// Later I plan to read the snarkJS' ptau files and get the powers from the ceremony there
const secret = getRandomElement(field)
const SRS_LENGTH = 64
let srsG1 = [ bls12_381.G1.ProjectivePoint.BASE ]
let srsG2 = [ bls12_381.G2.ProjectivePoint.BASE ]
for(let i = 1; i < SRS_LENGTH; i++) {
srsG1.push(srsG1[srsG1.length - 1].multiply(secret))
srsG2.push(srsG2[srsG2.length - 1].multiply(secret))
}
export function demoPolynomialCommitment() {
const coefficients = [10n, 20n, 30n, 40n]
const commitment = commit(coefficients)
// Single
const z = 77n
const y = polynomials.evaluate(coefficients, z, field)
let proof = prove(coefficients, z)
let verificationResult = verify(commitment, proof, z, y)
console.log("KZG single verification:", verificationResult)
// Multiple
const zs = [77n, 88n, 99n]
const ys = zs.map((z) => polynomials.evaluate(coefficients, z, field))
let proofMultiple = proveMultiple(coefficients, zs)
let verificationResultMultiple = verifyMultiple(commitment, proofMultiple, zs, ys)
console.log("KZG multiple verification:", verificationResultMultiple)
}
export function commit(coefficients) {
let result = bls12_381.G1.ProjectivePoint.ZERO
for (let i = 0; i < coefficients.length; i++) {
result = result.add(srsG1[i].multiply(coefficients[i]))
}
return result
}
export function prove(coefficients, z) {
const quotientPolynomial = getQuotientPolynomial(coefficients, z)
return commit(quotientPolynomial)
}
function getQuotientPolynomial(coefficients, z) {
const originalPol = coefficients
const identityPol = [0n, 1n]
const y = polynomials.evaluate(originalPol, z, field)
const yPol = [y]
const zPol = [z]
return polynomials.divide(
polynomials.sub(originalPol, yPol, field),
polynomials.sub(identityPol, zPol, field),
field
).quotient
}
export function verify(commitment, proof, z, y) {
const lhs = bls12_381.pairing(
proof,
srsG2[1].subtract(bls12_381.G2.ProjectivePoint.BASE.multiply(z))
)
const rhs = bls12_381.pairing(
commitment.subtract(bls12_381.G1.ProjectivePoint.BASE.multiply(y)),
bls12_381.G2.ProjectivePoint.BASE
)
return bls12_381.fields.Fp12.eql(lhs, rhs)
}
export function proveMultiple(coefficients, zs) {
const quotientPolynomial = getQuotientPolynomialMultiple(coefficients, zs)
return commit(quotientPolynomial)
}
function getQuotientPolynomialMultiple(coefficients, zs) {
const originalPol = coefficients
const ys = zs.map((z) => polynomials.evaluate(originalPol, z, field))
const iPol = polynomials.lagrange(
zs,
ys,
field
)
let zPol = [1n]
zs.forEach((z) => {
zPol = polynomials.multiply(
zPol,
[field.neg(z), 1n],
field
)
})
return polynomials.divide(
polynomials.sub(originalPol, iPol, field),
zPol,
field
).quotient
}
export function verifyMultiple(commitment, proof, zs, ys) {
const iPol = polynomials.lagrange(
zs,
ys,
field
)
let zPol = [1n]
zs.forEach((z) => {
zPol = polynomials.multiply(
zPol,
[field.neg(z), 1n],
field
)
})
let evalZatStimesGen2 = bls12_381.G2.ProjectivePoint.ZERO
for(let pos = 0; pos < zPol.length; pos++) {
evalZatStimesGen2 = evalZatStimesGen2.add(srsG2[pos].multiply(zPol[pos]))
}
let evalIatStimesGen1 = bls12_381.G1.ProjectivePoint.ZERO
for(let pos = 0; pos < iPol.length; pos++) {
evalIatStimesGen1 = evalIatStimesGen1.add(srsG1[pos].multiply(iPol[pos]))
}
const lhs = bls12_381.pairing(
proof,
evalZatStimesGen2 // srsG2[1].subtract(bls12_381.G2.ProjectivePoint.BASE.multiply(z))
)
const rhs = bls12_381.pairing(
commitment.subtract(evalIatStimesGen1), // commitment.subtract(bls12_381.G1.ProjectivePoint.BASE.multiply(y)),
bls12_381.G2.ProjectivePoint.BASE
)
return bls12_381.fields.Fp12.eql(lhs, rhs)
}