Skip to content

Commit

Permalink
fix scalarReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Nov 17, 2024
1 parent fca319f commit 178f1b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
13 changes: 9 additions & 4 deletions lib/crypto/crypto/cdsa/utils/ed25519_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';
import 'package:blockchain_utils/crypto/crypto/cdsa/crypto_ops/crypto_ops.dart';
import 'package:blockchain_utils/crypto/crypto/cdsa/curve/curves.dart';
import 'package:blockchain_utils/crypto/crypto/cdsa/utils/exp.dart';
import 'package:blockchain_utils/helper/helper.dart';
import 'package:blockchain_utils/utils/utils.dart';
Expand All @@ -23,9 +24,12 @@ class Ed25519Utils {
/// operation, and converts the result back to a byte array. This ensures
/// that the scalar remains within the valid range for Ed25519 operations.
static List<int> scalarReduce(List<int> scalar) {
final sc = List<int>.from(scalar);
CryptoOps.scReduce32(sc);
return sc;
final toint = BigintUtils.fromBytes(scalar, byteOrder: Endian.little);
final reduce = toint % Curves.generatorED25519.order!;
final tobytes = BigintUtils.toBytes(reduce,
order: Endian.little,
length: BigintUtils.orderLen(Curves.generatorED25519.order!));
return tobytes;
}

static BigInt asScalarInt(List<int> scalar) {
Expand Down Expand Up @@ -119,7 +123,8 @@ class Ed25519Utils {

static List<int> secretKeyToPubKey({required List<int> secretKey}) {
if (CryptoOps.scCheck(secretKey) != 0) {
throw const SquareRootError("The provided scalar exceeds the allowed range.");
throw const SquareRootError(
"The provided scalar exceeds the allowed range.");
}
final List<int> pubKey = zero();
final GroupElementP3 point = GroupElementP3();
Expand Down
42 changes: 22 additions & 20 deletions test/monero/monero_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import '../quick_hex.dart';
import 'test_vector.dart';

void main() {
for (final i in testVector) {
final seed = BytesUtils.fromHexString(i["seed"]);
final coin = MoneroCoins.values.firstWhere((element) =>
element.name.toLowerCase() ==
(i["coin"] as String).replaceAll("_", "").toLowerCase());
final w = MoneroAccount.fromSeed(seed, coinType: coin);
expect(w.privateSpendKey.raw.toHex(), i["private_sky"]);
expect(w.privateViewKey.raw.toHex(), i["private_vkey"]);
expect(w.publicSpendKey.compressed.toHex(), i["public_sky"]);
expect(w.publicViewKey.compressed.toHex(), i["public_vsky"]);
expect(w.primaryAddress, i["primary_address"]);
final paymentId = BytesUtils.fromHexString(i["payment_id"]);
expect(w.integratedAddress(paymentId), i["integrated_address"]);
final addresses = List.from(i["addresses"]);
for (final a in addresses) {
final minorIndex = a["minor_idx"];
final majorIndex = a["major_idx"];
final addr = w.subaddress(minorIndex, majorIndex: majorIndex);
expect(a["address"], addr);
test("monero account", () {
for (final i in testVector) {
final seed = BytesUtils.fromHexString(i["seed"]);
final coin = MoneroCoins.values.firstWhere((element) =>
element.name.toLowerCase() ==
(i["coin"] as String).replaceAll("_", "").toLowerCase());
final w = MoneroAccount.fromSeed(seed, coinType: coin);
expect(w.privateSpendKey.raw.toHex(), i["private_sky"]);
expect(w.privateViewKey.raw.toHex(), i["private_vkey"]);
expect(w.publicSpendKey.compressed.toHex(), i["public_sky"]);
expect(w.publicViewKey.compressed.toHex(), i["public_vsky"]);
expect(w.primaryAddress, i["primary_address"]);
final paymentId = BytesUtils.fromHexString(i["payment_id"]);
expect(w.integratedAddress(paymentId), i["integrated_address"]);
final addresses = List.from(i["addresses"]);
for (final a in addresses) {
final minorIndex = a["minor_idx"];
final majorIndex = a["major_idx"];
final addr = w.subaddress(minorIndex, majorIndex: majorIndex);
expect(a["address"], addr);
}
}
}
});
}

0 comments on commit 178f1b5

Please sign in to comment.