Skip to content
100 changes: 94 additions & 6 deletions src/keri/core/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ export class IndexerCodex {
ECDSA_256r1_Big_Crt_Sig: string = '2F'; // ECDSA secp256r1 sig appears in current list only.
Ed448_Big_Sig: string = '3A'; // Ed448 signature appears in both lists.
Ed448_Big_Crt_Sig: string = '3B'; // Ed448 signature appears in current list only.
ML_DSA_65_Big_Sig: string = '3C'; // ML-DSA-65 sig appears in both lists.
ML_DSA_65_Big_Crt_Sig: string = '3D'; // ML-DSA-65 sig appears in current list only.
SLH_DSA_SHA2_192s_Big_Sig: string = '3E'; // SLH-DSA-SHA2-192s sig appears in both lists.
SLH_DSA_SHA2_192s_Big_Crt_Sig: string = '3F'; // SLH-DSA-SHA2-192s sig appears in current list only.
SLH_DSA_SHAKE_192s_Big_Sig: string = '3G'; // SLH-DSA-SHAKE-192s sig appears in both lists.
SLH_DSA_SHAKE_192s_Big_Crt_Sig: string = '3H'; // SLH-DSA-SHAKE-192s sig appears in current list only.
ML_DSA_44_Big_Sig: string = '8A'; // ML-DSA-44 sig appears in both lists, lead size 1.
ML_DSA_44_Big_Crt_Sig: string = '8B'; // ML-DSA-44 sig appears in current list only, lead size 1.
SLH_DSA_SHA2_128s_Big_Sig: string = '8C'; // SLH-DSA-SHA2-128s sig appears in both lists, lead size 1.
SLH_DSA_SHA2_128s_Big_Crt_Sig: string = '8D'; // SLH-DSA-SHA2-128s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_128s_Big_Sig: string = '8E'; // SLH-DSA-SHAKE-128s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_128s_Big_Crt_Sig: string = '8F'; // SLH-DSA-SHAKE-128s sig appears in current list only, lead size 1.
SLH_DSA_SHA2_256s_Big_Sig: string = '8G'; // SLH-DSA-SHA2-256s sig appears in both lists, lead size 1.
SLH_DSA_SHA2_256s_Big_Crt_Sig: string = '8H'; // SLH-DSA-SHA2-256s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_256s_Big_Sig: string = '8I'; // SLH-DSA-SHAKE-256s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_256s_Big_Crt_Sig: string = '8J'; // SLH-DSA-SHAKE-256s sig appears in current list only, lead size 1.
ML_DSA_87_Big_Sig: string = '9A'; // ML-DSA-87 sig appears in both lists, lead size 2.
ML_DSA_87_Big_Crt_Sig: string = '9B'; // ML-DSA-87 sig appears in current list only, lead size 2.
}

