Skip to content

Commit 3b60583

Browse files
Squashed 'src/simplicity/' changes from 86ac0f92c4..c2d4c3d07b
c2d4c3d07b Update elements-sources.mk 5ba4a709ac Add explicit deallocation functions to the Simplicity API fd3a1a7c3f Fix comments e10d34fa49 Clarify comment about illegal hidden children 3b55e8150e Rename STOP code error message f67d1ad145 Generate contents of decodePrimitive automatically 991fddcb6a Correctly name identity hashes a840706c2f Static asserts on imr_buf not needed 7e91641218 Prep C code for VST proving git-subtree-dir: src/simplicity git-subtree-split: c2d4c3d07bb7e5973fc22cf172ed8fb9a84b4365
1 parent f502c24 commit 3b60583

32 files changed

+1352
-1228
lines changed

ctx8Pruned.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ const uint32_t ctx8Pruned_cmr[] = {
270270
0x7f11746fu, 0xb68fdaedu, 0x3cadda80u, 0xc7cd0245u, 0xa341b927u, 0xe98e60f8u, 0x745dc441u, 0xe11ce1a3u
271271
};
272272

273-
/* The identity Merkle root of the above ctx8Pruned Simplicity expression. */
274-
const uint32_t ctx8Pruned_imr[] = {
273+
/* The identity hash of the root of the above ctx8Pruned Simplicity expression. */
274+
const uint32_t ctx8Pruned_ihr[] = {
275275
0x8e8742acu, 0x27f42d29u, 0xd87f5229u, 0x02bc0ae2u, 0xbcfc1298u, 0x1641a2ddu, 0x77091830u, 0xb79bf12du
276276
};
277277

ctx8Pruned.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ extern const size_t sizeof_ctx8Pruned_witness;
1818
/* The commitment Merkle root of the above ctx8Pruned Simplicity expression. */
1919
extern const uint32_t ctx8Pruned_cmr[];
2020

21-
/* The identity Merkle root of the above ctx8Pruned Simplicity expression. */
22-
extern const uint32_t ctx8Pruned_imr[];
21+
/* The identity hash of the root of the above ctx8Pruned Simplicity expression. */
22+
extern const uint32_t ctx8Pruned_ihr[];
2323

2424
/* The annotated Merkle root of the above ctx8Pruned Simplicity expression. */
2525
extern const uint32_t ctx8Pruned_amr[];

ctx8Unpruned.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ const uint32_t ctx8Unpruned_cmr[] = {
260260
0x7f11746fu, 0xb68fdaedu, 0x3cadda80u, 0xc7cd0245u, 0xa341b927u, 0xe98e60f8u, 0x745dc441u, 0xe11ce1a3u
261261
};
262262

263-
/* The identity Merkle root of the above ctx8Unpruned Simplicity expression. */
264-
const uint32_t ctx8Unpruned_imr[] = {
263+
/* The identity hash of the root of the above ctx8Unpruned Simplicity expression. */
264+
const uint32_t ctx8Unpruned_ihr[] = {
265265
0x8e8742acu, 0x27f42d29u, 0xd87f5229u, 0x02bc0ae2u, 0xbcfc1298u, 0x1641a2ddu, 0x77091830u, 0xb79bf12du
266266
};
267267

ctx8Unpruned.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ extern const size_t sizeof_ctx8Unpruned_witness;
1818
/* The commitment Merkle root of the above ctx8Unpruned Simplicity expression. */
1919
extern const uint32_t ctx8Unpruned_cmr[];
2020

21-
/* The identity Merkle root of the above ctx8Unpruned Simplicity expression. */
22-
extern const uint32_t ctx8Unpruned_imr[];
21+
/* The identity hash of the root of the above ctx8Unpruned Simplicity expression. */
22+
extern const uint32_t ctx8Unpruned_ihr[];
2323

2424
/* The annotated Merkle root of the above ctx8Unpruned Simplicity expression. */
2525
extern const uint32_t ctx8Unpruned_amr[];

dag.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ static sha256_midstate amrIV(tag_t tag) {
7777
SIMPLICITY_UNREACHABLE;
7878
}
7979

80-
/* Given the IMR of a jet specification, return the CMR for a jet that implements that specification.
80+
/* Given the identity hash of a jet specification, return the CMR for a jet that implements that specification.
8181
*
82-
* Precondition: uint32_t imr[8]
82+
* Precondition: uint32_t ih[8]
8383
*/
84-
static sha256_midstate mkJetCMR(uint32_t *imr, uint_fast64_t weight) {
84+
static sha256_midstate mkJetCMR(uint32_t *ih, uint_fast64_t weight) {
8585
sha256_midstate result = jetIV;
8686
uint32_t block[16] = {0};
8787
block[6] = (uint32_t)(weight >> 32);
8888
block[7] = (uint32_t)weight;
89-
memcpy(&block[8], imr, sizeof(uint32_t[8]));
89+
memcpy(&block[8], ih, sizeof(uint32_t[8]));
9090
simplicity_sha256_compression(result.s, block);
9191

9292
return result;
@@ -100,7 +100,7 @@ sha256_midstate simplicity_computeWordCMR(const bitstring* value, size_t n) {
100100
/* 'stack' is an array of 30 hashes consisting of 8 'uint32_t's each. */
101101
uint32_t stack[8*30] = {0};
102102
uint32_t *stack_ptr = stack;
103-
sha256_midstate imr = identityIV;
103+
sha256_midstate ih = identityIV;
104104
simplicity_assert(n < 32);
105105
simplicity_assert((size_t)1 << n == value->len);
106106
/* Pass 1: Compute the CMR for the expression that writes 'value'.
@@ -135,14 +135,14 @@ sha256_midstate simplicity_computeWordCMR(const bitstring* value, size_t n) {
135135
/* value->len is a power of 2.*/
136136
simplicity_assert(stack_ptr == stack + 8);
137137

138-
/* Pass 2: Compute the IMR for the expression by adding the type roots of ONE and TWO^(2^n) to the CMR. */
139-
simplicity_sha256_compression(imr.s, stack);
138+
/* Pass 2: Compute the identity hash for the expression by adding the type roots of ONE and TWO^(2^n) to the CMR. */
139+
simplicity_sha256_compression(ih.s, stack);
140140
memcpy(&stack[0], word_type_root[0].s, sizeof(uint32_t[8]));
141141
memcpy(&stack[8], word_type_root[n+1].s, sizeof(uint32_t[8]));
142-
simplicity_sha256_compression(imr.s, stack);
142+
simplicity_sha256_compression(ih.s, stack);
143143

144-
/* Pass 3: Compute the jet's CMR from the specificion's IMR. */
145-
return mkJetCMR(imr.s, ((uint_fast64_t)1 << n));
144+
/* Pass 3: Compute the jet's CMR from the specificion's identity hash. */
145+
return mkJetCMR(ih.s, ((uint_fast64_t)1 << n));
146146
}
147147

148148
/* Given a well-formed dag[i + 1], such that for all 'j', 0 <= 'j' < 'i',
@@ -192,45 +192,45 @@ void simplicity_computeCommitmentMerkleRoot(dag_node* dag, const uint_fast32_t i
192192
}
193193
}
194194

195-
/* Computes the identity Merkle roots of every subexpression in a well-typed 'dag' with witnesses.
196-
* 'imr[i]' is set to the identity Merkle root of the subexpression 'dag[i]'.
197-
* When 'HIDDEN == dag[i].tag', then 'imr[i]' is instead set to a hidden root hash for that hidden node.
195+
/* Computes the identity hash roots of every subexpression in a well-typed 'dag' with witnesses.
196+
* 'ihr[i]' is set to the identity hash of the root of the subexpression 'dag[i]'.
197+
* When 'HIDDEN == dag[i].tag', then 'ihr[i]' is instead set to a hidden root hash for that hidden node.
198198
*
199-
* Precondition: sha256_midstate imr[len];
199+
* Precondition: sha256_midstate ihr[len];
200200
* dag_node dag[len] and 'dag' is well-typed with 'type_dag' and contains witnesses.
201201
*/
202-
static void computeIdentityMerkleRoot(sha256_midstate* imr, const dag_node* dag, const type* type_dag, const uint_fast32_t len) {
202+
static void computeIdentityHashRoots(sha256_midstate* ihr, const dag_node* dag, const type* type_dag, const uint_fast32_t len) {
203203
/* Pass 1 */
204204
for (size_t i = 0; i < len; ++i) {
205205
uint32_t block[16] = {0};
206206
size_t j = 8;
207207

208208
/* For jets, the first pass identity Merkle root is the same as their commitment Merkle root. */
209-
imr[i] = HIDDEN == dag[i].tag ? dag[i].cmr
209+
ihr[i] = HIDDEN == dag[i].tag ? dag[i].cmr
210210
: JET == dag[i].tag ? dag[i].cmr
211211
: WORD == dag[i].tag ? dag[i].cmr
212212
: imrIV(dag[i].tag);
213213
switch (dag[i].tag) {
214214
case WITNESS:
215215
simplicity_sha256_bitstring(block, &dag[i].compactValue);
216216
memcpy(block + 8, type_dag[WITNESS_B(dag, type_dag, i)].typeMerkleRoot.s, sizeof(uint32_t[8]));
217-
simplicity_sha256_compression(imr[i].s, block);
217+
simplicity_sha256_compression(ihr[i].s, block);
218218
break;
219219
case COMP:
220220
case ASSERTL:
221221
case ASSERTR:
222222
case CASE:
223223
case PAIR:
224224
case DISCONNECT:
225-
memcpy(block + j, imr[dag[i].child[1]].s, sizeof(uint32_t[8]));
225+
memcpy(block + j, ihr[dag[i].child[1]].s, sizeof(uint32_t[8]));
226226
j = 0;
227227
/*@fallthrough@*/
228228
case INJL:
229229
case INJR:
230230
case TAKE:
231231
case DROP:
232-
memcpy(block + j, imr[dag[i].child[0]].s, sizeof(uint32_t[8]));
233-
simplicity_sha256_compression(imr[i].s, block);
232+
memcpy(block + j, ihr[dag[i].child[0]].s, sizeof(uint32_t[8]));
233+
simplicity_sha256_compression(ihr[i].s, block);
234234
case IDEN:
235235
case UNIT:
236236
case HIDDEN:
@@ -245,16 +245,16 @@ static void computeIdentityMerkleRoot(sha256_midstate* imr, const dag_node* dag,
245245
uint32_t block[16] = {0};
246246

247247
if (HIDDEN == dag[i].tag) {
248-
memcpy(block + 8, imr[i].s, sizeof(uint32_t[8]));
249-
imr[i] = hiddenIV;
250-
simplicity_sha256_compression(imr[i].s, block);
248+
memcpy(block + 8, ihr[i].s, sizeof(uint32_t[8]));
249+
ihr[i] = hiddenIV;
250+
simplicity_sha256_compression(ihr[i].s, block);
251251
} else {
252-
memcpy(block + 8, imr[i].s, sizeof(uint32_t[8]));
253-
imr[i] = identityIV;
254-
simplicity_sha256_compression(imr[i].s, block);
252+
memcpy(block + 8, ihr[i].s, sizeof(uint32_t[8]));
253+
ihr[i] = identityIV;
254+
simplicity_sha256_compression(ihr[i].s, block);
255255
memcpy(block, type_dag[dag[i].sourceType].typeMerkleRoot.s, sizeof(uint32_t[8]));
256256
memcpy(block + 8, type_dag[dag[i].targetType].typeMerkleRoot.s, sizeof(uint32_t[8]));
257-
simplicity_sha256_compression(imr[i].s, block);
257+
simplicity_sha256_compression(ihr[i].s, block);
258258
}
259259
}
260260
}
@@ -559,30 +559,30 @@ simplicity_err simplicity_fillWitnessData(dag_node* dag, type* type_dag, const u
559559
return SIMPLICITY_NO_ERROR;
560560
}
561561

562-
/* Verifies that identity Merkle roots of every subexpression in a well-typed 'dag' with witnesses are all unique,
562+
/* Verifies that identity hash of every subexpression in a well-typed 'dag' with witnesses are all unique,
563563
* including that each hidden root hash for every 'HIDDEN' node is unique.
564564
*
565-
* if 'imr' is not NULL, then '*imr' is set to the identity Merkle root of the 'dag'.
565+
* if 'ihr' is not NULL, then '*ihr' is set to the identity hash of the root of the 'dag'.
566566
*
567567
* If malloc fails, returns 'SIMPLICITY_ERR_MALLOC'.
568-
* If all the identity Merkle roots (and hidden roots) are all unique, returns 'SIMPLICITY_NO_ERROR'.
568+
* If all the identity hahes (and hidden roots) are all unique, returns 'SIMPLICITY_NO_ERROR'.
569569
* Otherwise returns 'SIMPLICITY_ERR_UNSHARED_SUBEXPRESSION'.
570570
*
571571
* Precondition: dag_node dag[len] and 'dag' is well-typed with 'type_dag' and contains witnesses.
572572
*/
573-
simplicity_err simplicity_verifyNoDuplicateIdentityRoots(sha256_midstate* imr, const dag_node* dag, const type* type_dag, const uint_fast32_t dag_len) {
573+
simplicity_err simplicity_verifyNoDuplicateIdentityHashes(sha256_midstate* ihr, const dag_node* dag, const type* type_dag, const uint_fast32_t dag_len) {
574574
simplicity_assert(0 < dag_len);
575575
simplicity_assert(dag_len <= DAG_LEN_MAX);
576-
sha256_midstate* imr_buf = simplicity_malloc((size_t)dag_len * sizeof(sha256_midstate));
577-
if (!imr_buf) return SIMPLICITY_ERR_MALLOC;
576+
sha256_midstate* ih_buf = simplicity_malloc((size_t)dag_len * sizeof(sha256_midstate));
577+
if (!ih_buf) return SIMPLICITY_ERR_MALLOC;
578578

579-
computeIdentityMerkleRoot(imr_buf, dag, type_dag, dag_len);
579+
computeIdentityHashRoots(ih_buf, dag, type_dag, dag_len);
580580

581-
if (imr) *imr = imr_buf[dag_len-1];
581+
if (ihr) *ihr = ih_buf[dag_len-1];
582582

583-
int result = simplicity_hasDuplicates(imr_buf, dag_len);
583+
int result = simplicity_hasDuplicates(ih_buf, dag_len);
584584

585-
simplicity_free(imr_buf);
585+
simplicity_free(ih_buf);
586586

587587
switch (result) {
588588
case -1: return SIMPLICITY_ERR_MALLOC;

dag.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,17 @@ simplicity_err simplicity_verifyCanonicalOrder(dag_node* dag, const uint_fast32_
377377
*/
378378
simplicity_err simplicity_fillWitnessData(dag_node* dag, type* type_dag, const uint_fast32_t len, bitstream *witness);
379379

380-
/* Verifies that identity Merkle roots of every subexpression in a well-typed 'dag' with witnesses are all unique,
380+
/* Verifies that identity hash of every subexpression in a well-typed 'dag' with witnesses are all unique,
381381
* including that each hidden root hash for every 'HIDDEN' node is unique.
382382
*
383-
* if 'imr' is not NULL, then '*imr' is set to the identity Merkle root of the 'dag'.
383+
* if 'ihr' is not NULL, then '*ihr' is set to the identity hash of the root of the 'dag'.
384384
*
385385
* If malloc fails, returns 'SIMPLICITY_ERR_MALLOC'.
386-
* If all the identity Merkle roots (and hidden roots) are all unique, returns 'SIMPLICITY_NO_ERROR'.
386+
* If all the identity hahes (and hidden roots) are all unique, returns 'SIMPLICITY_NO_ERROR'.
387387
* Otherwise returns 'SIMPLICITY_ERR_UNSHARED_SUBEXPRESSION'.
388388
*
389389
* Precondition: dag_node dag[len] and 'dag' is well-typed with 'type_dag' and contains witnesses.
390390
*/
391-
simplicity_err simplicity_verifyNoDuplicateIdentityRoots(sha256_midstate* imr, const dag_node* dag, const type* type_dag, const uint_fast32_t dag_len);
391+
simplicity_err simplicity_verifyNoDuplicateIdentityHashes(sha256_midstate* ihr, const dag_node* dag, const type* type_dag, const uint_fast32_t dag_len);
392392

393393
#endif

0 commit comments

Comments
 (0)