Skip to content

Commit b2b196b

Browse files
Use precomputed signature tag
1 parent beb5b67 commit b2b196b

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

C/jets-secp256k1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "jets.h"
22

3+
#include "precomputed.h"
34
#include "prefix.h"
45
#include "sha256.h"
56
#include "secp256k1/secp256k1_impl.h"
6-
#include "tag.h"
77

88
/* Read a secp256k1 field element value from the 'src' frame, advancing the cursor 256 cells.
99
*
@@ -613,7 +613,7 @@ bool check_sig_verify(frameItem* dst, frameItem src, const txEnv* env) {
613613

614614
{
615615
sha256_midstate output;
616-
sha256_context ctx = MK_TAG(output.s, SIMPLICITY_PREFIX "\x1F" "Signature");
616+
sha256_context ctx = sha256_tagged_init(output.s, &signatureIV);
617617
read8s(msg, 64, &src);
618618
sha256_uchars(&ctx, msg, 64);
619619
sha256_finalize(&ctx);

C/precomputed.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include "sha256.h"
66

7+
/* Initial values for Simplicity's standard tagged message digest. */
8+
static const sha256_midstate signatureIV =
9+
{{0x9470c4e3u, 0xe445a32fu, 0x7e5273b8u, 0x33ead715u, 0xd509cbc5u, 0x1fd3feb9u, 0xacdac827u, 0xf31f4123u}};
10+
711
/* Initial values for all the 'typeName's. */
812
static const sha256_midstate unitIV =
913
{{0x12b4c4a9u, 0xa4b0edf6u, 0x5a44f30eu, 0xa762578fu, 0xdd59f105u, 0xf0e4d8f3u, 0x88cb9b6bu, 0xd2c13adfu}};

C/sha256.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ static inline sha256_context sha256_init(uint32_t* output) {
196196
return (sha256_context){ .output = output };
197197
}
198198

199+
/* Initialize a sha256_context given a buffer in which the final output will be written to,
200+
* and the midstate of a tagged hash.
201+
*
202+
* Note that the 'output' buffer may be updated during the computation to hold a SHA-256 midstate.
203+
* Precondition: unit32_t output[8]
204+
* unit32_t iv[8]
205+
*/
206+
static inline sha256_context sha256_tagged_init(uint32_t* output, const sha256_midstate* iv) {
207+
memcpy(output, iv->s, sizeof(uint32_t[8]));
208+
return (sha256_context){ .output = output, .counter = 64 };
209+
}
210+
199211
/* Add an array of bytes to be consumed by an ongoing SHA-256 evaluation.
200212
* Returns false if the counter overflows.
201213
*

Haskell-Generate/GenPrecomputed.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ prettyCHash h = bracket (format <$> chunksOf 8 str_h)
5050
declIV :: String -> IV -> Doc a
5151
declIV name iv = nest 2 $ (pretty $ "static const sha256_midstate "++name++"IV =") <-> (bracket . single . prettyCHash $ ivHash iv) <> semi
5252

53+
declareSignatureIV :: Doc a
54+
declareSignatureIV = vsep
55+
[ "/* Initial values for Simplicity's standard tagged message digest. */"
56+
, declIV "signature" signatureTag
57+
]
58+
5359
declareTyIVs :: Doc a
5460
declareTyIVs = vsep $ "/* Initial values for all the 'typeName's. */":(declTy <$> ["unit", "sum", "prod"])
5561
where
@@ -110,6 +116,7 @@ footer = vsep $
110116
precomputed_h :: SimpleDocStream a
111117
precomputed_h = layoutPretty layoutOptions $ vsep (map (<> line)
112118
[ header
119+
, declareSignatureIV
113120
, declareTyIVs
114121
, declareMRIVs
115122
, declareWord1CMR

Simplicity.Haskell.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mkDerivation (rec {
66
(lib.sourceByRegex ./. ["^LICENSE$" "^Simplicity\.cabal$" "^Setup.hs$" "^Tests.hs$" "^Haskell$" "^Haskell/.*"
77
"^Haskell-Generate$" "^Haskell-Generate/.*"
88
"^C$" "^C/uword.h" "^C/bitstring.h" "^C/frame.*" "^C/jets.*" "^C/sha256.*" "^C/unreachable.h"
9-
"^C/ascii.h" "^C/prefix.h" "^C/tag.h"
9+
"^C/precomputed.h" "^C/prefix.h"
1010
"^C/jets-secp256k1.c$" "^C/secp256k1$" "^C/secp256k1/.*"
1111
"^C/include$" "^C/include/simplicity$" "^C/include/simplicity/elements$" "^C/include/simplicity/elements/env.h"
1212
"^C/primitive$" "^C/primitive/elements$" "^C/primitive/elements/jets.*" "^C/primitive/elements/ops.*" "^C/primitive/elements/primitive.*" "^C/primitive/elements/env.c"])

0 commit comments

Comments
 (0)