Skip to content

Commit d3f86fb

Browse files
committed
update cryptoOps
1 parent 79ecf70 commit d3f86fb

File tree

5 files changed

+342
-9
lines changed

5 files changed

+342
-9
lines changed

lib/crypto/crypto/cdsa/crypto_ops/const/const.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10060,4 +10060,15 @@ class CryptoOpsConst {
1006010060
326686,
1006110061
11406482
1006210062
]); /* sqrt(-1) */
10063+
10064+
static const List<String> geBiPointBytes = [
10065+
"5866666666666666666666666666666666666666666666666666666666666666",
10066+
"d4b4f5784868c3020403246717ec169ff79e26608ea126a1ab69ee77d1b16712",
10067+
"edc876d6831fd2105d0b4389ca2e283166469289146e2ce06faefe98b22548df",
10068+
"b862409fb5c4c4123df2abf7462b88f041ad36dd6864ce872fd5472be363c5b1",
10069+
"c0f1225584444ec730446e231390781ffdd2f256e9fcbeb2f40dddc2c2233d7f",
10070+
"1337036ac32d8f30d4589c3c1c595812ce0fff40e37c6f5a97ab213f318290ad",
10071+
"801f40eaaee1ef8723279a28b2cf4037b889dad222604678748b53ed0db0db92",
10072+
"df5c2eadc44c6d94a19a9aa118afe5ac3193d26401f76251f522ff042dfbcb92"
10073+
];
1006310074
}

