Help with std.crypto.asn1.Oid.StaticMap

I am running into a compilation error when trying to use std.crypto.asn1.Oid.StaticMap that I can decipher.

So the error is:

..../Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/lib/std/crypto/asn1/Oid.zig:190:67: error: Each field of 'root.PubKeyFmt' must map to exactly one OID
            if (oid_to_enum.values().len != enum_info.fields.len) @compileError(error_msg);
                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~

And this is how I am trying to use it:

pub const PK = enum {
    rsa,
    ecdsa_p256,
};

    _ = std.crypto.asn1.Oid.StaticMap(PK).initComptime(.{
        .rsa = "1.2.840.113549.1.1.1", 
        .ecdsa_p256 = "1.2.840.10045.2.1", 
    });

The reason why I do not get why this is failing compilation is because, the fields in the PK enum matches what is being passed in as struct, and the enum is exhaustive.

So not sure why this check is failing at comp time

      if (!enum_info.is_exhaustive or enum_info.fields.len != struct_info.fields.len) {
                @compileError(error_msg);
            }

What am I doing wrong?

Not sure if it’s an intentional restriction, but all examples appear to put the oids inside the enum:

const PK = enum {
    rsa,
    ecdsa_p256,
    const oids = std.crypto.asn1.Oid.StaticMap(PK).initComptime(.{
        .rsa = "1.2.840.113549.1.1.1",
        .ecdsa_p256 = "1.2.840.10045.2.1",
    });
};

… where StaticMap(@This()) would also work

1 Like

Thanks! This worked. but not sure if there were anything in the compile error or even the implementation of initComptime that I peeked into that could have made this more obvious to me.

I think it’s just a buggy check that, in your original case, ends up comparing runtime and comptime lengths. If you put a @compileLog check in std, you’ll get

Compile Log Output:
@as(usize, [runtime value]), @as(usize, 2)

So if you make the call to StaticMap comptime, your original code compiles:

 const oids = comptime std.crypto.asn1.Oid.StaticMap(PK).initComptime(.{ ...

I tried to remove the len-check in std, and then the non-comptime version compiles and runs as well.

1 Like