Skip to content

Commit

Permalink
in ecmul, use double for scalar == 2 and check for infinity prior to …
Browse files Browse the repository at this point in the history
…doing multiplication

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Aug 23, 2024
1 parent 5fec469 commit f396cc2
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions gnark/gnark-jni/gnark-eip-196.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
EIP196PreallocateForG2 = EIP196PreallocateForG1 * 2 // G2 points are encoded as 2 concatenated G1 points
)

var EIP196ScalarTwo = big.NewInt(2)


// bn254Modulus is the value 21888242871839275222246405745257275088696311157297823662689037894645226208583
var bn254Modulus = new(big.Int).SetBytes([]byte{
Expand All @@ -40,9 +42,6 @@ var bn254Modulus = new(big.Int).SetBytes([]byte{
0x3c, 0x20, 0x8c, 0x16, 0xd8, 0x7c, 0xfd, 0x47,
})

// Predefine a zero slice of length 16
var zeroEIP196Slice = make([]byte, 16)

//export eip196altbn128G1Add
func eip196altbn128G1Add(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInputLen C.int, cOutputLen, cErrorLen *C.int) C.int {
inputLen := int(cInputLen)
Expand Down Expand Up @@ -77,7 +76,7 @@ func eip196altbn128G1Add(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp

if inputLen < 2*EIP196PreallocateForG1 {
// if incomplete input is all zero, return p0
if isAllZeroEIP196(input, 64) {
if isAllZeroEIP196(input, 64, 64) {
ret := p0.Marshal()
g1AffineEncode(ret, javaOutputBuf)
*outputLen = EIP196PreallocateForG1
Expand Down Expand Up @@ -128,6 +127,11 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
// Convert input C pointers to Go slice
input := (*[EIP196PreallocateForG1 + EIP196PreallocateForScalar]byte)(unsafe.Pointer(javaInputBuf))[:inputLen:inputLen]

// inifinity check:
if isAllZeroEIP196(input, 0, 64) {
return 0
}

// generate p0 g1 affine
var p0 bn254.G1Affine
err := safeUnmarshalEIP196(&p0, input, 0)
Expand All @@ -154,8 +158,14 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
scalar := big.NewInt(0)
scalar.SetBytes(scalarBytes[:])

// multiply g1 point by scalar
result := p0.ScalarMultiplication(&p0, scalar)
var result *bn254.G1Affine
if scalar.Cmp(EIP196ScalarTwo) == 0 {
// if scalar == 2, double is faster
result = p0.Double(&p0)
} else {
// multiply g1 point by scalar
result = p0.ScalarMultiplication(&p0, scalar)
}

// marshal the resulting point and encode directly to the output buffer
ret := result.Marshal()
Expand Down Expand Up @@ -326,9 +336,14 @@ func checkInFieldEIP196(data []byte) bool {
}

// isAllZero checks if all elements in the byte slice are zero
func isAllZeroEIP196(data []byte, offset int) bool {
if len(data) > 64 {
slice := data [offset:]
func isAllZeroEIP196(data []byte, offset, length int) bool {

if len(data) > offset {
tail := offset + length
if len(data) < tail {
tail = len(data)
}
slice := data [offset:tail]
for _, b := range slice {
if b != 0 {
return false
Expand Down

0 comments on commit f396cc2

Please sign in to comment.