Skip to content

Commit d9db7ab

Browse files
Richa-iitrmratsim
andauthored
C bindings for Banderwagon (#477)
* c bindings for verkle * comments correction Co-authored-by: Mamy Ratsimbazafy <[email protected]> * refactoring: * curve decl for ed * gen bindings for twedw * bigint bandderwagon types * banderwagon header * undo exportc of generic functions * add scalar mul * more exports for banderwagon scalar mul * c bindings for verkle * comments correction Co-authored-by: Mamy Ratsimbazafy <[email protected]> * refactoring: * curve decl for ed * gen bindings for twedw * bigint bandderwagon types * banderwagon header * undo exportc of generic functions * add scalar mul * more exports for banderwagon scalar mul * remove verkle header file changes * scalar field correction * tests for banderwagon.h * wrapper * update ipa wrapper * fix ffi exports * partial c tests * fix header creation failures * fixed test_parallel fails for verkle ipa * headers * headers * revert verkle api * empty line at EOF --------- Co-authored-by: Mamy Ratsimbazafy <[email protected]>
1 parent ea20f62 commit d9db7ab

9 files changed

+548
-1
lines changed

bindings/c_curve_decls.nim

+116
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,122 @@ template genBindings_EC_ShortW_NonAffine*(EC, EcAff, ScalarBig, ScalarField: unt
431431

432432
{.pop.}
433433

434+
template genBindings_EC_TwEdw_Affine*(EC, Field: untyped) =
435+
when appType == "lib":
436+
{.push noconv, dynlib, exportc, raises: [].} # No exceptions allowed
437+
else:
438+
{.push noconv, exportc, raises: [].} # No exceptions allowed
439+
440+
# --------------------------------------------------------------------------------------
441+
func `ctt _ EC _ is_eq`(P, Q: EC): SecretBool =
442+
P == Q
443+
444+
func `ctt _ EC _ is_neutral`(P: EC): SecretBool =
445+
P.isNeutral()
446+
447+
func `ctt _ EC _ set_neutral`(P: var EC) =
448+
P.setNeutral()
449+
450+
func `ctt _ EC _ ccopy`(P: var EC, Q: EC, ctl: SecretBool) =
451+
P.ccopy(Q, ctl)
452+
453+
func `ctt _ EC _ is_on_curve`(x, y: Field): SecretBool =
454+
isOnCurve(x, y)
455+
456+
func `ctt _ EC _ neg`(P: var EC, Q: EC) =
457+
P.neg(Q)
458+
459+
func `ctt _ EC _ neg_in_place`(P: var EC) =
460+
P.neg()
461+
462+
func `ctt _ EC _ cneg`(P: var EC, ctl: SecretBool) =
463+
P.cneg(ctl)
464+
465+
{.pop.}
466+
467+
template genBindings_EC_TwEdw_Projective*(EC, EcAff, ScalarBig, ScalarField: untyped) =
468+
when appType == "lib":
469+
{.push noconv, dynlib, exportc, raises: [].} # No exceptions allowed
470+
else:
471+
{.push noconv, exportc, raises: [].} # No exceptions allowed
472+
473+
# --------------------------------------------------------------------------------------
474+
func `ctt _ EC _ is_eq`(P, Q: EC): SecretBool =
475+
P == Q
476+
477+
func `ctt _ EC _ is_neutral`(P: EC): SecretBool =
478+
P.isNeutral()
479+
480+
func `ctt _ EC _ set_neutral`(P: var EC) =
481+
P.setNeutral()
482+
483+
func `ctt _ EC _ ccopy`(P: var EC, Q: EC, ctl: SecretBool) =
484+
P.ccopy(Q, ctl)
485+
486+
func `ctt _ EC _ neg`(P: var EC, Q: EC) =
487+
P.neg(Q)
488+
489+
func `ctt _ EC _ neg_in_place`(P: var EC) =
490+
P.neg()
491+
492+
func `ctt _ EC _ cneg`(P: var EC, ctl: SecretBool) =
493+
P.cneg(ctl)
494+
495+
func `ctt _ EC _ sum`(r: var EC, P, Q: EC) =
496+
r.sum(P, Q)
497+
498+
func `ctt _ EC _ double`(r: var EC, P: EC) =
499+
r.double(P)
500+
501+
func `ctt _ EC _ add_in_place`(P: var EC, Q: EC) =
502+
P += Q
503+
504+
func `ctt _ EC _ diff`(r: var EC, P, Q: EC) =
505+
r.diff(P,Q)
506+
507+
func `ctt _ EC _ diff_in_place`(P: var EC, Q: EC) =
508+
P -= Q
509+
510+
func `ctt _ EC _ mixed_diff_in_place`(P: var EC, Q: EcAff) =
511+
P -= Q
512+
513+
func `ctt _ EC _ affine`(dst: var EcAff, src: EC) =
514+
dst.affine(src)
515+
516+
func `ctt _ EC _ from_affine`(dst: var EC, src: EcAff) =
517+
dst.fromAffine(src)
518+
519+
func `ctt _ EC _ batch_affine`(dst: ptr UncheckedArray[EcAff], src: ptr UncheckedArray[EC], n: csize_t) =
520+
dst.batchAffine(src, cast[int](n))
521+
522+
func `ctt _ EC _ scalar_mul_big_coef`(P: var EC, scalar: ScalarBig) =
523+
P.scalarMul(scalar)
524+
525+
func `ctt _ EC _ scalar_mul_fr_coef`(P: var EC, scalar: ScalarField) =
526+
P.scalarMul(scalar)
527+
528+
func `ctt _ EC _ scalar_mul_big_coef_vartime`(P: var EC, scalar: ScalarBig) =
529+
P.scalarMul_vartime(scalar)
530+
531+
func `ctt _ EC _ scalar_mul_fr_coef_vartime`(P: var EC, scalar: ScalarField) =
532+
P.scalarMul_vartime(scalar)
533+
534+
func `ctt _ EC _ multi_scalar_mul_big_coefs_vartime`(
535+
r: var EC,
536+
coefs: ptr UncheckedArray[ScalarBig],
537+
points: ptr UncheckedArray[EcAff],
538+
len: csize_t) =
539+
r.multiScalarMul_vartime(coefs, points, cast[int](len))
540+
541+
func `ctt _ EC _ multi_scalar_mul_fr_coefs_vartime`(
542+
r: var EC,
543+
coefs: ptr UncheckedArray[ScalarField],
544+
points: ptr UncheckedArray[EcAff],
545+
len: csize_t)=
546+
r.multiScalarMul_vartime(coefs, points, cast[int](len))
547+
548+
{.pop.}
549+
434550
template genBindings_EC_hash_to_curve*(EC: untyped, mapping, hash: untyped, k: static int) =
435551
when appType == "lib":
436552
{.push noconv, dynlib, exportc, raises: [].} # No exceptions allowed

bindings/lib_curves.nim

+15
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import
1919
export c_curve_decls, c_curve_decls_parallel
2020

2121
type
22+
big253 = BigInt[253]
2223
big254 = BigInt[254]
2324
big255 = BigInt[255]
2425
big381 = BigInt[381]
2526

2627
collectBindings(cBindings_big):
28+
genBindingsBig(big253)
2729
genBindingsBig(big254)
2830
genBindingsBig(big255)
2931
genBindingsBig(big381)
@@ -140,3 +142,16 @@ collectBindings(cBindings_vesta_parallel):
140142
genParallelBindings_EC_ShortW_NonAffine(vesta_ec_prj, vesta_ec_aff, vesta_fr)
141143

142144
# ----------------------------------------------------------
145+
146+
type
147+
banderwagon_fr = Fr[Banderwagon]
148+
banderwagon_fp = Fp[Banderwagon]
149+
banderwagon_ec_aff = EC_TwEdw_Aff[Fp[Banderwagon]]
150+
banderwagon_ec_prj = EC_TwEdw_Prj[Fp[Banderwagon]]
151+
152+
collectBindings(cBindings_banderwagon):
153+
genBindingsField(big253, banderwagon_fr)
154+
genBindingsField(big255, banderwagon_fp)
155+
genBindingsFieldSqrt(banderwagon_fp)
156+
genBindings_EC_TwEdw_Affine(banderwagon_ec_aff, banderwagon_fp)
157+
genBindings_EC_TwEdw_Projective(banderwagon_ec_prj, banderwagon_ec_aff, big253, banderwagon_fr)

bindings/lib_headers.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ proc writeCurveHeaders(dir: string) =
134134
BLS12_381: cBindings_bls12_381,
135135
BN254_Snarks: cBindings_bn254_snarks,
136136
Pallas: cBindings_pallas,
137-
Vesta: cBindings_vesta
137+
Vesta: cBindings_vesta,
138+
Banderwagon: cBindings_banderwagon
138139
}
139140

140141
staticFor i, 0, curveMappings.len:
@@ -157,6 +158,9 @@ proc writeCurveParallelHeaders(dir: string) =
157158
bigSizes.incl(Fp[curveMappings[i][0]].bits())
158159
bigSizes.incl(Fr[curveMappings[i][0]].bits())
159160

161+
#bigInt header for banderwagon
162+
bigSizes.incl(Fp[Banderwagon].bits())
163+
bigSizes.incl(Fr[Banderwagon].bits())
160164
dir.writeBigIntHeader(bigSizes, cBindings_big)
161165

162166
when isMainModule:

constantine.nimble

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ proc testLib(path, testName: string, useGMP: bool) =
340340
task test_lib, "Test C library":
341341
exec "mkdir -p build/test_lib"
342342
testLib("examples-c", "t_libctt_bls12_381", useGMP = true)
343+
testLib("examples-c", "t_libctt_banderwagon", useGMP = true)
343344
testLib("examples-c", "ethereum_bls_signatures", useGMP = false)
344345
testLib("tests"/"c_api", "t_threadpool", useGMP = false)
345346

constantine/lowlevel_elliptic_curves.nim

+39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import
1111
./named/algebras,
1212
./named/[zoo_subgroups, zoo_generators],
1313
./math/ec_shortweierstrass,
14+
./math/ec_twistededwards,
1415
./math/elliptic/[
1516
ec_scalar_mul_vartime,
1617
ec_multi_scalar_mul],
@@ -58,6 +59,13 @@ export
5859
affine, jacobian, projective,
5960
projectiveFromJacobian
6061

62+
export
63+
ec_twistededwards.EC_TwEdw_Aff,
64+
ec_twistededwards.EC_TwEdw_Prj,
65+
ec_twistededwards.EC_TwEdw,
66+
ec_twistededwards.getName,
67+
affine, projective
68+
6169
export ec_shortweierstrass.`==`
6270
export ec_shortweierstrass.isNeutral
6371
export ec_shortweierstrass.setNeutral
@@ -87,6 +95,37 @@ export ec_shortweierstrass.scalarMul
8795
export ec_scalar_mul_vartime.scalarMul_vartime
8896
export ec_multi_scalar_mul.multiScalarMul_vartime
8997

98+
# Twisted edwards curve
99+
export ec_twistededwards.`==`
100+
export ec_twistededwards.isNeutral
101+
export ec_twistededwards.setNeutral
102+
export ec_twistededwards.ccopy
103+
export ec_twistededwards.isOnCurve
104+
export ec_twistededwards.neg
105+
export ec_twistededwards.cneg
106+
107+
export ec_twistededwards.sum
108+
export ec_twistededwards.mixedSum
109+
export ec_twistededwards.double
110+
export ec_twistededwards.`+=`
111+
export ec_twistededwards.diff
112+
export ec_twistededwards.mixedDiff
113+
export ec_twistededwards.`-=`
114+
export ec_twistededwards.affine
115+
export ec_twistededwards.projective
116+
export ec_twistededwards.fromAffine
117+
export ec_twistededwards.batchAffine
118+
export ec_twistededwards.sum_vartime
119+
export ec_twistededwards.mixedSum_vartime
120+
export ec_twistededwards.diff_vartime
121+
export ec_twistededwards.mixedDiff_vartime
122+
export ec_twistededwards.`~+=`
123+
export ec_twistededwards.`~-=`
124+
export ec_twistededwards.`+`
125+
export ec_twistededwards.`~+`
126+
export ec_twistededwards.`-`
127+
export ec_twistededwards.`~-`
128+
90129
export zoo_generators.getGenerator
91130
export zoo_subgroups.clearCofactor
92131
export zoo_subgroups.isInSubgroup

0 commit comments

Comments
 (0)