wallet-core: use crypto worker for eddsa signing

This commit is contained in:
Florian Dold 2021-12-09 10:39:50 +01:00
parent 684c53e105
commit dbfc599540
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 68 additions and 26 deletions

View File

@ -38,7 +38,6 @@ import {
* set up its own services.
*/
export async function runBench1(configJson: any): Promise<void> {
const logger = new Logger("Bench1");
// Validate the configuration file for this benchmark.
@ -53,7 +52,9 @@ export async function runBench1(configJson: any): Promise<void> {
const withdrawAmount = (numDeposits + 1) * 10;
logger.info(`Starting Benchmark iterations=${numIter} deposits=${numDeposits}`);
logger.info(
`Starting Benchmark iterations=${numIter} deposits=${numDeposits}`,
);
let wallet = {} as Wallet;
@ -70,6 +71,7 @@ export async function runBench1(configJson: any): Promise<void> {
persistentStoragePath: undefined,
httpLib: myHttpLib,
});
wallet.setInsecureTrustExchange();
await wallet.client.call(WalletApiOperation.InitWallet, {});
}
@ -86,12 +88,13 @@ export async function runBench1(configJson: any): Promise<void> {
stopWhenDone: true,
});
logger.info(`Finished withdrawal amount=${withdrawAmount} time=${Date.now() - start}`);
logger.info(
`Finished withdrawal amount=${withdrawAmount} time=${Date.now() - start}`,
);
for (let i = 0; i < numDeposits; i++) {
logger.trace(`Starting deposit amount=10`);
start = Date.now()
start = Date.now();
await wallet.client.call(WalletApiOperation.CreateDepositGroup, {
amount: b1conf.currency + ":10",

View File

@ -142,6 +142,21 @@ export interface PrimitiveWorker {
sig: string;
pub: string;
}): Promise<{ valid: boolean }>;
eddsaSign(req: { msg: string; priv: string }): Promise<{ sig: string }>;
}
async function myEddsaSign(
primitiveWorker: PrimitiveWorker | undefined,
req: { msg: string; priv: string },
): Promise<{ sig: string }> {
if (primitiveWorker) {
return primitiveWorker.eddsaSign(req);
}
const sig = eddsaSign(decodeCrock(req.msg), decodeCrock(req.priv));
return {
sig: encodeCrock(sig),
};
}
export class CryptoImplementation {
@ -153,13 +168,14 @@ export class CryptoImplementation {
* Create a pre-coin of the given denomination to be withdrawn from then given
* reserve.
*/
createPlanchet(req: PlanchetCreationRequest): PlanchetCreationResult {
async createPlanchet(
req: PlanchetCreationRequest,
): Promise<PlanchetCreationResult> {
if (
req.denomPub.cipher === DenomKeyType.Rsa ||
req.denomPub.cipher === DenomKeyType.LegacyRsa
) {
const reservePub = decodeCrock(req.reservePub);
const reservePriv = decodeCrock(req.reservePriv);
const denomPubRsa = decodeCrock(req.denomPub.rsa_public_key);
const derivedPlanchet = setupWithdrawPlanchet(
decodeCrock(req.secretSeed),
@ -180,7 +196,10 @@ export class CryptoImplementation {
.put(evHash)
.build();
const sig = eddsaSign(withdrawRequest, reservePriv);
const sigResult = await myEddsaSign(this.primitiveWorker, {
msg: encodeCrock(withdrawRequest),
priv: req.reservePriv,
});
const planchet: PlanchetCreationResult = {
blindingKey: encodeCrock(derivedPlanchet.bks),
@ -194,7 +213,7 @@ export class CryptoImplementation {
},
denomPubHash: encodeCrock(denomPubHash),
reservePub: encodeCrock(reservePub),
withdrawSig: encodeCrock(sig),
withdrawSig: sigResult.sig,
coinEvHash: encodeCrock(evHash),
};
return planchet;
@ -427,7 +446,9 @@ export class CryptoImplementation {
* Generate updated coins (to store in the database)
* and deposit permissions for each given coin.
*/
signDepositPermission(depositInfo: DepositInfo): CoinDepositPermission {
async signDepositPermission(
depositInfo: DepositInfo,
): Promise<CoinDepositPermission> {
// FIXME: put extensions here if used
const hExt = new Uint8Array(64);
let d: Uint8Array;
@ -460,12 +481,15 @@ export class CryptoImplementation {
} else {
throw Error("unsupported exchange protocol version");
}
const coinSig = eddsaSign(d, decodeCrock(depositInfo.coinPriv));
const coinSigRes = await myEddsaSign(this.primitiveWorker, {
msg: encodeCrock(d),
priv: depositInfo.coinPriv,
});
if (depositInfo.denomKeyType === DenomKeyType.Rsa) {
const s: CoinDepositPermission = {
coin_pub: depositInfo.coinPub,
coin_sig: encodeCrock(coinSig),
coin_sig: coinSigRes.sig,
contribution: Amounts.stringify(depositInfo.spendAmount),
h_denom: depositInfo.denomPubHash,
exchange_url: depositInfo.exchangeBaseUrl,
@ -478,7 +502,7 @@ export class CryptoImplementation {
} else if (depositInfo.denomKeyType === DenomKeyType.LegacyRsa) {
const s: CoinDepositPermission = {
coin_pub: depositInfo.coinPub,
coin_sig: encodeCrock(coinSig),
coin_sig: coinSigRes.sig,
contribution: Amounts.stringify(depositInfo.spendAmount),
h_denom: depositInfo.denomPubHash,
exchange_url: depositInfo.exchangeBaseUrl,
@ -611,10 +635,13 @@ export class CryptoImplementation {
.put(decodeCrock(meltCoinPub))
.build();
const confirmSig = eddsaSign(confirmData, decodeCrock(meltCoinPriv));
const confirmSigResp = await myEddsaSign(this.primitiveWorker, {
msg: encodeCrock(confirmData),
priv: meltCoinPriv,
});
const refreshSession: DerivedRefreshSession = {
confirmSig: encodeCrock(confirmSig),
confirmSig: confirmSigResp.sig,
hash: encodeCrock(sessionHash),
meltCoinPub: meltCoinPub,
planchetsForGammas: planchetsForGammas,
@ -641,22 +668,24 @@ export class CryptoImplementation {
return encodeCrock(hash(decodeCrock(encodedBytes)));
}
signCoinLink(
async signCoinLink(
oldCoinPriv: string,
newDenomHash: string,
oldCoinPub: string,
transferPub: string,
coinEv: string,
): string {
): Promise<string> {
const coinEvHash = hash(decodeCrock(coinEv));
const coinLink = buildSigPS(TalerSignaturePurpose.WALLET_COIN_LINK)
.put(decodeCrock(newDenomHash))
.put(decodeCrock(transferPub))
.put(coinEvHash)
.build();
const coinPriv = decodeCrock(oldCoinPriv);
const sig = eddsaSign(coinLink, coinPriv);
return encodeCrock(sig);
const sig = await myEddsaSign(this.primitiveWorker, {
msg: encodeCrock(coinLink),
priv: oldCoinPriv,
});
return sig.sig;
}
benchmark(repetitions: number): BenchmarkResult {

View File

@ -114,6 +114,16 @@ class MyPrimitiveWorker implements PrimitiveWorker {
args: req,
});
}
async eddsaSign(req: {
msg: string;
priv: string;
}): Promise<{ sig: string }> {
return this.queueRequest({
op: "eddsa_sign",
args: req,
});
}
}
/**

View File

@ -942,7 +942,7 @@ async function dispatchRequestInternal(
}
const components = pt.targetPath.split("/");
const creditorAcct = components[components.length - 1];
logger.info(`making testbank transfer to '${creditorAcct}''`);
logger.info(`making testbank transfer to '${creditorAcct}'`);
const fbReq = await ws.http.postJson(
new URL(`${creditorAcct}/admin/add-incoming`, req.bank).href,
{