Skip to content

Commit

Permalink
misc: generator()
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jun 18, 2024
1 parent 82e8b69 commit 8ac73c7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 37 deletions.
22 changes: 11 additions & 11 deletions benchmarks/bench_verkle_primitives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ proc equalityBench*(T: typedesc, iters: int) =
let Q = Banderwagon.getGenerator()
else:
var P, Q: Prj
P.fromAffine(Banderwagon.getGenerator())
Q.fromAffine(Banderwagon.getGenerator())
P.generator()
Q.generator()
bench("Banderwagon Equality ", T, iters):
assert (P == Q).bool()


proc serializeBench*(T: typedesc, iters: int) =
var bytes: array[32, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 9:
P.double()
bench("Banderwagon Serialization", T, iters):
Expand All @@ -103,7 +103,7 @@ proc serializeBench*(T: typedesc, iters: int) =
proc deserializeBench*(T: typedesc, iters: int) =
var bytes: array[32, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serialize(P)
Expand All @@ -113,7 +113,7 @@ proc deserializeBench*(T: typedesc, iters: int) =
proc deserializeBenchUnchecked*(T: typedesc, iters: int) =
var bytes: array[32, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serialize(P)
Expand All @@ -123,7 +123,7 @@ proc deserializeBenchUnchecked*(T: typedesc, iters: int) =
proc deserializeBench_vartime*(T: typedesc, iters: int) =
var bytes: array[32, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serialize(P)
Expand All @@ -133,7 +133,7 @@ proc deserializeBench_vartime*(T: typedesc, iters: int) =
proc deserializeBenchUnchecked_vartime*(T: typedesc, iters: int) =
var bytes: array[32, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serialize(P)
Expand All @@ -143,7 +143,7 @@ proc deserializeBenchUnchecked_vartime*(T: typedesc, iters: int) =
proc serializeUncompressedBench*(T: typedesc, iters: int) =
var bytes: array[64, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
bench("Banderwagon Serialization Uncompressed", T, iters):
Expand All @@ -152,7 +152,7 @@ proc serializeUncompressedBench*(T: typedesc, iters: int) =
proc deserializeUncompressedBench*(T: typedesc, iters: int) =
var bytes: array[64, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serializeUncompressed(P)
Expand All @@ -162,7 +162,7 @@ proc deserializeUncompressedBench*(T: typedesc, iters: int) =
proc deserializeUncompressedBenchUnchecked*(T: typedesc, iters: int) =
var bytes: array[64, byte]
var P: Prj
P.fromAffine(Banderwagon.getGenerator())
P.generator()
for i in 0 ..< 6:
P.double()
discard bytes.serializeUncompressed(P)
Expand All @@ -185,4 +185,4 @@ proc main() =
deserializeUncompressedBenchUnchecked(Prj, Iters)

main()
notes()
notes()
46 changes: 27 additions & 19 deletions constantine/commitments/pedersen_commitments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,55 @@ import
## ############################################################

func pedersen_commit*[EC, ECaff](
public_generators: openArray[ECaff],
r: var EC,
messages: openArray[Fr],
public_generators: openArray[ECaff]) {.inline.} =
messages: openArray[Fr]) {.inline.} =
## Vector Pedersen Commitment with elliptic curves
##
## Context
## - public generators G=(G₀,...,Gₙ₋₁)
##
## Inputs
## - messages m=(m₀,...,mₙ₋₁)
## - public generators G=(G₀,...,Gₙ₋₁)
##
## Computes: Commit(m, r) := ∑[mᵢ]Gᵢ
## Output:
## Commit(m) := ∑[mᵢ]Gᵢ
r.multiScalarMul_reference_vartime(messages, public_generators)

func pedersen_commit*[EC, ECaff, F](
public_generators: View[ECaff],
r: var EC,
messages: StridedView[F],
public_generators: StridedView[ECaff]) =
messages: View[F]) =
## Vector Pedersen Commitment with elliptic curves
##
## Context
## - public generators G=(G₀,...,Gₙ₋₁)
##
## Inputs
## - messages m=(m₀,...,mₙ₋₁)
## - public generators G=(G₀,...,Gₙ₋₁)
##
## Computes: Commit(m, r) := ∑[mᵢ]Gᵢ
## Output:
## Commit(m) := ∑[mᵢ]Gᵢ
r.pedersen_commit(messages.toOpenArray(), public_generators.toOpenArray())

func pedersen_commit*[EC, ECaff](
r: var EC,
messages: openArray[Fr],
public_generators: openArray[ECaff],
blinding_factor: Fr,
hiding_generator: ECaff) =
hiding_generator: ECaff,
output: var EC,
messages: openArray[Fr],
blinding_factor: Fr) =
## Blinded Vector Pedersen Commitment with elliptic curves
##
## Context
## - public generators G=(G₀,...,Gₙ₋₁)
## - Hiding generator H
##
## Inputs
## - messages m=(m₀,...,mₙ₋₁)=(G₀,...,Gₙ₋₁)
## - public generators G
## - messages m=(m₀,...,mₙ₋₁)
## - blinding factor r
## - hiding generator H
##
## Computes: Commit(m, r) := ∑[mᵢ]Gᵢ + [r]H

## Output:
## Commit(m, r) := ∑[mᵢ]Gᵢ + [r]H
# - Non-Interactive and Information-Theoretic Secure Verifiable Secret Sharing
# Torben Pryds Pedersen
# https://link.springer.com/content/pdf/10.1007/3-540-46766-1_9.pdf
Expand All @@ -76,11 +84,11 @@ func pedersen_commit*[EC, ECaff](
#
# - https://dankradfeist.de/ethereum/2021/07/27/inner-product-arguments.html

r.pedersen_commit(messages, public_generators)
output.pedersen_commit(messages, public_generators)

# We could run the following in MSM, but that would require extra alloc and copy
var rH {.noInit.}: EC
rH.fromAffine(hiding_generator)
rH.scalarMul_vartime(blinding_factor)

r += rH
output += rH
3 changes: 2 additions & 1 deletion constantine/curves_primitives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export
export ec_shortweierstrass.`==`
export ec_shortweierstrass.isInf
export ec_shortweierstrass.setInf
export ec_shortweierstrass.generator
export ec_shortweierstrass.ccopy
export ec_shortweierstrass.isOnCurve
export ec_shortweierstrass.neg
Expand Down Expand Up @@ -250,4 +251,4 @@ export pairings_generic.finalExp
# Hashing to Elliptic Curve
# ------------------------------------------------------------

export hash_to_curve.hash_to_curve
export hash_to_curve.hash_to_curve
3 changes: 1 addition & 2 deletions constantine/math/constants/zoo_generators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import
macro getGenerator*(C: static Curve, subgroup: static string = ""): untyped =
## Returns the curve subgroup generator.
## Pairing-friendly curves expect G1 or G2

if subgroup == "":
return bindSym($C & "_generator")
else:
return bindSym($C & "_generator_" & subgroup)
return bindSym($C & "_generator_" & subgroup)
9 changes: 8 additions & 1 deletion constantine/math/ec_shortweierstrass.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import
ec_shortweierstrass_projective,
ec_shortweierstrass_batch_ops,
ec_scalar_mul, ec_scalar_mul_vartime
]
],
./constants/zoo_generators

export ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_shortweierstrass_projective,
ec_shortweierstrass_batch_ops, ec_scalar_mul, ec_scalar_mul_vartime
Expand All @@ -39,3 +40,9 @@ func double_repeated*(P: var ECP_ShortW, num: int) {.inline.} =
## Repeated doublings
for _ in 0 ..< num:
P.double()

func generator*[F, G](g: var ECP_ShortW[F, G]) {.inline.} =
when g is ECP_ShortW_Aff:
g = F.C.getGenerator($G)
else:
g.fromAffine(F.C.getGenerator($G))
11 changes: 9 additions & 2 deletions constantine/math/ec_twistededwards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
# ############################################################

import
elliptic/[
./elliptic/[
ec_twistededwards_affine,
ec_twistededwards_projective,
ec_twistededwards_batch_ops,
ec_scalar_mul, ec_scalar_mul_vartime
]
],
./constants/zoo_generators

export ec_twistededwards_affine, ec_twistededwards_projective,
ec_twistededwards_batch_ops, ec_scalar_mul, ec_scalar_mul_vartime

type ECP_TwEdwards*[F] = ECP_TwEdwards_Aff[F] | ECP_TwEdwards_Prj[F]

func generator*[F](g: var ECP_TwEdwards[F]) {.inline.} =
when g is ECP_TwEdwards_Aff:
g = F.C.getGenerator()
else:
g.fromAffine(F.C.getGenerator())
2 changes: 1 addition & 1 deletion constantine/signatures/bls_signatures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func derivePubkey*[Pubkey, SecKey](pubkey: var Pubkey, seckey: SecKey) =
const EC = Field.C

var pk {.noInit.}: ECP_ShortW_Jac[Field, Group]
pk.fromAffine(EC.getGenerator($Group))
pk.generator()
pk.scalarMul(seckey)
pubkey.affine(pk)

Expand Down

0 comments on commit 8ac73c7

Please sign in to comment.