-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto/bn256: unmarshal constraint + start pure go impl
- Loading branch information
Showing
7 changed files
with
265 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build amd64,!appengine,!gccgo | ||
|
||
package bn256 | ||
|
||
// go:noescape | ||
func gfpNeg(c, a *gfP) | ||
|
||
//go:noescape | ||
func gfpAdd(c, a, b *gfP) | ||
|
||
//go:noescape | ||
func gfpSub(c, a, b *gfP) | ||
|
||
//go:noescape | ||
func gfpMul(c, a, b *gfP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build amd64,!appengine,!gccgo | ||
|
||
#include "gfp.h" | ||
#include "mul.h" | ||
#include "mul_bmi2.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// +build !amd64 appengine gccgo | ||
|
||
package bn256 | ||
|
||
func gfpNeg(c, a *gfP) { | ||
// c = p - a | ||
c[0] = p2[0] - a[0] | ||
c[1] = p2[1] - a[1] | ||
if p2[0] < a[0] { | ||
c[1]-- | ||
} | ||
c[2] = p2[2] - a[2] | ||
if p2[1] < a[1] { | ||
c[2]-- | ||
} | ||
c[3] = p2[3] - a[3] | ||
if p2[2] < a[2] { | ||
c[3]-- | ||
} | ||
// c < p ? c : c-p | ||
gfpCarry(c, p2[3] < a[3]) | ||
} | ||
|
||
func gfpAdd(c, a, b *gfP) { | ||
// c = a + b | ||
c[0] = a[0] + b[0] | ||
c[1] = a[1] + b[1] | ||
if c[0] < a[0] { | ||
c[1]++ | ||
} | ||
c[2] = a[2] + b[2] | ||
if c[1] < a[1] { | ||
c[2]++ | ||
} | ||
c[3] = a[3] + b[3] | ||
if c[2] < a[2] { | ||
c[3]++ | ||
} | ||
// c < p ? c : c-p | ||
gfpCarry(c, c[3] < a[3]) | ||
} | ||
|
||
func gfpSub(c, a, b *gfP) { | ||
// c = a - b | ||
c[0] = a[0] - b[0] | ||
c[1] = a[1] - b[1] | ||
if c[0] > a[0] { | ||
c[1]-- | ||
} | ||
c[2] = a[2] - b[2] | ||
if c[1] > a[1] { | ||
c[2]-- | ||
} | ||
c[3] = a[3] - b[3] | ||
if c[2] > a[2] { | ||
c[3]-- | ||
} | ||
// c < p ? c : c-p | ||
gfpCarry(c, c[3] < a[3]) | ||
} | ||
|
||
func gfpMul(c, a, b *gfP) { | ||
|
||
} | ||
|
||
func gfpCarry(c *gfP, carry bool) { | ||
if c[3] < p2[3] { | ||
return | ||
} else if c[3] == p2[3] { | ||
if c[2] < p2[3] { | ||
return | ||
} else if c[2] == p2[2] { | ||
if c[1] < p2[1] { | ||
return | ||
} else if c[1] == p2[1] { | ||
if c[0] < p2[0] { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
if c[0] < p2[0] { | ||
c[1]-- | ||
} | ||
c[0] -= p2[0] | ||
|
||
if c[1] < p2[1] { | ||
c[2]-- | ||
} | ||
c[1] -= p2[1] | ||
|
||
if c[2] < p2[2] { | ||
c[3]-- | ||
} | ||
c[2] -= p2[2] | ||
|
||
c[3] -= p2[3] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package bn256 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Tests that negation works the same way on both assembly-optimized and pure Go | ||
// implementation. | ||
func TestGFpNeg(t *testing.T) { | ||
n := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} | ||
w := &gfP{0xfedcba9876543211, 0x0123456789abcdef, 0x2152411021524110, 0x0114251201142512} | ||
h := &gfP{} | ||
|
||
gfpNeg(h, n) | ||
if *h != *w { | ||
t.Errorf("negation mismatch: have %#x, want %#x", *h, *w) | ||
} | ||
} | ||
|
||
// Tests that addition works the same way on both assembly-optimized and pure Go | ||
// implementation. | ||
func TestGFpAdd(t *testing.T) { | ||
a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} | ||
b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} | ||
w := &gfP{0xc3df73e9278302b8, 0x687e956e978e3572, 0x254954275c18417f, 0xad354b6afc67f9b4} | ||
h := &gfP{} | ||
|
||
gfpAdd(h, a, b) | ||
if *h != *w { | ||
t.Errorf("addition mismatch: have %#x, want %#x", *h, *w) | ||
} | ||
} | ||
|
||
// Tests that subtraction works the same way on both assembly-optimized and pure Go | ||
// implementation. | ||
func TestGFpSub(t *testing.T) { | ||
a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} | ||
b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} | ||
w := &gfP{0x02468acf13579bdf, 0xfdb97530eca86420, 0xdfc1e401dfc1e402, 0x203e1bfe203e1bfd} | ||
h := &gfP{} | ||
|
||
gfpSub(h, a, b) | ||
if *h != *w { | ||
t.Errorf("subtraction mismatch: have %#x, want %#x", *h, *w) | ||
} | ||
} | ||
|
||
// Tests that multiplication works the same way on both assembly-optimized and pure Go | ||
// implementation. | ||
func TestGFpMul(t *testing.T) { | ||
a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} | ||
b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} | ||
w := &gfP{0xcbcbd377f7ad22d3, 0x3b89ba5d849379bf, 0x87b61627bd38b6d2, 0xc44052a2a0e654b2} | ||
h := &gfP{} | ||
|
||
gfpMul(h, a, b) | ||
if *h != *w { | ||
t.Errorf("multiplication mismatch: have %#x, want %#x", *h, *w) | ||
} | ||
} |