oasisprotocol / sapphire-paratime

The Sapphire ParaTime monorepo.
https://oasisprotocol.org/sapphire
Apache License 2.0
34 stars 24 forks source link

docs: Better document the usage/behavior of `contextOrHash` for .sign() and .verify() #331

Open matevz opened 1 month ago

matevz commented 1 month ago

Someone on discord asked:

My question mainly about the use of contextOrHash as bytes in functions like sign(), verify() in Sapphire.sol

For example, in sign function:

function sign( SigningAlg alg, bytes memory secretKey, @> bytes memory contextOrHash, bytes memory message ) internal view returns (bytes memory signature) { (bool success, bytes memory sig) = SIGN_DIGEST.staticcall( abi.encode(alg, secretKey, contextOrHash, message) ); require(success, "sign: failed"); return sig; }

contextOrHash is expected to be passed as bytes argument but the comment states:

@param contextOrHash Domain-Separator Context, or precomputed hash bytes.

and as per Sapphire documentation for Secp256k1PrehashedKeccak256 alg:

(Secp256k1PrehashedKeccak256): 3,000 gas, pre-existing hash (32 bytes) as context, empty message.

What does exactly mean by pre-existing hash (32 bytes) as context?

If one of my function is returning contextOrHash as bytes data type then would that need to be converted to bytes32 hash >using keccak256 OR that contextOrHash as bytes can be directly used in sign() or verify() function.

Document better the behavior of contextOrHash based on which signature scheme is used. Check out the implementation here: https://github.com/oasisprotocol/oasis-sdk/blob/main/runtime-sdk/src/crypto/signature/mod.rs#L544-L621