Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix h2c #102

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ class CashuMint {
* @param _mintUrl requires mint URL to create this object
* @param _customRequest if passed, use custom request implementation for network communication with the mint
*/
constructor(
private _mintUrl: string,
private _customRequest?: typeof request
) {}
constructor(private _mintUrl: string, private _customRequest?: typeof request) {}

get mintUrl() {
return this._mintUrl;
Expand Down
23 changes: 14 additions & 9 deletions src/DHKE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import { encodeUint8toBase64 } from './base64.js';
import { MintKeys, Proof, SerializedBlindedSignature } from './model/types/index.js';
import { bytesToNumber } from './utils.js';
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex } from '@noble/curves/abstract/utils';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { Buffer } from 'buffer/';

const DOMAIN_SEPARATOR = hexToBytes('536563703235366b315f48617368546f43757276655f43617368755f');

function hashToCurve(secret: Uint8Array): ProjPointType<bigint> {
let point: ProjPointType<bigint> | undefined;
while (!point) {
const hash = sha256(secret);
const hashHex = bytesToHex(hash);
const pointX = '02' + hashHex;
const msgToHash = sha256(Buffer.concat([DOMAIN_SEPARATOR, secret]));
const counter = new Uint32Array(1);
const maxIterations = 2 ** 16;
for (let i = 0; i < maxIterations; i++) {
const counterBytes = new Uint8Array(counter.buffer);
const hash = sha256(Buffer.concat([msgToHash, counterBytes]));
try {
point = pointFromHex(pointX);
return pointFromHex(bytesToHex(Buffer.concat([new Uint8Array([0x02]), hash])));
} catch (error) {
secret = sha256(secret);
counter[0]++;
}
}
return point;
throw new Error('No valid point found');
}

export function pointFromHex(hex: string) {
return secp256k1.ProjectivePoint.fromHex(hex);
}
Expand Down
6 changes: 3 additions & 3 deletions test/dhke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ describe('testing hash to curve', () => {
let secret = hexToBytes('0000000000000000000000000000000000000000000000000000000000000000');
let Y = dhke.hashToCurve(secret);
let hexY = Y.toHex(true);
expect(hexY).toBe('0266687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925');
expect(hexY).toBe('024cce997d3b518f739663b757deaec95bcd9473c30a14ac2fd04023a739d1a725');
});

test('testing string 0000....01', async () => {
let secret = hexToBytes('0000000000000000000000000000000000000000000000000000000000000001');
let Y = dhke.hashToCurve(secret);
let hexY = Y.toHex(true);
expect(hexY).toBe('02ec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5');
expect(hexY).toBe('022e7158e11c9506f1aa4248bf531298daa7febd6194f003edcd9b93ade6253acf');
});
});

Expand All @@ -29,7 +29,7 @@ describe('test blinding message', () => {
bytesToNumber(hexToBytes('0000000000000000000000000000000000000000000000000000000000000001'))
);
expect(B_.toHex(true)).toBe(
'03c509bbdd8aaa81d5e67468d07b4b7dffd5769ac596ff3964e151adcefc6b06d0'
'030eded094dc2b0e3c78aae9fac76c1ad426a43e1f57fb32d2e64ca5dc82a85791'
);
});
});
Expand Down
Loading