### Get Extended Coordinates (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Retrieves the extended coordinates (X:Y:Z:T) of a point on the Edwards curve. The relationships x = X/Z, y = Y/Z, and xy = T/Z hold. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() // Get extended coordinates X, Y, Z, T := G.ExtendedCoordinates() fmt.Printf("X bytes length: %d\n", len(X.Bytes())) fmt.Printf("Y bytes length: %d\n", len(Y.Bytes())) fmt.Printf("Z bytes length: %d\n", len(Z.Bytes())) fmt.Printf("T bytes length: %d\n", len(T.Bytes())) // Output: // X bytes length: 32 // Y bytes length: 32 // Z bytes length: 32 // T bytes length: 32 } ``` -------------------------------- ### Point.Equal - Compare Two Points (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Compares two points, u and v. Returns 1 if they are equivalent, and 0 otherwise. The comparison is performed in constant time for security. Requires the 'filippo.io/edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G1 := edwards25519.NewGeneratorPoint() G2 := edwards25519.NewGeneratorPoint() identity := edwards25519.NewIdentityPoint() // Compare equal points if G1.Equal(G2) == 1 { fmt.Println("Two generators are equal") } // Compare different points if G1.Equal(identity) == 0 { fmt.Println("Generator and identity are different") } // Output: // Two generators are equal // Generator and identity are different } ``` -------------------------------- ### Point.Equal Source: https://context7.com/filosottile/edwards25519/llms.txt Compares two points for equality in constant time. ```APIDOC ## Point.Equal - Compare Two Points ### Description Returns 1 if v is equivalent to u, and 0 otherwise. Comparison is done in constant time. ### Method Equal ### Parameters #### Receiver - **v** (*Point) - The first point. #### Arguments - **u** (*Point) - The second point. ### Returns - **int** - 1 if the points are equal, 0 otherwise. ### Example ```go // Compare two generators if G1.Equal(G2) == 1 { fmt.Println("Generators are equal") } ``` ``` -------------------------------- ### Point.Double - Double a Point (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Doubles a point p by adding it to itself (p + p), setting the result to v. This is an optimized operation for point doubling, performed in constant time. It depends on the 'filippo.io/edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() // Double the generator using the Double method twoG := new(edwards25519.Point).Double(G) // Compare with addition twoGAdd := new(edwards25519.Point).Add(G, G) if twoG.Equal(twoGAdd) == 1 { fmt.Println("Double(G) = G + G verified") } // Output: Double(G) = G + G verified } ``` -------------------------------- ### Set Scalar with RFC 8032 Clamping (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Sets a scalar by applying the buffer pruning (clamping) mechanism specified in RFC 8032. This is the standard method for deriving Ed25519 private keys from a seed, ensuring proper scalar representation. ```go package main import ( "crypto/sha512" "encoding/hex" "fmt" "filippo.io/edwards25519" ) func main() { // Simulate Ed25519 key derivation seed := make([]byte, 32) // In practice, use crypto/rand to generate the seed // Hash the seed as per Ed25519 h := sha512.Sum512(seed) // Apply clamping to the first 32 bytes scalar, err := edwards25519.NewScalar().SetBytesWithClamping(h[:32]) if err != nil { fmt.Printf("Error: %v\n", err) return } // Compute the public key publicKey := new(edwards25519.Point).ScalarBaseMult(scalar) fmt.Printf("Public key: %s\n", hex.EncodeToString(publicKey.Bytes())) } ``` -------------------------------- ### Point.Select - Constant-Time Selection (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Selects between two points, a and b, based on a condition. If cond is 1, v is set to a; if cond is 0, v is set to b. This operation is performed in constant time for security. It requires the 'filippo.io/edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() identity := edwards25519.NewIdentityPoint() // Select based on condition (constant-time) result := new(edwards25519.Point) // When cond = 1, select G result.Select(G, identity, 1) if result.Equal(G) == 1 { fmt.Println("Selected G when cond=1") } // When cond = 0, select identity result.Select(G, identity, 0) if result.Equal(identity) == 1 { fmt.Println("Selected identity when cond=0") } // Output: // Selected G when cond=1 // Selected identity when cond=0 } ``` -------------------------------- ### Point.ScalarMult - Variable-Base Scalar Multiplication Source: https://context7.com/filosottile/edwards25519/llms.txt Performs variable-base scalar multiplication (v = x * q) in constant time using precomputed lookup tables. 'q' can be any point. ```APIDOC ## Point.ScalarMult - Variable-Base Scalar Multiplication ### Description Sets v = x * q, where q is any point, and returns v. This operation is performed in constant time using precomputed lookup tables. ### Method `ScalarMult(x *Scalar, p *Point) *Point` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go // Create a scalar value of 5 five := edwards25519.NewScalar() five.SetCanonicalBytes([]byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) G := edwards25519.NewGeneratorPoint() // Compute 5 * G using scalar multiplication fiveG := new(edwards25519.Point).ScalarMult(five, G) ``` ### Response #### Success Response (200) - ***Point** (type) - The resulting point after scalar multiplication. #### Response Example ```json { "example": "// The result is stored in the receiver point fiveG" } ``` ``` -------------------------------- ### Memory-Efficient Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Performs scalar multiplication v = x * q using less memory than the standard ScalarMult, at the cost of speed. This is useful in memory-constrained environments. It takes a scalar and a point as input and returns the resulting point. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Create a scalar scalar := edwards25519.NewScalar() scalar.SetCanonicalBytes([]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) G := edwards25519.NewGeneratorPoint() // Memory-efficient scalar multiplication result := new(edwards25519.Point).ScalarMultSlow(scalar, G) // Compare with regular scalar multiplication expected := new(edwards25519.Point).ScalarMult(scalar, G) if result.Equal(expected) == 1 { fmt.Println("ScalarMultSlow produces same result as ScalarMult") } // Output: ScalarMultSlow produces same result as ScalarMult } ``` -------------------------------- ### Point.Select Source: https://context7.com/filosottile/edwards25519/llms.txt Selects one of two points based on a condition in constant time. ```APIDOC ## Point.Select - Constant-Time Selection ### Description Sets v to a if cond == 1 and to b if cond == 0. This operation is performed in constant time for security. ### Method Select ### Parameters #### Receiver - **v** (*Point) - The point to store the result in. #### Arguments - **a** (*Point) - The point to select if cond is 1. - **b** (*Point) - The point to select if cond is 0. - **cond** (int) - The condition (1 for 'a', 0 for 'b'). ### Returns - **v** (*Point) - The selected point. ### Example ```go // Select G if cond is 1, otherwise select identity result := new(edwards25519.Point) result.Select(G, identity, 1) // Selects G ``` ``` -------------------------------- ### Point.ScalarMultSlow - Memory-Efficient Scalar Multiplication Source: https://context7.com/filosottile/edwards25519/llms.txt Performs scalar multiplication (v = x * q) using less memory than ScalarMult, at the cost of speed. Suitable for memory-constrained environments. ```APIDOC ## Point.ScalarMultSlow - Memory-Efficient Scalar Multiplication ### Description Sets v = x * q using less memory than ScalarMult at the cost of speed. Useful when memory is constrained. ### Method `ScalarMultSlow(x *Scalar, p *Point) *Point` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go // Create a scalar scalar := edwards25519.NewScalar() scalar.SetCanonicalBytes([]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) G := edwards25519.NewGeneratorPoint() // Memory-efficient scalar multiplication result := new(edwards25519.Point).ScalarMultSlow(scalar, G) ``` ### Response #### Success Response (200) - ***Point** (type) - The resulting point after memory-efficient scalar multiplication. #### Response Example ```json { "example": "// The result is stored in the receiver point result" } ``` ``` -------------------------------- ### Set Scalar from Uniform Random Bytes (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Sets a scalar from a 64-byte input by reducing it modulo the group order. This method is recommended for generating uniformly distributed random scalars, often used in cryptographic protocols. ```go package main import ( "crypto/rand" "encoding/hex" "fmt" "filippo.io/edwards25519" ) func main() { // Generate 64 random bytes var randomBytes [64]byte rand.Read(randomBytes[:]) // Create a uniformly distributed scalar scalar, err := edwards25519.NewScalar().SetUniformBytes(randomBytes[:]) if err != nil { fmt.Printf("Error: %v\n", err) return } // The result is reduced modulo the group order fmt.Printf("Random scalar: %s...\n", hex.EncodeToString(scalar.Bytes()[:8])) } ``` -------------------------------- ### Point.Subtract - Subtract Two Points (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Subtracts two points q from p using the group subtraction law, setting the result to v. This operation is performed in constant time. It requires the 'filippo.io/edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() // Compute 3*G twoG := new(edwards25519.Point).Add(G, G) threeG := new(edwards25519.Point).Add(twoG, G) // Compute 3*G - G = 2*G result := new(edwards25519.Point).Subtract(threeG, G) if result.Equal(twoG) == 1 { fmt.Println("3*G - G = 2*G verified") } // Output: 3*G - G = 2*G verified } ``` -------------------------------- ### Variable-Time Multi-Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Performs variable-time multi-scalar multiplication, computing v = sum(scalars[i] * points[i]). This method is faster than constant-time alternatives but its execution time depends on the input values. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() twoG := new(edwards25519.Point).Add(G, G) s1 := edwards25519.NewScalar() s1.SetCanonicalBytes([]byte{7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) s2 := edwards25519.NewScalar() s2.SetCanonicalBytes([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) // Compute 7*G + 3*(2G) = 7G + 6G = 13G (variable time, faster) scalars := []*edwards25519.Scalar{s1, s2} points := []*edwards25519.Point{G, twoG} result := new(edwards25519.Point).VarTimeMultiScalarMult(scalars, points) thirteen := edwards25519.NewScalar() thirteen.SetCanonicalBytes([]byte{13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) expected := new(edwards25519.Point).ScalarBaseMult(thirteen) if result.Equal(expected) == 1 { fmt.Println("VarTime multi-scalar: 7*G + 3*2G = 13*G") } // Output: VarTime multi-scalar: 7*G + 3*2G = 13*G } ``` -------------------------------- ### Point.Negate - Negate a Point (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Negates a point p, setting the result to v. The negation of P is -P such that P + (-P) equals the identity element. This operation is constant time and requires the 'filippo.io/edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() identity := edwards25519.NewIdentityPoint() // Compute -G negG := new(edwards25519.Point).Negate(G) // Verify G + (-G) = identity result := new(edwards25519.Point).Add(G, negG) if result.Equal(identity) == 1 { fmt.Println("G + (-G) = identity verified") } // Output: G + (-G) = identity verified } ``` -------------------------------- ### Constant-Time Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Performs scalar multiplication v = x * q in constant time using precomputed lookup tables. This method is efficient and suitable for cryptographic operations where timing attacks are a concern. It takes a scalar and a point as input and returns the resulting point. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Create a scalar value of 5 five := edwards25519.NewScalar() five.SetCanonicalBytes([]byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) G := edwards25519.NewGeneratorPoint() // Compute 5 * G using scalar multiplication fiveG := new(edwards25519.Point).ScalarMult(five, G) // Verify by repeated addition result := edwards25519.NewIdentityPoint() for i := 0; i < 5; i++ { result.Add(result, G) } if fiveG.Equal(result) == 1 { fmt.Println("5*G computed correctly") } // Output: 5*G computed correctly } ``` -------------------------------- ### Point.Double Source: https://context7.com/filosottile/edwards25519/llms.txt Doubles a point (sets v = p + p) using an optimized operation. ```APIDOC ## Point.Double - Double a Point ### Description Sets v = p + p and returns v. This is an optimized operation for point doubling. ### Method Double ### Parameters #### Receiver - **v** (*Point) - The point to store the result in. #### Arguments - **p** (*Point) - The point to double. ### Returns - **v** (*Point) - The doubled point (2*p). ### Example ```go // Double the generator G twoG := new(edwards25519.Point).Double(G) ``` ``` -------------------------------- ### Scalar.SetBytesWithClamping - Set with RFC 8032 Clamping Source: https://context7.com/filosottile/edwards25519/llms.txt Sets the scalar by applying the buffer pruning (clamping) described in RFC 8032. This is commonly used for Ed25519 private keys. ```APIDOC ## Scalar.SetBytesWithClamping ### Description Sets the scalar by applying the buffer pruning (clamping) mechanism as specified in RFC 8032. This method is essential for deriving Ed25519 private keys. ### Method POST (conceptual, as this is a method on a Scalar object) ### Endpoint N/A (Method call on a Scalar instance) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **bytes** ([]byte) - Required - A byte slice (typically 32 bytes) to which clamping will be applied. ### Request Example ```go // Example usage within Go: // h := sha512.Sum512(seed) // scalar, err := edwards25519.NewScalar().SetBytesWithClamping(h[:32]) ``` ### Response #### Success Response (200) - **Scalar** (*edwards25519.Scalar) - The scalar object after applying RFC 8032 clamping. #### Response Example ```go // No direct JSON response, returns a Scalar object in Go. ``` ``` -------------------------------- ### Variable-Time Double Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Computes v = a * A + b * B, where B is the canonical generator. The execution time depends on the input scalars, making it faster than constant-time methods but unsuitable for secret scalars. It's useful for operations like signature verification. Inputs are two scalars and two points; output is the resulting point. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Create scalars a and b a := edwards25519.NewScalar() a.SetCanonicalBytes([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) b := edwards25519.NewScalar() b.SetCanonicalBytes([]byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) G := edwards25519.NewGeneratorPoint() twoG := new(edwards25519.Point).Add(G, G) // Compute a*A + b*B in one operation (useful for signature verification) result := new(edwards25519.Point).VarTimeDoubleScalarBaseMult(a, twoG, b) // Verify: should equal 3*(2G) + 5*G = 6G + 5G = 11G eleven := edwards25519.NewScalar() eleven.SetCanonicalBytes([]byte{11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) expected := new(edwards25519.Point).ScalarBaseMult(eleven) if result.Equal(expected) == 1 { fmt.Println("3*(2G) + 5*G = 11*G verified") } // Output: 3*(2G) + 5*G = 11*G verified } ``` -------------------------------- ### Create Zero Scalar (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Initializes a new edwards25519.Scalar to the value zero. Scalars in this library are integers modulo the group order l = 2^252 + 27742317777372353535851937790883648493. ```go package main import ( "encoding/hex" "fmt" "filippo.io/edwards25519" ) func main() { // Create a zero scalar zero := edwards25519.NewScalar() // Verify it encodes to all zeros bytes := zero.Bytes() fmt.Printf("Zero scalar: %s\n", hex.EncodeToString(bytes)) // Zero scalar multiplication yields identity G := edwards25519.NewGeneratorPoint() result := new(edwards25519.Point).ScalarBaseMult(zero) identity := edwards25519.NewIdentityPoint() if result.Equal(identity) == 1 { fmt.Println("0 * G = identity") } // Output: // Zero scalar: 0000000000000000000000000000000000000000000000000000000000000000 // 0 * G = identity } ``` -------------------------------- ### Set Scalar from Canonical Bytes (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Sets a scalar from its 32-byte little-endian canonical encoding. Returns an error if the input is not canonical (i.e., less than the group order). This is useful for direct scalar initialization when the canonical form is known. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Set a scalar from canonical bytes (little-endian) // Value: 123456789 bytes := []byte{ 0x15, 0xcd, 0x5b, 0x07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } scalar, err := edwards25519.NewScalar().SetCanonicalBytes(bytes) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Scalar bytes: %x\n", scalar.Bytes()[:8]) // Output: Scalar bytes: 15cd5b0700000000 } ``` -------------------------------- ### Multi-Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Computes the sum of multiple scalar multiplications: v = sum(scalars[i] * points[i]). This operation is performed in constant time, with execution time depending only on the lengths of the input slices. It takes slices of scalars and points as input and returns the resulting point. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { G := edwards25519.NewGeneratorPoint() twoG := new(edwards25519.Point).Add(G, G) threeG := new(edwards25519.Point).Add(twoG, G) // Create scalars: [2, 3, 4] s1 := edwards25519.NewScalar() s1.SetCanonicalBytes([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) s2 := edwards25519.NewScalar() s2.SetCanonicalBytes([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) s3 := edwards25519.NewScalar() s3.SetCanonicalBytes([]byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) // Compute 2*G + 3*(2G) + 4*(3G) = 2G + 6G + 12G = 20G scalars := []*edwards25519.Scalar{s1, s2, s3} points := []*edwards25519.Point{G, twoG, threeG} result := new(edwards25519.Point).MultiScalarMult(scalars, points) // Verify twenty := edwards25519.NewScalar() twenty.SetCanonicalBytes([]byte{20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) expected := new(edwards25519.Point).ScalarBaseMult(twenty) if result.Equal(expected) == 1 { fmt.Println("Multi-scalar multiplication verified: 2*G + 3*2G + 4*3G = 20*G") } // Output: Multi-scalar multiplication verified: 2*G + 3*2G + 4*3G = 20*G } ``` -------------------------------- ### Element.SetBytes - Set Field Element from Bytes in Go Source: https://context7.com/filosottile/edwards25519/llms.txt Initializes a field element from a 32-byte little-endian representation. The most significant bit of the last byte is ignored, following RFC 7748. This function is part of the filippo.io/edwards25519/field package and handles potential errors during byte conversion. ```go package main import ( "encoding/hex" "fmt" "filippo.io/edwards25519/field" ) func main() { // Create a field element from bytes bytes := make([]byte, 32) bytes[0] = 42 // Little-endian: value = 42 elem, err := new(field.Element).SetBytes(bytes) if err != nil { fmt.Printf("Error: %v\n", err) return } // Encode back to bytes result := elem.Bytes() fmt.Printf("Field element: %s\n", hex.EncodeToString(result[:4])) // Output: Field element: 2a000000 } ``` -------------------------------- ### Create Identity Point in edwards25519 Source: https://context7.com/filosottile/edwards25519/llms.txt Creates a new Point set to the identity element (point at infinity), which serves as the neutral element for point addition in the edwards25519 library. This is useful for verifying additive properties. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Create the identity point (neutral element) identity := edwards25519.NewIdentityPoint() // Verify identity property: P + identity = P generator := edwards25519.NewGeneratorPoint() result := new(edwards25519.Point).Add(generator, identity) if result.Equal(generator) == 1 { fmt.Println("Identity property verified: G + 0 = G") } // Output: Identity property verified: G + 0 = 0 } ``` -------------------------------- ### Point.ScalarBaseMult - Fixed-Base Scalar Multiplication (Go) Source: https://context7.com/filosottile/edwards25519/llms.txt Performs fixed-base scalar multiplication, computing v = x * B, where B is the canonical generator of the Edwards25519 curve. This operation uses precomputed tables for the generator and runs in constant time. It requires 'crypto/rand', 'encoding/hex', and 'filippo.io/edwards25519'. ```go package main import ( "crypto/rand" "encoding/hex" "fmt" "filippo.io/edwards25519" ) func main() { // Generate a random scalar (like a private key) var randomBytes [64]byte rand.Read(randomBytes[:]) scalar, _ := edwards25519.NewScalar().SetUniformBytes(randomBytes[:]) // Compute public key: P = scalar * G publicKey := new(edwards25519.Point).ScalarBaseMult(scalar) fmt.Printf("Public key: %s\n", hex.EncodeToString(publicKey.Bytes())) } ``` -------------------------------- ### Point.ScalarBaseMult Source: https://context7.com/filosottile/edwards25519/llms.txt Performs fixed-base scalar multiplication (v = x * B) using the canonical generator. ```APIDOC ## Point.ScalarBaseMult - Fixed-Base Scalar Multiplication ### Description Sets v = x * B, where B is the canonical generator, and returns v. This operation uses precomputed tables for the generator and is performed in constant time. ### Method ScalarBaseMult ### Parameters #### Receiver - **v** (*Point) - The point to store the result in. #### Arguments - **x** (*Scalar) - The scalar multiplier. ### Returns - **v** (*Point) - The resulting point (x * B). ### Example ```go // Generate a random scalar scalar, _ := edwards25519.NewScalar().SetUniformBytes(randomBytes[:]) // Compute public key: P = scalar * G publicKey := new(edwards25519.Point).ScalarBaseMult(scalar) ``` ``` -------------------------------- ### Scalar.SetUniformBytes - Set from 64 Random Bytes Source: https://context7.com/filosottile/edwards25519/llms.txt Sets the scalar from a 64-byte input by reducing modulo the group order. Use this for uniformly distributed random scalars. ```APIDOC ## Scalar.SetUniformBytes ### Description Sets the scalar from a 64-byte input by reducing modulo the group order. This method is suitable for generating uniformly distributed random scalars. ### Method POST (conceptual, as this is a method on a Scalar object) ### Endpoint N/A (Method call on a Scalar instance) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **bytes** ([]byte) - Required - A 64-byte slice used to derive the scalar. ### Request Example ```go // Example usage within Go: // var randomBytes [64]byte // rand.Read(randomBytes[:]) // scalar, err := edwards25519.NewScalar().SetUniformBytes(randomBytes[:]) ``` ### Response #### Success Response (200) - **Scalar** (*edwards25519.Scalar) - The scalar object derived from the 64-byte input. #### Response Example ```go // No direct JSON response, returns a Scalar object in Go. ``` ``` -------------------------------- ### Point.Negate Source: https://context7.com/filosottile/edwards25519/llms.txt Negates a point (sets v = -p) such that p + (-p) equals the identity point. ```APIDOC ## Point.Negate - Negate a Point ### Description Sets v = -p and returns v. The negation of a point P is the point -P such that P + (-P) = identity. ### Method Negate ### Parameters #### Receiver - **v** (*Point) - The point to store the result in. #### Arguments - **p** (*Point) - The point to negate. ### Returns - **v** (*Point) - The negated point (-p). ### Example ```go // Compute -G negG := new(edwards25519.Point).Negate(G) ``` ``` -------------------------------- ### Square Root Ratio Calculation in Go Source: https://context7.com/filosottile/edwards25519/llms.txt Computes the square root of the ratio u/v in the finite field GF(2^255-19). It returns the result and a boolean indicating whether u/v was a quadratic residue. This is used in certain cryptographic protocols. ```go package main import ( "fmt" "filippo.io/edwards25519/field" ) func main() { // Create u = 4, v = 1 uBytes := make([]byte, 32) uBytes[0] = 4 u, _ := new(field.Element).SetBytes(uBytes) v := new(field.Element).One() // Compute sqrt(4/1) = sqrt(4) = 2 result, wasSquare := new(field.Element).SqrtRatio(u, v) if wasSquare == 1 { fmt.Printf("sqrt(4) = %d\n", result.Bytes()[0]) } // Output: sqrt(4) = 2 } ``` -------------------------------- ### Point.MultiScalarMult - Multi-Scalar Multiplication Source: https://context7.com/filosottile/edwards25519/llms.txt Performs multi-scalar multiplication (v = sum(scalars[i] * points[i])) in constant time. The execution time depends only on the lengths of the input slices. ```APIDOC ## Point.MultiScalarMult - Multi-Scalar Multiplication ### Description Sets v = sum(scalars[i] * points[i]) in constant time. Execution time depends only on the lengths of the slices. ### Method `MultiScalarMult(scalars []*Scalar, points []*Point) *Point` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go G := edwards25519.NewGeneratorPoint() twoG := new(edwards25519.Point).Add(G, G) threeG := new(edwards25519.Point).Add(twoG, G) // Create scalars: [2, 3, 4] s1 := edwards25519.NewScalar() s1.SetCanonicalBytes([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) s2 := edwards25519.NewScalar() s2.SetCanonicalBytes([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) s3 := edwards25519.NewScalar() s3.SetCanonicalBytes([]byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) // Compute 2*G + 3*(2G) + 4*(3G) = 2G + 6G + 12G = 20G scalars := []*edwards25519.Scalar{s1, s2, s3} points := []*edwards25519.Point{G, twoG, threeG} result := new(edwards25519.Point).MultiScalarMult(scalars, points) ``` ### Response #### Success Response (200) - ***Point** (type) - The resulting point after multi-scalar multiplication. #### Response Example ```json { "example": "// The result is stored in the receiver point result" } ``` ``` -------------------------------- ### Scalar.MultiplyAdd - Multiply and Add Scalars in Go Source: https://context7.com/filosottile/edwards25519/llms.txt Performs a modular multiplication (x * y) followed by an addition ( + z) on scalar values, all modulo the Edwards25519 curve order (l). The result is stored in scalar 's'. This operation can be more efficient than performing separate Multiply and Add operations and requires the 'edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { x := edwards25519.NewScalar() x.SetCanonicalBytes([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) y := edwards25519.NewScalar() y.SetCanonicalBytes([]byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) z := edwards25519.NewScalar() z.SetCanonicalBytes([]byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) // Compute 3 * 4 + 5 = 17 result := edwards25519.NewScalar().MultiplyAdd(x, y, z) seventeen := edwards25519.NewScalar() seventeen.SetCanonicalBytes([]byte{17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) if result.Equal(seventeen) == 1 { fmt.Println("3 * 4 + 5 = 17 verified") } // Output: 3 * 4 + 5 = 17 verified } ``` -------------------------------- ### Field Element Inversion in Go Source: https://context7.com/filosottile/edwards25519/llms.txt Computes the modular multiplicative inverse of a field element modulo 2^255 - 19. Returns zero if the input element is zero. This operation is crucial for division in finite fields. ```go package main import ( "fmt" "filippo.io/edwards25519/field" ) func main() { // Create element with value 7 bytes := make([]byte, 32) bytes[0] = 7 seven, _ := new(field.Element).SetBytes(bytes) // Compute inverse sevenInv := new(field.Element).Invert(seven) // Verify: 7 * 7^-1 = 1 product := new(field.Element).Multiply(seven, sevenInv) one := new(field.Element).One() if product.Equal(one) == 1 { fmt.Println("7 * 7^-1 = 1 in GF(2^255-19) verified") } // Output: 7 * 7^-1 = 1 in GF(2^255-19) verified } ``` -------------------------------- ### Scalar.Invert - Compute Modular Inverse in Go Source: https://context7.com/filosottile/edwards25519/llms.txt Calculates the modular multiplicative inverse of a nonzero scalar 't' modulo the Edwards25519 curve order (l). The result is stored in scalar 's'. If 't' is zero, 's' will also be zero. This operation is crucial for division in modular arithmetic and requires the 'edwards25519' package. ```go package main import ( "fmt" "filippo.io/edwards25519" ) func main() { // Create a scalar seven := edwards25519.NewScalar() seven.SetCanonicalBytes([]byte{7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) // Compute inverse sevenInv := edwards25519.NewScalar().Invert(seven) // Verify: 7 * (7^-1) = 1 product := edwards25519.NewScalar().Multiply(seven, sevenInv) one := edwards25519.NewScalar() one.SetCanonicalBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) if product.Equal(one) == 1 { fmt.Println("7 * (7^-1) = 1 verified") } // Output: 7 * (7^-1) = 1 verified } ```