Skip to content

Commit

Permalink
Merge #192: build(depends): update simplicity to b547f5041f0
Browse files Browse the repository at this point in the history
99d7b9f Run update_jets.sh (Christian Lewe)
3243e1d build(depends): update simplicity to b547f5041f0 (Byron Hambly)

Pull request description:

  used the `vendor-simplicity` script to update to the latest simplicity upstream which fixes windows compilation

  related to: #187

ACKs for top commit:
  uncomputable:
    ACK 99d7b9f The updates to the depend directory look good. I updated the jets in a second commit.

Tree-SHA512: 71ba038c6e63ae1b67386fb7d7b5fb0c429d9d906fe7953be3ff881645f836e082a3fe69515635fb42e68df61b89af5d09787d7ceb03283508735b1fb0c7eff4
  • Loading branch information
uncomputable committed Jan 16, 2024
2 parents 6813a5a + 99d7b9f commit f57f134
Show file tree
Hide file tree
Showing 28 changed files with 12,732 additions and 647 deletions.
4 changes: 2 additions & 2 deletions simplicity-sys/depend/simplicity-HEAD-revision.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# This file was automatically created by vendor-simplicity.sh
9c38bc04f5b6fbb62380bc4141ca0c980b4ee417
# This file was automatically created by ./vendor-simplicity.sh
b547f5041f061136d30d2291630b4b671a85ced5
12 changes: 6 additions & 6 deletions simplicity-sys/depend/simplicity/bitstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
#include "simplicity_assert.h"