lib/crypto/crypto/cdsa/crypto_ops/operations/ops.dart

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
// OF THE POSSIBILITY OF SUCH DAMAGE.
5555
import 'package:blockchain_utils/crypto/crypto/cdsa/crypto_ops/const/const.dart';
5656
import 'package:blockchain_utils/crypto/crypto/cdsa/crypto_ops/models/models.dart';
57+
import 'package:blockchain_utils/crypto/crypto/cdsa/point/edwards.dart';
58+
import 'package:blockchain_utils/crypto/crypto/cdsa/utils/ed25519.dart';
5759
import 'package:blockchain_utils/crypto/crypto/exception/exception.dart';
5860
import 'package:blockchain_utils/helper/extensions/extensions.dart';
5961
import 'package:blockchain_utils/utils/binary/utils.dart';
@@ -5048,6 +5050,268 @@ class CryptoOps {
50485050
geP1P1ToP2(r, t);
50495051
}
50505052
}
5053+
5054+
/// these method is not related to cryptoOps
5055+
static List<EDPoint> geDsmPrecompVartimeFast(EDPoint point) {
5056+
final List<EDPoint> oddMultiples = [];
5057+
final twoP = point.doublePoint();
5058+
var current = point;
5059+
5060+
for (int i = 0; i < 8; i++) {
5061+
oddMultiples.add(current);
5062+
current = current + twoP; // Next odd multiple
5063+
}
5064+
5065+
return oddMultiples;
5066+
}
5067+
5068+
static EDPoint geDoubleScalarMultPrecompVartimeFast(
5069+
List<int> a, List<EDPoint> aI, List<int> b, List<EDPoint> bI) {
5070+
final aslide = List<int>.filled(256, 0);
5071+
final bslide = List<int>.filled(256, 0);
5072+
CryptoOps.slide(aslide, a);
5073+
CryptoOps.slide(bslide, b);
5074+
5075+
EDPoint? r;
5076+
5077+
for (int i = 255; i >= 0; i--) {
5078+
final aVal = aslide[i];
5079+
final bVal = bslide[i];
5080+
5081+
if (r == null) {
5082+
if (aVal != 0) {
5083+
final idx = (aVal.abs() - 1) ~/ 2;
5084+
final ai = aI[idx];
5085+
r = aVal > 0 ? ai : -ai;
5086+
}
5087+
5088+
if (bVal != 0) {
5089+
final idx = (bVal.abs() - 1) ~/ 2;
5090+
final bi = bI[idx];
5091+
r = (r == null) ? (bVal > 0 ? bi : -bi) : (r + (bVal > 0 ? bi : -bi));
5092+
}
5093+
5094+
if (r == null) continue; // still no op
5095+
} else {
5096+
r = r * BigInt.two;
5097+
5098+
if (aVal != 0) {
5099+
final idx = (aVal.abs() - 1) ~/ 2;
5100+
final ai = aI[idx];
5101+
r += aVal > 0 ? ai : -ai;
5102+
}
5103+
5104+
if (bVal != 0) {
5105+
final idx = (bVal.abs() - 1) ~/ 2;
5106+
final bi = bI[idx];
5107+
r += bVal > 0 ? bi : -bi;
5108+
}
5109+
}
5110+
}
5111+
5112+
if (r == null) {
5113+
throw Exception(
5114+
"Both scalars are zero; result undefined without identity support.");
5115+
}
5116+
5117+
return r;
5118+
}
5119+
5120+
static EDPoint geTripleScalarMultBaseVartimeFast(
5121+
{required List<int> a,
5122+
required List<int> b,
5123+
required List<EDPoint> bI,
5124+
required List<int> c,
5125+
required List<EDPoint> cI}) {
5126+
final aslide = List<int>.filled(256, 0);
5127+
final bslide = List<int>.filled(256, 0);
5128+
final cslide = List<int>.filled(256, 0);
5129+
CryptoOps.slide(aslide, a);
5130+
CryptoOps.slide(bslide, b);
5131+
CryptoOps.slide(cslide, c);
5132+
5133+
EDPoint? r;
5134+
// Logg.error("calll ?!");
5135+
for (int i = 255; i >= 0; i--) {
5136+
final aVal = aslide[i];
5137+
final bVal = bslide[i];
5138+
final cVal = cslide[i];
5139+
5140+
if (r == null) {
5141+
if (aVal != 0) {
5142+
final idx = (aVal.abs() - 1) ~/ 2;
5143+
final ai = Ed25519Utils.asPoint(
5144+
BytesUtils.fromHexString(CryptoOpsConst.geBiPointBytes[idx]));
5145+
r = aVal > 0 ? ai : -ai;
5146+
}
5147+
5148+
if (bVal != 0) {
5149+
final idx = (bVal.abs() - 1) ~/ 2;
5150+
final bi = bI[idx];
5151+
r = (r == null) ? (bVal > 0 ? bi : -bi) : (r + (bVal > 0 ? bi : -bi));
5152+
}
5153+
5154+
if (cVal != 0) {
5155+
final idx = (cVal.abs() - 1) ~/ 2;
5156+
final ci = cI[idx];
5157+
r = (r == null) ? (cVal > 0 ? ci : -ci) : (r + (cVal > 0 ? ci : -ci));
5158+
}
5159+
5160+
if (r == null) continue;
5161+
} else {
5162+
r = r * BigInt.two;
5163+
5164+
if (aVal != 0) {
5165+
final idx = (aVal.abs() - 1) ~/ 2;
5166+
final ai = Ed25519Utils.asPoint(
5167+
BytesUtils.fromHexString(CryptoOpsConst.geBiPointBytes[idx]));
5168+
r += aVal > 0 ? ai : -ai;
5169+
}
5170+
5171+
if (bVal != 0) {
5172+
final idx = (bVal.abs() - 1) ~/ 2;
5173+
final bi = bI[idx];
5174+
r += bVal > 0 ? bi : -bi;
5175+
}
5176+
5177+
if (cVal != 0) {
5178+
final idx = (cVal.abs() - 1) ~/ 2;
5179+
final ci = cI[idx];
5180+
r += cVal > 0 ? ci : -ci;
5181+
}
5182+
}
5183+
}
5184+
5185+
if (r == null) {
5186+
throw Exception(
5187+
"All scalars are zero; result undefined without identity support.");
5188+
}
5189+
5190+
return r;
5191+
}
5192+
5193+
static EDPoint geDoubleScalarMultBaseVartimeFast(
5194+
{required List<int> a, required EDPoint gA, required List<int> b}) {
5195+
final aslide = List<int>.filled(256, 0);
5196+
final bslide = List<int>.filled(256, 0);
5197+
CryptoOps.slide(aslide, a);
5198+
CryptoOps.slide(bslide, b);
5199+
final aI = geDsmPrecompVartimeFast(gA);
5200+
final Map<int, String> res = {};
5201+
EDPoint? r;
5202+
// Logg.error("calll ?!");
5203+
for (int i = 255; i >= 0; i--) {
5204+
final aVal = aslide[i];
5205+
final bVal = bslide[i];
5206+
5207+
if (r == null) {
5208+
if (aVal != 0) {
5209+
final idx = (aVal.abs() - 1) ~/ 2;
5210+
final bi = aI[idx];
5211+
r = aVal > 0 ? bi : -bi;
5212+
}
5213+
if (bVal != 0) {
5214+
final idx = (bVal.abs() - 1) ~/ 2;
5215+
final ai = Ed25519Utils.asPoint(
5216+
BytesUtils.fromHexString(CryptoOpsConst.geBiPointBytes[idx]));
5217+
r = (r == null) ? (bVal > 0 ? ai : -ai) : (r + (bVal > 0 ? ai : -ai));
5218+
}
5219+
5220+
// if (r == null) continue;
5221+
} else {
5222+
r = r * BigInt.two;
5223+
5224+
if (aVal != 0) {
5225+
final idx = (aVal.abs() - 1) ~/ 2;
5226+
final bi = aI[idx];
5227+
r += aVal > 0 ? bi : -bi;
5228+
}
5229+
if (bVal != 0) {
5230+
final idx = (bVal.abs() - 1) ~/ 2;
5231+
final ai = Ed25519Utils.asPoint(
5232+
BytesUtils.fromHexString(CryptoOpsConst.geBiPointBytes[idx]));
5233+
r += bVal > 0 ? ai : -ai;
5234+
}
5235+
res.addAll({i: r.toHex()});
5236+
}
5237+
}
5238+
5239+
if (r == null) {
5240+
throw Exception(
5241+
"All scalars are zero; result undefined without identity support.");
5242+
}
5243+
5244+
return r;
5245+
}
5246+
5247+
static EDPoint geTripleScalarMultPrecompVartimeFast(
5248+
List<int> a,
5249+
List<EDPoint> aI,
5250+
List<int> b,
5251+
List<EDPoint> bI,
5252+
List<int> c,
5253+
List<EDPoint> cI) {
5254+
final aslide = List<int>.filled(256, 0);
5255+
final bslide = List<int>.filled(256, 0);
5256+
final cslide = List<int>.filled(256, 0);
5257+
5258+
CryptoOps.slide(aslide, a);
5259+
CryptoOps.slide(bslide, b);
5260+
CryptoOps.slide(cslide, c);
5261+
5262+
EDPoint? r;
5263+
5264+
for (int i = 255; i >= 0; i--) {
5265+
final aVal = aslide[i];
5266+
final bVal = bslide[i];
5267+
final cVal = cslide[i];
5268+
5269+
if (r == null) {
5270+
if (aVal != 0) {
5271+
final idx = (aVal.abs() - 1) ~/ 2;
5272+
final ai = aI[idx];
5273+
r = aVal > 0 ? ai : -ai;
5274+
}
5275+
if (bVal != 0) {
5276+
final idx = (bVal.abs() - 1) ~/ 2;
5277+
final bi = bI[idx];
5278+
r = (r == null) ? (bVal > 0 ? bi : -bi) : (r + (bVal > 0 ? bi : -bi));
5279+
}
5280+
if (cVal != 0) {
5281+
final idx = (cVal.abs() - 1) ~/ 2;
5282+
final ci = cI[idx];
5283+
r = (r == null) ? (cVal > 0 ? ci : -ci) : (r + (cVal > 0 ? ci : -ci));
5284+
}
5285+
5286+
if (r == null) continue; // still no op
5287+
} else {
5288+
r = r * BigInt.two;
5289+
5290+
if (aVal != 0) {
5291+
final idx = (aVal.abs() - 1) ~/ 2;
5292+
final ai = aI[idx];
5293+
r += aVal > 0 ? ai : -ai;
5294+
}
5295+
if (bVal != 0) {
5296+
final idx = (bVal.abs() - 1) ~/ 2;
5297+
final bi = bI[idx];
5298+
r += bVal > 0 ? bi : -bi;
5299+
}
5300+
if (cVal != 0) {
5301+
final idx = (cVal.abs() - 1) ~/ 2;
5302+
final ci = cI[idx];
5303+
r += cVal > 0 ? ci : -ci;
5304+
}
5305+
}
5306+
}
5307+
5308+
if (r == null) {
5309+
throw CryptoException(
5310+
"All scalars are zero; result undefined without identity support.");
5311+
}
5312+
5313+
return r;
5314+
}
50515315
}
50525316

