Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ guest):
secp256k1 and P-256 signatures, modexp, BN254, BLS12-381 and KZG point
evaluation. The field arithmetic runs on Airbender's u256 delegation
(CSR 0x7ca), Keccak on its Keccak-f delegation.
- BLAKE2s-256 for ZKsync OS guests (`airbender_blake2s256`, `airbender_blake2s_compress`) on the
BLAKE2s round-function delegation (CSR 0x7c7), ten rounds per compression.

- Guests opt in to witness prehashing by calling `airbender_read_prehashed_input`
with the same arguments as `read_input`. It hashes witness nodes and code
Expand Down
4 changes: 2 additions & 2 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ case "$platform" in
*) echo "unknown platform $platform" >&2; exit 1 ;;
esac

sources=(keccak sha256 ripemd160 blake2f ecc modexp ec bn254 bls12 mont "platform_$platform")
sources=(keccak sha256 ripemd160 blake2f blake2s ecc modexp ec bn254 bls12 mont "platform_$platform")
# The u256 delegation exists only on Airbender; elsewhere u256_soft computes its results.
[ "$platform" = airbender ] || sources+=(u256_soft)
out="$TOP/output/$platform"
Expand Down Expand Up @@ -53,7 +53,7 @@ llvm-ar rcs "$out/libairbender.a" "$obj/libairbender.o"
# bflat provides: the memory functions of musl, the compiler-rt builtins and the platform interface of the entry.
required=(read_input write_output sys_write zkvm_keccak256 zkvm_sha256 zkvm_secp256k1_ecrecover)
[ "$platform" = airbender ] && required+=(airbender_read_prehashed_input airbender_exit airbender_fail airbender_u256_op airbender_keccak256
airbender_secp256k1_ecrecover_batch airbender_keccak_f1600_in_place)
airbender_secp256k1_ecrecover_batch airbender_keccak_f1600_in_place airbender_blake2s256 airbender_blake2s_compress)
for s in "${required[@]}"; do
grep -qx "$s" "$obj/exports.txt" || { echo "libairbender.a does not define $s" >&2; exit 1; }
done
Expand Down
153 changes: 153 additions & 0 deletions src/blake2s.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* BLAKE2s-256 (RFC 7693, 32-byte digest, no key, salt or personalization), the hash of ZKsync OS's state tree,
* preimages and commitments.
*
* On Airbender a compression is one run of the BLAKE2s round-function delegation (CSR 0x7c7,
* common_constants delegation_types/blake2s_with_control.rs of zksync-airbender 7a147da32e0c): x10 holds the
* state, the eight chaining words then the sixteen working words, 128-byte aligned in RAM; x11 the sixteen message
* words, 64-byte aligned in RAM; x12 the control word. In normal mode with full rounds the delegation loads the
* working words from the chaining words and the IV, except words 12 and 14, which the caller sets to t ^ IV[4] and
* f ^ IV[6]. Word 13 is always IV[5]: the length must stay below 4 GiB. The tenth round XORs both halves of the
* working words into the chaining words. The transpiler runs the ten rounds at the first of ten consecutive CSR
* accesses, so they are one asm block. Elsewhere the same compression is computed in software.
*/
#include "zkvm.h"

enum
{
BlockBytes = 64,
NormalModeFullRounds = 0x8u << 16
};

static const uint32_t Iv[8] = {0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19};

static uint32_t State[24] __attribute__((aligned(128)));
static uint32_t Block[16] __attribute__((aligned(64)));

#ifndef AIRBENDER
static const uint8_t Sigma[10][16] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4}, {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13}, {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11}, {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5}, {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}};

static inline uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }

static void mix(uint32_t *v, int a, int b, int c, int d, uint32_t x, uint32_t y)
{
v[a] = v[a] + v[b] + x;
v[d] = rotr(v[d] ^ v[a], 16);
v[c] = v[c] + v[d];
v[b] = rotr(v[b] ^ v[c], 12);
v[a] = v[a] + v[b] + y;
v[d] = rotr(v[d] ^ v[a], 8);
v[c] = v[c] + v[d];
v[b] = rotr(v[b] ^ v[c], 7);
}
#endif

