Skip to content

Commit

Permalink
rm commented old code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Feb 25, 2024
1 parent 2315f4f commit bf32995
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 190 deletions.
20 changes: 3 additions & 17 deletions encoding/fft/fft_fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ func (fs *FFTSettings) simpleFT(vals []fr.Element, valsOffset uint64, valsStride
v.Mul(jv, r)
tmp.Set(&last)
last.Add(&tmp, &v)

//bls.MulModFr(&v, jv, r)
//bls.CopyFr(&tmp, &last)
//bls.AddModFr(&last, &tmp, &v)
}
out[i].Set(&last)
//bls.CopyFr(&out[i], &last)
}
}

Expand All @@ -75,16 +70,10 @@ func (fs *FFTSettings) _fft(vals []fr.Element, valsOffset uint64, valsStride uin
x.Set(&out[i])
y.Set(&out[i+half])

//bls.CopyFr(&x, &out[i])
//bls.CopyFr(&y, &out[i+half])
root := &rootsOfUnity[i*rootsOfUnityStride]
yTimesRoot.Mul(&y, root)
out[i].Add(&x, &yTimesRoot)
out[i+half].Sub(&x, &yTimesRoot)

//bls.MulModFr(&yTimesRoot, &y, root)
//bls.AddModFr(&out[i], &x, &yTimesRoot)
//bls.SubModFr(&out[i+half], &x, &yTimesRoot)
}
}

