Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:crypto/crypto.dart';
- var pcurve = BigInt.parse('115792089237316195423570985008687907853269984665640564039457584007908834671663',radix:10); // The proven prime
- var chiavePrivataBinString;
- Future<void> main() async {
- var gx = BigInt.parse('55066263022277343669578718895168534326250603453777594175500187360389116729240',radix:10);
- var gy = BigInt.parse('32670510020758816978083085130507043184471273380659243275938904335757337482424',radix:10);
- List<BigInt> gpoint = [gx, gy];
- var chiaveutf = utf8.encode("p@ssw0rd");
- var chiavesha = sha256.convert(chiaveutf);
- var chiavePrivataBigInt = BigInt.parse(chiavesha.toString(),radix:16);
- chiavePrivataBinString = chiavePrivataBigInt.toRadixString(2);
- var chiavePrivataBinBigint = BigInt.parse(chiavePrivataBinString);
- print("******* Public Key Generation *********");
- var publicKey = await eccMultiply(gpoint,chiavePrivataBigInt);
- print('publicKey:${publicKey}');
- }
- BigInt modinv(BigInt a,BigInt n){
- var lm=BigInt.one;
- var hm=BigInt.zero;
- BigInt low = a%(n);
- BigInt high = n;
- while (low > BigInt.one){
- var ratio = high~/low;
- var nm = hm-lm*ratio;
- var new1 = high-low*ratio;
- hm =lm;
- lm = nm;
- high = low;
- low = new1;
- }
- return lm%(n);
- }
- ecadd(List a,List b){
- BigInt lamAdd = ((b[1]-a[1])* modinv(b[0]-a[0],pcurve))%(pcurve);
- BigInt x = (lamAdd*lamAdd-a[0]-b[0])%(pcurve);
- BigInt y;
- y = (lamAdd*(a[0]-x)-a[1])%(pcurve);
- List<BigInt> ecaddList = [x, y];
- return ecaddList;
- }
- ecdouble(List a){
- BigInt a0=BigInt.parse(a[0].toString());
- BigInt a1=BigInt.parse(a[1].toString());
- BigInt lam = ((BigInt.from(3)*a0*a0)* (modinv((BigInt.from(2)*a1),pcurve)) )%(pcurve);
- BigInt x = (lam*lam-BigInt.two*a0)%(pcurve);
- BigInt y = (lam*(a0-x)-a1)%(pcurve);
- List<BigInt> ecdoubleList = [x, y];
- return ecdoubleList;
- }
- List StringToList(scalarBin){
- var string = '${scalarBin}';
- string.split('');
- return (string.codeUnits.map((unit) {
- return new String.fromCharCode(unit);}).toList());
- }
- eccMultiply(genPoint,scalarHex){
- var q = new List<BigInt>();
- var scalarBin = chiavePrivataBinString;
- q=genPoint;
- List listString=StringToList(chiavePrivataBinString);
- listString.sublist(1).forEach((element) async {
- q= ecdouble(q);
- if(element=='1'){
- q= ecadd(q,genPoint);
- }
- });
- return(q);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement