Skip to content

Commit

Permalink
chore: all hashes in ts (#3333)
Browse files Browse the repository at this point in the history
This is mostly just removing a bunch of async/await stuff as we convert
the final 3 functions in abis.ts to use `foundation/crypto/pedersen`.

There are two things that are iffy, but not sure if that should block a
merge as everything passes:
* `vkHash`, I've hardcoded a couple of values I don't fully understand
(coset generator and root, which are pulled from evalutation domain in
the C++).
* `computePublicCallStackItemHash` the C++ did something funky when
`isExecutionRequest` is true. I don't fully understand what and if it
needs reproducing here, but again, everything passes.

Have left the commented C++ code in the two places above for reference.
  • Loading branch information
charlielye committed Nov 18, 2023
1 parent 48d8c7f commit 6307e12
Show file tree
Hide file tree
Showing 121 changed files with 1,888 additions and 930 deletions.
25 changes: 14 additions & 11 deletions barretenberg/cpp/src/barretenberg/crypto/aes128/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include "c_bind.hpp"
#include "aes128.hpp"
#include "barretenberg/common/wasm_export.hpp"
#include "barretenberg/common/serialize.hpp"

WASM_EXPORT void aes__encrypt_buffer_cbc(uint8_t* in, uint8_t* iv, const uint8_t* key, const size_t length, uint8_t* r)
WASM_EXPORT void aes_encrypt_buffer_cbc(
uint8_t const* in, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r)
{
crypto::aes128::encrypt_buffer_cbc(in, iv, key, length);
for (size_t i = 0; i < length; ++i) {
r[i] = in[i];
}
auto len = ntohl(*length);
crypto::aes128::encrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
std::vector<uint8_t> result(in, in + len);
*r = to_heap_buffer(result);
}

WASM_EXPORT void aes__decrypt_buffer_cbc(uint8_t* in, uint8_t* iv, const uint8_t* key, const size_t length, uint8_t* r)
WASM_EXPORT void aes_decrypt_buffer_cbc(
uint8_t const* in, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r)
{
crypto::aes128::decrypt_buffer_cbc(in, iv, key, length);
for (size_t i = 0; i < length; ++i) {
r[i] = in[i];
}
auto len = ntohl(*length);
crypto::aes128::decrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
std::vector<uint8_t> result(in, in + len);
*r = to_heap_buffer(result);
}
11 changes: 11 additions & 0 deletions barretenberg/cpp/src/barretenberg/crypto/aes128/c_bind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <barretenberg/common/serialize.hpp>
#include <barretenberg/common/wasm_export.hpp>
#include <cstddef>
#include <cstdint>

WASM_EXPORT void aes_encrypt_buffer_cbc(
uint8_t const* input, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r);

WASM_EXPORT void aes_decrypt_buffer_cbc(
uint8_t const* input, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r);
10 changes: 10 additions & 0 deletions barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ WASM_EXPORT void pedersen_hash(uint8_t const* inputs_buffer, uint32_t const* has
auto r = crypto::pedersen_hash::hash(to_hash, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen_hash_buffer(uint8_t const* input_buffer, uint32_t const* hash_index, uint8_t* output)
{
std::vector<uint8_t> to_hash;
read(input_buffer, to_hash);
crypto::GeneratorContext<curve::Grumpkin> ctx;
ctx.offset = static_cast<size_t>(ntohl(*hash_index));
auto r = crypto::pedersen_hash::hash_buffer(to_hash, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ extern "C" {
using namespace barretenberg;

WASM_EXPORT void pedersen_hash(fr::vec_in_buf inputs_buffer, uint32_t const* hash_index, fr::out_buf output);

WASM_EXPORT void pedersen_hash_buffer(uint8_t const* input_buffer, uint32_t const* hash_index, fr::out_buf output);
}
76 changes: 76 additions & 0 deletions barretenberg/exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@
],
"isAsync": false
},
{
"functionName": "pedersen_hash_buffer",
"inArgs": [
{
"name": "input_buffer",
"type": "const uint8_t *"
},
{
"name": "hash_index",
"type": "const uint32_t *"
}
],
"outArgs": [
{
"name": "output",
"type": "fr::out_buf"
}
],
"isAsync": false
},
{
"functionName": "blake2s",
"inArgs": [
Expand Down Expand Up @@ -274,6 +294,62 @@
],
"isAsync": false
},
{
"functionName": "aes_encrypt_buffer_cbc",
"inArgs": [
{
"name": "input",
"type": "const uint8_t *"
},
{
"name": "iv",
"type": "const uint8_t *"
},
{
"name": "key",
"type": "const uint8_t *"
},
{
"name": "length",
"type": "const uint32_t *"
}
],
"outArgs": [
{
"name": "r",
"type": "uint8_t **"
}
],
"isAsync": false
},
{
"functionName": "aes_decrypt_buffer_cbc",
"inArgs": [
{
"name": "input",
"type": "const uint8_t *"
},
{
"name": "iv",
"type": "const uint8_t *"
},
{
"name": "key",
"type": "const uint8_t *"
},
{
"name": "length",
"type": "const uint32_t *"
}
],
"outArgs": [
{
"name": "r",
"type": "uint8_t **"
}
],
"isAsync": false
},
{
"functionName": "srs_init_srs",
"inArgs": [
Expand Down
1 change: 1 addition & 0 deletions barretenberg/scripts/c_bind_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
./cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp
./cpp/src/barretenberg/crypto/blake2s/c_bind.hpp
./cpp/src/barretenberg/crypto/schnorr/c_bind.hpp
./cpp/src/barretenberg/crypto/aes128/c_bind.hpp
./cpp/src/barretenberg/srs/c_bind.hpp
./cpp/src/barretenberg/examples/c_bind.hpp
./cpp/src/barretenberg/common/c_bind.hpp
Expand Down
156 changes: 156 additions & 0 deletions barretenberg/ts/src/barretenberg/__snapshots__/pedersen.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pedersen sync pedersenCommit 1`] = `
Point {
"x": Fr {
"value": Uint8Array [
40,
159,
125,
144,
234,
153,
219,
166,
76,
75,
47,
51,
253,
27,
9,
101,
2,
145,
223,
38,
43,
114,
5,
21,
90,
97,
2,
6,
219,
97,
109,
152,
],
},
"y": Fr {
"value": Uint8Array [
5,
175,
199,
200,
35,
67,
88,
76,
19,
203,
45,
50,
137,
153,
67,
200,
57,
87,
22,
209,
141,
173,
205,
189,
23,
215,
206,
3,
174,
112,
128,
11,
],
},
}
`;

exports[`pedersen sync pedersenHash 1`] = `
Fr {
"value": Uint8Array [
4,
194,
53,
42,
6,
13,
74,
193,
205,
251,
96,
62,
188,
67,
39,
181,
118,
69,
151,
35,
22,
20,
246,
29,
36,
91,
243,
87,
114,
192,
134,
150,
],
}
`;

exports[`pedersen sync pedersenHashBuffer 1`] = `
Fr {
"value": Uint8Array [
43,
213,
196,
82,
160,
201,
113,
98,
41,
79,
201,
223,
208,
241,
224,
157,
14,
9,
201,
95,
165,
237,
63,
241,
73,
251,
222,
243,
102,
203,
81,
249,
],
}
`;
70 changes: 70 additions & 0 deletions barretenberg/ts/src/barretenberg/blake2s.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Barretenberg, BarretenbergSync } from './index.js';
import { Buffer32, Fr } from '../types/index.js';

describe('blake2s async', () => {
let api: Barretenberg;

beforeAll(async () => {
api = await Barretenberg.new(1);
});

afterAll(async () => {
await api.destroy();
});

it('blake2s', async () => {
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = Buffer32.fromBuffer(
new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]),
);
const result = await api.blake2s(input);
expect(result).toEqual(expected);
});

it('blake2sToField', async () => {
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = Fr.fromBufferReduce(
new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]),
);
const result = await api.blake2sToField(input);
expect(result).toEqual(expected);
});
});

describe('blake2s sync', () => {
let api: BarretenbergSync;

beforeAll(async () => {
api = await BarretenbergSync.new();
});

it('blake2s', () => {
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = Buffer32.fromBuffer(
new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]),
);
const result = api.blake2s(input);
expect(result).toEqual(expected);
});

it('blake2sToField', () => {
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = Fr.fromBufferReduce(
new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]),
);
const result = api.blake2sToField(input);
expect(result).toEqual(expected);
});
});

0 comments on commit 6307e12

Please sign in to comment.