Compare commits

..

4 Commits

Author SHA1 Message Date
Eduard S
bffc894c62 Fix Travis to really test 32 bits
To make sure 32 bits arch is being tested, try the following test which should
fail in 32 bits mode:
```go
package utils

import (
	"math/bits"
	"testing"
)

func TestBreak(t *testing.T) {
	if bits.UintSize != 64 {
		panic("bits.UintSize != 64")
	}
}
```
2020-04-09 16:59:02 +02:00
Eduard S
3d81ae3d8b Test break in 386 2020-04-09 16:38:21 +02:00
arnau
eb41fe0757 Merge pull request #18 from iden3/feature/fix32bits
Fix compat with 32 bit arch
2020-03-18 11:55:56 +01:00
Eduard S
e10db811aa Fix compat with 32 bit arch 2020-03-17 17:17:45 +01:00
8 changed files with 164 additions and 150 deletions

View File

@@ -4,5 +4,22 @@ language: go
go:
- "1.12"
# Travis overrides the GOARCH env var probably in its `travis_setup_go`
# function, so we need a work around...
jobs:
include:
- name: "Unit Tests 64 bit arch"
env:
- XGOARCH="amd64"
- name: "Unit Test 32 bit arch"
env:
- XGOARCH="386"
env:
- GO111MODULE=on
before_install:
- export GOARCH=$XGOARCH
script:
- go test -v ./...

View File

