-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ethereum EIP-4844: Go wrapper for KZG commitments #318
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 just two small nits.
@@ -221,29 +221,29 @@ proc load_ckzg4844(ctx: ptr EthereumKZGContext, f: File): TrustedSetupStatus = | |||
|
|||
block: | |||
# G1 points - 96 characters + newline | |||
var bufG1Hex {.noInit.}: array[2*g1Bytes, char] | |||
var bufG1Hex {.noInit.}: array[2*g1Bytes+1, char] # On MacOS, an extra byte seems to be needed for fscanf or AddressSanitizer complains |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good find by the sanitizer. It's right. Because you're reading a string (rather than a byte like it's done in c-kzg-4844) it will add a null terminator at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually open files in binary mode not text (wb
):
constantine/constantine/platforms/fileio.nim
Lines 40 to 57 in c73ce0e
const | |
childProcNoInherit = block: | |
# Child processes should not inherit open files | |
when defined(windows): | |
"N" | |
elif defined(linux) or defined(bsd): | |
"e" | |
else: | |
"" | |
MapFileMode = [ | |
# We want to ensure no dynamic alloc at runtime with strings | |
kRead: cstring("rb" & childProcNoInherit), | |
kOverwrite: cstring("wb" & childProcNoInherit), | |
kAppend: cstring("ab" & childProcNoInherit), | |
kReadWrite: cstring("rb+" & childProcNoInherit), | |
kReadOverwrite: cstring("wb+" & childProcNoInherit) | |
] |
And the modifier for strings is %s, which doesn't appear in const readHexG1 = "%96[^\r\n]%n%*[\r\n]"
In fact, the sanitizer on Linux doesn't complain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't matter that it's opened in binary mode. It just matters that it's reading a string.
Here's an example with sscanf
which should operate the same:
#include <stdio.h>
int main(void)
{
char buffer[97];
int i, n;
const char *staticString = \
"0123456789" // 10
"0123456789" // 20
"0123456789" // 30
"0123456789" // 40
"0123456789" // 50
"0123456789" // 60
"0123456789" // 70
"0123456789" // 80
"0123456789" // 90
"012345"; // 96
for (i = 0; i < 97; i++) {
buffer[i] = 0xFF;
}
printf("\nInitial buffer contents:");
for (i = 0; i < 97; i++) {
if (i % 10 == 0) printf("\n");
printf("%02X ", (unsigned char)buffer[i]);
}
printf("\n");
sscanf(staticString, "%96[^\r\n]%n%*[\r\n]", buffer, &n);
printf("\nBuffer contents after sscanf:");
for (i = 0; i < 97; i++) {
if (i % 10 == 0) printf("\n");
printf("%02X ", (unsigned char)buffer[i]);
}
printf("\n");
printf("\nNumber of characters read: %d\n", n);
return 0;
}
For me this is the output:
$ make test && ./test
cc -I/opt/homebrew/include/ -L/opt/homebrew/include/ -L/opt/homebrew/lib/ test.c -o test
Initial buffer contents:
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF
Buffer contents after sscanf:
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 00
Number of characters read: 96
As you can see, the 97th byte has been set to 0 but only 96 bytes were read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the modifier for strings is
%s
, which doesn't appear inconst readHexG1 = "%96[^\r\n]%n%*[\r\n]"
In fact, the sanitizer on Linux doesn't complain.
Oops didn't respond to this. It doesn't need to be %s
for it to be recognized as a string.
This is a follow-up of #304.
It adds a go wrapper for Ethereum KZG commitments.
In particular this will allow differential fuzzing vs c-kzg-4844 and go-kzg-4844 in kzg-fuzz cc @jtraglia.
Some peculiarities:
.lib
file directly like C, Nim or Rust. We need to use-Wl,-Bstatic -lconstantine -Wl,-Bdynamic
as a workaround. Fix raised upstream cmd/go/internal/work/security.go: allow direct linking of Windows .lib static libraries golang/go#64679.Note that that workaround doesn't work on MacOS due to Xcode/Apple Clang not supporting the flag.
constantine/constantine/trusted_setups/ethereum_kzg_srs.nim
Lines 201 to 206 in 1f3e6b1
Running go, using the optional
CC=clang
for maximum performance:Constantine comes with a separate
go_test.mod
for extra test dependencies. Test dependencies must be specified with-modfile=../go_test.mod
. The library itself is dependency-free.For the trusted setup, Constantine supports the same format as c-kzg-4844.