50535317
extension _BytesHelper on List<int> {

lib/crypto/crypto/cdsa/secp256k1/constants/constants.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,6 @@ class Secp256k1Const {
146146
]),
147147
modulusInv: BigInt.parse("3815112494326173377"));
148148

149-
static Secp256k1ModinvInfo get secp256k1FieldModinfo => Secp256k1ModinvInfo(
150-
modulus: Secp256k1ModinvSigned.constants([
151-
BigInt.from(0x414036CD8BFD25BB),
152-
BigInt.from(0x03A64AF6DCEBAE00),
153-
BigInt.from(0xFFFFFEFFFFFEFFFF),
154-
BigInt.from(0xFFFFFFFFFFFFFFFF),
155-
BigInt.from(0xFFFFFFFFFFFFFFFF),
156-
]),
157-
modulusInv: BigInt.from(0xD838091DD2253531));
158149
static Secp256k1ModinvSigned get modeInvOne =>
159150
Secp256k1ModinvSigned.constants([
160151
BigInt.one,

lib/crypto/crypto/cdsa/utils/ed25519.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,67 @@ class Ed25519Utils {
174174
CryptoOps.geToBytes(res, p2);
175175
return res;
176176
}
177+
178+
static List<int> scMulFast(List<int> scalar, List<int> scalar2) {
179+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
180+
final b = BigintUtils.fromBytes(scalar2, byteOrder: Endian.little);
181+
final r = (b * a) % Curves.generatorED25519.order!;
182+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
183+
}
184+
185+
static List<int> scMulFastBigInt(List<int> scalar, BigInt scalar2) {
186+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
187+
final r = (scalar2 * a) % Curves.generatorED25519.order!;
188+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
189+
}
190+
191+
static List<int> scSubFast(List<int> scalar, List<int> scalar2) {
192+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
193+
final b = BigintUtils.fromBytes(scalar2, byteOrder: Endian.little);
194+
final r = (a - b) % Curves.generatorED25519.order!;
195+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
196+
}
197+
198+
static List<int> scSubFastBig(List<int> scalar, BigInt scalar2) {
199+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
200+
final r = (a - scalar2) % Curves.generatorED25519.order!;
201+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
202+
}
203+
204+
static List<int> scAddFast(List<int> scalar, List<int> scalar2) {
205+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
206+
final b = BigintUtils.fromBytes(scalar2, byteOrder: Endian.little);
207+
final r = (a + b) % Curves.generatorED25519.order!;
208+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
209+
}
210+
211+
static List<int> scAddFastBig(List<int> scalar, BigInt scalar2) {
212+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
213+
final r = (a + scalar2) % Curves.generatorED25519.order!;
214+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
215+
}
216+
217+
static BigInt scalarAsBig(List<int> scalar) {
218+
return BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
219+
}
220+
221+
static List<int> scMulAddFast(
222+
List<int> scalar, List<int> scalar2, List<int> scalar3) {
223+
final a = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
224+
final b = BigintUtils.fromBytes(scalar2, byteOrder: Endian.little);
225+
final c = BigintUtils.fromBytes(scalar3, byteOrder: Endian.little);
226+
final r = ((b * a) + c) % Curves.generatorED25519.order!;
227+
return BigintUtils.toBytes(r, length: 32, order: Endian.little);
228+
}
229+
230+
static bool scCheckFast(List<int> scalar) {
231+
assert(scalar.length == 32, 'invalid scalar size');
232+
final order = Curves.generatorED25519.order!;
233+
final scalarInt = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
234+
if (scalarInt >= order) {
235+
return false;
236+
}
237+
238+
return true;
239+
}
177240
}

lib/signer/substrate/substrate.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class SubstrateSigner {
2222
return _signer.sign(digest.asBytes);
2323
}
2424

25+
List<int> signConst(List<int> digest) {
26+
return _signer.signConst(digest.asBytes);
27+
}
28+
2529
List<int> vrfSign(List<int> message, {List<int>? context, List<int>? extra}) {
2630
return _signer.vrfSign(message.asBytes,
2731
extra: extra?.asBytes, context: context?.asBytes);

0 commit comments

Comments
 (0)