fix: signature fail in gen inputs

This commit is contained in:
0xvikasrushi
2024-07-28 08:29:21 +00:00
parent ed1397b923
commit 85b4f3a9c5
5 changed files with 123 additions and 581 deletions

View File

@@ -68,7 +68,7 @@ export function genMockPassportData_sha1WithECDSA(): PassportData {
const concatenatedDataHashes = formatAndConcatenateDataHashes(
[[1, mrzHash], ...sampleDataHashes],
hashLen,
33 // ? TODO replace with original dg1HashOffset
33
);
const eContent = assembleEContent(hash(signatureAlgorithm, concatenatedDataHashes));

View File

@@ -1792,441 +1792,34 @@ export const mockPassPortData_sha1_ecdsa = {
"mrz": "P<FRADUPONT<<ALPHONSE<HUGUES<ALBERT<<<<<<<<<24HB818324FRA0402111M3111115<<<<<<<<<<<<<<02",
"signatureAlgorithm": "ecdsa-with-SHA1",
"pubKey": {
"publicKeyQ": "(da3b0e6d85de52725134db8f916885bb008335d3f5f1b667a3c0daedf0118440,9e18de2795c22a0083600d5b8cd11910beb2774ef090de80f97be85c7bdf5da3,1,fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc)"
"publicKeyQ": "(4a43aa53ec47c5ae156a60b78b5c696f6b311abe90d3564ce0801e08e59a89ba,44a7a04ad97057b79d0e839dd353b49094e6f6cb870633c9b9da16dee31781f7,1,fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc)"
},
"dataGroupHashes": [
99,
97,
54,
-115,
-13,
-30,
71,
36,
-84,
41,
-81,
118,
-34,
100,
-35,
26,
-55,
-124,
63,
-99,
31,
-107,
-108,
40,
62,
-100,
-55,
103,
-108,
115,
51,
61,
-15,
-96,
114,
-93,
25,
75,
70,
94,
85,
95,
40,
-11,
-101,
88,
-85,
-108,
-10,
-44,
104,
-62,
-117,
-96,
114,
-93,
25,
75,
70,
94,
85,
95,
40,
-11,
-101,
88,
-85,
-108,
-10,
-44,
104,
-62,
-117,
-66,
82,
-76,
-21,
-34,
33,
79,
50,
-104,
-120,
-114,
35,
116,
-32,
6,
-14,
-100,
-115,
-128,
-8,
10,
61,
98,
86,
-8,
45,
-49,
-46,
90,
-24,
-81,
38,
0,
-62,
104,
108,
-19,
-10,
97,
-26,
116,
-58,
69,
110,
26,
87,
17,
89,
110,
-57,
108,
-6,
36,
21,
39,
87,
110,
102,
-6,
-43,
-82,
-125,
-85,
-82,
-120,
-101,
87,
-112,
111,
15,
-104,
127,
85,
25,
-102,
81,
20,
58,
51,
75,
-63,
116,
-22,
0,
60,
30,
29,
30,
-73,
-115,
72,
-9,
-1,
-53,
100,
124,
41,
-22,
106,
78,
31,
11,
114,
-119,
-19,
17,
92,
71,
-122,
47,
62,
78,
-67,
-23,
-55,
-42,
53,
4,
47,
-67,
-55,
-123,
6,
121,
34,
-125,
64,
-114,
91,
-34,
-46,
-63,
62,
-34,
104,
82,
36,
41,
-118,
-3,
70,
15,
-108,
-48,
-100,
45,
105,
-85,
-15,
-61,
-71,
43,
-39,
-94,
-110,
-55,
-34,
89,
-18,
38,
76,
123,
-40,
13,
51,
-29,
72,
-11,
59,
-63,
-18,
-90,
103,
49,
23,
-92,
-85,
-68,
-62,
-59,
-100,
-69,
-7,
28,
-58,
95,
69,
15,
-74,
56,
54,
38
20, -74, 1, -24, 42, 71, 11, -118, -39, 103, -4, 111, -15, 52, -122, 33, 17, -81, -2, 32, 9, 82,
-7, 81, -31, -6, -30, 86, 105, -87, 43, 33, 42, -96, 114, -93, 25, 75, 70, 94, 85, 95, 40, -11,
-101, 88, -85, -108, -10, -44, 104, -62, -117, -96, 114, -93, 25, 75, 70, 94, 85, 95, 40, -11,
-101, 88, -85, -108, -10, -44, 104, -62, -117, -66, 82, -76, -21, -34, 33, 79, 50, -104, -120,
-114, 35, 116, -32, 6, -14, -100, -115, -128, -8, 10, 61, 98, 86, -8, 45, -49, -46, 90, -24,
-81, 38, 0, -62, 104, 108, -19, -10, 97, -26, 116, -58, 69, 110, 26, 87, 17, 89, 110, -57, 108,
-6, 36, 21, 39, 87, 110, 102, -6, -43, -82, -125, -85, -82, -120, -101, 87, -112, 111, 15, -104,
127, 85, 25, -102, 81, 20, 58, 51, 75, -63, 116, -22, 0, 60, 30, 29, 30, -73, -115, 72, -9, -1,
-53, 100, 124, 41, -22, 106, 78, 31, 11, 114, -119, -19, 17, 92, 71, -122, 47, 62, 78, -67, -23,
-55, -42, 53, 4, 47, -67, -55, -123, 6, 121, 34, -125, 64, -114, 91, -34, -46, -63, 62, -34,
104, 82, 36, 41, -118, -3, 70, 15, -108, -48, -100, 45, 105, -85, -15, -61, -71, 43, -39, -94,
-110, -55, -34, 89, -18, 38, 76, 123, -40, 13, 51, -29, 72, -11, 59, -63, -18, -90, 103, 49, 23,
-92, -85, -68, -62, -59, -100, -69, -7, 28, -58, 95, 69, 15, -74, 56, 54, 38
],
"eContent": [
49,
102,
48,
21,
6,
9,
42,
-122,
72,
-122,
-9,
13,
1,
9,
3,
49,
8,
6,
6,
103,
-127,
8,
1,
1,
1,
48,
28,
6,
9,
42,
-122,
72,
-122,
-9,
13,
1,
9,
5,
49,
15,
23,
13,
49,
57,
49,
50,
49,
54,
49,
55,
50,
50,
51,
56,
90,
48,
47,
6,
9,
42,
-122,
72,
-122,
-9,
13,
1,
9,
4,
49,
34,
4,
32,
95,
-126,
77,
-22,
-46,
-65,
-73,
-24,
-47,
-36,
51,
-41,
-72,
-31,
-41,
-103,
-84,
-102,
-26,
-89
49, 102, 48, 21, 6, 9, 42, -122, 72, -122, -9, 13, 1, 9, 3, 49, 8, 6, 6, 103, -127, 8, 1, 1, 1,
48, 28, 6, 9, 42, -122, 72, -122, -9, 13, 1, 9, 5, 49, 15, 23, 13, 49, 57, 49, 50, 49, 54, 49,
55, 50, 50, 51, 56, 90, 48, 47, 6, 9, 42, -122, 72, -122, -9, 13, 1, 9, 4, 49, 34, 4, 32, -105,
9, -25, -7, 38, -22, 108, 77, 115, 46, 68, 30, 39, 117, -101, 8, 109, 44, -60, 92
],
"encryptedDigest": [
48,
69,
2,
32,
97,
200,
107,
237,
56,
23,
80,
12,
29,
163,
177,
12,
251,
95,
128,
248,
142,
153,
227,
85,
133,
14,
144,
57,
240,
195,
131,
135,
118,
81,
124,
214,
2,
33,
0,
145,
109,
54,
3,
120,
193,
246,
48,
166,
167,
62,
22,
22,
125,
211,
120,
51,
81,
83,
132,
132,
199,
205,
109,
56,
232,
30,
169,
251,
239,
59,
2
48, 69, 2, 33, 0, 222, 191, 241, 181, 247, 75, 185, 183, 85, 119, 18, 145, 171, 100, 208, 237,
75, 147, 230, 196, 103, 166, 24, 140, 76, 19, 240, 68, 171, 125, 48, 55, 2, 32, 33, 123, 109,
246, 29, 121, 171, 5, 250, 159, 117, 151, 88, 162, 251, 240, 167, 240, 120, 189, 237, 179, 81,
162, 241, 91, 35, 169, 54, 14, 157, 253
],
"photoBase64": "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIw3..."
}

View File

@@ -37,7 +37,6 @@ export function generateCircuitInputsRegister(
if (DEVELOPMENT_MODE) {
for (const mockPassportData of mocks) {
console.log('mockPassportData', mockPassportData);
tree.insert(getLeaf(mockPassportData).toString());
}
}
@@ -93,6 +92,7 @@ export function generateCircuitInputsRegister(
);
}
// ! TODO
const [messagePadded, messagePaddedLen] = shaPad(
signatureAlgorithm,
new Uint8Array(dataGroupHashes),
@@ -100,21 +100,24 @@ export function generateCircuitInputsRegister(
);
let dsc_modulus: any;
let signature: any;
if (signatureAlgorithm === 'ecdsa-with-SHA1') {
console.log('pubKey', pubKey);
const curve_params = pubKey.publicKeyQ.replace(/[()]/g, '').split(',');
let k = hexToDecimal(curve_params[0]);
let m = hexToDecimal(curve_params[1]);
dsc_modulus = [
...splitToWords(BigInt(k), BigInt(n_dsc), BigInt(k_dsc)),
...splitToWords(BigInt(m), BigInt(n_dsc), BigInt(k_dsc)),
];
dsc_modulus = [curve_params[0], curve_params[1]]; // ! TODO REFACTOR SPLIT HERE WHAT IF WORKS
signature = passportData.encryptedDigest;
} else {
dsc_modulus = splitToWords(
BigInt(passportData.pubKey.modulus as string),
BigInt(n_dsc),
BigInt(k_dsc)
);
signature = splitToWords(
BigInt(bytesToBigDecimal(passportData.encryptedDigest)),
BigInt(n_dsc),
BigInt(k_dsc)
);
// eContent = Array.from(messagePadded).map((x) => x.toString());
}
return {
secret: [secret],
@@ -123,11 +126,7 @@ export function generateCircuitInputsRegister(
econtent: Array.from(messagePadded).map((x) => x.toString()),
datahashes_padded_length: [messagePaddedLen.toString()],
signed_attributes: eContent.map(toUnsignedByte).map((byte) => String(byte)),
signature: splitToWords(
BigInt(bytesToBigDecimal(passportData.encryptedDigest)),
BigInt(n_dsc),
BigInt(k_dsc)
),
signature: signature,
dsc_modulus: dsc_modulus,
attestation_id: [attestation_id],
dsc_secret: [dscSecret],

View File

@@ -2,6 +2,7 @@ import { LeanIMT } from '@zk-kit/lean-imt';
import { sha256 } from 'js-sha256';
import { sha1 } from 'js-sha1';
import { sha384 } from 'js-sha512';
import forge from 'node-forge';
export function formatMrz(mrz: string) {
const mrzCharcodes = [...mrz].map((char) => char.charCodeAt(0));
@@ -319,3 +320,21 @@ export function generateMerkleProof(imt: LeanIMT, _index: number, maxDepth: numb
export function findSubarrayIndex(arr: any[], subarray: any[]): number {
return arr.findIndex((_, index) => subarray.every((element, i) => element === arr[index + i]));
}
export function extractRSFromSignature(signatureBytes: number[]): { r: string; s: string } {
const derSignature = Buffer.from(signatureBytes).toString('binary');
const asn1 = forge.asn1.fromDer(derSignature);
const signatureAsn1 = asn1.value;
if (signatureAsn1.length !== 2) {
throw new Error('Invalid signature format');
}
if (!Array.isArray(asn1.value) || asn1.value.length !== 2) {
throw new Error('Invalid signature format');
}
const r = forge.util.createBuffer(asn1.value[0].value as string).toHex();
const s = forge.util.createBuffer(asn1.value[1].value as string).toHex();
return { r, s };
}