export const IdrDex = new IndexerCodex();
Expand All @@ -40,6 +58,24 @@ export class IndexedSigCodex {
ECDSA_256r1_Big_Crt_Sig: string = '2F'; // ECDSA secp256r1 sig appears in current list only.
Ed448_Big_Sig: string = '3A'; // Ed448 signature appears in both lists.
Ed448_Big_Crt_Sig: string = '3B'; // Ed448 signature appears in current list only.
ML_DSA_65_Big_Sig: string = '3C'; // ML-DSA-65 sig appears in both lists.
ML_DSA_65_Big_Crt_Sig: string = '3D'; // ML-DSA-65 sig appears in current list only.
SLH_DSA_SHA2_192s_Big_Sig: string = '3E'; // SLH-DSA-SHA2-192s sig appears in both lists.
SLH_DSA_SHA2_192s_Big_Crt_Sig: string = '3F'; // SLH-DSA-SHA2-192s sig appears in current list only.
SLH_DSA_SHAKE_192s_Big_Sig: string = '3G'; // SLH-DSA-SHAKE-192s sig appears in both lists.
SLH_DSA_SHAKE_192s_Big_Crt_Sig: string = '3H'; // SLH-DSA-SHAKE-192s sig appears in current list only.
ML_DSA_44_Big_Sig: string = '8A'; // ML-DSA-44 sig appears in both lists, lead size 1.
ML_DSA_44_Big_Crt_Sig: string = '8B'; // ML-DSA-44 sig appears in current list only, lead size 1.
SLH_DSA_SHA2_128s_Big_Sig: string = '8C'; // SLH-DSA-SHA2-128s sig appears in both lists, lead size 1.
SLH_DSA_SHA2_128s_Big_Crt_Sig: string = '8D'; // SLH-DSA-SHA2-128s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_128s_Big_Sig: string = '8E'; // SLH-DSA-SHAKE-128s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_128s_Big_Crt_Sig: string = '8F'; // SLH-DSA-SHAKE-128s sig appears in current list only, lead size 1.
SLH_DSA_SHA2_256s_Big_Sig: string = '8G'; // SLH-DSA-SHA2-256s sig appears in both lists, lead size 1.
SLH_DSA_SHA2_256s_Big_Crt_Sig: string = '8H'; // SLH-DSA-SHA2-256s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_256s_Big_Sig: string = '8I'; // SLH-DSA-SHAKE-256s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_256s_Big_Crt_Sig: string = '8J'; // SLH-DSA-SHAKE-256s sig appears in current list only, lead size 1.
ML_DSA_87_Big_Sig: string = '9A'; // ML-DSA-87 sig appears in both lists, lead size 2.
ML_DSA_87_Big_Crt_Sig: string = '9B'; // ML-DSA-87 sig appears in current list only, lead size 2.

has(prop: string): boolean {
const m = new Map(
Expand All @@ -60,6 +96,15 @@ export class IndexedCurrentSigCodex {
ECDSA_256k1_Big_Crt_Sig: string = '2D'; // ECDSA secp256k1 sig appears in current list only.
ECDSA_256r1_Big_Crt_Sig: string = '2F'; // ECDSA secp256r1 sig appears in current list only.
Ed448_Big_Crt_Sig: string = '3B'; // Ed448 signature appears in current list only.
ML_DSA_65_Big_Crt_Sig: string = '3D'; // ML-DSA-65 sig appears in current list only.
SLH_DSA_SHA2_192s_Big_Crt_Sig: string = '3F'; // SLH-DSA-SHA2-192s sig appears in current list only.
SLH_DSA_SHAKE_192s_Big_Crt_Sig: string = '3H'; // SLH-DSA-SHAKE-192s sig appears in current list only.
ML_DSA_44_Big_Crt_Sig: string = '8B'; // ML-DSA-44 sig appears in current list only, lead size 1.
SLH_DSA_SHA2_128s_Big_Crt_Sig: string = '8D'; // SLH-DSA-SHA2-128s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_128s_Big_Crt_Sig: string = '8F'; // SLH-DSA-SHAKE-128s sig appears in current list only, lead size 1.
SLH_DSA_SHA2_256s_Big_Crt_Sig: string = '8H'; // SLH-DSA-SHA2-256s sig appears in current list only, lead size 1.
SLH_DSA_SHAKE_256s_Big_Crt_Sig: string = '8J'; // SLH-DSA-SHAKE-256s sig appears in current list only, lead size 1.
ML_DSA_87_Big_Crt_Sig: string = '9B'; // ML-DSA-87 sig appears in current list only, lead size 2.

has(prop: string): boolean {
const m = new Map(
Expand All @@ -74,10 +119,21 @@ export const IdxCrtSigDex = new IndexedCurrentSigCodex();
export class IndexedBothSigCodex {
Ed25519_Sig: string = 'A'; // Ed25519 sig appears same in both lists if any.
ECDSA_256k1_Sig: string = 'C'; // ECDSA secp256k1 sig appears same in both lists if any.
ECDSA_256r1_Sig: string = 'E'; // ECDSA secp256r1 sig appears same in both lists if any.
Ed448_Sig: string = '0A'; // Ed448 signature appears in both lists.
Ed25519_Big_Sig: string = '2A'; // Ed25519 sig appears in both listsy.
ECDSA_256k1_Big_Sig: string = '2C'; // ECDSA secp256k1 sig appears in both lists.
ECDSA_256r1_Big_Sig: string = '2E'; // ECDSA secp256r1 sig appears in both lists.
Ed448_Big_Sig: string = '3A'; // Ed448 signature appears in both lists.
ML_DSA_65_Big_Sig: string = '3C'; // ML-DSA-65 sig appears in both lists.
SLH_DSA_SHA2_192s_Big_Sig: string = '3E'; // SLH-DSA-SHA2-192s sig appears in both lists.
SLH_DSA_SHAKE_192s_Big_Sig: string = '3G'; // SLH-DSA-SHAKE-192s sig appears in both lists.
ML_DSA_44_Big_Sig: string = '8A'; // ML-DSA-44 sig appears in both lists, lead size 1.
SLH_DSA_SHA2_128s_Big_Sig: string = '8C'; // SLH-DSA-SHA2-128s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_128s_Big_Sig: string = '8E'; // SLH-DSA-SHAKE-128s sig appears in both lists, lead size 1.
SLH_DSA_SHA2_256s_Big_Sig: string = '8G'; // SLH-DSA-SHA2-256s sig appears in both lists, lead size 1.
SLH_DSA_SHAKE_256s_Big_Sig: string = '8I'; // SLH-DSA-SHAKE-256s sig appears in both lists, lead size 1.
ML_DSA_87_Big_Sig: string = '9A'; // ML-DSA-87 sig appears in both lists, lead size 2.

has(prop: string): boolean {
const m = new Map(
Expand Down Expand Up @@ -176,6 +232,8 @@ export class Indexer {
['2', 2],
['3', 2],
['4', 2],
['8', 2],
['9', 2],
]);

static Sizes = new Map(
Expand All @@ -198,6 +256,24 @@ export class Indexer {

'3A': new Xizage(2, 6, 3, 160, 0),
'3B': new Xizage(2, 6, 3, 160, 0),
'3C': new Xizage(2, 6, 3, 4420, 0),
'3D': new Xizage(2, 6, 3, 4420, 0),
'3E': new Xizage(2, 6, 3, 21640, 0),
'3F': new Xizage(2, 6, 3, 21640, 0),
'3G': new Xizage(2, 6, 3, 21640, 0),
'3H': new Xizage(2, 6, 3, 21640, 0),
'8A': new Xizage(2, 6, 3, 3236, 1),
'8B': new Xizage(2, 6, 3, 3236, 1),
'8C': new Xizage(2, 6, 3, 10484, 1),
'8D': new Xizage(2, 6, 3, 10484, 1),
'8E': new Xizage(2, 6, 3, 10484, 1),
'8F': new Xizage(2, 6, 3, 10484, 1),
'8G': new Xizage(2, 6, 3, 39732, 1),
'8H': new Xizage(2, 6, 3, 39732, 1),
'8I': new Xizage(2, 6, 3, 39732, 1),
'8J': new Xizage(2, 6, 3, 39732, 1),
'9A': new Xizage(2, 6, 3, 6180, 2),
'9B': new Xizage(2, 6, 3, 6180, 2),

'0z': new Xizage(2, 2, 0, undefined, 0),
'1z': new Xizage(2, 2, 1, 76, 1),
Expand Down Expand Up @@ -243,7 +319,11 @@ export class Indexer {
if (
ondex != undefined &&
xizage.os != 0 &&
!(ondex >= 0 && ondex <= 64 ** os - 1)
!(
Number.isInteger(ondex) &&
ondex >= 0 &&
ondex <= 64 ** os - 1
)
) {
throw new Error(`Invalid ondex=${ondex} for code=${code}.`);
}
Expand Down Expand Up @@ -276,7 +356,7 @@ export class Indexer {
// raise InvalidCodeSizeError(f"Non-zero other index size for "
// f"variable length material. os={os}.")
// fs = (index * 4) + cs
const rawsize = Math.floor(((fs - cs) * 3) / 4);
const rawsize = Math.floor(((fs - cs) * 3) / 4) - xizage.ls; // lead bytes are not raw
raw = raw.slice(0, rawsize);

if (raw.length != rawsize) {
Expand Down Expand Up @@ -309,7 +389,8 @@ export class Indexer {

public static _rawSize(code: string) {
const xizage = Indexer.Sizes.get(code)!;
return Math.floor(xizage.fs! - ((xizage.hs + xizage.ss) * 3) / 4);
const cs = xizage.hs + xizage.ss;
return Math.floor(((xizage.fs! - cs) * 3) / 4) - xizage.ls;
}

get code(): string {
Expand Down Expand Up @@ -364,7 +445,11 @@ export class Indexer {
if (
ondex != undefined &&
xizage.os != 0 &&
!(ondex >= 0 && ondex <= 64 ** xizage.os - 1)
!(
Number.isInteger(ondex) &&
ondex >= 0 &&
ondex <= 64 ** xizage.os - 1
)
) {
throw new Error(
`Invalid ondex=${ondex} for os=${xizage.os} and code=${code}.`
Expand Down Expand Up @@ -468,7 +553,7 @@ export class Indexer {

qb64 = qb64.slice(0, xizage.fs);
const ps = cs % 4;
const pbs = 2 * ps != 0 ? ps : xizage.ls;
const pbs = 2 * (ps != 0 ? ps : xizage.ls); // pad bit size, as in Matter
let raw;
if (ps != 0) {
const base = new Array(ps + 1).join('A') + qb64.slice(cs);
Expand All @@ -495,7 +580,10 @@ export class Indexer {
raw = paw.slice(xizage!.ls);
}

if (raw.length != Math.floor(((qb64.length - cs) * 3) / 4)) {
if (
raw.length !=
Math.floor(((qb64.length - cs) * 3) / 4) - xizage.ls
) {
throw new Error(`Improperly qualified material = ${qb64}`);
}

Expand Down
59 changes: 26 additions & 33 deletions src/keri/core/matter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,40 +165,33 @@ export class Matter {
'1AAH': new Sizage(4, 0, 100, 0),
'1AAI': new Sizage(4, 0, 48, 0),
'1AAJ': new Sizage(4, 0, 48, 0),
// CESR 1.1 post-quantum codes. Every post-quantum primitive is
// large — the smallest signature here is 666 B and the largest
// 29,792 B — so the spec requires them all in the four-character
// fixed tables rather than spending scarce one- and two-character
// entries. The selector fixes the lead size: 1 -> 0, 2 -> 1, 3 -> 2.
'1AAQ': new Sizage(4, 0, 1200, 0), // FN-DSA-512 public verification key, 897 B
'1AAR': new Sizage(4, 0, 892, 0), // FN-DSA-512 signature, 666 B
'1AAS': new Sizage(4, 0, 3460, 0), // ML-DSA-87 public verification key, 2592 B
'1AAT': new Sizage(4, 0, 4416, 0), // ML-DSA-65 signature, 3309 B
// Post-quantum codes, following trustoverip/kswg-cesr-specification
// #185 (provisional until it merges). Every post-quantum primitive
// sits in the four-character fixed tables, and the raw size mod 3
// picks the selector: 1 -> no lead byte, 2 -> one, 3 -> two.
'1AAQ': new Sizage(4, 0, 4416, 0), // ML-DSA-65 signature, 3309 B
'1AAR': new Sizage(4, 0, 3460, 0), // ML-DSA-87 public verification key, 2592 B
'1AAS': new Sizage(4, 0, 68, 0), // Seed of SLH-DSA-SHA2-128s private key, 48 B
'1AAT': new Sizage(4, 0, 68, 0), // Seed of SLH-DSA-SHAKE-128s private key, 48 B
'1AAU': new Sizage(4, 0, 68, 0), // SLH-DSA-SHA2-192s public verification key, 48 B
'1AAV': new Sizage(4, 0, 68, 0), // SLH-DSA-SHAKE-192s public verification key, 48 B
'1AAW': new Sizage(4, 0, 21636, 0), // SLH-DSA-SHA2-192s signature, 16224 B
'1AAX': new Sizage(4, 0, 21636, 0), // SLH-DSA-SHAKE-192s signature, 16224 B
'1AAY': new Sizage(4, 0, 68, 0), // Seed of SLH-DSA-SHA2-128s private key, 48 B
'1AAZ': new Sizage(4, 0, 68, 0), // Seed of SLH-DSA-SHAKE-128s private key, 48 B
'1AAa': new Sizage(4, 0, 100, 0), // Seed of SLH-DSA-SHA2-192s private key, 72 B
'1AAb': new Sizage(4, 0, 100, 0), // Seed of SLH-DSA-SHAKE-192s private key, 72 B
'1AAc': new Sizage(4, 0, 132, 0), // Seed of SLH-DSA-SHA2-256s private key, 96 B
'1AAd': new Sizage(4, 0, 132, 0), // Seed of SLH-DSA-SHAKE-256s private key, 96 B
'2AAA': new Sizage(4, 0, 2396, 1), // FN-DSA-1024 public verification key, 1793 B
'2AAB': new Sizage(4, 0, 1712, 1), // FN-DSA-1024 signature, 1280 B
'2AAC': new Sizage(4, 0, 48, 1), // Seed of FN-DSA-512 private key, 32 B
'2AAD': new Sizage(4, 0, 48, 1), // Seed of FN-DSA-1024 private key, 32 B
'2AAE': new Sizage(4, 0, 2608, 1), // ML-DSA-65 public verification key, 1952 B
'2AAF': new Sizage(4, 0, 3232, 1), // ML-DSA-44 signature, 2420 B
'2AAG': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-44 private key, 32 B
'2AAH': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-65 private key, 32 B
'2AAI': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-87 private key, 32 B
'2AAJ': new Sizage(4, 0, 48, 1), // SLH-DSA-SHA2-128s public verification key, 32 B
'2AAK': new Sizage(4, 0, 48, 1), // SLH-DSA-SHAKE-128s public verification key, 32 B
'2AAL': new Sizage(4, 0, 10480, 1), // SLH-DSA-SHA2-128s signature, 7856 B
'2AAM': new Sizage(4, 0, 10480, 1), // SLH-DSA-SHAKE-128s signature, 7856 B
'2AAN': new Sizage(4, 0, 39728, 1), // SLH-DSA-SHA2-256s signature, 29792 B
'2AAO': new Sizage(4, 0, 39728, 1), // SLH-DSA-SHAKE-256s signature, 29792 B
'1AAV': new Sizage(4, 0, 21636, 0), // SLH-DSA-SHA2-192s signature, 16224 B
'1AAW': new Sizage(4, 0, 100, 0), // Seed of SLH-DSA-SHA2-192s private key, 72 B
'1AAX': new Sizage(4, 0, 68, 0), // SLH-DSA-SHAKE-192s public verification key, 48 B
'1AAY': new Sizage(4, 0, 21636, 0), // SLH-DSA-SHAKE-192s signature, 16224 B
'1AAZ': new Sizage(4, 0, 100, 0), // Seed of SLH-DSA-SHAKE-192s private key, 72 B
'1AAa': new Sizage(4, 0, 132, 0), // Seed of SLH-DSA-SHA2-256s private key, 96 B
'1AAb': new Sizage(4, 0, 132, 0), // Seed of SLH-DSA-SHAKE-256s private key, 96 B
'2AAA': new Sizage(4, 0, 3232, 1), // ML-DSA-44 signature, 2420 B
'2AAB': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-44 private key, 32 B
'2AAC': new Sizage(4, 0, 2608, 1), // ML-DSA-65 public verification key, 1952 B
'2AAD': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-65 private key, 32 B
'2AAE': new Sizage(4, 0, 48, 1), // Seed of ML-DSA-87 private key, 32 B
'2AAF': new Sizage(4, 0, 48, 1), // SLH-DSA-SHA2-128s public verification key, 32 B
'2AAG': new Sizage(4, 0, 10480, 1), // SLH-DSA-SHA2-128s signature, 7856 B
'2AAH': new Sizage(4, 0, 48, 1), // SLH-DSA-SHAKE-128s public verification key, 32 B
'2AAI': new Sizage(4, 0, 10480, 1), // SLH-DSA-SHAKE-128s signature, 7856 B
'2AAJ': new Sizage(4, 0, 39728, 1), // SLH-DSA-SHA2-256s signature, 29792 B
'2AAK': new Sizage(4, 0, 39728, 1), // SLH-DSA-SHAKE-256s signature, 29792 B
'3AAA': new Sizage(4, 0, 1756, 2), // ML-DSA-44 public verification key, 1312 B
'3AAB': new Sizage(4, 0, 6176, 2), // ML-DSA-87 signature, 4627 B
'3AAC': new Sizage(4, 0, 92, 2), // SLH-DSA-SHA2-256s public verification key, 64 B
Expand Down
Loading
Loading