-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwistededwards.go
65 lines (59 loc) · 2.49 KB
/
twistededwards.go
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
// twistededwards package provides helper circuit functions to transform points
// (x, y) from the TwistedEdwards format to Reduced TwistedEdwards format and
// vice versa, over BabyJubJub curve. These functions are required because
// Gnark uses the Reduced TwistedEdwards formula while Iden3 uses the standard
// TwistedEdwards formula.
//
// Read more about this here: https://github.com/bellesmarta/baby_jubjub
package twistededwards
import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/algebra/emulated/sw_bn254"
"github.com/consensys/gnark/std/math/emulated"
)
var (
scalingFactor = frontend.Variable("6360561867910373094066688120553762416144456282423235903351243436111059670888")
emulatedScalingFactor = emulated.Element[sw_bn254.ScalarField]{
Limbs: []frontend.Variable{7817090900423792488, 8405395627841593623, 3205086078052995447, 1013295966202553675},
}
)
func FromRTEtoTE(api frontend.API, x, y frontend.Variable) (frontend.Variable, frontend.Variable) {
// compute negF = -f mod p
negF := api.Neg(scalingFactor)
// compute the inverse of negF in the field
negFInv := api.Inverse(negF)
// compute xTE = x / (-f)
xTE := api.Mul(x, negFInv)
return xTE, y
}
func FromTEtoRTE(api frontend.API, x, y frontend.Variable) (frontend.Variable, frontend.Variable) {
// compute negF = -f mod p
negF := api.Neg(scalingFactor)
// compute xRTE = x * (-f)
xRTE := api.Mul(x, negF)
return xRTE, y
}
func FromEmulatedRTEtoTE(api frontend.API, x, y emulated.Element[sw_bn254.ScalarField]) (emulated.Element[sw_bn254.ScalarField], emulated.Element[sw_bn254.ScalarField], error) {
field, err := emulated.NewField[sw_bn254.ScalarField](api)
if err != nil {
return emulated.Element[sw_bn254.ScalarField]{}, emulated.Element[sw_bn254.ScalarField]{}, err
}
// compute negF = -f mod p
negF := field.Neg(&emulatedScalingFactor)
// compute the inverse of negF in the field
negFInv := field.Inverse(negF)
// compute xTE = x / (-f)
xTE := field.Mul(&x, negFInv)
return *xTE, y, nil
}
func FromEmulatedTEtoRTE(api frontend.API, x, y emulated.Element[sw_bn254.ScalarField]) (emulated.Element[sw_bn254.ScalarField], emulated.Element[sw_bn254.ScalarField], error) {
field, err := emulated.NewField[sw_bn254.ScalarField](api)
if err != nil {
return emulated.Element[sw_bn254.ScalarField]{}, emulated.Element[sw_bn254.ScalarField]{}, err
}
// compute negF = -f mod p
negF := field.Neg(&emulatedScalingFactor)
// compute xRTE = x * (-f)
xRTE := field.Mul(&x, negF)
return *xRTE, y, nil
}