@@ -5,15 +5,14 @@ import (
"math/big"
"github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-iden3-crypto/ff"
"github.com/iden3/go-iden3-crypto/utils"
)
// A is one of the babyjub constants.
var A *ff.Element
var A *big.Int
// D is one of the babyjub constants.
var D *ff.Element
var D *big.Int
// Order of the babyjub curve.
var Order *big.Int
@@ -28,52 +27,29 @@ var B8 *Point
// init initializes global numbers and the subgroup base.
func init() {
A = ff.NewElement().SetString("168700")
D = ff.NewElement().SetString("168696")
A = utils.NewIntFromString("168700")
D = utils.NewIntFromString("168696")
Order = utils.NewIntFromString(
"21888242871839275222246405745257275088614511777268538073601725287587578984328")
SubOrder = new(big.Int).Rsh(Order, 3)
B8 = NewPoint()
B8.X = ff.NewElement().SetString(
B8.X = utils.NewIntFromString(
"5299619240641551281634865583518297030282874472190772894086521144482721001553")
B8.Y = ff.NewElement().SetString(
B8.Y = utils.NewIntFromString(
"16950150798460657717958625567821834550301663161624707787222815936182638968203")
}
// PointBI represents a point of the babyjub curve.
type PointBI struct {
// Point represents a point of the babyjub curve.
type Point struct {
X *big.Int
Y *big.Int
}
type Point struct {
X *ff.Element
Y *ff.Element
}
func PointBIToPoint(p *PointBI) *Point {
return &Point{
X: ff.NewElement().SetBigInt(p.X),
Y: ff.NewElement().SetBigInt(p.Y),
}
}
func PointToPointBI(p *Point) *PointBI {
return &PointBI{
X: p.X.BigInt(),
Y: p.Y.BigInt(),
}
}
// NewPoint creates a new PointBI.
func NewPointBI() *PointBI {
return &PointBI{X: big.NewInt(0), Y: big.NewInt(1)}
}
// NewPoint creates a new Point.
func NewPoint() *Point {
return &Point{X: ff.NewElement().SetZero(), Y: ff.NewElement().SetOne()}
return &Point{X: big.NewInt(0), Y: big.NewInt(1)}
}
// Set copies a Point c into the Point p
@@ -83,45 +59,44 @@ func (p *Point) Set(c *Point) *Point {
return p
}
func (p *Point) Equal(q *Point) bool {
// return p.X.Cmp(q.X) == 0 && p.Y.Cmp(q.Y) == 0
return p.X.Equal(q.X) && p.Y.Equal(q.Y)
}
// Add adds Point a and b into res
func (res *Point) Add(a *Point, b *Point) *Point {
// x = (a.x * b.y + b.x * a.y) * (1 + D * a.x * b.x * a.y * b.y)^-1 mod q
x1a := ff.NewElement().Mul(a.X, b.Y)
x1b := ff.NewElement().Mul(b.X, a.Y)
x1a := new(big.Int).Mul(a.X, b.Y)
x1b := new(big.Int).Mul(b.X, a.Y)
x1a.Add(x1a, x1b) // x1a = a.x * b.y + b.x * a.y
x2 := ff.NewElement().Set(D)
x2 := new(big.Int).Set(D)
x2.Mul(x2, a.X)
x2.Mul(x2, b.X)
x2.Mul(x2, a.Y)
x2.Mul(x2, b.Y)
x2.Add(ff.NewElement().SetOne(), x2)
x2.Inverse(x2) // x2 = (1 + D * a.x * b.x * a.y * b.y)^-1
x2.Add(constants.One, x2)
x2.Mod(x2, constants.Q)
x2.ModInverse(x2, constants.Q) // x2 = (1 + D * a.x * b.x * a.y * b.y)^-1
// y = (a.y * b.y - A * a.x * b.x) * (1 - D * a.x * b.x * a.y * b.y)^-1 mod q
y1a := ff.NewElement().Mul(a.Y, b.Y)
y1b := ff.NewElement().Set(A)
y1a := new(big.Int).Mul(a.Y, b.Y)
y1b := new(big.Int).Set(A)
y1b.Mul(y1b, a.X)
y1b.Mul(y1b, b.X)
y1a.Sub(y1a, y1b) // y1a = a.y * b.y - A * a.x * b.x
y2 := ff.NewElement().Set(D)
y2 := new(big.Int).Set(D)
y2.Mul(y2, a.X)
y2.Mul(y2, b.X)
y2.Mul(y2, a.Y)
y2.Mul(y2, b.Y)
y2.Sub(ff.NewElement().SetOne(), y2)
y2.Inverse(y2) // y2 = (1 - D * a.x * b.x * a.y * b.y)^-1
y2.Sub(constants.One, y2)
y2.Mod(y2, constants.Q)
y2.ModInverse(y2, constants.Q) // y2 = (1 - D * a.x * b.x * a.y * b.y)^-1
res.X = x1a.Mul(x1a, x2)
res.X = res.X.Mod(res.X, constants.Q)
res.Y = y1a.Mul(y1a, y2)
res.Y = res.Y.Mod(res.Y, constants.Q)
return res
}
@@ -129,8 +104,8 @@ func (res *Point) Add(a *Point, b *Point) *Point {
// Mul multiplies the Point p by the scalar s and stores the result in res,
// which is also returned.
func (res *Point) Mul(s *big.Int, p *Point) *Point {
res.X = ff.NewElement().SetZero()
res.Y = ff.NewElement().SetOne()
res.X = big.NewInt(0)
res.Y = big.NewInt(1)
exp := NewPoint().Set(p)
for i := 0; i < s.BitLen(); i++ {
@@ -145,21 +120,25 @@ func (res *Point) Mul(s *big.Int, p *Point) *Point {
// InCurve returns true when the Point p is in the babyjub curve.
func (p *Point) InCurve() bool {
x2 := ff.NewElement().Set(p.X)
x2 := new(big.Int).Set(p.X)
x2.Mul(x2, x2)
x2.Mod(x2, constants.Q)
y2 := ff.NewElement().Set(p.Y)
y2 := new(big.Int).Set(p.Y)
y2.Mul(y2, y2)
y2.Mod(y2, constants.Q)
a := ff.NewElement().Mul(A, x2)
a := new(big.Int).Mul(A, x2)
a.Add(a, y2)
a.Mod(a, constants.Q)
b := ff.NewElement().Set(D)
b := new(big.Int).Set(D)
b.Mul(b, x2)
b.Mul(b, y2)
b.Add(ff.NewElement().SetOne(), b)
b.Add(constants.One, b)
b.Mod(b, constants.Q)
return a.Equal(b)
return a.Cmp(b) == 0
}
// InSubGroup returns true when the Point p is in the subgroup of the babyjub
@@ -169,7 +148,7 @@ func (p *Point) InSubGroup() bool {
return false
}
res := NewPoint().Mul(SubOrder, p)
return res.X.Equal(ff.NewElement().SetZero()) && res.Y.Equal(ff.NewElement().SetOne())
return (res.X.Cmp(constants.Zero) == 0) && (res.Y.Cmp(constants.One) == 0)
}
// PointCoordSign returns the sign of the curve point coordinate. It returns
@@ -192,9 +171,8 @@ func PackPoint(ay *big.Int, sign bool) [32]byte {
// Compress the point into a 32 byte array that contains the y coordinate in
// little endian and the sign of the x coordinate.
func (p *Point) Compress() [32]byte {
pBI := PointToPointBI(p)
sign := PointCoordSign(pBI.X)
return PackPoint(pBI.Y, sign)
sign := PointCoordSign(p.X)
return PackPoint(p.Y, sign)
}
// Decompress a compressed Point into p, and also returns the decompressed
@@ -205,37 +183,34 @@ func (p *Point) Decompress(leBuf [32]byte) (*Point, error) {
sign = true
leBuf[31] = leBuf[31] & 0x7F
}
y := big.NewInt(0)
utils.SetBigIntFromLEBytes(y, leBuf[:])
if y.Cmp(constants.Q) >= 0 {
utils.SetBigIntFromLEBytes(p.Y, leBuf[:])
if p.Y.Cmp(constants.Q) >= 0 {
return nil, fmt.Errorf("p.y >= Q")
}
p.Y = ff.NewElement().SetBigInt(y)
y2 := ff.NewElement().Mul(p.Y, p.Y)
xa := ff.NewElement().SetOne()
y2 := new(big.Int).Mul(p.Y, p.Y)
y2.Mod(y2, constants.Q)
xa := big.NewInt(1)
xa.Sub(xa, y2) // xa == 1 - y^2
xb := ff.NewElement().Mul(D, y2)
xb := new(big.Int).Mul(D, y2)
xb.Mod(xb, constants.Q)
xb.Sub(A, xb) // xb = A - d * y^2
if xb.Equal(ff.NewElement().SetZero()) {
if xb.Cmp(big.NewInt(0)) == 0 {
return nil, fmt.Errorf("division by 0")
}
xb.Inverse(xb)
xb.ModInverse(xb, constants.Q)
p.X.Mul(xa, xb) // xa / xb
q := PointToPointBI(p)
noSqrt := q.X.ModSqrt(q.X, constants.Q)
p.X.Mod(p.X, constants.Q)
noSqrt := p.X.ModSqrt(p.X, constants.Q)
if noSqrt == nil {
return nil, fmt.Errorf("x is not a square mod q")
}
if (sign && !PointCoordSign(q.X)) || (!sign && PointCoordSign(q.X)) {
q.X.Mul(q.X, constants.MinusOne)
if (sign && !PointCoordSign(p.X)) || (!sign && PointCoordSign(p.X)) {
p.X.Mul(p.X, constants.MinusOne)
}
q.X.Mod(q.X, constants.Q)
p = PointBIToPoint(q)
p.X.Mod(p.X, constants.Q)
return p, nil
}

View File

@@ -7,21 +7,13 @@ import (
"testing"
"github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-iden3-crypto/ff"
"github.com/iden3/go-iden3-crypto/utils"
"github.com/stretchr/testify/assert"
)
func zero() *ff.Element {
return ff.NewElement().SetZero()
}
func one() *ff.Element {
return ff.NewElement().SetOne()
}
func TestAdd1(t *testing.T) {
a := &Point{X: zero(), Y: one()}
b := &Point{X: zero(), Y: one()}
a := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
b := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
c := NewPoint().Add(a, b)
// fmt.Printf("%v = 2 * %v", *c, *a)
@@ -30,15 +22,15 @@ func TestAdd1(t *testing.T) {
}
func TestAdd2(t *testing.T) {
aX := ff.NewElement().SetString(
aX := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
aY := ff.NewElement().SetString(
aY := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
a := &Point{X: aX, Y: aY}
bX := ff.NewElement().SetString(
bX := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
bY := ff.NewElement().SetString(
bY := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
b := &Point{X: bX, Y: bY}
@@ -53,15 +45,15 @@ func TestAdd2(t *testing.T) {
}
func TestAdd3(t *testing.T) {
aX := ff.NewElement().SetString(
aX := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
aY := ff.NewElement().SetString(
aY := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
a := &Point{X: aX, Y: aY}
bX := ff.NewElement().SetString(
bX := utils.NewIntFromString(
"16540640123574156134436876038791482806971768689494387082833631921987005038935")
bY := ff.NewElement().SetString(
bY := utils.NewIntFromString(
"20819045374670962167435360035096875258406992893633759881276124905556507972311")
b := &Point{X: bX, Y: bY}
@@ -76,15 +68,15 @@ func TestAdd3(t *testing.T) {
}
func TestAdd4(t *testing.T) {
aX := ff.NewElement().SetString(
aX := utils.NewIntFromString(
"0")
aY := ff.NewElement().SetString(
aY := utils.NewIntFromString(
"1")
a := &Point{X: aX, Y: aY}
bX := ff.NewElement().SetString(
bX := utils.NewIntFromString(
"16540640123574156134436876038791482806971768689494387082833631921987005038935")
bY := ff.NewElement().SetString(
bY := utils.NewIntFromString(
"20819045374670962167435360035096875258406992893633759881276124905556507972311")
b := &Point{X: bX, Y: bY}
@@ -99,19 +91,19 @@ func TestAdd4(t *testing.T) {
}
func TestInCurve1(t *testing.T) {
p := &Point{X: zero(), Y: one()}
p := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
assert.Equal(t, true, p.InCurve())
}
func TestInCurve2(t *testing.T) {
p := &Point{X: one(), Y: zero()}
p := &Point{X: big.NewInt(1), Y: big.NewInt(0)}
assert.Equal(t, false, p.InCurve())
}
func TestMul0(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString("3")
@@ -131,9 +123,9 @@ func TestMul0(t *testing.T) {
}
func TestMul1(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString(
@@ -148,9 +140,9 @@ func TestMul1(t *testing.T) {
}
func TestMul2(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString(
@@ -165,45 +157,45 @@ func TestMul2(t *testing.T) {
}
func TestInCurve3(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InCurve())
}
func TestInCurve4(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InCurve())
}
func TestInSubGroup1(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InSubGroup())
}
func TestInSubGroup2(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InSubGroup())
}
func TestCompressDecompress1(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
@@ -217,9 +209,9 @@ func TestCompressDecompress1(t *testing.T) {
}
func TestCompressDecompress2(t *testing.T) {
x := ff.NewElement().SetString(
x := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := ff.NewElement().SetString(
y := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
@@ -238,8 +230,7 @@ func TestCompressDecompressRnd(t *testing.T) {
buf := p1.Compress()
p2, err := NewPoint().Decompress(buf)
assert.Equal(t, nil, err)
// assert.Equal(t, p1, p2)
assert.True(t, p1.Equal(p2))
assert.Equal(t, p1, p2)
}
}
@@ -250,15 +241,15 @@ func BenchmarkBabyjub(b *testing.B) {
var badpoints [n]*Point
for i := 0; i < n; i++ {
x := ff.NewElement().SetRandom()
y := ff.NewElement().SetRandom()
x := new(big.Int).Rand(rnd, constants.Q)
y := new(big.Int).Rand(rnd, constants.Q)
badpoints[i] = &Point{X: x, Y: y}
}
var points [n]*Point
baseX := ff.NewElement().SetString(
baseX := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
baseY := ff.NewElement().SetString(
baseY := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
base := &Point{X: baseX, Y: baseY}
for i := 0; i < n; i++ {
@@ -272,8 +263,8 @@ func BenchmarkBabyjub(b *testing.B) {
}
b.Run("AddConst", func(b *testing.B) {
p0 := &Point{X: zero(), Y: one()}
p1 := &Point{X: zero(), Y: one()}
p0 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
p1 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
p2 := NewPoint()
for i := 0; i < b.N; i++ {

View File

@@ -180,7 +180,7 @@ func (k *PrivateKey) SignMimc7(msg *big.Int) *Signature {
r.Mod(r, SubOrder)
R8 := NewPoint().Mul(r, B8) // R8 = r * 8 * B
A := k.Public().Point()
hmInput := []*big.Int{R8.X.BigInt(), R8.Y.BigInt(), A.X.BigInt(), A.Y.BigInt(), msg}
hmInput := []*big.Int{R8.X, R8.Y, A.X, A.Y, msg}
hm, err := mimc7.Hash(hmInput, nil) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
if err != nil {
panic(err)
@@ -196,7 +196,7 @@ func (k *PrivateKey) SignMimc7(msg *big.Int) *Signature {
// VerifyMimc7 verifies the signature of a message encoded as a big.Int in Zq
// using blake-512 hash for buffer hashing and mimc7 for big.Int hashing.
func (p *PublicKey) VerifyMimc7(msg *big.Int, sig *Signature) bool {
hmInput := []*big.Int{sig.R8.X.BigInt(), sig.R8.Y.BigInt(), p.X.BigInt(), p.Y.BigInt(), msg}
hmInput := []*big.Int{sig.R8.X, sig.R8.Y, p.X, p.Y, msg}
hm, err := mimc7.Hash(hmInput, nil) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
if err != nil {
panic(err)
@@ -207,7 +207,7 @@ func (p *PublicKey) VerifyMimc7(msg *big.Int, sig *Signature) bool {
r1.Mul(r1, hm)
right := NewPoint().Mul(r1, p.Point())
right.Add(sig.R8, right) // right = 8 * R + 8 * hm * A
return left.X.Equal(right.X) && left.Y.Equal(right.Y)
return (left.X.Cmp(right.X) == 0) && (left.Y.Cmp(right.Y) == 0)
}
// SignPoseidon signs a message encoded as a big.Int in Zq using blake-512 hash
@@ -223,7 +223,7 @@ func (k *PrivateKey) SignPoseidon(msg *big.Int) *Signature {
R8 := NewPoint().Mul(r, B8) // R8 = r * 8 * B
A := k.Public().Point()
hmInput := [poseidon.T]*big.Int{R8.X.BigInt(), R8.Y.BigInt(), A.X.BigInt(), A.Y.BigInt(), msg, big.NewInt(int64(0))}
hmInput := [poseidon.T]*big.Int{R8.X, R8.Y, A.X, A.Y, msg, big.NewInt(int64(0))}
hm, err := poseidon.PoseidonHash(hmInput) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
if err != nil {
panic(err)
@@ -240,7 +240,7 @@ func (k *PrivateKey) SignPoseidon(msg *big.Int) *Signature {
// VerifyPoseidon verifies the signature of a message encoded as a big.Int in Zq
// using blake-512 hash for buffer hashing and Poseidon for big.Int hashing.
func (p *PublicKey) VerifyPoseidon(msg *big.Int, sig *Signature) bool {
hmInput := [poseidon.T]*big.Int{sig.R8.X.BigInt(), sig.R8.Y.BigInt(), p.X.BigInt(), p.Y.BigInt(), msg, big.NewInt(int64(0))}
hmInput := [poseidon.T]*big.Int{sig.R8.X, sig.R8.Y, p.X, p.Y, msg, big.NewInt(int64(0))}
hm, err := poseidon.PoseidonHash(hmInput) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
if err != nil {
panic(err)
@@ -251,5 +251,5 @@ func (p *PublicKey) VerifyPoseidon(msg *big.Int, sig *Signature) bool {
r1.Mul(r1, hm)
right := NewPoint().Mul(r1, p.Point())
right.Add(sig.R8, right) // right = 8 * R + 8 * hm * A
return left.X.Equal(right.X) && left.Y.Equal(right.Y)
return (left.X.Cmp(right.X) == 0) && (left.Y.Cmp(right.Y) == 0)
}

View File

@@ -31,8 +31,8 @@ func TestPublicKey(t *testing.T) {
hex.Decode(k[:], []byte{byte(i)})
}
pk := k.Public()
assert.True(t, pk.X.BigInt().Cmp(constants.Q) == -1)
assert.True(t, pk.Y.BigInt().Cmp(constants.Q) == -1)
assert.True(t, pk.X.Cmp(constants.Q) == -1)
assert.True(t, pk.Y.Cmp(constants.Q) == -1)
}
func TestSignVerifyMimc7(t *testing.T) {

View File

@@ -513,15 +513,33 @@ func (z *Element) String() string {
// ToBigInt returns z as a big.Int in Montgomery form
func (z *Element) ToBigInt(res *big.Int) *big.Int {
bits := (*[4]big.Word)(unsafe.Pointer(z))
return res.SetBits(bits[:])
if bits.UintSize == 64 {
bits := (*[4]big.Word)(unsafe.Pointer(z))
return res.SetBits(bits[:])
} else {
var bits [8]big.Word
for i := 0; i < len(z); i++ {
bits[i*2] = big.Word(z[i])
bits[i*2+1] = big.Word(z[i] >> 32)
}
return res.SetBits(bits[:])
}
}
// ToBigIntRegular returns z as a big.Int in regular form
func (z Element) ToBigIntRegular(res *big.Int) *big.Int {
z.FromMont()
bits := (*[4]big.Word)(unsafe.Pointer(&z))
return res.SetBits(bits[:])
if bits.UintSize == 64 {
bits := (*[4]big.Word)(unsafe.Pointer(&z))
return res.SetBits(bits[:])
} else {
var bits [8]big.Word
for i := 0; i < len(z); i++ {
bits[i*2] = big.Word(z[i])
bits[i*2+1] = big.Word(z[i] >> 32)
}
return res.SetBits(bits[:])
}
}
// SetBigInt sets z to v (regular form) and returns z in Montgomery form
@@ -548,8 +566,18 @@ func (z *Element) SetBigInt(v *big.Int) *Element {
}
// v should
vBits := vv.Bits()
for i := 0; i < len(vBits); i++ {
z[i] = uint64(vBits[i])
if bits.UintSize == 64 {
for i := 0; i < len(vBits); i++ {
z[i] = uint64(vBits[i])
}
} else {
for i := 0; i < len(vBits); i++ {
if i%2 == 0 {
z[i/2] = uint64(vBits[i])
} else {
z[i/2] |= uint64(vBits[i]) << 32
}
}
}
return z.ToMont()
}

View File

@@ -1,13 +1,5 @@
package ff
import "math/big"
func NewElement() *Element {
return &Element{}
}
func (e *Element) BigInt() *big.Int {
b := big.NewInt(0)
e.ToBigIntRegular(b)
return b
}

View File

@@ -0,0 +1,11 @@
package utils
import (
"fmt"
"math/bits"
"testing"
)
func TestShowArchBits(t *testing.T) {
fmt.Printf("Architecture is %v bits\n", bits.UintSize)
}