/* Compresses Block into the chaining words of State, with working words 12 and 14 already set. */
static inline __attribute__((always_inline)) void compress(void)
{
#ifdef AIRBENDER
register uint32_t x10 __asm__("a0") = (uint32_t)State;
register uint32_t x11 __asm__("a1") = (uint32_t)Block;
register uint32_t x12 __asm__("a2") = NormalModeFullRounds;
__asm__ volatile("csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n"
"csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n"
"csrrw x0, 0x7c7, x0\n csrrw x0, 0x7c7, x0\n"
: "+r"(x12)
: "r"(x10), "r"(x11)
: "memory");
#else
uint32_t *v = State + 8;
for (int i = 0; i < 8; i++)
v[i] = State[i];
v[8] = Iv[0];
v[9] = Iv[1];
v[10] = Iv[2];
v[11] = Iv[3];
v[13] = Iv[5];
v[15] = Iv[7];
for (int round = 0; round < 10; round++)
{
const uint8_t *s = Sigma[round];
mix(v, 0, 4, 8, 12, Block[s[0]], Block[s[1]]);
mix(v, 1, 5, 9, 13, Block[s[2]], Block[s[3]]);
mix(v, 2, 6, 10, 14, Block[s[4]], Block[s[5]]);
mix(v, 3, 7, 11, 15, Block[s[6]], Block[s[7]]);
mix(v, 0, 5, 10, 15, Block[s[8]], Block[s[9]]);
mix(v, 1, 6, 11, 12, Block[s[10]], Block[s[11]]);
mix(v, 2, 7, 8, 13, Block[s[12]], Block[s[13]]);
mix(v, 3, 4, 9, 14, Block[s[14]], Block[s[15]]);
}
for (int i = 0; i < 8; i++)
State[i] ^= v[i] ^ v[i + 8];
#endif
}

static inline __attribute__((always_inline)) void compress_counted(uint32_t length, uint32_t last)
{
State[8 + 12] = length ^ Iv[4];
State[8 + 14] = (0u - (last != 0)) ^ Iv[6];
compress();
}

/* Loads len (at most 64) bytes into Block, zero-padded. */
static inline __attribute__((always_inline)) void load_block(const uint8_t *data, uint32_t len)
{
uint32_t words = len / 4;
for (uint32_t i = words; i < 16; i++)
Block[i] = 0;
if (((uintptr_t)data & 3) == 0)
{
for (uint32_t i = 0; i < words; i++)
__builtin_memcpy(&Block[i], __builtin_assume_aligned(data + 4 * i, 4), 4);
for (uint32_t i = words * 4; i < len; i++)
((uint8_t *)Block)[i] = data[i];
}
else
memcpy(Block, data, len);
}

static inline __attribute__((always_inline)) void store_chaining(uint32_t *h)
{
for (int i = 0; i < 8; i++)
h[i] = State[i];
}

/* One compression of the chaining words h (word aligned): the 64-byte block, the total length absorbed with it
* (below 4 GiB) and whether it is the last block. The block may have any alignment. */
void airbender_blake2s_compress(uint32_t h[8], const uint8_t *block, uint32_t length, uint32_t last)
{
for (int i = 0; i < 8; i++)
State[i] = h[i];
load_block(block, BlockBytes);
compress_counted(length, last);
store_chaining(h);
}

