Skip to content
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

Optimizations in bn254 ECMUL #207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ Cargo.lock

*.a
*.dylib
constantine/out
constantine/out
gnark/gnark-jni/*.h

34 changes: 25 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,12 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
// Convert input C pointers to Go slice
input := (*[EIP196PreallocateForG1 + EIP196PreallocateForScalar]byte)(unsafe.Pointer(javaInputBuf))[:inputLen:inputLen]

// infinity check:
if isAllZeroEIP196(input, 0, 64) {
*outputLen = EIP196PreallocateForG1
return 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add *outputLen = EIP196PreallocateForG1

}

// generate p0 g1 affine
var p0 bn254.G1Affine
err := safeUnmarshalEIP196(&p0, input, 0)
Expand All @@ -154,8 +159,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 +337,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
Loading