Skip to content

Commit

Permalink
Remove idea of point from load_trusted_setup params
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Jul 8, 2024
1 parent d176593 commit 5e4be92
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 191 deletions.
25 changes: 6 additions & 19 deletions bindings/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,25 @@ LoadTrustedSetup is the binding for:
C_KZG_RET load_trusted_setup(
KZGSettings *out,
const uint8_t *g1_monomial_bytes,
size_t num_g1_monomial_bytes,
const uint8_t *g1_lagrange_bytes,
size_t num_g1_points,
size_t num_g1_lagrange_bytes,
const uint8_t *g2_monomial_bytes,
size_t num_g2_points,
size_t num_g2_monomial_bytes,
size_t precompute);
*/
func LoadTrustedSetup(g1MonomialBytes, g1LagrangeBytes, g2MonomialBytes []byte, precompute uint) error {
if loaded {
panic("trusted setup is already loaded")
}
if len(g1MonomialBytes)%C.BYTES_PER_G1 != 0 {
panic(fmt.Sprintf("len(g1MonomialBytes) (%v) is not a multiple of %v", len(g1MonomialBytes), C.BYTES_PER_G1))
}
if len(g1LagrangeBytes)%C.BYTES_PER_G1 != 0 {
panic(fmt.Sprintf("len(g1LagrangeBytes) (%v) is not a multiple of %v", len(g1LagrangeBytes), C.BYTES_PER_G1))
}
if len(g2MonomialBytes)%C.BYTES_PER_G2 != 0 {
panic(fmt.Sprintf("len(g2MonomialBytes) (%v) is not a multiple of %v", len(g2MonomialBytes), C.BYTES_PER_G2))
}
numG1Monomial := len(g1MonomialBytes) / C.BYTES_PER_G1
numG1Lagrange := len(g1LagrangeBytes) / C.BYTES_PER_G1
numG2Monomial := len(g2MonomialBytes) / C.BYTES_PER_G2
if numG1Monomial != numG1Lagrange {
panic(fmt.Sprintf("numG1Monomial (%v) != numG1Lagrange (%v)", numG1Monomial, numG1Lagrange))
}
ret := C.load_trusted_setup(
&settings,
*(**C.uint8_t)(unsafe.Pointer(&g1MonomialBytes)),
(C.size_t)(len(g1MonomialBytes)),
*(**C.uint8_t)(unsafe.Pointer(&g1LagrangeBytes)),
(C.size_t)(numG1Monomial),
(C.size_t)(len(g1LagrangeBytes)),
*(**C.uint8_t)(unsafe.Pointer(&g2MonomialBytes)),
(C.size_t)(numG2Monomial),
(C.size_t)(len(g2MonomialBytes)),
(C.size_t)(precompute))
if ret == C.C_KZG_OK {
loaded = true
Expand Down
62 changes: 21 additions & 41 deletions bindings/java/c_kzg_4844_jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,59 +86,39 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup__Ljav
}
}

JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3B_3BJ_3BJJ(JNIEnv *env, jclass thisCls, jbyteArray g1Monomial, jbyteArray g1Lagrange, jlong g1Count, jbyteArray g2Monomial, jlong g2Count, jlong precompute)
JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3B_3B_3BJ(JNIEnv *env, jclass thisCls, jbyteArray g1MonomialBytes, jbyteArray g1LagrangeBytes, jbyteArray g2MonomialBytes, jlong precompute)
{
if (settings)
{
throw_exception(env, "Trusted Setup is already loaded. Free it before loading a new one.");
return;
}

size_t g1_monomial_bytes = (size_t)(*env)->GetArrayLength(env, g1Monomial);
size_t g1_monomial_expected_bytes = (size_t)g1Count * 48;

if (g1_monomial_bytes != g1_monomial_expected_bytes)
{
throw_invalid_size_exception(env, "Invalid g1 monomial size.", g1_monomial_bytes, g1_monomial_expected_bytes);
return;
}

size_t g1_lagrange_bytes = (size_t)(*env)->GetArrayLength(env, g1Lagrange);
size_t g1_lagrange_expected_bytes = (size_t)g1Count * 48;

if (g1_lagrange_bytes != g1_lagrange_expected_bytes)
{
throw_invalid_size_exception(env, "Invalid g1 lagrange size.", g1_lagrange_bytes, g1_lagrange_expected_bytes);
return;
}

/* Ensure the number of monomial/lagrange bytes is the same */
if (g1_monomial_bytes != g1_lagrange_bytes) {
throw_invalid_size_exception(env, "Invalid g1 lagrange size.", g1_lagrange_bytes, g1_monomial_bytes);
return;
}

size_t g2_monomial_bytes = (size_t)(*env)->GetArrayLength(env, g2Monomial);
size_t g2_monomial_expected_bytes = (size_t)g2Count * 96;

if (g2_monomial_bytes != g2_monomial_expected_bytes)
{
throw_invalid_size_exception(env, "Invalid g2 monomial size.", g2_monomial_bytes, g2_monomial_expected_bytes);
return;
}
size_t g1_monomial_bytes_count = (size_t)(*env)->GetArrayLength(env, g1MonomialBytes);
size_t g1_lagrange_bytes_count = (size_t)(*env)->GetArrayLength(env, g1LagrangeBytes);
size_t g2_monomial_bytes_count = (size_t)(*env)->GetArrayLength(env, g2MonomialBytes);

settings = allocate_settings(env);

jbyte *g1_monomial_native = (*env)->GetByteArrayElements(env, g1Monomial, NULL);
jbyte *g1_lagrange_native = (*env)->GetByteArrayElements(env, g1Lagrange, NULL);
jbyte *g2_monomial_native = (*env)->GetByteArrayElements(env, g2Monomial, NULL);
jbyte *g1_monomial_bytes_native = (*env)->GetByteArrayElements(env, g1MonomialBytes, NULL);
jbyte *g1_lagrange_bytes_native = (*env)->GetByteArrayElements(env, g1LagrangeBytes, NULL);
jbyte *g2_monomial_bytes_native = (*env)->GetByteArrayElements(env, g2MonomialBytes, NULL);
size_t precompute_native = (size_t)precompute;

C_KZG_RET ret = load_trusted_setup(settings, (uint8_t *)g1_monomial_native, (uint8_t *)g1_lagrange_native, (size_t)g1Count, (uint8_t *)g2_monomial_native, (size_t)g2Count, precompute_native);

(*env)->ReleaseByteArrayElements(env, g1Monomial, g1_monomial_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, g1Lagrange, g1_lagrange_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, g2Monomial, g2_monomial_native, JNI_ABORT);
C_KZG_RET ret = load_trusted_setup(
settings,
(uint8_t *)g1_monomial_bytes_native,
g1_monomial_bytes_count,
(uint8_t *)g1_lagrange_bytes_native,
g1_lagrange_bytes_count,
(uint8_t *)g2_monomial_bytes_native,
g2_monomial_bytes_count,
precompute_native
);

(*env)->ReleaseByteArrayElements(env, g1MonomialBytes, g1_monomial_bytes_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, g1LagrangeBytes, g1_lagrange_bytes_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, g2MonomialBytes, g2_monomial_bytes_native, JNI_ABORT);

if (ret != C_KZG_OK)
{
Expand Down
10 changes: 5 additions & 5 deletions bindings/java/c_kzg_4844_jni.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 4 additions & 11 deletions bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,14 @@ private CKZG4844JNI() {}
* An alternative to {@link #loadTrustedSetup(String,long)}. Loads the trusted setup from method
* parameters instead of a file.
*
* @param g1Monomial g1 values in monomial form as bytes
* @param g1Lagrange g1 values in Lagrange form as bytes
* @param g1Count the count of the g1 values
* @param g2Monomial g2 values in monomial form as bytes
* @param g2Count the count of the g2 values
* @param g1MonomialBytes g1 values in monomial form as bytes
* @param g1LagrangeBytes g1 values in Lagrange form as bytes
* @param g2MonomialBytes g2 values in monomial form as bytes
* @param precompute configurable value between 0-15
* @throws CKZGException if there is a crypto error
*/
public static native void loadTrustedSetup(
byte[] g1Monomial,
byte[] g1Lagrange,
long g1Count,
byte[] g2Monomial,
long g2Count,
long precompute);
byte[] g1MonomialBytes, byte[] g1LagrangeBytes, byte[] g2MonomialBytes, long precompute);

/**
* An alternative to {@link #loadTrustedSetup(String,long)}. Loads the trusted setup from a
Expand Down
45 changes: 29 additions & 16 deletions bindings/java/src/test/java/ethereum/ckzg4844/CKZG4844JNITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,35 +437,53 @@ public void shouldThrowExceptionOnIncorrectTrustedSetupParameters() {
final LoadTrustedSetupParameters parameters =
TestUtils.createLoadTrustedSetupParameters(TRUSTED_SETUP_FILE);

// wrong g1Count
// wrong g1 monomial points
CKZGException exception =
assertThrows(
CKZGException.class,
() ->
CKZG4844JNI.loadTrustedSetup(
parameters.getG1Monomial(),
new byte[27], // wrong g1 monomial
parameters.getG1Lagrange(),
parameters.getG1Count() + 1,
parameters.getG2Monomial(),
parameters.getG2Count(),
0));
assertEquals(C_KZG_BADARGS, exception.getError());
assertTrue(exception.getErrorMessage().contains("Invalid g1 monomial size."));
assertTrue(
exception
.getErrorMessage()
.contains("There was an error while loading the Trusted Setup."));

// wrong g2Count
// wrong g1 lagrange points
exception =
assertThrows(
CKZGException.class,
() ->
CKZG4844JNI.loadTrustedSetup(
parameters.getG1Monomial(),
parameters.getG1Lagrange(),
parameters.getG1Count(),
new byte[27], // wrong g1 lagrange
parameters.getG2Monomial(),
parameters.getG2Count() + 1,
0));
assertEquals(C_KZG_BADARGS, exception.getError());
assertTrue(exception.getErrorMessage().contains("Invalid g2 monomial size."));
assertTrue(
exception
.getErrorMessage()
.contains("There was an error while loading the Trusted Setup."));

// wrong g2 monomial points
exception =
assertThrows(
CKZGException.class,
() ->
CKZG4844JNI.loadTrustedSetup(
parameters.getG1Monomial(),
parameters.getG1Lagrange(),
new byte[27], // wrong g1 lagrange
0));
assertEquals(C_KZG_BADARGS, exception.getError());
assertTrue(
exception
.getErrorMessage()
.contains("There was an error while loading the Trusted Setup."));
}

private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) {
Expand Down Expand Up @@ -494,12 +512,7 @@ private static void loadTrustedSetupFromParameters() {
final LoadTrustedSetupParameters parameters =
TestUtils.createLoadTrustedSetupParameters(TRUSTED_SETUP_FILE);
CKZG4844JNI.loadTrustedSetup(
parameters.getG1Monomial(),
parameters.getG1Lagrange(),
parameters.getG1Count(),
parameters.getG2Monomial(),
parameters.getG2Count(),
0);
parameters.getG1Monomial(), parameters.getG1Lagrange(), parameters.getG2Monomial(), 0);
}

public static void loadTrustedSetupFromResource() {
Expand Down
61 changes: 34 additions & 27 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,67 +85,74 @@ proc loadTrustedSetup*(fileName: string, precompute: Natural): Result[KzgCtx, st
except IOError as ex:
return err(ex.msg)

proc loadTrustedSetup*(g1Monomial: openArray[G1Data],
g1Lagrange: openArray[G1Data],
g2Monomial: openArray[G2Data],
proc loadTrustedSetup*(g1MonomialBytes: openArray[byte],
g1LagrangeBytes: openArray[byte],
g2MonomialBytes: openArray[byte],
precompute: Natural):
Result[KzgCtx, string] =
if g1Monomial.len == 0 or g1Lagrange.len == 0 or g2Monomial.len == 0:
return err($KZG_BADARGS)
if g1Monomial.len != g1Lagrange.len:
if g1MonomialBytes.len == 0 or g1LagrangeBytes.len == 0 or g2MonomialBytes.len == 0:
return err($KZG_BADARGS)

let
ctx = newKzgCtx()
res = load_trusted_setup(ctx.val,
g1Monomial[0][0].getPtr,
g1Lagrange[0][0].getPtr,
g1Monomial.len.csize_t,
g2Monomial[0][0].getPtr,
g2Monomial.len.csize_t,
g1MonomialBytes[0].getPtr,
g1MonomialBytes.len.csize_t,
g1LagrangeBytes[0].getPtr,
g1LagrangeBytes.len.csize_t,
g2MonomialBytes[0].getPtr,
g2MonomialBytes.len.csize_t,
precompute.csize_t)
verify(res, ctx)

proc loadTrustedSetupFromString*(input: string, precompute: Natural): Result[KzgCtx, string] =
const
NumG1 = FIELD_ELEMENTS_PER_BLOB
NumG2 = 65
G1Len = G1Data.len
G2Len = G2Data.len
G1Len = 48
G2Len = 96

var
s = newStringStream(input)
g1Monomial: array[FIELD_ELEMENTS_PER_BLOB, G1Data]
g1Lagrange: array[FIELD_ELEMENTS_PER_BLOB, G1Data]
g2Monomial: array[NumG2, G2Data]
g1MonomialBytes: seq[byte] = newSeq[byte](NumG1 * G1Len)
g1LagrangeBytes: seq[byte] = newSeq[byte](NumG1 * G1Len)
g2MonomialBytes: seq[byte] = newSeq[byte](NumG2 * G2Len)

try:
let fieldElems = s.readLine().parseInt()
if fieldElems != FIELD_ELEMENTS_PER_BLOB:
return err("invalid field elements per blob, expect $1, got $2" % [
$FIELD_ELEMENTS_PER_BLOB, $fieldElems
let numG1 = s.readLine().parseInt()
if numG1 != NumG1:
return err("invalid number of G1 points, expect $1, got $2" % [
$NumG1, $numG1
])
let numG2 = s.readLine().parseInt()
if numG2 != NumG2:
return err("invalid number of G2, expect $1, got $2" % [
return err("invalid number of G2 points, expect $1, got $2" % [
$NumG2, $numG2
])

for i in 0 ..< FIELD_ELEMENTS_PER_BLOB:
g1Lagrange[i] = hexToByteArray[G1Len](s.readLine())
for i in 0 ..< NumG1:
let p = hexToByteArray[G1Len](s.readLine())
for j in 0 ..< G1Len:
g1LagrangeBytes[i * G1Len + j] = p[j]

for i in 0 ..< NumG2:
g2Monomial[i] = hexToByteArray[G2Len](s.readLine())
let p = hexToByteArray[G2Len](s.readLine())
for j in 0 ..< G2Len:
g2MonomialBytes[i * G2Len + j] = p[j]

for i in 0 ..< NumG1:
let p = hexToByteArray[G1Len](s.readLine())
for j in 0 ..< G1Len:
g1MonomialBytes[i * G1Len + j] = p[j]

for i in 0 ..< FIELD_ELEMENTS_PER_BLOB:
g1Monomial[i] = hexToByteArray[G1Len](s.readLine())
except ValueError as ex:
return err(ex.msg)
except OSError as ex:
return err(ex.msg)
except IOError as ex:
return err(ex.msg)

loadTrustedSetup(g1Monomial, g1Lagrange, g2Monomial, precompute)
loadTrustedSetup(g1MonomialBytes, g1LagrangeBytes, g2MonomialBytes, precompute)

proc toCommitment*(ctx: KzgCtx,
blob: KzgBlob):
Expand Down
5 changes: 3 additions & 2 deletions bindings/nim/kzg_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ type

proc load_trusted_setup*(res: KzgSettings,
g1MonomialBytes: ptr byte,
numG1MonomialBytes: csize_t,
g1LagrangeBytes: ptr byte,
numG1Points: csize_t,
numG1LagrangeBytes: csize_t,
g2MonomialBytes: ptr byte,
numG2Points: csize_t,
numG2MonomialBytes: csize_t,
precompute: csize_t): KZG_RET {.kzg_abi.}

proc load_trusted_setup_file*(res: KzgSettings,
Expand Down
5 changes: 3 additions & 2 deletions bindings/nim/tests/test_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ proc readSetup(): KzgSettings =

let res = load_trusted_setup(result,
g1MonomialBytes[0].addr,
g1MonomialBytes.len.csize_t,
g1LagrangeBytes[0].addr,
n1.csize_t,
g1LagrangeBytes.len.csize_t,
g2MonomialBytes[0].addr,
n2.csize_t,
g2MonomialBytes.len.csize_t,
0)

doAssert(res == KZG_OK,
Expand Down
Loading

0 comments on commit 5e4be92

Please sign in to comment.