Skip to content

Commit

Permalink
fix bounds problem with eip196 ecmul, add test cases covering partial…
Browse files Browse the repository at this point in the history
… scalar behavior

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Jun 20, 2024
1 parent da1afbf commit c101a3d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ input,result,gas,notes
d2acf800b3ba0ff68ef8d5fd4d6c250d3e70b3bed17894f958579644c83fa9d485121d580e2b061c697e68f9502977680d6ad8e12b4f61e3e2a2252ce11428941f2a84b7f0a821cb8cc7699303bd4fec2247870562618fd8d6169072d9b33614,,6000,invalid input parameters, Failed to parse Fp element
1c2cb2b4504b19c7e073679432e625f2706b7c4728cd9bd3ce36579f4de2f3902c8605f723ac2f73baa15eac674f62ab,,6000,invalid input parameters
,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,6000,
1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE31A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F60100000000000000000000000000000000,0x220cb1540fa85ba04d863dca86de9359a43ac9fc084aebb9f2a936d989abbb602ccdc6c020dd2cf78332132b3f1d1122391b515035623cd6f53d4aea24ea2466,6000,
1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE31A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F60000000000000000000000000000000100000000000000000000000000000000,1051acb0700ec6d42a88215852d582efbaef31529b6fcbc3277b5c1b300f5cf0135b2394bb45ab04b8bd7611bd2dfe1de6a4e6e2ccea1ea1955f577cd66af85b,6000,
43 changes: 31 additions & 12 deletions gnark/gnark-jni/gnark-eip-196.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
EIP196PreallocateForResult = 256
EIP196PreallocateForResult = 128
EIP196PreallocateForError = 256
EIP196PreallocateForScalar = 32 // scalar int is 32 byte
EIP196PreallocateForFp = 32 // field elements are 32 bytes
Expand All @@ -38,13 +38,20 @@ func eip196altbn128G1Add(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
inputLen := int(cInputLen)
errorLen := int(cErrorLen)


if (inputLen > 2*EIP196PreallocateForG1) {
// trunc if input too long
inputLen = 2*EIP196PreallocateForG1
}

if inputLen < EIP196PreallocateForG1 {
// if we do not have complete input, return 0
return 0
}

// Convert error C pointers to Go slices
errorBuf := castBuffer(javaErrorBuf, errorLen)
errorBuf := castErrorBuffer(javaErrorBuf, errorLen)


// Convert input C pointers to Go slices
input := (*[2*EIP196PreallocateForG1]byte)(unsafe.Pointer(javaInputBuf))[:inputLen:inputLen]
Expand Down Expand Up @@ -89,18 +96,19 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
inputLen := int(cInputLen)
errorLen := int(cErrorLen)

if inputLen == 0 {
return 0
}

// Convert error C pointers to Go slices
errorBuf := castBuffer(javaErrorBuf, errorLen)
errorBuf := castErrorBuffer(javaErrorBuf, errorLen)

if inputLen < EIP196PreallocateForG1 {
// if we do not have complete input, return 0
return 0
}

if (inputLen > EIP196PreallocateForG1 + EIP196PreallocateForScalar) {
// trunc if input too long
inputLen = EIP196PreallocateForG1 + EIP196PreallocateForScalar
}

// Convert input C pointers to Go slice
input := (*[EIP196PreallocateForG1 + EIP196PreallocateForScalar]byte)(unsafe.Pointer(javaInputBuf))[:inputLen:inputLen]

Expand All @@ -114,12 +122,15 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
}

// Convert byte slice to *big.Int
scalarEndIndex := 96;
if (scalarEndIndex > int(cInputLen)) {
scalarEndIndex = int(cInputLen)
scalarBytes := input[64:]
if (96 > int(cInputLen)) {
// if the input is truncated, copy the bytes to the high order portion of the scalar
scalarBytes = make([]byte, 32)
copy(scalarBytes[:], input[64:int(cInputLen)])
}

scalar := big.NewInt(0)
scalar.SetBytes(input[64:scalarEndIndex])
scalar.SetBytes(scalarBytes[:])

// multiply g1 point by scalar
result := p0.ScalarMultiplication(&p0, scalar)
Expand All @@ -140,7 +151,7 @@ func eip196altbn128Pairing(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cI
output := castBuffer(javaOutputBuf, outputLen)

// Convert error C pointers to Go slices
errorBuf := castBuffer(javaErrorBuf, errorLen)
errorBuf := castErrorBuffer(javaErrorBuf, errorLen)

if inputLen == 0 {
output[31]=0x01
Expand Down Expand Up @@ -234,6 +245,14 @@ func castBuffer(javaOutputBuf *C.char, length int) []byte {
return (*[EIP196PreallocateForResult]byte)(unsafe.Pointer(javaOutputBuf))[:bufSize:bufSize]
}

func castErrorBuffer(javaOutputBuf *C.char, length int) []byte {
bufSize := length
if bufSize < EIP196PreallocateForError {
bufSize = EIP196PreallocateForError
}
return (*[EIP196PreallocateForError]byte)(unsafe.Pointer(javaOutputBuf))[:bufSize:bufSize]
}

func main() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class LibGnarkEIP196 {

public static final int EIP196_PREALLOCATE_FOR_RESULT_BYTES = 256; // includes error string
public static final int EIP196_PREALLOCATE_FOR_RESULT_BYTES = 128;
public static final int EIP196_PREALLOCATE_FOR_ERROR_BYTES = 256; // includes error string
@SuppressWarnings("WeakerAccess")
public static final byte EIP196_ADD_OPERATION_RAW_VALUE = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ input,result,gas,notes
d2acf800b3ba0ff68ef8d5fd4d6c250d3e70b3bed17894f958579644c83fa9d485121d580e2b061c697e68f9502977680d6ad8e12b4f61e3e2a2252ce11428941f2a84b7f0a821cb8cc7699303bd4fec2247870562618fd8d6169072d9b33614,,6000,invalid compressed coordinate
1c2cb2b4504b19c7e073679432e625f2706b7c4728cd9bd3ce36579f4de2f3902c8605f723ac2f73baa15eac674f62ab,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,6000,invalid input parameters,
,00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,6000,
1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE31A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F60100000000000000000000000000000000,0x220cb1540fa85ba04d863dca86de9359a43ac9fc084aebb9f2a936d989abbb602ccdc6c020dd2cf78332132b3f1d1122391b515035623cd6f53d4aea24ea2466,6000,
1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE31A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F60000000000000000000000000000000100000000000000000000000000000000,1051acb0700ec6d42a88215852d582efbaef31529b6fcbc3277b5c1b300f5cf0135b2394bb45ab04b8bd7611bd2dfe1de6a4e6e2ccea1ea1955f577cd66af85b,6000,

0 comments on commit c101a3d

Please sign in to comment.