/* Closes a bitstream by consuming all remaining bits.
* Returns 'SIMPLICITY_ERR_BITSTREAM_UNUSED_BYTES' if CHAR_BIT or more bits remain in the stream.
* Otherwise, returns 'SIMPLICITY_ERR_BITSTREAM_UNUSED_BITS' if any remaining bits are non-zero.
* Returns 'SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES' if CHAR_BIT or more bits remain in the stream.
* Otherwise, returns 'SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING' if any remaining bits are non-zero.
* Otherwise returns 'SIMPLICITY_NO_ERROR'.
*
* Precondition: NULL != stream
*/
simplicity_err closeBitstream(bitstream* stream) {
if (1 < stream->len) return SIMPLICITY_ERR_BITSTREAM_UNUSED_BYTES; /* If there is more than one byte remaining. */
if (1 < stream->len) return SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES; /* If there is more than one byte remaining. */
if (1 == stream->len) {
if (0 == stream->offset) return SIMPLICITY_ERR_BITSTREAM_UNUSED_BYTES; /* If there is one byte remaining */
if (0 != (*stream->arr & (UCHAR_MAX >> stream->offset))) { /* If any of the unconsumed bits are non-zero */
return SIMPLICITY_ERR_BITSTREAM_UNUSED_BITS;
if (0 == stream->offset) return SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES; /* If there is one byte remaining */
if (0 != (*stream->arr & (UCHAR_MAX >> stream->offset))) { /* If any of the unconsumed bits are non-zero */
return SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING;
}
}
/* Otherwise there are either 0 bits remaining or there are between 1 and CHAR_BITS-1 bits remaining and they are all zero. */
Expand Down
6 changes: 3 additions & 3 deletions simplicity-sys/depend/simplicity/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef SIMPLICITY_BITSTREAM_H
#define SIMPLICITY_BITSTREAM_H

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <simplicity/errorCodes.h>
Expand Down Expand Up @@ -30,8 +30,8 @@ static inline bitstream initializeBitstream(const unsigned char* arr, size_t len
}

/* Closes a bitstream by consuming all remaining bits.
* Returns 'SIMPLICITY_ERR_BITSTREAM_UNUSED_BYTES' if CHAR_BIT or more bits remain in the stream.
* Otherwise, returns 'SIMPLICITY_ERR_BITSTREAM_UNUSED_BITS' if any remaining bits are non-zero.
* Returns 'SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES' if CHAR_BIT or more bits remain in the stream.
* Otherwise, returns 'SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING' if any remaining bits are non-zero.
* Otherwise returns 'SIMPLICITY_NO_ERROR'.
*
* Precondition: NULL != stream
Expand Down
19 changes: 19 additions & 0 deletions simplicity-sys/depend/simplicity/bitstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef SIMPLICITY_BITSTRING_H
#define SIMPLICITY_BITSTRING_H

#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include "simplicity_assert.h"
Expand Down Expand Up @@ -32,4 +34,21 @@ static inline bool getBit(const bitstring *s, size_t n) {
return 1 & (s->arr[total_offset / CHAR_BIT] >> (CHAR_BIT - 1 - (total_offset % CHAR_BIT)));
}

/* Return 8 bits from a bitstring staring from the nth bit.
*
* Precondition: NULL != s
* n + 8 <= s->len;
*/
static inline uint_fast8_t getByte(const bitstring *s, size_t n) {
simplicity_assert(8 <= s->len);
simplicity_assert(n <= s->len - 8);
size_t total_offset = s->offset + n;
if (total_offset % CHAR_BIT <= CHAR_BIT - 8) {
return (uint_fast8_t)(0xff & (s->arr[total_offset / CHAR_BIT] >> (CHAR_BIT - 8 - (total_offset % CHAR_BIT))));
} else {
/* CHAR_BIT < total_offset % CHAR_BIT + 8 */
return (uint_fast8_t)(0xff & (((1U * s->arr[total_offset / CHAR_BIT]) << (total_offset % CHAR_BIT - (CHAR_BIT - 8)))
| (s->arr[total_offset / CHAR_BIT + 1] >> (CHAR_BIT - (total_offset % CHAR_BIT - (CHAR_BIT - 8))))));
}
}
#endif
6 changes: 3 additions & 3 deletions simplicity-sys/depend/simplicity/bounded.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
typedef uint_least32_t ubounded;
#define UBOUNDED_MAX UINT32_MAX

static inline ubounded max(ubounded x, ubounded y) {
static inline ubounded bounded_max(ubounded x, ubounded y) {
return x <= y ? y : x;
}

Expand All @@ -27,8 +27,8 @@ static inline void bounded_inc(ubounded* x) {
* 'pad( true, a, b)' computes the PADR(a, b) function.
*/
static inline ubounded pad(bool right, ubounded a, ubounded b) {
return max(a, b) - (right ? b : a);
return bounded_max(a, b) - (right ? b : a);
}

static const ubounded overhead = 100 /* milli weight units */;
enum { overhead = 100 }; /* milli weight units */
#endif
40 changes: 24 additions & 16 deletions simplicity-sys/depend/simplicity/dag.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,39 @@ static sha256_midstate mkJetCMR(uint32_t *imr, uint_fast64_t weight) {
* Precondition: 2^n == value->len
*/
sha256_midstate computeWordCMR(const bitstring* value, size_t n) {
/* 'stack' is an array of 33 hashes consisting of 8 'uint32_t's each. */
uint32_t stack[8*33] = {0};
/* 'stack' is an array of 30 hashes consisting of 8 'uint32_t's each. */
uint32_t stack[8*30] = {0};
uint32_t *stack_ptr = stack;
sha256_midstate imr = identityIV;
simplicity_assert(n < 32);
simplicity_assert((size_t)1 << n == value->len);
/* Pass 1: Compute the CMR for the expression that writes 'value'.
* This expression consists of deeply nested PAIRs of expressions that write one bit each.
*
* :TODO: This can be optimized by a constant factor by precomputing a table of CMRs of expressions
* that, for example, write out every possible byte sequence.
*/
/* stack[0..7] (8 bytes) is kept as all zeros for later.
* We start the stack_ptr at the second item.
*/
for(size_t i = 0; i < value->len; ++i) {
/* stack_ptr == stack + 8*<count of the number of set bits in the value i> */
if (n < 3) {
stack_ptr += 8;
memcpy(stack_ptr, &bit_cmr[getBit(value, i)], sizeof(uint32_t[8]));
/* This inner for loop runs in ammortized constant time. */
for (size_t j = i; j & 1; j = j >> 1) {
sha256_midstate pair = cmrIV(PAIR);
stack_ptr -= 8;
sha256_compression(pair.s, stack_ptr);
memcpy(stack_ptr, pair.s, sizeof(uint32_t[8]));
size_t i;
switch(n) {
case 0: i = getBit(value, 0); break;
case 1: i = 2 + ((1U * getBit(value, 0) << 1) | getBit(value, 1)); break;
case 2: i = 6 + ((1U * getBit(value, 0) << 3) | (1U * getBit(value, 1) << 2) | (1U * getBit(value, 2) << 1) | getBit(value, 3)); break;
}
memcpy(stack_ptr, &word_cmr[i], sizeof(uint32_t[8]));
} else {
for (size_t i = 0; i < value->len >> 3; ++i) {
/* stack_ptr == stack + 8*<count of the number of set bits in the value i> */
stack_ptr += 8;
memcpy(stack_ptr, &word_cmr[22 + getByte(value, 8*i)], sizeof(uint32_t[8]));
/* This inner for loop runs in amortized constant time. */
for (size_t j = i; j & 1; j = j >> 1) {
sha256_midstate pair = cmrIV(PAIR);
stack_ptr -= 8;
sha256_compression(pair.s, stack_ptr);
memcpy(stack_ptr, pair.s, sizeof(uint32_t[8]));
}
}
}
/* value->len is a power of 2.*/
Expand Down Expand Up @@ -464,7 +472,7 @@ simplicity_err verifyCanonicalOrder(dag_node* dag, const size_t len) {
* For each 'WITNESS' : A |- B expression in 'dag', the bits from the 'witness' bitstring are decoded in turn
* to construct a compact representation of a witness value of type B.
* This function only returns 'SIMPLICITY_NO_ERROR' when exactly 'witness.len' bits are consumed by all the 'dag's witness values.
* If extra bits remain, then 'SIMPLICITY_ERR_WITNESS_UNUSED_BITS' is returned.
* If extra bits remain, then 'SIMPLICITY_ERR_WITNESS_TRAILING_BITS' is returned.
* If there are not enough bits, then 'SIMPLICITY_ERR_WITNESS_EOF' is returned.
*
* Note: the 'witness' value is passed by copy because the implementation manipulates a local copy of the structure.
Expand Down Expand Up @@ -549,7 +557,7 @@ simplicity_err fillWitnessData(dag_node* dag, type* type_dag, const size_t len,
}
}
}
return 0 == witness.len ? SIMPLICITY_NO_ERROR : SIMPLICITY_ERR_WITNESS_UNUSED_BITS;
return 0 == witness.len ? SIMPLICITY_NO_ERROR : SIMPLICITY_ERR_WITNESS_TRAILING_BITS;
}

/* Verifies that identity Merkle roots of every subexpression in a well-typed 'dag' with witnesses are all unique,
Expand Down
2 changes: 1 addition & 1 deletion simplicity-sys/depend/simplicity/dag.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ simplicity_err verifyCanonicalOrder(dag_node* dag, const size_t len);
* For each 'WITNESS' : A |- B expression in 'dag', the bits from the 'witness' bitstring are decoded in turn
* to construct a compact representation of a witness value of type B.
* This function only returns 'SIMPLICITY_NO_ERROR' when exactly 'witness.len' bits are consumed by all the 'dag's witness values.
* If extra bits remain, then 'SIMPLICITY_ERR_WITNESS_UNUSED_BITS' is returned.
* If extra bits remain, then 'SIMPLICITY_ERR_WITNESS_TRAILING_BITS' is returned.
* If there are not enough bits, then 'SIMPLICITY_ERR_WITNESS_EOF' is returned.
*
* Precondition: dag_node dag[len] and 'dag' without witness data and is well-typed with 'type_dag';
Expand Down
Loading

0 comments on commit f57f134

Please sign in to comment.