Expand All @@ -98,11 +87,10 @@ func (fs *FFTSettings) FFT(vals []fr.Element, inv bool) ([]fr.Element, error) {
valsCopy := make([]fr.Element, n)
for i := 0; i < len(vals); i++ {
valsCopy[i].Set(&vals[i])
//bls.CopyFr(&valsCopy[i], &vals[i])

}
for i := uint64(len(vals)); i < n; i++ {
valsCopy[i].SetZero()
//bls.CopyFr(&valsCopy[i], &bls.ZERO)
}
out := make([]fr.Element, n)
if err := fs.InplaceFFT(valsCopy, out, inv); err != nil {
Expand All @@ -121,9 +109,9 @@ func (fs *FFTSettings) InplaceFFT(vals []fr.Element, out []fr.Element, inv bool)
}
if inv {
var invLen fr.Element
//bls.AsFr(&invLen, n)

invLen.SetInt64(int64(n))
//bls.InvModFr(&invLen, &invLen)

invLen.Inverse(&invLen)
rootz := fs.ReverseRootsOfUnity[:fs.MaxWidth]
stride := fs.MaxWidth / n
Expand All @@ -132,9 +120,7 @@ func (fs *FFTSettings) InplaceFFT(vals []fr.Element, out []fr.Element, inv bool)
var tmp fr.Element
for i := 0; i < len(out); i++ {
tmp.Mul(&out[i], &invLen)
//bls.MulModFr(&tmp, &out[i], &invLen)
out[i].Set(&tmp)
//bls.CopyFr(&out[i], &tmp) // TODO: depending on Fr implementation, allow to directly write back to an input
}
return nil
} else {
Expand Down
29 changes: 13 additions & 16 deletions encoding/fft/recover_from_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,46 @@ import (
// unshift poly, in-place. Multiplies each coeff with 1/shift_factor**i
func (fs *FFTSettings) ShiftPoly(poly []fr.Element) {
var shiftFactor fr.Element
//bls.AsFr(&shiftFactor, 5) // primitive root of unity
shiftFactor.SetInt64(int64(5))
var factorPower fr.Element
factorPower.SetOne()
//bls.CopyFr(&factorPower, &ONE)
var invFactor fr.Element
//bls.InvModFr(&invFactor, &shiftFactor)
invFactor.Inverse(&shiftFactor)
var tmp fr.Element
for i := 0; i < len(poly); i++ {
//bls.CopyFr(&tmp, &poly[i])

tmp.Set(&poly[i])

//bls.MulModFr(&poly[i], &tmp, &factorPower)

poly[i].Mul(&tmp, &factorPower)

// TODO: pre-compute all these shift scalars
//bls.CopyFr(&tmp, &factorPower)

tmp.Set(&factorPower)

//bls.MulModFr(&factorPower, &tmp, &invFactor)

factorPower.Mul(&tmp, &invFactor)
}
}

// unshift poly, in-place. Multiplies each coeff with shift_factor**i
func (fs *FFTSettings) UnshiftPoly(poly []fr.Element) {
var shiftFactor fr.Element
//bls.AsFr(&shiftFactor, 5) // primitive root of unity

shiftFactor.SetInt64(int64(5))
var factorPower fr.Element
factorPower.SetOne()
//bls.CopyFr(&factorPower, &bls.ONE)

var tmp fr.Element
for i := 0; i < len(poly); i++ {
tmp.Set(&poly[i])
//bls.CopyFr(&tmp, &poly[i])

poly[i].Mul(&tmp, &factorPower)
//bls.MulModFr(&poly[i], &tmp, &factorPower)

// TODO: pre-compute all these shift scalars
//bls.CopyFr(&tmp, &factorPower)

tmp.Set(&factorPower)
//bls.MulModFr(&factorPower, &tmp, &shiftFactor)

factorPower.Mul(&tmp, &shiftFactor)
}
}
Expand Down Expand Up @@ -104,10 +101,10 @@ func (fs *FFTSettings) RecoverPolyFromSamples(samples []*fr.Element, zeroPolyFn
polyEvaluationsWithZero := make([]fr.Element, len(samples))
for i, s := range samples {
if s == nil {
//bls.CopyFr(&polyEvaluationsWithZero[i], &ZERO)

polyEvaluationsWithZero[i].SetZero()
} else {
//bls.MulModFr(&polyEvaluationsWithZero[i], s, &zeroEval[i])

polyEvaluationsWithZero[i].Mul(s, &zeroEval[i])
}
}
Expand All @@ -133,7 +130,7 @@ func (fs *FFTSettings) RecoverPolyFromSamples(samples []*fr.Element, zeroPolyFn

evalShiftedReconstructedPoly := evalShiftedPolyWithZero
for i := 0; i < len(evalShiftedReconstructedPoly); i++ {
//bls.DivModFr(&evalShiftedReconstructedPoly[i], &evalShiftedPolyWithZero[i], &evalShiftedZeroPoly[i])

evalShiftedReconstructedPoly[i].Div(&evalShiftedPolyWithZero[i], &evalShiftedZeroPoly[i])
}
shiftedReconstructedPoly, err := fs.FFT(evalShiftedReconstructedPoly, true)
Expand Down
34 changes: 16 additions & 18 deletions encoding/fft/zero_poly.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,29 @@ func (fs *FFTSettings) makeZeroPolyMulLeaf(dst []fr.Element, indices []uint64, d
// zero out the unused slots
for i := len(indices) + 1; i < len(dst); i++ {
dst[i].SetZero()
//bls.CopyFr(&dst[i], &ZERO)

}
//bls.CopyFr(&dst[len(indices)], &bls.ONE)

dst[len(indices)].SetOne()
var negDi fr.Element

var frZero fr.Element
frZero.SetZero()

for i, v := range indices {
//bls.SubModFr(&negDi, &bls.ZERO, &fs.ExpandedRootsOfUnity[v*domainStride])

negDi.Sub(&frZero, &fs.ExpandedRootsOfUnity[v*domainStride])
//bls.CopyFr(&dst[i], &negDi)

dst[i].Set(&negDi)
if i > 0 {
//bls.AddModFr(&dst[i], &dst[i], &dst[i-1])

dst[i].Add(&dst[i], &dst[i-1])
for j := i - 1; j > 0; j-- {
dst[j].Mul(&dst[j], &negDi)
//bls.MulModFr(&dst[j], &dst[j], &negDi)
//bls.AddModFr(&dst[j], &dst[j], &dst[j-1])

dst[j].Add(&dst[j], &dst[j-1])
}
//bls.MulModFr(&dst[0], &dst[0], &negDi)

dst[0].Mul(&dst[0], &negDi)
}
}
Expand All @@ -80,11 +79,11 @@ func (fs *FFTSettings) makeZeroPolyMulLeaf(dst []fr.Element, indices []uint64, d
// Copy all of the values of poly into out, and fill the remainder of out with zeroes.
func padPoly(out []fr.Element, poly []fr.Element) {
for i := 0; i < len(poly); i++ {
//bls.CopyFr(&out[i], &poly[i])

out[i].Set(&poly[i])
}
for i := len(poly); i < len(out); i++ {
//bls.CopyFr(&out[i], &bls.ZERO)

out[i].SetZero()
}
}
Expand Down Expand Up @@ -137,15 +136,15 @@ func (fs *FFTSettings) reduceLeaves(scratch []fr.Element, dst []fr.Element, ps [
for i := uint64(0); i < last; i++ {
p := ps[i]
for j := 0; j < len(p); j++ {
//bls.CopyFr(&pPadded[j], &p[j])

pPadded[j].Set(&p[j])
}
if err := fs.InplaceFFT(pPadded, pEval, false); err != nil {
return nil, err
}
for j := uint64(0); j < n; j++ {
mulEvalPs[j].Mul(&mulEvalPs[j], &pEval[j])
//bls.MulModFr(&mulEvalPs[j], &mulEvalPs[j], &pEval[j])

}
}
if err := fs.InplaceFFT(mulEvalPs, dst, true); err != nil {
Expand Down Expand Up @@ -279,27 +278,26 @@ func (fs *FFTSettings) ZeroPolyViaMultiplication(missingIndices []uint64, length

func EvalPolyAt(dst *fr.Element, coeffs []fr.Element, x *fr.Element) {
if len(coeffs) == 0 {
//CopyFr(dst, &ZERO)

dst.SetZero()
return
}
if x.IsZero() {
//CopyFr(dst, &coeffs[0])

dst.Set(&coeffs[0])
return
}
// Horner's method: work backwards, avoid doing more than N multiplications
// https://en.wikipedia.org/wiki/Horner%27s_method
var last fr.Element
//CopyFr(&last, &coeffs[len(coeffs)-1])

last.Set(&coeffs[len(coeffs)-1])
var tmp fr.Element
for i := len(coeffs) - 2; i >= 0; i-- {
tmp.Mul(&last, x)
//MulModFr(&tmp, &last, x)
//AddModFr(&last, &tmp, &coeffs[i])

last.Add(&tmp, &coeffs[i])
}
//CopyFr(dst, &last)

dst.Set(&last)
}
4 changes: 1 addition & 3 deletions encoding/kzg/prover/parametrized_prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (g *ParametrizedProver) Encode(inputFr []fr.Element) (*bn254.G1Affine, *bn2
if err != nil {
return nil, nil, nil, nil, nil, err
}
//lowDegreeCommitment := bls.LinCombG2(g.Srs.G2[:len(poly.Coeffs)], poly.Coeffs)

config := ecc.MultiExpConfig{}

Expand Down Expand Up @@ -207,7 +206,6 @@ func (p *ParametrizedProver) ProveAllCosetThreads(polyFr []fr.Element, numChunks

fmt.Printf("mult-th %v, msm %v,fft1 %v, fft2 %v,\n", t0.Sub(begin), t1.Sub(t0), t2.Sub(t1), t3.Sub(t2))

//rb.ReverseBitOrderG1Point(proofs)
return proofs, nil
}

Expand Down Expand Up @@ -251,7 +249,7 @@ func (p *ParametrizedProver) GetSlicesCoeff(polyFr []fr.Element, dimE, j, l uint

toeV := make([]fr.Element, 2*dimE-1)
for i := uint64(0); i < dim; i++ {
//bls.CopyFr(&toeV[i], &polyFr[m-(j+i*l)])

toeV[i].Set(&polyFr[m-(j+i*l)])
}

Expand Down
22 changes: 0 additions & 22 deletions encoding/kzg/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,28 +243,6 @@ func (g *Prover) newProver(params encoding.EncodingParams) (*ParametrizedProver,
}, nil
}

// get Fiat-Shamir challenge
// func createFiatShamirChallenge(byteArray [][32]byte) *fr.Element {
// alphaBytesTmp := make([]byte, 0)
// for i := 0; i < len(byteArray); i++ {
// for j := 0; j < len(byteArray[i]); j++ {
// alphaBytesTmp = append(alphaBytesTmp, byteArray[i][j])
// }
// }
// alphaBytes := crypto.Keccak256(alphaBytesTmp)
// alpha := new(fr.Element)
// fr.ElementSetBytes(alpha, alphaBytes)
//
// return alpha
// }

// invert the divisor, then multiply
// func polyFactorDiv(dst *fr.Element, a *fr.Element, b *fr.Element) {
// // TODO: use divmod instead.
// var tmp fr.Element
// bls.InvModFr(&tmp, b)
// bls.MulModFr(dst, &tmp, a)
// }

// Detect the precomputed table from the specified directory
// the file name follow the name convention of
Expand Down
13 changes: 6 additions & 7 deletions encoding/kzg/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func GenerateTestingSetup(secret string, n uint64) ([]bn254.G1Affine, []bn254.G2
s1Out := make([]bn254.G1Affine, n)
s2Out := make([]bn254.G2Affine, n)
for i := uint64(0); i < n; i++ {
//bls.MulG1(&s1Out[i], &GenG1, &sPow)

s1Out[i].ScalarMultiplication(&GenG1, sPow.BigInt(new(big.Int)))

//bls.MulG2(&s2Out[i], &GenG2, &sPow)

s2Out[i].ScalarMultiplication(&GenG2, sPow.BigInt(new(big.Int)))

sPow.Mul(&sPow, &s)
Expand All @@ -75,11 +75,11 @@ func WriteGeneratorPoints(n uint64) error {
if err != nil {
return err
}
//bls.SetFr(&s, secret)


var sPow fr.Element
sPow.SetOne()
//bls.CopyFr(&sPow, &bls.ONE)


g1f, err := os.Create("g1.point." + ns)
if err != nil {
Expand All @@ -95,15 +95,14 @@ func WriteGeneratorPoints(n uint64) error {
}
g2w := bufio.NewWriter(g2f)

//delimiter := [1]byte{'\n'}


start := time.Now()
for i := uint64(0); i < n; i++ {
var s1Out bn254.G1Affine
var s2Out bn254.G2Affine
s1Out.ScalarMultiplication(&GenG1, sPow.BigInt(new(big.Int)))
//bls.MulG1(&s1Out, &bls.GenG1, &sPow)
//bls.MulG2(&s2Out, &bls.GenG2, &sPow)

s2Out.ScalarMultiplication(&GenG2, sPow.BigInt(new(big.Int)))

g1Byte := s1Out.Bytes()
Expand Down
7 changes: 2 additions & 5 deletions encoding/kzg/verifier/batch_commit_equivalence.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@ func CreateRandomnessVector(g1commits []bn254.G1Affine, g2commits []bn254.G2Affi
n := len(g1commits)

if len(g1commits) != len(g2commits) {
return nil, errors.New("Inconsistent number of blobs for g1 and g2")
return nil, errors.New("inconsistent number of blobs for g1 and g2")
}

randomsFr := make([]fr.Element, n)
//bn254.CopyFr(&randomsFr[0], &r)

randomsFr[0].Set(&r)

// power of r
for j := 0; j < n-1; j++ {
randomsFr[j+1].Mul(&randomsFr[j], &r)
//bn254.MulModFr(&randomsFr[j+1], &randomsFr[j], &r)
}

return randomsFr, nil
Expand Down Expand Up @@ -105,10 +104,8 @@ func (group *Verifier) BatchVerifyCommitEquivalence(commitmentsPair []Commitment
return err
}

//lhsG1 := bn254.LinCombG1(g1commits, randomsFr)
lhsG2 := &kzg.GenG2

//rhsG2 := bn254.LinCombG2(g2commits, randomsFr)
var rhsG2 bn254.G2Affine
_, err = rhsG2.MultiExp(g2commits, randomsFr, ecc.MultiExpConfig{})
if err != nil {
Expand Down
Loading

0 comments on commit bf32995

Please sign in to comment.