本文整理汇总了Golang中github.com/dedis/crypto/nist.Int类的典型用法代码示例。如果您正苦于以下问题:Golang Int类的具体用法?Golang Int怎么用?Golang Int使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Int类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: decodePoint
// Decode an Edwards curve point into the given x,y coordinates.
// Returns an error if the input does not denote a valid curve point.
// Note that this does NOT check if the point is in the prime-order subgroup:
// an adversary could create an encoding denoting a point
// on the twist of the curve, or in a larger subgroup.
// However, the "safecurves" criteria (http://safecurves.cr.yp.to)
// ensure that none of these other subgroups are small
// other than the tiny ones represented by the cofactor;
// hence Diffie-Hellman exchange can be done without subgroup checking
// without exposing more than the least-significant bits of the scalar.
func (c *curve) decodePoint(bb []byte, x, y *nist.Int) error {
// Convert from little-endian
//fmt.Printf("decoding:\n%s\n", hex.Dump(bb))
b := make([]byte, len(bb))
util.Reverse(b, bb)
// Extract the sign of the x-coordinate
xsign := uint(b[0] >> 7)
b[0] &^= 0x80
// Extract the y-coordinate
y.V.SetBytes(b)
y.M = &c.P
// Compute the corresponding x-coordinate
if !c.solveForX(x, y) {
return errors.New("invalid elliptic curve point")
}
if c.coordSign(x) != xsign {
x.Neg(x)
}
return nil
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:35,代码来源:curve.go
示例2: encodePoint
// Encode an Edwards curve point.
// We use little-endian encoding for consistency with Ed25519.
func (c *curve) encodePoint(x, y *nist.Int) []byte {
// Encode the y-coordinate
b, _ := y.MarshalBinary()
// Encode the sign of the x-coordinate.
if y.M.BitLen()&7 == 0 {
// No unused bits at the top of y-coordinate encoding,
// so we must prepend a whole byte.
b = append(make([]byte, 1), b...)
}
if c.coordSign(x) != 0 {
b[0] |= 0x80
}
// Convert to little-endian
util.Reverse(b, b)
//fmt.Printf("encoding %s,%s:\n%s\n", x.String(), y.String(),
// hex.Dump(b))
return b
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:23,代码来源:curve.go
示例3: Equal
// Equality test for two Points on the same curve.
// We can avoid inversions here because:
//
// (X1/Z1,Y1/Z1) == (X2/Z2,Y2/Z2)
// iff
// (X1*Z2,Y1*Z2) == (X2*Z1,Y2*Z1)
//
func (P1 *projPoint) Equal(CP2 abstract.Point) bool {
P2 := CP2.(*projPoint)
var t1, t2 nist.Int
xeq := t1.Mul(&P1.X, &P2.Z).Equal(t2.Mul(&P2.X, &P1.Z))
yeq := t1.Mul(&P1.Y, &P2.Z).Equal(t2.Mul(&P2.Y, &P1.Z))
return xeq && yeq
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:14,代码来源:proj.go
示例4: Param1174
// Parameters defining Curve1174, as specified in:
// Bernstein et al, "Elligator: Elliptic-curve points indistinguishable
// from uniform random strings"
// http://elligator.cr.yp.to/elligator-20130828.pdf
//
func Param1174() *Param {
var p Param
var mi nist.Int
p.Name = "1174"
p.P.SetBit(zero, 251, 1).Sub(&p.P, big.NewInt(9))
p.Q.SetString("45330879683285730139092453152713398835", 10)
p.Q.Sub(&p.P, &p.Q).Div(&p.Q, big.NewInt(4))
p.R = 4
p.A.SetInt64(1)
p.D.SetInt64(-1174)
// Full-group generator is (4/V,3/5)
mi.InitString("4", "19225777642111670230408712442205514783403012708409058383774613284963344096", 10, &p.P)
p.FBX.Set(&mi.V)
mi.InitString("3", "5", 10, &p.P)
p.FBY.Set(&mi.V)
// Elligator1 parameter s for Curve1174 (Elligator paper section 4.1)
p.Elligator1s.SetString("1806494121122717992522804053500797229648438766985538871240722010849934886421", 10)
return &p
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:28,代码来源:param.go
示例5: init
// Initialize Elligator 1 parameters given magic point s
func (el *el2param) init(ec *curve, u *big.Int) *el2param {
el.ec = ec
el.u.Init(u, &ec.P)
// Compute the parameters for the Montgomery conversion:
// A = 2(a+d)/(a-d)
// B = 4/(a-d)
// See Bernstein et al, "Twisted Edwards Curves", theorem 3.2
// http://eprint.iacr.org/2008/013.pdf
var amd nist.Int
amd.Sub(&ec.a, &ec.d) // t = a-d
el.A.Add(&ec.a, &ec.d).Add(&el.A, &el.A).Div(&el.A, &amd)
el.B.Init64(4, &ec.P).Div(&el.B, &amd)
// Other precomputed constants
el.sqrtB.Sqrt(&el.B)
el.negA.Neg(&el.A)
el.pp3d8.Add(&ec.P, big.NewInt(3)).Div(&el.pp3d8, big.NewInt(8))
el.pm1d2.Sub(&ec.P, big.NewInt(1)).Div(&el.pm1d2, big.NewInt(2))
el.sqrtm1.Init64(-1, &ec.P).Sqrt(&el.sqrtm1)
return el
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:24,代码来源:el2.go
示例6: Sub
// Subtract points.
func (P *extPoint) Sub(CP1, CP2 abstract.Point) abstract.Point {
P1 := CP1.(*extPoint)
P2 := CP2.(*extPoint)
X1, Y1, Z1, T1 := &P1.X, &P1.Y, &P1.Z, &P1.T
X2, Y2, Z2, T2 := &P2.X, &P2.Y, &P2.Z, &P2.T
X3, Y3, Z3, T3 := &P.X, &P.Y, &P.Z, &P.T
var A, B, C, D, E, F, G, H nist.Int
A.Mul(X1, X2)
B.Mul(Y1, Y2)
C.Mul(T1, T2).Mul(&C, &P.c.d)
D.Mul(Z1, Z2)
E.Add(X1, Y1).Mul(&E, F.Sub(Y2, X2)).Add(&E, &A).Sub(&E, &B)
F.Add(&D, &C)
G.Sub(&D, &C)
H.Mul(&P.c.a, &A).Add(&B, &H)
X3.Mul(&E, &F)
Y3.Mul(&G, &H)
T3.Mul(&E, &H)
Z3.Mul(&F, &G)
return P
}
开发者ID:Liamsi,项目名称:crypto,代码行数:23,代码来源:ext.go
示例7: pointString
// Convert a point to string representation.
func (c *curve) pointString(x, y *nist.Int) string {
return fmt.Sprintf("(%s,%s)", x.String(), y.String())
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:4,代码来源:curve.go
示例8: HideDecode
// Elligator 1 forward-map from representative to Edwards curve point.
// Currently a straightforward, unoptimized implementation.
// See section 3.2 of the Elligator paper.
func (el *el1param) HideDecode(P point, rep []byte) {
ec := el.ec
var t, u, u2, v, Chiv, X, Y, x, y, t1, t2 nist.Int
l := ec.PointLen()
if len(rep) != l {
panic("el1Map: wrong representative length")
}
// Take the appropriate number of bits from the representative.
b := make([]byte, l)
copy(b, rep)
b[0] &^= el.padmask() // mask off the padding bits
t.InitBytes(b, &ec.P)
// u = (1-t)/(1+t)
u.Div(t1.Sub(&ec.one, &t), t2.Add(&ec.one, &t))
// v = u^5 + (r^2-2)u^3 + u
u2.Mul(&u, &u) // u2 = u^2
v.Mul(&u2, &u2) // v = u^4
v.Add(&v, t1.Mul(&el.r2m2, &u2)) // v = u^4 + (r^2-2)u^2
v.Add(&v, &ec.one).Mul(&v, &u) // v = u^5 + (r^2-2)u^3 + u
// X = Chi(v)u
chi(&Chiv, &v)
X.Mul(&Chiv, &u)
// Y = (Chi(v)v)^((q+1)/4) Chi(v) Chi(u^2+1/c^2)
t1.Add(&u2, &el.invc2)
chi(&t1, &t1) // t1 = Chi(u^2+1/c^2)
Y.Mul(&Chiv, &v)
Y.Exp(&Y, &el.pp1d4).Mul(&Y, &Chiv).Mul(&Y, &t1)
// x = (c-1)sX(1+X)/Y
x.Add(&ec.one, &X).Mul(&X, &x).Mul(&el.cm1s, &x).Div(&x, &Y)
// y = (rX-(1+X)^2)/(rX+(1+X)^2)
t1.Mul(&el.r, &X) // t1 = rX
t2.Add(&ec.one, &X).Mul(&t2, &t2) // t2 = (1+X)^2
y.Div(u.Sub(&t1, &t2), v.Add(&t1, &t2))
// Sanity-check
if !ec.onCurve(&x, &y) {
panic("elligator1 produced invalid point")
}
P.initXY(&x.V, &y.V, ec.self)
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:52,代码来源:el1.go
示例9: HideEncode
// Elligator 2 reverse-map from point to uniform representative.
// Returns nil if point has no uniform representative.
// See section 5.3 of the Elligator paper.
func (el *el2param) HideEncode(P point, rand cipher.Stream) []byte {
edx, edy := P.getXY()
var x, y, r, xpA, t1 nist.Int
// convert Edwards to Montgomery coordinates
el.ed2mont(&x, &y, edx, edy)
// condition 1: x != -A
if x.Equal(&el.negA) {
return nil // x = -A, no representative
}
// condition 2: if y=0, then x=0
if y.V.Sign() == 0 && x.V.Sign() != 0 {
return nil // y=0 but x!=0, no representative
}
// condition 3: -ux(x+A) is a square
xpA.Add(&x, &el.A)
t1.Mul(&el.u, &x).Mul(&t1, &xpA).Neg(&t1)
if math.Jacobi(&t1.V, t1.M) < 0 {
return nil // not a square, no representative
}
if y.V.Cmp(&el.pm1d2) <= 0 { // y in image of sqrt function
r.Mul(&xpA, &el.u).Div(&x, &r)
} else { // y not in image of sqrt function
r.Mul(&el.u, &x).Div(&xpA, &r)
}
r.Neg(&r)
el.sqrt(&r, &r)
// Sanity check on result
if r.V.Cmp(&el.pm1d2) > 0 {
panic("el2: r too big")
}
// Map representative to a byte-string by padding the upper byte.
// This assumes that the prime c.P is close enough to a power of 2
// that the adversary will never notice the "missing" values;
// this is true for the class of curves Elligator1 was designed for.
rep, _ := r.MarshalBinary()
padmask := el.padmask()
if padmask != 0 {
var pad [1]byte
rand.XORKeyStream(pad[:], pad[:])
rep[0] |= pad[0] & padmask
}
return rep
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:53,代码来源:el2.go
示例10: double
// Optimized point doubling for use in scalar multiplication.
func (P *projPoint) double() {
var B, C, D, E, F, H, J nist.Int
B.Add(&P.X, &P.Y).Mul(&B, &B)
C.Mul(&P.X, &P.X)
D.Mul(&P.Y, &P.Y)
E.Mul(&P.c.a, &C)
F.Add(&E, &D)
H.Mul(&P.Z, &P.Z)
J.Add(&H, &H).Sub(&F, &J)
P.X.Sub(&B, &C).Sub(&P.X, &D).Mul(&P.X, &J)
P.Y.Sub(&E, &D).Mul(&F, &P.Y)
P.Z.Mul(&F, &J)
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:15,代码来源:proj.go
示例11: Add
// Add two points using the basic unified addition laws for Edwards curves:
//
// x' = ((x1*y2 + x2*y1) / (1 + d*x1*x2*y1*y2))
// y' = ((y1*y2 - a*x1*x2) / (1 - d*x1*x2*y1*y2))
//
func (P *basicPoint) Add(P1, P2 abstract.Point) abstract.Point {
E1 := P1.(*basicPoint)
E2 := P2.(*basicPoint)
x1, y1 := E1.x, E1.y
x2, y2 := E2.x, E2.y
var t1, t2, dm, nx, dx, ny, dy nist.Int
// Reused part of denominator: dm = d*x1*x2*y1*y2
dm.Mul(&P.c.d, &x1).Mul(&dm, &x2).Mul(&dm, &y1).Mul(&dm, &y2)
// x' numerator/denominator
nx.Add(t1.Mul(&x1, &y2), t2.Mul(&x2, &y1))
dx.Add(&P.c.one, &dm)
// y' numerator/denominator
ny.Sub(t1.Mul(&y1, &y2), t2.Mul(&x1, &x2).Mul(&P.c.a, &t2))
dy.Sub(&P.c.one, &dm)
// result point
P.x.Div(&nx, &dx)
P.y.Div(&ny, &dy)
return P
}
开发者ID:Liamsi,项目名称:crypto,代码行数:29,代码来源:basic.go
示例12: pickPoint
// Pick a [pseudo-]random curve point with optional embedded data,
// filling in the point's x,y coordinates
// and returning any remaining data not embedded.
func (c *curve) pickPoint(P point, data []byte, rand cipher.Stream) []byte {
// How much data to embed?
dl := c.pickLen()
if dl > len(data) {
dl = len(data)
}
// Retry until we find a valid point
var x, y nist.Int
var Q abstract.Point
for {
// Get random bits the size of a compressed Point encoding,
// in which the topmost bit is reserved for the x-coord sign.
l := c.PointLen()
b := make([]byte, l)
rand.XORKeyStream(b, b) // Interpret as little-endian
if data != nil {
b[0] = byte(dl) // Encode length in low 8 bits
copy(b[1:1+dl], data) // Copy in data to embed
}
util.Reverse(b, b) // Convert to big-endian form
xsign := b[0] >> 7 // save x-coordinate sign bit
b[0] &^= 0xff << uint(c.P.BitLen()&7) // clear high bits
y.M = &c.P // set y-coordinate
y.SetBytes(b)
if !c.solveForX(&x, &y) { // Corresponding x-coordinate?
continue // none, retry
}
// Pick a random sign for the x-coordinate
if c.coordSign(&x) != uint(xsign) {
x.Neg(&x)
}
// Initialize the point
P.initXY(&x.V, &y.V, c.self)
if c.full {
// If we're using the full group,
// we just need any point on the curve, so we're done.
return data[dl:]
}
// We're using the prime-order subgroup,
// so we need to make sure the point is in that subgroup.
// If we're not trying to embed data,
// we can convert our point into one in the subgroup
// simply by multiplying it by the cofactor.
if data == nil {
P.Mul(P, &c.cofact) // multiply by cofactor
if P.Equal(c.null) {
continue // unlucky; try again
}
return data[dl:]
}
// Since we need the point's y-coordinate to make sense,
// we must simply check if the point is in the subgroup
// and retry point generation until it is.
if Q == nil {
Q = c.self.Point()
}
Q.Mul(P, &c.order)
if Q.Equal(c.null) {
return data[dl:]
}
// Keep trying...
}
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:76,代码来源:curve.go
示例13: solveForX
// Given a y-coordinate, solve for the x-coordinate on the curve,
// using the characteristic equation rewritten as:
//
// x^2 = (1 - y^2)/(a - d*y^2)
//
// Returns true on success,
// false if there is no x-coordinate corresponding to the chosen y-coordinate.
//
func (c *curve) solveForX(x, y *nist.Int) bool {
var yy, t1, t2 nist.Int
yy.Mul(y, y) // yy = y^2
t1.Sub(&c.one, &yy) // t1 = 1 - y^-2
t2.Mul(&c.d, &yy).Sub(&c.a, &t2) // t2 = a - d*y^2
t2.Div(&t1, &t2) // t2 = x^2
return x.Sqrt(&t2) // may fail if not a square
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:17,代码来源:curve.go
示例14: checkT
// Check the validity of the T coordinate
func (P *extPoint) checkT() {
var t1, t2 nist.Int
if !t1.Mul(&P.X, &P.Y).Equal(t2.Mul(&P.Z, &P.T)) {
panic("oops")
}
}
开发者ID:Liamsi,项目名称:crypto,代码行数:7,代码来源:ext.go
示例15: sqrt
// Compute the square root function,
// specified in section 5.5 of the Elligator paper.
func (el *el2param) sqrt(r, a *nist.Int) {
var b, b2 nist.Int
b.Exp(a, &el.pp3d8) // b = a^((p+3)/8); b in {a,-a}
b2.Mul(&b, &b) // b^2 = a?
if !b2.Equal(a) {
b.Mul(&b, &el.sqrtm1) // b*sqrt(-1)
}
if b.V.Cmp(&el.pm1d2) > 0 { // |b|
b.Neg(&b)
}
r.Set(&b)
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:17,代码来源:el2.go
示例16: mont2ed
// Convert from Montgomery form (u,v) to Edwards (x,y) via:
//
// x = sqrt(B)u/v
// y = (u-1)/(u+1)
//
func (el *el2param) mont2ed(x, y, u, v *nist.Int) {
ec := el.ec
var t1, t2 nist.Int
x.Mul(u, &el.sqrtB).Div(x, v)
y.Div(t1.Sub(u, &ec.one), t2.Add(u, &ec.one))
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:11,代码来源:el2.go
示例17: ed2mont
// Convert point from Twisted Edwards form: ax^2+y^2 = 1+dx^2y^2
// to Montgomery form: v^2 = u^3+Au^2+u
// via the equivalence:
//
// u = (1+y)/(1-y)
// v = sqrt(B)u/x
//
// where A=2(a+d)/(a-d) and B=4(a-d)
//
// Beware: the Twisted Edwards Curves paper uses B as a factor for v^2,
// whereas the Elligator 2 paper uses B as a factor for the last u term.
//
func (el *el2param) ed2mont(u, v, x, y *nist.Int) {
ec := el.ec
var t1, t2 nist.Int
u.Div(t1.Add(&ec.one, y), t2.Sub(&ec.one, y))
v.Mul(u, &el.sqrtB).Div(v, x)
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:18,代码来源:el2.go
示例18: onCurve
// Test if a supposed point is on the curve,
// by checking the characteristic equation for Edwards curves:
//
// a*x^2 + y^2 = 1 + d*x^2*y^2
//
func (c *curve) onCurve(x, y *nist.Int) bool {
var xx, yy, l, r nist.Int
xx.Mul(x, x) // xx = x^2
yy.Mul(y, y) // yy = y^2
l.Mul(&c.a, &xx).Add(&l, &yy) // l = a*x^2 + y^2
r.Mul(&c.d, &xx).Mul(&r, &yy).Add(&c.one, &r)
// r = 1 + d*x^2*y^2
return l.Equal(&r)
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:16,代码来源:curve.go
示例19: double
// Optimized point doubling for use in scalar multiplication.
// Uses the formulae in section 3.3 of:
// https://www.iacr.org/archive/asiacrypt2008/53500329/53500329.pdf
func (P *extPoint) double() {
X1, Y1, Z1, T1 := &P.X, &P.Y, &P.Z, &P.T
var A, B, C, D, E, F, G, H nist.Int
A.Mul(X1, X1)
B.Mul(Y1, Y1)
C.Mul(Z1, Z1).Add(&C, &C)
D.Mul(&P.c.a, &A)
E.Add(X1, Y1).Mul(&E, &E).Sub(&E, &A).Sub(&E, &B)
G.Add(&D, &B)
F.Sub(&G, &C)
H.Sub(&D, &B)
X1.Mul(&E, &F)
Y1.Mul(&G, &H)
T1.Mul(&E, &H)
Z1.Mul(&F, &G)
}
开发者ID:Liamsi,项目名称:crypto,代码行数:20,代码来源:ext.go
示例20: init
// Initialize a twisted Edwards curve with given parameters.
// Caller passes pointers to null and base point prototypes to be initialized.
func (c *curve) init(self abstract.Group, p *Param, fullGroup bool,
null, base point) *curve {
c.self = self
c.Param = *p
c.full = fullGroup
c.null = null
// Edwards curve parameters as ModInts for convenience
c.a.Init(&p.A, &p.P)
c.d.Init(&p.D, &p.P)
// Cofactor
c.cofact.Init64(int64(p.R), &c.P)
// Determine the modulus for scalars on this curve.
// Note that we do NOT initialize c.order with Init(),
// as that would normalize to the modulus, resulting in zero.
// Just to be sure it's never used, we leave c.order.M set to nil.
// We want it to be in a ModInt so we can pass it to P.Mul(),
// but the scalar's modulus isn't needed for point multiplication.
if fullGroup {
// Scalar modulus is prime-order times the ccofactor
c.order.V.SetInt64(int64(p.R)).Mul(&c.order.V, &p.Q)
} else {
c.order.V.Set(&p.Q) // Prime-order subgroup
}
// Useful ModInt constants for this curve
c.zero.Init64(0, &c.P)
c.one.Init64(1, &c.P)
// Identity element is (0,1)
null.initXY(zero, one, self)
// Base point B
var bx, by *big.Int
if !fullGroup {
bx, by = &p.PBX, &p.PBY
} else {
bx, by = &p.FBX, &p.FBY
base.initXY(&p.FBX, &p.FBY, self)
}
if by.Sign() == 0 {
// No standard base point was defined, so pick one.
// Find the lowest-numbered y-coordinate that works.
//println("Picking base point:")
var x, y nist.Int
for y.Init64(2, &c.P); ; y.Add(&y, &c.one) {
if !c.solveForX(&x, &y) {
continue // try another y
}
if c.coordSign(&x) != 0 {
x.Neg(&x) // try positive x first
}
base.initXY(&x.V, &y.V, self)
if c.validPoint(base) {
break // got one
}
x.Neg(&x) // try -bx
if c.validPoint(base) {
break // got one
}
}
//println("BX: "+x.V.String())
//println("BY: "+y.V.String())
bx, by = &x.V, &y.V
}
base.initXY(bx, by, self)
// Uniform representation encoding methods,
// only useful when using the full group.
// (Points taken from the subgroup would be trivially recognizable.)
if fullGroup {
if p.Elligator1s.Sign() != 0 {
c.hide = new(el1param).init(c, &p.Elligator1s)
} else if p.Elligator2u.Sign() != 0 {
c.hide = new(el2param).init(c, &p.Elligator2u)
}
// XXX Elligator Squared
}
// Sanity checks
if !c.validPoint(null) {
panic("invalid identity point " + null.String())
}
if !c.validPoint(base) {
panic("invalid base point " + base.String())
}
return c
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:93,代码来源:curve.go
注:本文中的github.com/dedis/crypto/nist.Int类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论