Skip to content

Commit 9ada798

Browse files
authored
Merge pull request #497 from ConsenSys/perf/ecdsa
perf: in-circuit ECDSA on secp256k1
2 parents 80c6bf7 + 98cd61c commit 9ada798

7 files changed

Lines changed: 456 additions & 49 deletions

File tree

std/algebra/weierstrass/doc.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ This package uses field emulation (unlike packages
2525
[github.com/consensys/gnark/std/algebra/sw_bls12377] and
2626
[github.com/consensys/gnark/std/algebra/sw_bls24315], which use 2-chains). This
2727
allows to use any curve over any native (SNARK) field. The drawback of this
28-
approach is the extreme cost of the operations. In R1CS, point addition on
29-
256-bit fields is approximately 3500 constraints and doubling is approximately
30-
4300 constraints. A full scalar multiplication is approximately 2M constraints.
31-
It is several times more in PLONKish aritmetisation.
28+
approach is the extreme cost of the operations.
3229
*/
3330
package weierstrass

std/algebra/weierstrass/params.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
//
1414
// The base point is defined by (Gx, Gy).
1515
type CurveParams struct {
16-
A *big.Int // a in curve equation
17-
B *big.Int // b in curve equation
18-
Gx *big.Int // base point x
19-
Gy *big.Int // base point y
16+
A *big.Int // a in curve equation
17+
B *big.Int // b in curve equation
18+
Gx *big.Int // base point x
19+
Gy *big.Int // base point y
20+
Gm [][2]*big.Int // m*base point coords
2021
}
2122

2223
// GetSecp256k1Params returns curve parameters for the curve secp256k1. When
@@ -30,6 +31,7 @@ func GetSecp256k1Params() CurveParams {
3031
B: big.NewInt(7),
3132
Gx: gx,
3233
Gy: gy,
34+
Gm: computeSecp256k1Table(),
3335
}
3436
}
3537

@@ -39,11 +41,13 @@ func GetSecp256k1Params() CurveParams {
3941
func GetBN254Params() CurveParams {
4042
gx := big.NewInt(1)
4143
gy := big.NewInt(2)
44+
4245
return CurveParams{
4346
A: big.NewInt(0),
4447
B: big.NewInt(3),
4548
Gx: gx,
4649
Gy: gy,
50+
Gm: computeBN254Table(),
4751
}
4852
}
4953

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package weierstrass
2+
3+
import (
4+
"math/big"
5+
6+
"github.com/consensys/gnark-crypto/ecc/bn254"
7+
"github.com/consensys/gnark-crypto/ecc/secp256k1"
8+
)
9+
10+
func computeSecp256k1Table() [][2]*big.Int {
11+
Gjac, _ := secp256k1.Generators()
12+
table := make([][2]*big.Int, 256)
13+
tmp := new(secp256k1.G1Jac).Set(&Gjac)
14+
aff := new(secp256k1.G1Affine)
15+
jac := new(secp256k1.G1Jac)
16+
for i := 1; i < 256; i++ {
17+
tmp = tmp.Double(tmp)
18+
switch i {
19+
case 1, 2:
20+
jac.Set(tmp).AddAssign(&Gjac)
21+
aff.FromJacobian(jac)
22+
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
23+
case 3:
24+
jac.Set(tmp).SubAssign(&Gjac)
25+
aff.FromJacobian(jac)
26+
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
27+
fallthrough
28+
default:
29+
aff.FromJacobian(tmp)
30+
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
31+
}
32+
}
33+
return table[:]
34+
}
35+
36+
func computeBN254Table() [][2]*big.Int {
37+
Gjac, _, _, _ := bn254.Generators()
38+
table := make([][2]*big.Int, 256)
39+
tmp := new(bn254.G1Jac).Set(&Gjac)
40+
aff := new(bn254.G1Affine)
41+
jac := new(bn254.G1Jac)
42+
for i := 1; i < 256; i++ {
43+
tmp = tmp.Double(tmp)
44+
switch i {
45+
case 1, 2:
46+
jac.Set(tmp).AddAssign(&Gjac)
47+
aff.FromJacobian(jac)
48+
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
49+
case 3:
50+
jac.Set(tmp).SubAssign(&Gjac)
51+
aff.FromJacobian(jac)
52+
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
53+
fallthrough
54+
default:
55+
aff.FromJacobian(tmp)
56+
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
57+
}
58+
}
59+
return table
60+
}

0 commit comments

Comments
 (0)