Separate OTBN spec from RISC-V documentation
The specification for the OTBN base instruction subset is currently defined in terms of the RISC-V specification, but should be self-contained. Consumers and implementers shouldn't be required to cross-reference documentation not maintained by OpenTitan.
The base instruction subset is already not identical to an RV subset, and will likely continue to diverge. Cross-referencing adds a lot of cognitive overhead, especially since the RV documentation lacks an instruction-by-instruction specification: https://riscv.org/specifications/isa-spec-pdf/
Hi @sydgibs, welcome to the project, and thanks for bringing up this proposal!
A large specification, like the one for OTBN, needs to cater to a variety of different audiences with different background. We, as the people who wrote it, have a background in RISC-V and are quite familiar with its specification. For us, the risk of introducing errors when replicating the subset of the RV32I specification outweighs the benefits of a more self-contained specification.
Of course, if you're coming from a different background, a good trade-off will look different for you. As always, I'm sure we'll be able to find a good middle ground somewhere. To get there, can you tell us a bit more about what you're doing with the OTBN specification, and how your "ideal" specification would look like?
I think the critical point here is that the OTBN spec is distinct from the RISC-V spec, may well diverge further and has a different audience. A more specialized, cryptographic implementer audience -- or, like @sydgibs a background in generating formal code for same.
Getting either of those tasks correct is already a major challenge. Forcing implementers, who are experts in cryptography, formal methods, or more widely used and implemented ISAs than RISC-V to slog through a spec. that has yet to achieve widespread adoption seems both risky for adoption and unnecessarily punitive.
I think both @sydgibs and @felixmiller are correct in that this is a distinct spec and should be documented and treated as such (even if it's inspired by RISC-V).
Taking into account the discussions about this issue on other mediums as well, I think there's broad support for Sydney's proposal "The specification for the OTBN base instruction subset is currently defined in terms of the RISC-V specification, but should be self-contained. Consumers and implementers shouldn't be required to cross-reference documentation not maintained by OpenTitan." It's a +1 from me as well.
I can see there's some risk of transcription errors, but failing to make the spec self-contained just offloads the risk of misunderstanding instruction definitions to anyone trying to understand the spec, as they have to switch between different documents presenting instruction semantics in slightly different ways.
Thanks for your input @asb and @domrizz0.
With us moving forward in making more of the OTBN specification machine-readable, we'll also be able to give a similar level of detail, especially the Decoding and Operation sections, as we have them for the big number instruction subset. In line with the request in this issue, we can make this information available in the specification document, aligning the instruction descriptions for the base and big number instruction subsets to make the document more self-contained.
@sydgibs I'd still like to make sure that these descriptions are actually what you're looking for. If you have a free minute, please let us know what you're looking for in the ISA description, and how you're using it.
One thing I forgot to mention: for a couple days now we already include the instruction format in the spec for all instructions, including the base instruction subset. The same tooling will be at work for the decoding and operation sections, once that's ready.
@imphil Instruction descriptions along the lines of what currently exist for the big number instruction set is approximately what I would suggest.
I'm working on a formal model of the OTBN specification that will be used to formally verify crypto procedures written in the OTBN ISA. This method of verification relies on the model being correct, so the most helpful documentation for me would be documentation that is very clear about semantic behaviors, e.g. which instructions read and modify flags, how do relevant instructions handle overflows, how many bits are in an immediate value and how is that immediate value interpreted, etc.
Thanks @sydgibs, that's good to know. For the base instruction subset, are you re-using one of the existing RISC-V formal models, or are you writing one from scratch?
@sydgibs: This sounds like a good thing to have, and the formal instruction definitions should probably be in the OpenTitan codebase. We've got a simple-minded "instruction database" in a YAML file (see insns.yml). Would you be willing to add your formal descriptions to that?
Personally, my preferred end result is that these formal descriptions are the definition of the instruction semantics. We should also have informative text, but the normative bit is what you're doing.
@imphil @rswarbrick I'm writing a model in Dafny (https://github.com/dafny-lang/dafny) and Vale (https://github.com/project-everest/vale), which requires working from scratch. As a sanity check, I've been using existing formalizations of RISC-V as references in addition to the official documentation, e.g. https://github.com/mit-plv/riscv-semantics.
I think a misunderstanding here might be that when I say I'm writing a formal model, what I mean is that I'm writing an implementation of a formal semantics, which in this case isn't written down anywhere since I'm working straight from the current instruction descriptions. I would like for this work to eventually serve as the definition of the instruction semantics, but it will exist as code within a verification DSL.
You can find my work so far here: https://github.com/secure-foundations/veri-titan/tree/master/otbn_model. When this work is complete, it would be great to advertise that OTBN code has been formally verified, but for now I don't believe that it makes sense to include this work in official documentation.
The OTBN documentation should now be free-standing, in the sense that every instruction has an automatically-generated "Operation" section giving what it does in pseudo-code. There are still improvements we could make to how that looks, but it's definitely no longer defined in terms of the RISC-V spec.