charm-crypto-lite 0.61.1__cp314-cp314-macosx_10_15_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ """
2
+ ECGroup: Elliptic Curve Group abstraction for charm-crypto-lite.
3
+
4
+ This module provides a minimal interface for elliptic curve operations
5
+ on the secp256k1 curve using OpenSSL's EC implementation.
6
+
7
+ Supported operations:
8
+ - EC point operations (multiplicative group notation):
9
+ * Point addition: h * k (group operation)
10
+ * Scalar multiplication: g ** x (exponentiation)
11
+ * Point negation: -h
12
+ * Serialize/deserialize
13
+ - Scalar operations (additive notation):
14
+ * Addition: x + y
15
+ * Subtraction: x - y
16
+ * Multiplication: x * y
17
+ * Inversion: ~x
18
+ * Random generation
19
+ - generator() to get the curve generator point
20
+
21
+ Example:
22
+ from charm_lite.toolbox.ecgroup import ECGroup, ZR, G
23
+ from charm_lite.toolbox.eccurve import secp256k1
24
+
25
+ group = ECGroup(secp256k1)
26
+
27
+ # Get the generator point
28
+ g = group.generator()
29
+
30
+ # Generate random scalar
31
+ x = group.random(ZR)
32
+
33
+ # Scalar multiplication: h = g^x
34
+ h = g ** x
35
+
36
+ # Point addition (multiplicative notation)
37
+ k = g ** group.random(ZR)
38
+ p = h * k
39
+
40
+ # Point negation
41
+ neg_h = -h
42
+
43
+ # Scalar inversion
44
+ x_inv = ~x
45
+
46
+ # Serialization
47
+ data = group.serialize(h)
48
+ h2 = group.deserialize(data)
49
+ """
50
+
51
+ try:
52
+ from charm_lite.core.math.elliptic_curve import (
53
+ elliptic_curve, ec_element, ZR, G, init, random, order,
54
+ getGenerator, serialize, deserialize
55
+ )
56
+ except Exception as err:
57
+ raise ImportError(
58
+ "Cannot import elliptic_curve module. "
59
+ "Ensure charm-crypto-lite C extensions are compiled: %s" % err
60
+ )
61
+
62
+ # Re-export element types for convenience
63
+ __all__ = ['ECGroup', 'ZR', 'G', 'ec_element']
64
+
65
+
66
+ class ECGroup:
67
+ """Elliptic Curve Group for secp256k1 cryptographic operations.
68
+
69
+ This is a minimal implementation focused on:
70
+ - EC point operations: add, scalar multiply, negate
71
+ - Scalar operations: add, subtract, multiply, invert
72
+ - Serialization/deserialization
73
+ - Random element generation
74
+ """
75
+
76
+ def __init__(self, curve_id):
77
+ """Initialize an EC group with secp256k1 curve.
78
+
79
+ Args:
80
+ curve_id: Curve identifier (use secp256k1 = 714)
81
+ """
82
+ self.ec_group = elliptic_curve(nid=curve_id)
83
+ self.param = curve_id
84
+
85
+ def __str__(self):
86
+ return str(self.ec_group)
87
+
88
+ def order(self):
89
+ """Returns the order of the group (number of points on curve)."""
90
+ return order(self.ec_group)
91
+
92
+ def generator(self):
93
+ """Returns the generator point G of the curve.
94
+
95
+ This is the standard base point for secp256k1.
96
+ """
97
+ return getGenerator(self.ec_group)
98
+
99
+ def init(self, _type=ZR, value=None):
100
+ """Initialize an element with a specified type and optional value.
101
+
102
+ Args:
103
+ _type: Element type - ZR (scalar) or G (point)
104
+ value: Optional initial value (integer for ZR)
105
+
106
+ Returns:
107
+ New element of the specified type
108
+ """
109
+ if value is not None:
110
+ return init(self.ec_group, _type, value)
111
+ return init(self.ec_group, _type)
112
+
113
+ def random(self, _type=ZR):
114
+ """Generate a random element in ZR (scalar) or G (point).
115
+
116
+ Args:
117
+ _type: Element type - ZR for random scalar, G for random point
118
+
119
+ Returns:
120
+ Random element of the specified type
121
+ """
122
+ if _type == ZR or _type == G:
123
+ return random(self.ec_group, _type)
124
+ return None
125
+
126
+ def serialize(self, element):
127
+ """Serialize an EC element (point or scalar) to bytes.
128
+
129
+ Args:
130
+ element: EC element to serialize
131
+
132
+ Returns:
133
+ bytes representation of the element
134
+ """
135
+ return serialize(element)
136
+
137
+ def deserialize(self, bytes_object):
138
+ """Deserialize bytes back to an EC element.
139
+
140
+ Args:
141
+ bytes_object: Serialized element bytes
142
+
143
+ Returns:
144
+ Deserialized EC element
145
+ """
146
+ return deserialize(self.ec_group, bytes_object)
147
+