Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions curve/ecgfp5/point.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ecgfp5

import (
"fmt"

g "github.com/elliottech/poseidon_crypto/field/goldilocks"
gFp5 "github.com/elliottech/poseidon_crypto/field/goldilocks_quintic_extension"
)
Expand Down Expand Up @@ -65,6 +67,73 @@ var (
u: gFp5.FP5_ONE,
t: gFp5.Element{4, 0, 0, 0, 0},
}

GeneratorWindowAffine = []AffinePoint{
{
x: gFp5.Element{12883135586176881569, 4356519642755055268, 5248930565894896907, 2165973894480315022, 2448410071095648785},
u: gFp5.Element{13835058052060938241, 18446744069414584321, 18446744069414584321, 18446744069414584321, 18446744069414584321},
},
{
x: gFp5.Element{16517537419581740386, 6962630169123120981, 12147752690379666704, 16637325971742264607, 2335078582315237010},
u: gFp5.Element{8457587110646932172, 138591869800252458, 3187444967472352324, 18179149801168653736, 9453003655195557048},
},
{
x: gFp5.Element{4546139357324501584, 1393728687664685160, 15208040286522119521, 7903224051455420834, 12463930627278381774},
u: gFp5.Element{16373828487211693378, 5899455736915524900, 17616512450102495476, 17643201028570366669, 2833280130550676525},
},
{
x: gFp5.Element{4341836049185169731, 9111482874850194930, 7798994609726992878, 12619124383509403661, 13047834166950680886},
u: gFp5.Element{3584786391427904733, 1717626083626375072, 16549008311909030594, 17550175197111849143, 18374971670674568416},
},
{
x: gFp5.Element{18121072711119258927, 3394315639035318724, 2648370499809919556, 13348924736921714137, 3428166646246873447},
u: gFp5.Element{9264305576790077869, 7426254234280836405, 5107777768036114824, 9390769538758625122, 9788182195111344062},
},
{
x: gFp5.Element{11080635543643017332, 3122290570793204485, 16632474826839786439, 14883711538614796285, 10396852362099782295},
u: gFp5.Element{14253916706639980511, 15728038457561632290, 3947138785484546318, 4740958322851071718, 17384736114265519442},
},
{
x: gFp5.Element{4763058716218401568, 17879823368956058516, 13578954599286698938, 8634670560943921567, 13706660844700767685},
u: gFp5.Element{3354778288360932917, 13842278303693121409, 4717821645259836467, 7978743897613094276, 10118963888992569394},
},
{
x: gFp5.Element{4026958896735257282, 13595990041314210204, 11499471878438064392, 10019455879458851233, 11986847968355927330},
u: gFp5.Element{14532821659997761913, 9582789969382797985, 3082219099923033594, 2859656980617778370, 3746047816071136016},
},
{
x: gFp5.Element{15935900828168308224, 8668680449802005535, 491315506768012688, 6584881037682113026, 12386385009372860460},
u: gFp5.Element{13217832923050551864, 51671271962049328, 15400792709153778477, 6752203529649104660, 2855313280735340066},
},
{
x: gFp5.Element{8473506523195244465, 2446964921175324878, 17962771942831363202, 6949608686158330138, 9315492999547366751},
u: gFp5.Element{5171814696081600409, 3025466154945175207, 453302446979841822, 14135305892339872079, 2556388051049291052},
},
{
x: gFp5.Element{3960231187580500028, 3695840168764199059, 2914577777792670911, 9249939676680902688, 17553522813502241416},
u: gFp5.Element{3015152305907361949, 10730034543155667220, 3314242046485170944, 1984395553885795852, 13781645774758249860},
},
{
x: gFp5.Element{11575997426281090678, 1534495174840625570, 7539338128385981583, 10393042019577161985, 10667466219175771157},
u: gFp5.Element{16681365912970185037, 11287896019745355117, 11069899752345274504, 15487604769605237513, 13467978440572613228},
},
{
x: gFp5.Element{11192179397773394280, 3555953455665397909, 5346523552109387121, 4514445299325204396, 3932728981135688453},
u: gFp5.Element{5421638117266109845, 204299445119713184, 6067390115784997081, 16191134954342419157, 4139938600224417293},
},
{
x: gFp5.Element{13189785832536261642, 8777097377506996162, 17497140949916325738, 15140279769427597032, 15517274717131999881},
u: gFp5.Element{1040464435413162742, 9262701069034606854, 2990438819650713743, 18129195737333990255, 12490074042478236606},
},
{
x: gFp5.Element{17716508479149156535, 14351380558651795729, 3644546258883003807, 5171318241596472386, 294806796132518330},
u: gFp5.Element{7535225611936271281, 14682077054502188499, 784215514926156349, 5280586574139275596, 14407528916988559545},
},
{
x: gFp5.Element{8681294642569802563, 7751765660802747503, 16382129702876313971, 7447155060842833278, 6859908403876474879},
u: gFp5.Element{9674486254207846385, 5248970165164951259, 3611784478790504991, 18437168019170350173, 3537959913875671086},
},
}
)

