-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61b7a65
commit 38281cb
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// twistededwards package provides helper functions to transform points (x, y) | ||
// from TwistedEdwards to Reduced TwistedEdwards and vice versa. These functions | ||
// are required because Gnark uses the Reduced TwistedEdwards formula while | ||
// Iden3 uses the standard TwistedEdwards formula. | ||
// See https://github.com/bellesmarta/baby_jubjub for more information. | ||
package twistededwards | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/consensys/gnark-crypto/ecc/bn254/fr" | ||
) | ||
|
||
var scalingFactor, _ = new(big.Int).SetString("6360561867910373094066688120553762416144456282423235903351243436111059670888", 10) | ||
|
||
type Point struct { | ||
X, Y *big.Int | ||
} | ||
|
||
// Convert Reduced TwistedEdwards x' to TwistedEdwards: | ||
// | ||
// x = x'/(-f) | ||
// y' = y | ||
func (p *Point) FromRTEtoTE() *Point { | ||
// Step 1: Convert scalingFactor to fr.Element (mod p) | ||
var f fr.Element | ||
f.SetBigInt(scalingFactor) // f = scalingFactor mod p | ||
|
||
// Step 2: Compute negF = -f mod p | ||
var negF fr.Element | ||
negF.Neg(&f) // negF = -f mod p | ||
|
||
// Step 3: Compute the inverse of negF in the field | ||
var negFInv fr.Element | ||
negFInv.Inverse(&negF) // negFInv = (-f)^{-1} mod p | ||
|
||
xTE := new(fr.Element) | ||
xTE.SetBigInt(p.X) | ||
// Step 4: Multiply g.inner.X by negFInv to get xTE | ||
xRTE := new(fr.Element) | ||
xRTE.Mul(xTE, &negFInv) // xTE = g.inner.X * negFInv mod p | ||
|
||
// Step 5: Convert xTE and g.inner.Y to *big.Int | ||
xRTEBigInt := new(big.Int) | ||
xRTE.BigInt(xRTEBigInt) | ||
return &Point{ | ||
X: xRTEBigInt, // x = x' / (-f) | ||
Y: p.Y, // y' = y | ||
} | ||
} | ||
|
||
// Convert TwistedEdwards to Reduced TwistedEdwards: | ||
// | ||
// x' = x*(-f) | ||
// y = y' | ||
func (p *Point) FromTEtoRTE() *Point { | ||
// Step 1: Convert scalingFactor to fr.Element (mod p) | ||
var f fr.Element | ||
f.SetBigInt(scalingFactor) // f = scalingFactor mod p | ||
|
||
// Step 2: Compute negF = -f mod p | ||
var negF fr.Element | ||
negF.Neg(&f) // negF = -f mod p | ||
|
||
// Step 4: Multiply g.inner.X by negF to get xTE | ||
xRTE := new(fr.Element).SetBigInt(p.X) | ||
xTE := new(fr.Element) | ||
xTE.Mul(xRTE, &negF) // xTE = g.inner.X * -f mod p | ||
|
||
// Step 5: Convert xTE and g.inner.Y to *big.Int | ||
xTEBigInt := new(big.Int) | ||
xTE.BigInt(xTEBigInt) | ||
return &Point{ | ||
X: xTEBigInt, // x' = x * (-f) | ||
Y: p.Y, // y = y' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package twistededwards | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestXxx(t *testing.T) { | ||
p := new(Point) | ||
p.X, _ = new(big.Int).SetString("20284931487578954787250358776722960153090567235942462656834196519767860852891", 10) | ||
p.Y, _ = new(big.Int).SetString("21185575020764391300398134415668786804224896114060668011215204645513129497221", 10) | ||
|
||
expectedRTE, _ := new(big.Int).SetString("5730906301301611931737915251485454905492689746504994962065413628158661689313", 10) | ||
pPrime := p.FromTEtoRTE() | ||
if pPrime.X.Cmp(expectedRTE) != 0 { | ||
t.Errorf("Expected %v, got %v", expectedRTE, pPrime.X) | ||
} | ||
pPrimePrime := pPrime.FromRTEtoTE() | ||
if pPrimePrime.X.Cmp(p.X) != 0 || pPrimePrime.Y.Cmp(p.Y) != 0 { | ||
t.Errorf("Expected %v, got %v", p, pPrimePrime) | ||
} | ||
} |