/* BLAKE2s-256 of the len (below 4 GiB) bytes at data, any alignment, into out (any alignment). */
void airbender_blake2s256(const uint8_t *data, uint32_t len, uint8_t out[32])
{
for (int i = 0; i < 8; i++)
State[i] = Iv[i];
/* Parameter block word 0: digest length 32, key length 0, fanout 1, depth 1. */
State[0] ^= 0x01010020;
uint32_t absorbed = 0;
while (len - absorbed > BlockBytes)
{
load_block(data, BlockBytes);
data += BlockBytes;
absorbed += BlockBytes;
compress_counted(absorbed, 0);
}
load_block(data, len - absorbed);
compress_counted(len, 1);
if (((uintptr_t)out & 3) == 0)
__builtin_memcpy(__builtin_assume_aligned(out, 4), State, 32);
else
memcpy(out, State, 32);
}
3 changes: 3 additions & 0 deletions src/zkvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void airbender_keccak256_laid_out(const uint8_t *data, size_t len, uint8_t out[3
#endif
void keccak_f1600_soft(uint64_t state[25]);

void airbender_blake2s256(const uint8_t *data, uint32_t len, uint8_t out[32]);
void airbender_blake2s_compress(uint32_t h[8], const uint8_t *block, uint32_t length, uint32_t last);

/* Recovers the uncompressed public keys (x || y, big-endian) of count signatures: msgs holds 32-byte hashes,
sigs 64-byte r || s, recids one byte each. ok[i] is 1 when outputs[i] holds signature i's key, as
zkvm_secp256k1_ecrecover would give it, and 0 when that call would fail. The inverses of every r and of every
Expand Down
17 changes: 17 additions & 0 deletions tests/blake2s.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <string.h>

#include "../src/zkvm.h"

int main(void)
{
/* The sanitizer must see the actual object bounds at the end of the partial word. */
_Alignas(4) const uint8_t abc[3] = {'a', 'b', 'c'};
uint8_t digest[32];
airbender_blake2s256(abc, sizeof(abc), digest);
assert(memcmp(digest, "\x50\x8c\x5e\x8c\x32\x7c\x14\xe2\xe1\xa7\x2b\xa3\x4e\xeb\x45\x2f"
"\x37\x45\x8b\x20\x9e\xd6\x3a\x29\x4d\x99\x9b\x4c\x86\x67\x59\x82", 32) == 0);
airbender_blake2s256(NULL, 0, digest);
assert(memcmp(digest, "\x69\x21\x7a\x30\x79\x90\x80\x94\xe1\x11\x21\xd0\x42\x35\x4a\x7c"
"\x1f\x55\xb6\x48\x2c\xa1\xa5\x1e\x1b\x25\x0d\xfd\x1e\xd0\xee\xf9", 32) == 0);
}
26 changes: 26 additions & 0 deletions tests/harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ int main(void)
st = zkvm_ripemd160(Args[0], ArgLen[0], out);
print_hex(out, 32);
}
else if (!strcmp(op, "blake2s"))
{
/* The one-shot hash from a misaligned copy too, and the same digest by the compression entry point. */
_Alignas(4) static uint8_t shifted[sizeof(Args[0]) + 1];
static uint32_t h[8];
static uint8_t block[64];
st = ZKVM_EOK;
memcpy(shifted + 1, Args[0], ArgLen[0]);
_Alignas(4) uint8_t again[33];
airbender_blake2s256(Args[0], (uint32_t)ArgLen[0], out);
airbender_blake2s256(shifted + 1, (uint32_t)ArgLen[0], again + 1);
static const uint32_t iv[8] = {0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19};
memcpy(h, iv, 32);
h[0] ^= 0x01010020;
uint32_t len = (uint32_t)ArgLen[0], done = 0;
for (; len - done > 64; done += 64)
airbender_blake2s_compress(h, shifted + 1 + done, done + 64, 0);
memset(block, 0, 64);
memcpy(block, Args[0] + done, len - done);
airbender_blake2s_compress(h, block, len, UINT32_MAX);
if (memcmp(out, again + 1, 32) || memcmp(out, h, 32))
printf("mismatch");
else
print_hex(out, 32);
}
else if (!strcmp(op, "blake2f"))
{
/* EIP-152 input: rounds(4 BE) h(64) m(128) t(16) f(1) */
Expand Down
3 changes: 3 additions & 0 deletions tests/platform/run-native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ flags=(-O2 -ffunction-sections -fdata-sections -Wl,--gc-sections -I src)
"$cc" "${flags[@]}" -fsanitize=address,undefined -fno-sanitize-recover=undefined \
tests/platform/input.c src/keccak.c src/u256_soft.c -o "$out/input"
ASAN_OPTIONS=detect_leaks=0 "$out/input"
"$cc" "${flags[@]}" -fsanitize=address,undefined -fno-sanitize-recover=undefined \
tests/blake2s.c src/blake2s.c -o "$out/blake2s"
"$out/blake2s"
"$cc" "${flags[@]}" tests/pairing-witness.c src/{ecc,bn254,bls12,mont,u256_soft}.c -o "$out/pairing-witness"
# One known non-identity pair on each curve, from the precompile corpus.
awk '$3 == "0" && (($1 == "bnpair" && length($2) == 384) || ($1 == "blspair" && length($2) == 576)) && !seen[$1]++ { print $1, $2 }' \
Expand Down
3 changes: 3 additions & 0 deletions tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def sign(cv, d, h):
cases.append(("ripemd -", "00" * 12 + "9c1185a5c5e9fc54612808977ee8f548b2258d31"))
cases.append(("ripemd 616263", "00" * 12 + "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"))

for data in [b"", b"abc"] + [rng.randbytes(n) for n in (1, 3, 31, 32, 63, 64, 65, 127, 128, 129, 1000)]:
cases.append((f"blake2s {data.hex() or '-'}", hashlib.blake2s(data).hexdigest()))

# blake2b("abc") is one F call with the final flag, rounds 12, over the parameter-xored IV.
iv = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
Expand Down
Loading