func (p ECgFp5Point) Equals(rhs ECgFp5Point) bool {
Expand Down Expand Up @@ -435,6 +504,26 @@ func BatchToAffine(src []ECgFp5Point) []AffinePoint {
return res
}

// To pre-compute G, 2G, 3G...15G
func GenerateG() {
p := GENERATOR_ECgFp5Point
tmp := make([]ECgFp5Point, WIN_SIZE)
tmp[0] = p
for i := 1; i < WIN_SIZE; i++ {
if (i & 1) == 0 {
tmp[i] = tmp[i-1].Add(p)
} else {
tmp[i] = tmp[i>>1].Double()
}
}
res := BatchToAffine(tmp)
for i := 0; i < 16; i++ {
elx := fmt.Sprintf("gFp5.Element{%d, %d, %d, %d, %d}", res[i].x[0], res[i].x[1], res[i].x[2], res[i].x[3], res[i].x[4])
elu := fmt.Sprintf("gFp5.Element{%d, %d, %d, %d, %d}", res[i].u[0], res[i].u[1], res[i].u[2], res[i].u[3], res[i].u[4])
fmt.Printf("AffinePoint{\n x : %s,\n u : %s,\n},\n", elx, elu)
}
}

func (p ECgFp5Point) MakeWindowAffine() []AffinePoint {
tmp := make([]ECgFp5Point, WIN_SIZE)
tmp[0] = p
Expand Down Expand Up @@ -466,3 +555,27 @@ func (r ECgFp5Point) Mul(s ECgFp5Scalar) ECgFp5Point {

return p
}

// Returns G*scalarA + b*scalarB
func MulAddG(b ECgFp5Point, scalarA, scalarB ECgFp5Scalar) ECgFp5Point {
winA := GeneratorWindowAffine
winB := b.MakeWindowAffine()

digitsA := make([]int32, (319+WINDOW)/WINDOW)
scalarA.RecodeSigned(digitsA, int32(WINDOW))
digitsB := make([]int32, (319+WINDOW)/WINDOW)
scalarB.RecodeSigned(digitsB, int32(WINDOW))

n := len(digitsA) // == len(digitsB)

// Top window digit of each scalar, combined into the running point.
p := LookupVarTime(winA, digitsA[n-1]).ToPoint()
p = p.AddAffine(LookupVarTime(winB, digitsB[n-1]))

for i := n - 2; i >= 0; i-- {
p.SetMDouble(uint32(WINDOW)) // ×2^WINDOW, shared by both scalars
p = p.AddAffine(LookupVarTime(winA, digitsA[i]))
p = p.AddAffine(LookupVarTime(winB, digitsB[i]))
}
return p
}
28 changes: 14 additions & 14 deletions field/goldilocks/goldilocks_plonky2.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (z GoldilocksField) ToCanonicalUint64() uint64 {
// lhs, rhs in non-canonical form
func AddF(lhs, rhs GoldilocksField) GoldilocksField {
sum, over := bits.Add64(uint64(lhs), uint64(rhs), 0)
sum, over = bits.Add64(sum, over*EPSILON, 0)
sum += EPSILON * over // this can't overflow
sum, over = bits.Add64(sum, (EPSILON & -over), 0)
sum += (EPSILON & -over) // this can't overflow

return GoldilocksField(sum)
}
Expand All @@ -65,7 +65,7 @@ func AddF(lhs, rhs GoldilocksField) GoldilocksField {
func AddCanonicalUint64(lhs GoldilocksField, rhs uint64) GoldilocksField {
sum, over := bits.Add64(uint64(lhs), rhs, 0)
// if overflowed, sum := lhs + rhs - 2^64 => sum + EPSILON = lhs + rhs - 2^64 + 2^32 -1 = lhs + rhs - ORDER < ORDER + 2^64 - ORDER = 2^64, so there is no overflow in this case.
return GoldilocksField(sum + over*EPSILON)
return GoldilocksField(sum + (EPSILON & -over))
}

func DoubleF(lhs GoldilocksField) GoldilocksField {
Expand All @@ -75,8 +75,8 @@ func DoubleF(lhs GoldilocksField) GoldilocksField {
// lhs, rhs in non-canonical form
func SubF(lhs, rhs GoldilocksField) GoldilocksField {
diff, borrow := bits.Sub64(uint64(lhs), uint64(rhs), 0)
diff, borrow = bits.Sub64(diff, borrow*EPSILON, 0)
diff -= EPSILON * borrow // this can't underflow
diff, borrow = bits.Sub64(diff, (EPSILON & -borrow), 0)
diff -= (EPSILON & -borrow) // this can't underflow

return GoldilocksField(diff)
}
Expand All @@ -89,12 +89,12 @@ func MulF(lhs, rhs GoldilocksField) GoldilocksField {
x_hi_lo := x_hi & EPSILON

t0, borrow := bits.Sub64(x_lo, x_hi_hi, 0)
t0 -= EPSILON * borrow
t0 -= (EPSILON & -borrow)

t1 := x_hi_lo * EPSILON

sum, over := bits.Add64(t0, t1, 0)
t2 := sum + EPSILON*over
t2 := sum + (EPSILON & -over)
return GoldilocksField(t2)
}

Expand All @@ -105,12 +105,12 @@ func SquareF(x GoldilocksField) GoldilocksField {
x_hi_lo := x_hi & EPSILON

t0, borrow := bits.Sub64(x_lo, x_hi_hi, 0)
t0 -= EPSILON * borrow
t0 -= (EPSILON & -borrow)

t1 := x_hi_lo * EPSILON

sum, over := bits.Add64(t0, t1, 0)
t2 := sum + EPSILON*over
t2 := sum + (EPSILON & -over)
return GoldilocksField(t2)
}

Expand All @@ -121,9 +121,9 @@ func MulAccF(self, x, y GoldilocksField) GoldilocksField {
hi += c

t0, borrow := bits.Sub64(lo, hi>>32, 0)
t0 -= EPSILON * borrow
t0 -= (EPSILON & -borrow)
resWrapped, c := bits.Add64(t0, (hi&EPSILON)*EPSILON, 0)
return GoldilocksField(resWrapped + EPSILON*c)
return GoldilocksField(resWrapped + (EPSILON & -c))
}

func ExpPowerOf2(x GoldilocksField, n uint) GoldilocksField {
Expand Down Expand Up @@ -189,20 +189,20 @@ func Reduce96Bit(x UInt128) GoldilocksField {
t1 := x.Hi * EPSILON
resWrapped, carry := bits.Add64(x.Lo, t1, 0)

return GoldilocksField(resWrapped) + GoldilocksField(carry*EPSILON)
return GoldilocksField(resWrapped) + GoldilocksField((EPSILON & -carry))
}

func Reduce128Bit(x UInt128) GoldilocksField {
x_hi_hi := x.Hi >> 32
x_hi_lo := x.Hi & EPSILON

t0, borrow := bits.Sub64(x.Lo, x_hi_hi, 0)
t0 -= EPSILON * borrow
t0 -= (EPSILON & -borrow)

t1 := x_hi_lo * EPSILON

resWrapped, carry := bits.Add64(t0, t1, 0)
t2 := resWrapped + EPSILON*carry
t2 := resWrapped + (EPSILON & -carry)
return GoldilocksField(t2)
}

Expand Down
Loading
Loading